dulwich disable empty commit #936
-
how to? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
Please post questions on stackoverflow rather than here on GitHub. Do you mean "pointless" commits, i.e. commits without changes? One way in which you could check for empty commits is to create a tree first, check whether that's different from the current tree and fail otherwise::
|
Beta Was this translation helpful? Give feedback.
-
sorry @jelmer
|
Beta Was this translation helpful? Give feedback.
-
did you mean: tree = index.commit(repo.object_store) ? |
Beta Was this translation helpful? Give feedback.
-
On Thu, Jan 30, 2020 at 05:46:17PM -0800, Too large to fit in the margin wrote:
did you mean: tree = index.commit(self.object_store) ?
Yep.
…--
Jelmer Vernooij <[email protected]>
PGP Key: https://www.jelmer.uk/D729A457.asc
|
Beta Was this translation helpful? Give feedback.
-
In case of already using If from dulwich_tree import TreeWriter
class TreeWriterNoEmptyCommits(TreeWriter):
"""
Allows to not commit empty commits
https://github.com/dulwich/dulwich/issues/740
"""
def do_commit(self, allow_empty_commits=False, **kwargs):
self.add_changed_to_object_store()
tree: bytes = self.tree.id
if not allow_empty_commits:
index = self.repo.open_index()
index.commit(self.repo.object_store)
head_tree = self.repo[self.ref].tree
if tree == head_tree:
# unchanged => no file changes
self.reset()
return None
# end if
# end if
ret = self.repo.do_commit(tree=tree, ref=self.ref, **kwargs)
self.reset()
return ret
# end def
# end class Usage example:writer = TreeWriterNoEmptyCommits(repo)
file_content = b'file b'
writer.set_data('a/b', file_content)
commit_hash = writer.do_commit(message=b'Add a/b.')
assert commit_hash is not None
writer.set_data('a/b', file_content)
commit_hash = writer.do_commit(message=b'Unchanged a/b, this will not will be commited')
assert commit_hash is None
writer.set_data('a/b', file_content)
commit_hash = writer.do_commit(message=b'Unchanged a/b, but will be commited and be empty', allow_empty_commits=True)
assert commit_hash is not None |
Beta Was this translation helpful? Give feedback.
In case of already using
dulwich_tree
, subclassing with this overwritten function includes that check:If
allow_empty_commits
isFalse
(default), it will check for the tree to be changed, and not commit in that case.