commit:     a19030fda246d49a3ff24f44a0f005e99dd65a1a
Author:     Arthur Zamarin <arthurzam <AT> gentoo <DOT> org>
AuthorDate: Tue Oct  3 18:25:27 2023 +0000
Commit:     Arthur Zamarin <arthurzam <AT> gentoo <DOT> org>
CommitDate: Tue Oct  3 18:25:27 2023 +0000
URL:        
https://gitweb.gentoo.org/proj/pkgcore/pkgcheck.git/commit/?id=a19030fd

addons.git: add helping message on failure on git remote setup

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: https://github.com/pkgcore/pkgcheck/issues/608
Resolves: https://github.com/pkgcore/pkgdev/issues/107
Signed-off-by: Arthur Zamarin <arthurzam <AT> gentoo.org>

 src/pkgcheck/addons/git.py | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/src/pkgcheck/addons/git.py b/src/pkgcheck/addons/git.py
index 5d41ba3b..0db52091 100644
--- a/src/pkgcheck/addons/git.py
+++ b/src/pkgcheck/addons/git.py
@@ -353,6 +353,26 @@ class _ScanGit(argparse.Action):
     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:
@@ -368,6 +388,8 @@ class _ScanGit(argparse.Action):
             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:

Reply via email to