Skip to content

Commit

Permalink
Merge support for ref argument to do_commit, with some tweaks.
Browse files Browse the repository at this point in the history
  • Loading branch information
jelmer committed Aug 8, 2011
2 parents ac2521f + 03c0b85 commit 60ed785
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
0.8.1 UNRELEASED

FEATURES

* Repo.do_commit has a new argument 'ref'.

0.8.0 2011-08-07

FEATURES
Expand Down
12 changes: 7 additions & 5 deletions dulwich/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,8 @@ def __delitem__(self, name):
def do_commit(self, message, committer=None,
author=None, commit_timestamp=None,
commit_timezone=None, author_timestamp=None,
author_timezone=None, tree=None, encoding=None):
author_timezone=None, tree=None, encoding=None,
ref='HEAD'):
"""Create a new commit.
:param message: Commit message
Expand All @@ -1018,6 +1019,7 @@ def do_commit(self, message, committer=None,
:param tree: SHA1 of the tree root to use (if not specified the
current index will be committed).
:param encoding: Encoding
:param ref: Ref to commit to
:return: New commit SHA1
"""
import time
Expand Down Expand Up @@ -1053,18 +1055,18 @@ def do_commit(self, message, committer=None,
c.encoding = encoding
c.message = message
try:
old_head = self.refs["HEAD"]
old_head = self.refs[ref]
c.parents = [old_head]
self.object_store.add_object(c)
ok = self.refs.set_if_equals("HEAD", old_head, c.id)
ok = self.refs.set_if_equals(ref, old_head, c.id)
except KeyError:
c.parents = []
self.object_store.add_object(c)
ok = self.refs.add_if_new("HEAD", c.id)
ok = self.refs.add_if_new(ref, c.id)
if not ok:
# Fail if the atomic compare-and-swap failed, leaving the commit and
# all its objects as garbage.
raise CommitError("HEAD changed during commit")
raise CommitError("%s changed during commit" % (ref,))

return c.id

Expand Down
26 changes: 26 additions & 0 deletions dulwich/tests/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,32 @@ def add_if_new(name, new_ref):
self.assertEqual(r[self._root_commit].tree, new_commit.tree)
self.assertEqual('failed commit', new_commit.message)

def test_commit_branch(self):
r = self._repo

commit_sha = r.do_commit('commit to branch',
committer='Test Committer <test@nodomain.com>',
author='Test Author <test@nodomain.com>',
commit_timestamp=12395, commit_timezone=0,
author_timestamp=12395, author_timezone=0,
ref="refs/heads/new_branch")
self.assertEqual(self._root_commit, r["HEAD"].id)
self.assertEqual(commit_sha, r["refs/heads/new_branch"].id)
self.assertEqual([], r[commit_sha].parents)
self.assertTrue("refs/heads/new_branch" in r)

new_branch_head = commit_sha

commit_sha = r.do_commit('commit to branch 2',
committer='Test Committer <test@nodomain.com>',
author='Test Author <test@nodomain.com>',
commit_timestamp=12395, commit_timezone=0,
author_timestamp=12395, author_timezone=0,
ref="refs/heads/new_branch")
self.assertEqual(self._root_commit, r["HEAD"].id)
self.assertEqual(commit_sha, r["refs/heads/new_branch"].id)
self.assertEqual([new_branch_head], r[commit_sha].parents)

def test_stage_deleted(self):
r = self._repo
os.remove(os.path.join(r.path, 'a'))
Expand Down

0 comments on commit 60ed785

Please sign in to comment.