Kiln » Dependencies » Dulwich Read More
Clone URL:  
Pushed to one repository · View In Graph Contained in master-1, master-0, and 0.9.4

parse and serialize graftpoints

Changeset c024478d901f

Parent e53711cbc980

committed by Jelmer Vernooij

authored by milki

Changes to one file · Browse files at c024478d901f Showing diff from parent e53711cbc980 Diff from another changeset...

Change 1 of 7 Show Entire File dulwich/​repo.py Stacked
 
51
52
53
 
54
55
56
 
96
97
98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
100
101
 
117
118
119
 
120
121
122
 
477
478
479
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
480
481
482
 
620
621
622
 
 
 
 
623
624
625
 
640
641
642
 
 
 
643
644
645
 
867
868
869
 
 
 
870
871
872
 
51
52
53
54
55
56
57
 
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
 
169
170
171
172
173
174
175
 
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
 
701
702
703
704
705
706
707
708
709
710
 
725
726
727
728
729
730
731
732
733
 
955
956
957
958
959
960
961
962
963
@@ -51,6 +51,7 @@
  ObjectStoreGraphWalker,   )  from dulwich.objects import ( + check_hexsha,   Blob,   Commit,   ShaFile, @@ -96,6 +97,57 @@
  ]     +def parse_graftpoints(graftpoints=iter([])): + """Convert a list of graftpoints into a dict + + :param graftpoints: Iterator of graftpoint lines + + Each line is formatted as: + <commit sha1> <parent sha1> [<parent sha1>]* + + Resulting dictionary is: + <commit sha1>: [<parent sha1>*] + + https://git.wiki.kernel.org/index.php/GraftPoint + """ + grafts = {} + for l in graftpoints: + raw_graft = l.split(None, 1) + + commit = raw_graft[0] + if len(raw_graft) == 2: + parents = raw_graft[1].split() + else: + parents = [] + + for sha in [commit] + parents: + check_hexsha(sha, 'Invalid graftpoint') + + grafts[commit] = parents + return grafts + + +def serialize_graftpoints(graftpoints={}): + """Convert a dictionary of grafts into string + + The graft dictionary is: + <commit sha1>: [<parent sha1>*] + + Each line is formatted as: + <commit sha1> <parent sha1> [<parent sha1>]* + + https://git.wiki.kernel.org/index.php/GraftPoint + + """ + graft_lines = [] + for commit, parents in graftpoints.iteritems(): + if parents: + graft_lines.append('%s %s' % (commit, ' '.join(parents))) + else: + graft_lines.append(commit) + return '\n'.join(graft_lines) + +  class BaseRepo(object):   """Base class for a git repository.   @@ -117,6 +169,7 @@
  self.object_store = object_store   self.refs = refs   + self.graftpoints = {}   self.hooks = {}     def _init_files(self, bare): @@ -477,6 +530,34 @@
  return "%s <%s>" % (   config.get(("user", ), "name"),   config.get(("user", ), "email")) + + def add_graftpoints(self, updated_graftpoints): + """Add or modify graftpoints + + :param updated_graftpoints: Dict of commit shas to list of parent shas + """ + + # Simple validation + for commit, parents in updated_graftpoints.iteritems(): + for sha in [commit] + parents: + check_hexsha(sha, 'Invalid graftpoint') + + self.graftpoints.update(updated_graftpoints) + + def remove_graftpoints(self, to_remove=[]): + """Remove graftpoints + + :param to_remove: List of commit shas + """ + for sha in to_remove: + del self.graftpoints[sha] + + def serialize_graftpoints(self): + """Get the string representation of the graftpoints + + This format is writable to a graftpoint file. + """ + return serialize_graftpoints(self.graftpoints)     def do_commit(self, message=None, committer=None,   author=None, commit_timestamp=None, @@ -620,6 +701,10 @@
  refs = DiskRefsContainer(self.controldir())   BaseRepo.__init__(self, object_store, refs)   + graft_file = self.get_named_file(os.path.join("info", "grafts")) + if graft_file: + self.graftpoints = parse_graftpoints(graft_file) +   self.hooks['pre-commit'] = PreCommitShellHook(self.controldir())   self.hooks['commit-msg'] = CommitMsgShellHook(self.controldir())   self.hooks['post-commit'] = PostCommitShellHook(self.controldir()) @@ -640,6 +725,9 @@
  f.write(contents)   finally:   f.close() + + if path == os.path.join("info", "grafts"): + self.graftpoints = parse_graftpoints(iter(contents.splitlines()))     def get_named_file(self, path):   """Get a file from the control dir with a specific name. @@ -867,6 +955,9 @@
  """   self._named_files[path] = contents   + if path == os.path.join("info", "grafts"): + self.graftpoints = parse_graftpoints(contents.splitlines()) +   def get_named_file(self, path):   """Get a file from the control dir with a specific name.