← Back to team overview

dulwich-users team mailing list archive

[PATCH 4/6] parse_graftpoints

 

https://git.wiki.kernel.org/index.php/GraftPoint

Graftpoints are dictionaries of shas. The key is the commit sha, the
value is a list of parent shas.

In a graft file, each line is formatted as:

  <commit sha1> <parent sha1> [<parent sha1>]*
---
 dulwich/repo.py |   34 ++++++++++++++++++++++++++++++++++
 1 files changed, 34 insertions(+), 0 deletions(-)

diff --git a/dulwich/repo.py b/dulwich/repo.py
index edba3c4..3e754f2 100644
--- a/dulwich/repo.py
+++ b/dulwich/repo.py
@@ -792,6 +792,27 @@ def write_packed_refs(f, packed_refs, peeled_refs=None):
             f.write('^%s\n' % peeled_refs[refname])
 
 
+def parse_graftpoints(graft_lines=[]):
+    """Convert a list of graftpoints into a dict
+
+    Each line is formatted as:
+        <commit sha1> <parent sha1> [<parent sha1>]*
+
+    https://git.wiki.kernel.org/index.php/GraftPoint
+    """
+    grafts = {}
+    for l in graft_lines:
+        raw_graft = l.split(None, 1)
+
+        sha = raw_graft[0]
+        if len(raw_graft) == 2:
+            parents = raw_graft[1].split()
+        else:
+            parents = []
+        grafts[sha] = parents
+    return grafts
+
+
 class BaseRepo(object):
     """Base class for a git repository.
 
@@ -1298,6 +1319,11 @@ class Repo(BaseRepo):
         refs = DiskRefsContainer(self.controldir())
         BaseRepo.__init__(self, object_store, refs)
 
+        graft_file = self.get_named_file(os.path.join("info", "grafts"))
+        if graft_file:
+            grafts = parse_graftpoints(graft_file.read().splitlines())
+            self.object_store.add_grafts(grafts)
+
         self.hooks['pre-commit'] = PreCommitShellHook(self.controldir())
         self.hooks['commit-msg'] = CommitMsgShellHook(self.controldir())
         self.hooks['post-commit'] = PostCommitShellHook(self.controldir())
@@ -1319,6 +1345,10 @@ class Repo(BaseRepo):
         finally:
             f.close()
 
+        if path == os.path.join("info", "grafts"):
+            grafts = parse_graftpoints(contents.splitlines())
+            self.object_store.add_grafts(grafts)
+
     def get_named_file(self, path):
         """Get a file from the control dir with a specific name.
 
@@ -1532,6 +1562,10 @@ class MemoryRepo(BaseRepo):
         """
         self._named_files[path] = contents
 
+        if path == os.path.join("info", "grafts"):
+            grafts = parse_graftpoints(contents.splitlines())
+            self.object_store.add_grafts(grafts)
+
     def get_named_file(self, path):
         """Get a file from the control dir with a specific name.
 
-- 
1.7.7.1.9.g13da8



References