diff options
-rw-r--r-- | lib/git/refs.py | 17 | ||||
-rw-r--r-- | test/git/test_refs.py | 18 |
2 files changed, 32 insertions, 3 deletions
diff --git a/lib/git/refs.py b/lib/git/refs.py index 47c37af6..26e7c09e 100644 --- a/lib/git/refs.py +++ b/lib/git/refs.py @@ -118,7 +118,7 @@ class Reference(LazyMixin, Iterable): """ commit = self.object if commit.type != "commit": - raise TypeError("Object of reference %s did not point to a commit" % self) + raise TypeError("Object of reference %s did not point to a commit, but to %r" % (self, commit)) return commit commit = property(_get_commit, _set_commit, doc="Return Commit object the reference points to") @@ -250,8 +250,7 @@ class SymbolicReference(object): def _get_path(self): return os.path.join(self.repo.path, self.name) - @property - def commit(self): + def _get_commit(self): """ Returns: Commit object we point to, works for detached and non-detached @@ -271,6 +270,18 @@ class SymbolicReference(object): # Otherwise it would have detached it return Head(self.repo, tokens[1]).commit + def _set_commit(self, commit): + """ + Set our commit, possibly dereference our symbolic reference first. + """ + if self.is_detached: + return self._set_reference(commit) + + # set the commit on our reference + self._get_reference().commit = commit + + commit = property(_get_commit, _set_commit, doc="Query or set commits directly") + def _get_reference(self): """ Returns diff --git a/test/git/test_refs.py b/test/git/test_refs.py index 696b95c7..979165ef 100644 --- a/test/git/test_refs.py +++ b/test/git/test_refs.py @@ -198,3 +198,21 @@ class TestRefs(TestBase): assert head.commit == old_commit # and the ref did not change head.object = head_tree assert head.object == head_tree + self.failUnlessRaises(TypeError, getattr, head, 'commit') # object is a tree, not a commit + + # set the commit directly using the head. This would never detach the head + assert not cur_head.is_detached + head.object = old_commit + cur_head.reference = head.commit + assert cur_head.is_detached + parent_commit = head.commit.parents[0] + assert cur_head.is_detached + cur_head.commit = parent_commit + assert cur_head.is_detached and cur_head.commit == parent_commit + + cur_head.reference = head + assert not cur_head.is_detached + cur_head.commit = parent_commit + assert not cur_head.is_detached + assert head.commit == parent_commit + |