Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

show proper error message when head commit not found #878

Merged
merged 6 commits into from
Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
JayjeetAtGithub marked this conversation as resolved.
Show resolved Hide resolved
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