Skip to content

Commit c19b5a6

Browse files
authored
Merge pull request #389 from simvue-io/hotfix/388-git-repo-metadata-crash
Expanded try except to handle half initialized git repos
2 parents 4618dd5 + 52e9adc commit c19b5a6

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

simvue/metadata.py

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,41 +30,41 @@ def git_info(repository: str) -> dict[str, typing.Any]:
3030
"""
3131
try:
3232
git_repo = git.Repo(repository, search_parent_directories=True)
33-
except git.InvalidGitRepositoryError:
34-
return {}
35-
current_commit: git.Commit = git_repo.head.commit
36-
author_list: set[str] = set(
37-
email
38-
for commit in git_repo.iter_commits("--all")
39-
if "noreply" not in (email := (commit.author.email or ""))
40-
and "[bot]" not in (commit.author.name or "")
41-
)
33+
except (git.InvalidGitRepositoryError, ValueError):
34+
current_commit: git.Commit = git_repo.head.commit
35+
author_list: set[str] = set(
36+
email
37+
for commit in git_repo.iter_commits("--all")
38+
if "noreply" not in (email := (commit.author.email or ""))
39+
and "[bot]" not in (commit.author.name or "")
40+
)
4241

43-
ref: str = current_commit.hexsha
42+
ref: str = current_commit.hexsha
4443

45-
# In the case where the repository is dirty blame should point to the
46-
# current developer, not the person responsible for the latest commit
47-
if dirty := git_repo.is_dirty():
48-
blame = git_repo.config_reader().get_value("user", "email", "unknown")
49-
else:
50-
blame = current_commit.committer.email
44+
# In the case where the repository is dirty blame should point to the
45+
# current developer, not the person responsible for the latest commit
46+
if dirty := git_repo.is_dirty():
47+
blame = git_repo.config_reader().get_value("user", "email", "unknown")
48+
else:
49+
blame = current_commit.committer.email
5150

52-
for tag in git_repo.tags:
53-
if tag.commit == current_commit:
54-
ref = tag.name
55-
break
51+
for tag in git_repo.tags:
52+
if tag.commit == current_commit:
53+
ref = tag.name
54+
break
5655

57-
return {
58-
"git.authors": json.dumps(list(author_list)),
59-
"git.ref": ref,
60-
"git.msg": current_commit.message.strip(),
61-
"git.time_stamp": current_commit.committed_datetime.strftime(
62-
"%Y-%m-%d %H:%M:%S %z UTC"
63-
),
64-
"git.blame": blame,
65-
"git.url": git_repo.remote().url,
66-
"git.dirty": dirty,
67-
}
56+
return {
57+
"git.authors": json.dumps(list(author_list)),
58+
"git.ref": ref,
59+
"git.msg": current_commit.message.strip(),
60+
"git.time_stamp": current_commit.committed_datetime.strftime(
61+
"%Y-%m-%d %H:%M:%S %z UTC"
62+
),
63+
"git.blame": blame,
64+
"git.url": git_repo.remote().url,
65+
"git.dirty": dirty,
66+
}
67+
return {}
6868

6969

7070
if __name__ in "__main__":

0 commit comments

Comments
 (0)