diff --git a/cli/popper/scm.py b/cli/popper/scm.py index 4d60a3711..cd31e452d 100644 --- a/cli/popper/scm.py +++ b/cli/popper/scm.py @@ -164,7 +164,7 @@ def get_sha(repo, short=None): first 7 characters, otherwise it returns the entire SHA1 string. """ if not repo: - return 'na' + return None if short: return repo.git.rev_parse(repo.head.object.hexsha, short=short) @@ -178,7 +178,7 @@ def get_branch(repo): GIT_BRANCH (Jenkins), CIRCLE_BRANCH and CI_COMMIT_REF_NAME (Gitlab) """ if not repo: - return 'na' + return None if not repo.head.is_detached: return repo.active_branch.name @@ -195,3 +195,5 @@ def get_branch(repo): branch = os.environ.get('CI_COMMIT_REF_NAME') if branch: return branch + + return get_sha(repo) diff --git a/cli/test/test_config.py b/cli/test/test_config.py index e5fa6d283..38510a15c 100644 --- a/cli/test/test_config.py +++ b/cli/test/test_config.py @@ -53,9 +53,9 @@ def test_config_non_defaults(self): def test_config_without_git_repo(self): conf = PopperConfig(workspace_dir='/tmp/foo') - self.assertEqual('na', conf.git_commit) - self.assertEqual('na', conf.git_branch) - self.assertEqual('na', conf.git_sha_short) + self.assertIsNone(conf.git_commit) + self.assertIsNone(conf.git_branch) + self.assertIsNone(conf.git_sha_short) def test_config_with_git_repo(self): r = self.mk_repo() diff --git a/cli/test/test_scm.py b/cli/test/test_scm.py index 6a1973987..639bf981f 100644 --- a/cli/test/test_scm.py +++ b/cli/test/test_scm.py @@ -27,6 +27,7 @@ def test_get_remote_url(self): def test_get_branch_in_detached_head_state(self): repo = self.mk_repo() + repo.git.checkout('HEAD~1') os.environ['TRAVIS_BRANCH'] = 'travis' @@ -45,6 +46,13 @@ def test_get_branch_in_detached_head_state(self): self.assertEqual('gitlab', scm.get_branch(repo)) os.environ.pop('CI_COMMIT_REF_NAME') + self.assertEqual(scm.get_sha(repo), scm.get_branch(repo)) + + # None given as arg + self.assertIsNone(scm.get_sha(None)) + self.assertIsNone(scm.get_sha(None, short=8)) + self.assertIsNone(scm.get_branch(None)) + def test_clone(self): tempdir = tempfile.mkdtemp() tdir = os.path.join(tempdir, 'test_clone')