commit: 123abbc101b4fafc25e3c288f9893dda7625c1de
Author: Arthur Zamarin gentoo org>
AuthorDate: Sun Jul 9 19:13:33 2023 +
Commit: Arthur Zamarin gentoo org>
CommitDate: Sun Jul 9 19:13:33 2023 +
URL:
https://gitweb.gentoo.org/proj/pkgcore/pkgcheck.git/commit/?id=123abbc1
scan: add `--git-remote` option to select remote
For repos with multiple remotes, it might be useful to select a specific
remote to use (and not the default origin). This can be set using the
`--git-remote` option for cmd call, or by adding `git-remote=value` to
the config file.
Resolves: https://github.com/pkgcore/pkgcheck/issues/600
Signed-off-by: Arthur Zamarin gentoo.org>
src/pkgcheck/addons/git.py | 67 +-
tests/addons/test_git.py | 13 +
2 files changed, 50 insertions(+), 30 deletions(-)
diff --git a/src/pkgcheck/addons/git.py b/src/pkgcheck/addons/git.py
index 2547bcc5..669ac262 100644
--- a/src/pkgcheck/addons/git.py
+++ b/src/pkgcheck/addons/git.py
@@ -342,16 +342,16 @@ class _ScanGit(argparse.Action):
def __init__(self, *args, staged=False, **kwargs):
super().__init__(*args, **kwargs)
if staged:
-default_ref = "HEAD"
diff_cmd = ["git", "diff-index", "--name-only", "--cached", "-z"]
else:
-default_ref = "origin..HEAD"
diff_cmd = ["git", "diff-tree", "-r", "--name-only", "-z"]
self.staged = staged
-self.default_ref = default_ref
self.diff_cmd = diff_cmd
+def default_ref(self, remote):
+return "HEAD" if self.staged else f"{remote}..HEAD"
+
def generate_restrictions(self, parser, namespace, ref):
"""Generate restrictions for a given diff command."""
try:
@@ -363,10 +363,10 @@ class _ScanGit(argparse.Action):
check=True,
encoding="utf8",
)
-except FileNotFoundError as e:
-parser.error(str(e))
-except subprocess.CalledProcessError as e:
-error = e.stderr.splitlines()[0]
+except FileNotFoundError as exc:
+parser.error(str(exc))
+except subprocess.CalledProcessError as exc:
+error = exc.stderr.splitlines()[0]
parser.error(f"failed running git: {error}")
if not p.stdout:
@@ -417,7 +417,7 @@ class _ScanGit(argparse.Action):
namespace.enabled_checks.update(objects.CHECKS.select(GitCommitsCheck).values())
# determine target ref
-ref = value if value is not None else self.default_ref
+ref = value if value is not None else
self.default_ref(namespace.git_remote)
setattr(namespace, self.dest, ref)
# generate scanning restrictions
@@ -441,6 +441,9 @@ class GitAddon(caches.CachedAddon):
Additionally, the origin/HEAD ref must exist. If it doesn't, running ``git
remote set-head origin master`` or similar for other branches will create
it.
+
+You can override the default git remote used for all git comparison using
+``--git-remote``.
"""
# cache registry
@@ -448,7 +451,7 @@ class GitAddon(caches.CachedAddon):
@classmethod
def mangle_argparser(cls, parser):
-group = parser.add_argument_group("git", docs=cls.__doc__)
+group: argparse.ArgumentParser = parser.add_argument_group("git",
docs=cls.__doc__)
git_opts = group.add_mutually_exclusive_group()
git_opts.add_argument(
"--commits",
@@ -484,6 +487,17 @@ class GitAddon(caches.CachedAddon):
temporarily stashing them during the scanning process.
""",
)
+group.add_argument(
+"--git-remote",
+default="origin",
+metavar="REMOTE",
+help="git remote used for all git comparison and operations",
+docs="""
+The git remote to be used for all operations by pkgcheck. The
+default value, and the recommended value is ``origin``, but
+you can use any valid git remote name.
+""",
+)
def __init__(self, *args):
super().__init__(*args)
@@ -551,11 +565,11 @@ class GitAddon(caches.CachedAddon):
return p.stdout.strip()
@staticmethod
-def _get_default_branch(path):
+def _get_default_branch(path, remote):
"""Retrieve a git repo's default branch used with origin remote."""
try:
p = subprocess.run(
-["git", "symbolic-ref", "refs/remotes/origin/HEAD"],
+["git", "symbolic-ref", f"refs/remotes/{remote}/HEAD"],
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
cwd=path,
@@ -591,10 +605,11 @@ class GitAddon(caches.CachedAddon):
def update_cache(self, force=False):
"""Update related cache and push updates to disk."""
+remote = self.options.git_remot