From 01b13f78ffc2c35c4cb6c0b22a950dcf889d71f7 Mon Sep 17 00:00:00 2001 From: Bill Sacks Date: Tue, 24 Nov 2020 13:57:39 -0700 Subject: [PATCH 1/3] 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. --- manic/repository_git.py | 29 ++++++++++++++--------------- test/test_unit_repository_git.py | 2 +- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/manic/repository_git.py b/manic/repository_git.py index f986051001..ef10fb97cd 100644 --- a/manic/repository_git.py +++ b/manic/repository_git.py @@ -109,27 +109,21 @@ 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() @@ -137,7 +131,12 @@ def _current_ref(self): 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 = '' diff --git a/test/test_unit_repository_git.py b/test/test_unit_repository_git.py index 4a0a334bb1..c8460c182a 100644 --- a/test/test_unit_repository_git.py +++ b/test/test_unit_repository_git.py @@ -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) From 10288430f230445b261d61e90fc119ee85d6e303 Mon Sep 17 00:00:00 2001 From: Bill Sacks Date: Tue, 24 Nov 2020 14:09:17 -0700 Subject: [PATCH 2/3] Fix pre-existing pylint issues --- manic/externals_description.py | 3 +++ test/test_sys_checkout.py | 1 - 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/manic/externals_description.py b/manic/externals_description.py index 918d616e37..42755e1a52 100644 --- a/manic/externals_description.py +++ b/manic/externals_description.py @@ -193,6 +193,9 @@ def parse_submodules_desc_section(section_items, file_path): def read_gitmodules_file(root_dir, file_name): # pylint: disable=deprecated-method # Disabling this check because the method is only used for python2 + # pylint: disable=too-many-locals + # pylint: disable=too-many-branches + # pylint: disable=too-many-statements """Read a .gitmodules file and convert it to be compatible with an externals description. """ diff --git a/test/test_sys_checkout.py b/test/test_sys_checkout.py index 118bee5308..9889feba0c 100644 --- a/test/test_sys_checkout.py +++ b/test/test_sys_checkout.py @@ -38,7 +38,6 @@ import os import os.path import shutil -import sys import unittest from manic.externals_description import ExternalsDescription From 952e44d51191aff422baf0581461d39a1b5bac3f Mon Sep 17 00:00:00 2001 From: Bill Sacks Date: Tue, 24 Nov 2020 14:09:26 -0700 Subject: [PATCH 3/3] Change output: put tag/hash before branch name I feel like the tag/hash is the more important thing, so put it first --- manic/repository_git.py | 2 +- test/test_unit_repository_git.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/manic/repository_git.py b/manic/repository_git.py index ef10fb97cd..3a6a0f1716 100644 --- a/manic/repository_git.py +++ b/manic/repository_git.py @@ -135,7 +135,7 @@ def _current_ref(self): # 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) + current_ref = "{} (branch {})".format(current_ref, branch_name) else: # If we still can't find a ref, return empty string. This # can happen if we're not actually in a git repo diff --git a/test/test_unit_repository_git.py b/test/test_unit_repository_git.py index c8460c182a..b153e556a9 100644 --- a/test/test_unit_repository_git.py +++ b/test/test_unit_repository_git.py @@ -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 (foo_tag)' + expected = 'foo_tag (branch feature3)' result = self._repo._current_ref() self.assertEqual(result, expected)