Skip to content

Commit

Permalink
show proper error message when head commit not found (#878)
Browse files Browse the repository at this point in the history
fixes #848
  • Loading branch information
JayjeetAtGithub committed Jul 6, 2020
1 parent 250d69e commit ba7f53d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/popper/scm.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,19 @@ def get_sha(repo, short=None):
if not repo:
return None

if short:
return repo.git.rev_parse(repo.head.object.hexsha, short=short)
try:
if short:
sha = repo.git.rev_parse(repo.head.object.hexsha, short=short)
else:
sha = repo.git.rev_parse(repo.head.object.hexsha)

except ValueError as e:
sha = None
log.warning(
f"Could not obtain commit ID (SHA1) due to the Git repository at {repo.git_dir} being empty."
)

return repo.git.rev_parse(repo.head.object.hexsha)
return sha


def get_branch(repo):
Expand Down
10 changes: 10 additions & 0 deletions src/test/test_scm.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ def test_get_branch_in_detached_head_state(self):
self.assertIsNone(scm.get_sha(None, short=8))
self.assertIsNone(scm.get_branch(None))

# drop head commit
with self.assertLogs("popper", level="WARNING") as cm:
repo.git.update_ref("-d", "HEAD")
self.assertEqual(scm.get_sha(repo), None)
self.assertEqual(len(cm.output), 1)
self.assertTrue(
f"WARNING:popper:Could not obtain commit ID (SHA1) due to the Git repository at {repo.git_dir} being empty."
in cm.output[0]
)

def test_clone(self):
tempdir = tempfile.mkdtemp()
tdir = os.path.join(tempdir, "test_clone")
Expand Down

0 comments on commit ba7f53d

Please sign in to comment.