Skip to content

Commit

Permalink
Fixes markfinal#129 Do not let an inaccessible Git repo from blocking…
Browse files Browse the repository at this point in the history
… opening a recipe
  • Loading branch information
foundry-markf committed Jul 8, 2024
1 parent 20909fd commit 5344a92
Showing 1 changed file with 46 additions and 36 deletions.
82 changes: 46 additions & 36 deletions src/cruiz/recipe/recipewidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,39 +586,46 @@ def _create_statusbar(self) -> None:
self._refresh_statusbar()

def _refresh_statusbar(self) -> None:
if self._git_repository:
head_sha = self._git_repository.head.object.hexsha
try:
branch = self._git_repository.active_branch
tracking_branch = branch.tracking_branch()
if tracking_branch is None:
tooltip = "Untracked branch"
except TypeError:
branch = head_sha
tracking_branch = None
tooltip = "Detached head"
message = f"{self._git_repository.working_tree_dir} " f"({branch})"
if tracking_branch:
commits_behind = self._git_repository.iter_commits(
f"{branch}..{tracking_branch}"
)
num_commits_behind = sum(1 for _ in commits_behind)
tooltip = f"Tracking {tracking_branch}"
if num_commits_behind > 0:
message += f" \u2193{num_commits_behind}"
tooltip += f" {num_commits_behind} commits behind"
commits_ahead = self._git_repository.iter_commits(
f"{tracking_branch}..{branch}"
)
num_commits_ahead = sum(1 for _ in commits_ahead)
if num_commits_ahead > 0:
message += f" \u2191{num_commits_ahead}"
tooltip += f" {num_commits_ahead} commits ahead"
tooltip += f"\ncommit {head_sha}"

self._git_workspace_label.setText(message)
if tooltip:
self._git_workspace_label.setToolTip(tooltip)
try:
if self._git_repository:
head_sha = self._git_repository.head.object.hexsha
try:
branch = self._git_repository.active_branch
tracking_branch = branch.tracking_branch()
if tracking_branch is None:
tooltip = "Untracked branch"
except TypeError:
branch = head_sha
tracking_branch = None
tooltip = "Detached head"
message = f"{self._git_repository.working_tree_dir} " f"({branch})"
if tracking_branch:
commits_behind = self._git_repository.iter_commits(
f"{branch}..{tracking_branch}"
)
num_commits_behind = sum(1 for _ in commits_behind)
tooltip = f"Tracking {tracking_branch}"
if num_commits_behind > 0:
message += f" \u2193{num_commits_behind}"
tooltip += f" {num_commits_behind} commits behind"
commits_ahead = self._git_repository.iter_commits(
f"{tracking_branch}..{branch}"
)
num_commits_ahead = sum(1 for _ in commits_ahead)
if num_commits_ahead > 0:
message += f" \u2191{num_commits_ahead}"
tooltip += f" {num_commits_ahead} commits ahead"
tooltip += f"\ncommit {head_sha}"

self._git_workspace_label.setText(message)
if tooltip:
self._git_workspace_label.setToolTip(tooltip)
except ValueError as exc:
logger.exception(exc)
self._git_workspace_label.setText(
f"Unable to resolve details from {self._git_repository.remotes[0].url}"
)
self._git_workspace_label.setToolTip(str(exc))

def _on_git_context_menu(self, position: QtCore.QPoint) -> None:
menu = QtWidgets.QMenu(self)
Expand All @@ -629,9 +636,12 @@ def _on_git_context_menu(self, position: QtCore.QPoint) -> None:

def _fetch_git_repository(self) -> None:
assert self._git_repository
for remote in self._git_repository.remotes:
remote.fetch()
self._refresh_statusbar()
try:
for remote in self._git_repository.remotes:
remote.fetch()
self._refresh_statusbar()
except git.exc.GitCommandError as exc:
logger.exception(exc)

def on_preferences_update(self) -> None:
"""
Expand Down

0 comments on commit 5344a92

Please sign in to comment.