Skip to content

Commit

Permalink
addons.git: add helping message on failure on git remote setup
Browse files Browse the repository at this point in the history
Upon initial failure to get the git diff-tree output (so not the hot
flow, but sad failure flow), try to catch output of incorrectly
configured git remote (the remote should have a correct HEAD configured).

Recommend the user to run `git remote set-head origin -a` to solve it.

Resolves: #608
Resolves: pkgcore/pkgdev#107
Signed-off-by: Arthur Zamarin <arthurzam@gentoo.org>
  • Loading branch information
arthurzam committed Oct 3, 2023
1 parent 9e7637d commit a19030f
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/pkgcheck/addons/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,26 @@ def __init__(self, *args, staged=False, **kwargs):
def default_ref(self, remote):
return "HEAD" if self.staged else f"{remote}..HEAD"

def _try_git_remote(self, parser, namespace):
"""Try to catch case of missing git remote HEAD ref."""
try:
subprocess.run(
["git", "rev-parse", namespace.git_remote],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd=namespace.target_repo.location,
check=True,
encoding="utf8",
)
except FileNotFoundError as exc:
parser.error(str(exc))
except subprocess.CalledProcessError as exc:
error = exc.stderr.splitlines()[0]
if "ambiguous argument" in error and "unknown revision" in error:
parser.error(
f"failed running git: {error}\nSuggested to configure the remote by running 'git remote set-head {namespace.git_remote} -a'"
)

def generate_restrictions(self, parser, namespace, ref):
"""Generate restrictions for a given diff command."""
try:
Expand All @@ -368,6 +388,8 @@ def generate_restrictions(self, parser, namespace, ref):
parser.error(str(exc))
except subprocess.CalledProcessError as exc:
error = exc.stderr.splitlines()[0]
if "ambiguous argument" in error and "unknown revision" in error:
self._try_git_remote(parser, namespace)
parser.error(f"failed running git: {error}")

if not p.stdout:
Expand Down

0 comments on commit a19030f

Please sign in to comment.