Skip to content

Commit

Permalink
When on a branch, show tag/hash, too
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
billsacks committed Nov 24, 2020
1 parent 39ad532 commit 01b13f7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
29 changes: 14 additions & 15 deletions manic/repository_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,35 +109,34 @@ def _clone_repo(self, base_dir_path, repo_dir_name, verbosity):
def _current_ref(self):
"""Determine the *name* associated with HEAD.
If we're on a branch, then returns the branch name; otherwise,
if we're on a tag, then returns the tag name; otherwise, returns
If we're on a tag, then returns the tag name; otherwise, returns
the current hash. Returns an empty string if no reference can be
determined (e.g., if we're not actually in a git repository).
If we're on a branch, then the branch name is also included in
the returned string (in addition to the tag / hash).
"""
ref_found = False

# If we're on a branch, then use that as the current ref
branch_found, branch_name = self._git_current_branch()
if branch_found:
current_ref = branch_name
# If we're exactly at a tag, use that as the current ref
tag_found, tag_name = self._git_current_tag()
if tag_found:
current_ref = tag_name
ref_found = True

if not ref_found:
# Otherwise, if we're exactly at a tag, use that as the
# current ref
tag_found, tag_name = self._git_current_tag()
if tag_found:
current_ref = tag_name
ref_found = True

if not ref_found:
# Otherwise, use current hash as the current ref
hash_found, hash_name = self._git_current_hash()
if hash_found:
current_ref = hash_name
ref_found = True

if not ref_found:
if ref_found:
# If we're on a branch, include branch name in current ref
branch_found, branch_name = self._git_current_branch()
if branch_found:
current_ref = "{} ({})".format(branch_name, current_ref)
else:
# If we still can't find a ref, return empty string. This
# can happen if we're not actually in a git repo
current_ref = ''
Expand Down
2 changes: 1 addition & 1 deletion test/test_unit_repository_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def test_ref_branch(self):
True, 'feature3')
self._repo._git_current_tag = self._git_current_tag(True, 'foo_tag')
self._repo._git_current_hash = self._git_current_hash(True, 'abc123')
expected = 'feature3'
expected = 'feature3 (foo_tag)'
result = self._repo._current_ref()
self.assertEqual(result, expected)

Expand Down

0 comments on commit 01b13f7

Please sign in to comment.