Skip to content

Commit 8e29a91

Browse files
committed
refs: Fixed incorrect retrieval of symbolic reference types - previously we only really knew heads, now we know references as a common base. The adjustment make the ref system as flexible as it was originally meant to be
1 parent e4ed59a commit 8e29a91

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

lib/git/refs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def _get_commit(self):
119119
# Otherwise it would have detached it
120120
if tokens[0] != "ref:":
121121
raise ValueError("Failed to parse symbolic refernce: wanted 'ref: <hexsha>', got %r" % value)
122-
return Head(self.repo, tokens[1]).commit
122+
return Reference.from_path(self.repo, tokens[1]).commit
123123

124124
def _set_commit(self, commit):
125125
"""
@@ -149,13 +149,13 @@ def _get_reference(self):
149149

150150
def _set_reference(self, ref):
151151
"""
152-
Set ourselves to the given ref. It will stay a symbol if the ref is a Head.
152+
Set ourselves to the given ref. It will stay a symbol if the ref is a Reference.
153153
Otherwise we try to get a commit from it using our interface.
154154
155155
Strings are allowed but will be checked to be sure we have a commit
156156
"""
157157
write_value = None
158-
if isinstance(ref, Head):
158+
if isinstance(ref, SymbolicReference):
159159
write_value = "ref: %s" % ref.path
160160
elif isinstance(ref, Commit):
161161
write_value = ref.sha

test/git/test_refs.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,28 @@ def test_head_reset(self, rw_repo):
8989
for head in heads:
9090
cur_head.reference = head
9191
assert cur_head.reference == head
92+
assert isinstance(cur_head.reference, Head)
9293
assert cur_head.commit == head.commit
9394
assert not cur_head.is_detached
9495
# END for each head
9596

9697
# detach
97-
cur_head.reference = heads[0].commit
98-
assert cur_head.commit == heads[0].commit
98+
active_head = heads[0]
99+
curhead_commit = active_head.commit
100+
cur_head.reference = curhead_commit
101+
assert cur_head.commit == curhead_commit
99102
assert cur_head.is_detached
100103
self.failUnlessRaises(TypeError, getattr, cur_head, "reference")
101104

105+
# tags are references, hence we can point to them
102106
some_tag = rw_repo.tags[0]
103107
cur_head.reference = some_tag
104-
assert cur_head.is_detached
108+
assert not cur_head.is_detached
105109
assert cur_head.commit == some_tag.commit
110+
assert isinstance(cur_head.reference, TagReference)
111+
112+
# put HEAD back to a real head, otherwise everything else fails
113+
cur_head.reference = active_head
106114

107115
# type check
108116
self.failUnlessRaises(ValueError, setattr, cur_head, "reference", "that")

0 commit comments

Comments
 (0)