public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-commits] proj/pkgcore/pkgcheck:master commit in: tests/addons/, src/pkgcheck/addons/
@ 2023-07-10 20:07 Arthur Zamarin
  0 siblings, 0 replies; 2+ messages in thread
From: Arthur Zamarin @ 2023-07-10 20:07 UTC (permalink / raw
  To: gentoo-commits

commit:     123abbc101b4fafc25e3c288f9893dda7625c1de
Author:     Arthur Zamarin <arthurzam <AT> gentoo <DOT> org>
AuthorDate: Sun Jul  9 19:13:33 2023 +0000
Commit:     Arthur Zamarin <arthurzam <AT> gentoo <DOT> org>
CommitDate: Sun Jul  9 19:13:33 2023 +0000
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 <arthurzam <AT> 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_remote
         for repo in self.options.target_repo.trees:
             try:
                 branch = self._get_current_branch(repo.location)
-                default_branch = self._get_default_branch(repo.location)
+                default_branch = self._get_default_branch(repo.location, remote)
                 # skip cache usage when not running on the default branch
                 if branch != default_branch:
                     logger.debug(
@@ -603,7 +618,7 @@ class GitAddon(caches.CachedAddon):
                         branch,
                     )
                     continue
-                commit = self._get_commit_hash(repo.location, "origin/HEAD")
+                commit = self._get_commit_hash(repo.location, f"{remote}/HEAD")
             except GitError:
                 continue
 
@@ -619,17 +634,17 @@ class GitAddon(caches.CachedAddon):
                 logger.debug("updating %s git repo cache to %s", repo, commit[:13])
                 if git_cache is None:
                     data = {}
-                    commit_range = "origin/HEAD"
+                    commit_range = f"{remote}/HEAD"
                 else:
                     data = git_cache.data
-                    commit_range = f"{git_cache.commit}..origin/HEAD"
+                    commit_range = f"{git_cache.commit}..{remote}/HEAD"
 
                 try:
                     self.pkg_history(
                         repo, commit_range, data=data, verbosity=self.options.verbosity
                     )
-                except GitError as e:
-                    raise PkgcheckUserException(str(e))
+                except GitError as exc:
+                    raise PkgcheckUserException(str(exc))
                 git_cache = GitCache(data, self.cache, commit=commit)
             else:
                 cache_repo = False
@@ -652,29 +667,31 @@ class GitAddon(caches.CachedAddon):
 
     def commits_repo(self, repo_cls):
         target_repo = self.options.target_repo
+        remote = self.options.git_remote
         data = {}
 
         try:
-            origin = self._get_commit_hash(target_repo.location, "origin/HEAD")
+            origin = self._get_commit_hash(target_repo.location, f"{remote}/HEAD")
             head = self._get_commit_hash(target_repo.location, "HEAD")
             if origin != head:
-                data = self.pkg_history(target_repo, "origin/HEAD..HEAD", local=True)
-        except GitError as e:
-            raise PkgcheckUserException(str(e))
+                data = self.pkg_history(target_repo, f"{remote}/HEAD..HEAD", local=True)
+        except GitError as exc:
+            raise PkgcheckUserException(str(exc))
 
         repo_id = f"{target_repo.repo_id}-commits"
         return repo_cls(data, repo_id=repo_id)
 
     def commits(self):
         target_repo = self.options.target_repo
+        remote = self.options.git_remote
         commits = ()
 
         try:
-            origin = self._get_commit_hash(target_repo.location, "origin/HEAD")
+            origin = self._get_commit_hash(target_repo.location, f"{remote}/HEAD")
             head = self._get_commit_hash(target_repo.location, "HEAD")
             if origin != head:
-                commits = GitRepoCommits(target_repo.location, "origin/HEAD..HEAD")
-        except GitError as e:
-            raise PkgcheckUserException(str(e))
+                commits = GitRepoCommits(target_repo.location, f"{remote}/HEAD..HEAD")
+        except GitError as exc:
+            raise PkgcheckUserException(str(exc))
 
         return iter(commits)

diff --git a/tests/addons/test_git.py b/tests/addons/test_git.py
index da88d501..b896304d 100644
--- a/tests/addons/test_git.py
+++ b/tests/addons/test_git.py
@@ -71,7 +71,8 @@ class TestPkgcheckScanCommitsParseArgs:
             options, _func = self.tool.parse_args(self.args + ["-r", local.path, "--commits"])
         assert excinfo.value.code == 0
 
-    def test_commits_existing(self, make_repo, make_git_repo, tmp_path):
+    @pytest.mark.parametrize("remote", ("origin", "pkgcheck"))
+    def test_commits_existing(self, remote, make_repo, make_git_repo, tmp_path):
         # create parent repo
         parent = make_repo()
         origin = make_git_repo(parent.location, commit=True)
@@ -80,9 +81,9 @@ class TestPkgcheckScanCommitsParseArgs:
 
         # create child repo and pull from parent
         local = make_git_repo(str(tmp_path), commit=False)
-        local.run(["git", "remote", "add", "origin", origin.path])
-        local.run(["git", "pull", "origin", "main"])
-        local.run(["git", "remote", "set-head", "origin", "main"])
+        local.run(["git", "remote", "add", remote, origin.path])
+        local.run(["git", "pull", remote, "main"])
+        local.run(["git", "remote", "set-head", remote, "main"])
         child = make_repo(local.path)
 
         # create local commits on child repo
@@ -91,7 +92,9 @@ class TestPkgcheckScanCommitsParseArgs:
         child.create_ebuild("cat/pkg-2")
         local.add_all("cat/pkg-2")
 
-        options, _func = self.tool.parse_args(self.args + ["-r", local.path, "--commits"])
+        options, _func = self.tool.parse_args(
+            self.args + ["-r", local.path, "--commits", "--git-remote", remote]
+        )
         atom_restricts = [atom_cls("cat/pkg")]
         assert list(options.restrictions) == [
             (base.package_scope, packages.OrRestriction(*atom_restricts))


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [gentoo-commits] proj/pkgcore/pkgcheck:master commit in: tests/addons/, src/pkgcheck/addons/
@ 2024-03-23  8:13 Arthur Zamarin
  0 siblings, 0 replies; 2+ messages in thread
From: Arthur Zamarin @ 2024-03-23  8:13 UTC (permalink / raw
  To: gentoo-commits

commit:     39dd2fbd816129dd674665f0198aa58c8668a2c4
Author:     Arthur Zamarin <arthurzam <AT> gentoo <DOT> org>
AuthorDate: Sat Mar 23 08:12:51 2024 +0000
Commit:     Arthur Zamarin <arthurzam <AT> gentoo <DOT> org>
CommitDate: Sat Mar 23 08:12:51 2024 +0000
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 <arthurzam <AT> 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)


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-03-23  8:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-03-23  8:13 [gentoo-commits] proj/pkgcore/pkgcheck:master commit in: tests/addons/, src/pkgcheck/addons/ Arthur Zamarin
  -- strict thread matches above, loose matches on Subject: below --
2023-07-10 20:07 Arthur Zamarin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox