[gentoo-commits] proj/pkgcore/pkgcheck:master commit in: tests/addons/, src/pkgcheck/addons/

2024-03-23 Thread Arthur Zamarin
commit: 39dd2fbd816129dd674665f0198aa58c8668a2c4
Author: Arthur Zamarin  gentoo  org>
AuthorDate: Sat Mar 23 08:12:51 2024 +
Commit: Arthur Zamarin  gentoo  org>
CommitDate: Sat Mar 23 08:12:51 2024 +
URL:
https://gitweb.gentoo.org/proj/pkgcore/pkgcheck.git/commit/?id=39dd2fbd

git addon: support user global gitignore

Resolves: https://github.com/pkgcore/pkgcheck/issues/671
Resolves: https://github.com/pkgcore/pkgcheck/issues/672
Signed-off-by: Arthur Zamarin  gentoo.org>

 src/pkgcheck/addons/git.py | 9 +++--
 tests/addons/test_git.py   | 4 
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/src/pkgcheck/addons/git.py b/src/pkgcheck/addons/git.py
index 7b2435ac..1874e8a6 100644
--- a/src/pkgcheck/addons/git.py
+++ b/src/pkgcheck/addons/git.py
@@ -536,9 +536,14 @@ class GitAddon(caches.CachedAddon):
 def _gitignore(self):
 """Load a repo's .gitignore and .git/info/exclude files for path 
matching."""
 patterns = []
-for path in (".gitignore", ".git/info/exclude"):
+paths = (
+pjoin(self.options.target_repo.location, ".gitignore"),
+pjoin(self.options.target_repo.location, ".git/info/exclude"),
+pjoin(os.environ.get("XDG_CONFIG_HOME", 
os.path.expanduser("~/.config")), "git/ignore"),
+)
+for path in paths:
 try:
-with open(pjoin(self.options.target_repo.location, path)) as f:
+with open(path) as f:
 patterns.extend(f)
 except (FileNotFoundError, IOError):
 pass

diff --git a/tests/addons/test_git.py b/tests/addons/test_git.py
index b896304d..596a2b34 100644
--- a/tests/addons/test_git.py
+++ b/tests/addons/test_git.py
@@ -12,6 +12,7 @@ from pkgcore.ebuild.atom import MalformedAtom
 from pkgcore.ebuild.atom import atom as atom_cls
 from pkgcore.restrictions import packages
 from snakeoil.cli.exceptions import UserException
+from snakeoil.contexts import os_environ
 from snakeoil.fileutils import touch
 from snakeoil.osutils import pjoin
 from snakeoil.process import CommandNotFound, find_binary
@@ -466,6 +467,9 @@ class TestGitAddon:
 self.addon = git.GitAddon(options)
 self.cache_file = self.addon.cache_file(self.repo)
 
+with os_environ(XDG_CONFIG_HOME=self.cache_dir):
+yield
+
 def test_git_unavailable(self, tool):
 args = ["scan", "--cache-dir", self.cache_dir, "--repo", 
self.repo.location]
 options, _ = tool.parse_args(args)



[gentoo-commits] proj/pkgcore/pkgcheck:master commit in: tests/addons/, src/pkgcheck/addons/

2023-07-10 Thread Arthur Zamarin
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