Skip to content

Commit 01b13f7

Browse files
committed
When on a branch, show tag/hash, too
For the sake of recording provenance, it is not sufficient to know the branch you're on: you also need to know the hash or tag. So show both when printing the current reference.
1 parent 39ad532 commit 01b13f7

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

manic/repository_git.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -109,35 +109,34 @@ def _clone_repo(self, base_dir_path, repo_dir_name, verbosity):
109109
def _current_ref(self):
110110
"""Determine the *name* associated with HEAD.
111111
112-
If we're on a branch, then returns the branch name; otherwise,
113-
if we're on a tag, then returns the tag name; otherwise, returns
112+
If we're on a tag, then returns the tag name; otherwise, returns
114113
the current hash. Returns an empty string if no reference can be
115114
determined (e.g., if we're not actually in a git repository).
115+
116+
If we're on a branch, then the branch name is also included in
117+
the returned string (in addition to the tag / hash).
116118
"""
117119
ref_found = False
118120

119-
# If we're on a branch, then use that as the current ref
120-
branch_found, branch_name = self._git_current_branch()
121-
if branch_found:
122-
current_ref = branch_name
121+
# If we're exactly at a tag, use that as the current ref
122+
tag_found, tag_name = self._git_current_tag()
123+
if tag_found:
124+
current_ref = tag_name
123125
ref_found = True
124126

125-
if not ref_found:
126-
# Otherwise, if we're exactly at a tag, use that as the
127-
# current ref
128-
tag_found, tag_name = self._git_current_tag()
129-
if tag_found:
130-
current_ref = tag_name
131-
ref_found = True
132-
133127
if not ref_found:
134128
# Otherwise, use current hash as the current ref
135129
hash_found, hash_name = self._git_current_hash()
136130
if hash_found:
137131
current_ref = hash_name
138132
ref_found = True
139133

140-
if not ref_found:
134+
if ref_found:
135+
# If we're on a branch, include branch name in current ref
136+
branch_found, branch_name = self._git_current_branch()
137+
if branch_found:
138+
current_ref = "{} ({})".format(branch_name, current_ref)
139+
else:
141140
# If we still can't find a ref, return empty string. This
142141
# can happen if we're not actually in a git repo
143142
current_ref = ''

test/test_unit_repository_git.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def test_ref_branch(self):
101101
True, 'feature3')
102102
self._repo._git_current_tag = self._git_current_tag(True, 'foo_tag')
103103
self._repo._git_current_hash = self._git_current_hash(True, 'abc123')
104-
expected = 'feature3'
104+
expected = 'feature3 (foo_tag)'
105105
result = self._repo._current_ref()
106106
self.assertEqual(result, expected)
107107

0 commit comments

Comments
 (0)