Skip to content

Commit

Permalink
[web] Fix exceptions during blame information storage
Browse files Browse the repository at this point in the history
The python git library may throw not only `TypeError` exceptions but
`ValueError` exceptions as well. For example when the `HEAD` file exists
in the `.git` directory but the user who is running the `CodeChecker store`
command doesn't have permission to this file.

To solve this problem we will catch all the exceptions when getting the
tracking branch and remote url.
  • Loading branch information
csordasmarton committed Apr 6, 2022
1 parent 5adc515 commit 21e93e0
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions web/client/codechecker_client/blame_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,41 @@
FileBlameInfo = Dict[str, Optional[Dict]]


def __get_tracking_branch(repo: Repo) -> Optional[str]:
"""
Get the tracking branch name or the current commit hash from the given
repository.
"""
try:
# If a commit is checked out, accessing the active_branch member will
# throw an error.
return str(repo.active_branch.tracking_branch())
except Exception:
pass

try:
# Use the current commit hash if it's available.
return repo.head.commit.hexsha
except Exception:
pass

return None


def __get_blame_info(file_path: str):
""" Get blame info for the given file. """
try:
repo = Repo(file_path, search_parent_directories=True)
except InvalidGitRepositoryError:
return

tracking_branch = None
try:
# If a commit is checked out, accessing the active_branch member will
# throw a type error. In this case we will use the current commit hash.
tracking_branch = str(repo.active_branch.tracking_branch())
except TypeError:
tracking_branch = repo.head.commit.hexsha
tracking_branch = __get_tracking_branch(repo)

remote_url = None
try:
# Handle the use case when a repository doesn't have a remote url.
remote_url = next(repo.remote().urls, None)
except ValueError:
except Exception:
pass

try:
Expand Down

0 comments on commit 21e93e0

Please sign in to comment.