dulwich disable empty commit #936
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:: |
0 replies
|
sorry @jelmer |
0 replies
|
did you mean: tree = index.commit(repo.object_store) ? |
0 replies
|
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 <jelmer@jelmer.uk>
PGP Key: https://www.jelmer.uk/D729A457.asc
|
0 replies
|
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 classUsage 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 |
0 replies
Answer selected by
jelmer
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In case of already using
dulwich_tree, subclassing with this overwritten function includes that check:If
allow_empty_commitsisFalse(default), it will check for the tree to be changed, and not commit in that case.