Skip to content

Commit 2a20f74

Browse files
committed
[WIP] add replace method to git.Commit
(marked WIP until I add appropriate tests) This adds a replace method to git.Commit. The replace method returns a copy of the Commit object with attributes replaced from keyword arguments. For example: >>> old = repo.head.commit >>> new = old.replace(message='This is a test') closes gitpython-developers#1123
1 parent e1cd58b commit 2a20f74

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

git/objects/commit.py

+19-7
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,24 @@ def __init__(self, repo, binsha, tree=None, author=None, authored_date=None, aut
136136
def _get_intermediate_items(cls, commit):
137137
return commit.parents
138138

139+
@classmethod
140+
def _calculate_sha_(cls, repo, commit):
141+
stream = BytesIO()
142+
commit._serialize(stream)
143+
streamlen = stream.tell()
144+
stream.seek(0)
145+
146+
istream = repo.odb.store(IStream(cls.type, streamlen, stream))
147+
return istream.binsha
148+
149+
def replace(self, **kwargs):
150+
attrs = {k: getattr(self, k) for k in self.__slots__}
151+
attrs.update(kwargs)
152+
new_commit = self.__class__(self.repo, self.__class__.NULL_BIN_SHA, **attrs)
153+
new_commit.binsha = self._calculate_sha_(self.repo, new_commit)
154+
155+
return new_commit
156+
139157
def _set_cache_(self, attr):
140158
if attr in Commit.__slots__:
141159
# read the data in a chunk, its faster - then provide a file wrapper
@@ -375,13 +393,7 @@ def create_from_tree(cls, repo, tree, message, parent_commits=None, head=False,
375393
committer, committer_time, committer_offset,
376394
message, parent_commits, conf_encoding)
377395

378-
stream = BytesIO()
379-
new_commit._serialize(stream)
380-
streamlen = stream.tell()
381-
stream.seek(0)
382-
383-
istream = repo.odb.store(IStream(cls.type, streamlen, stream))
384-
new_commit.binsha = istream.binsha
396+
new_commit.binsha = cls._calculate_sha_(repo, new_commit)
385397

386398
if head:
387399
# need late import here, importing git at the very beginning throws

0 commit comments

Comments
 (0)