[projects/git-slug] Reject unknown git commands before fanning out to all repos
arekm
arekm at pld-linux.org
Thu Nov 19 22:06:32 CET 2026
commit 1f6fbfa9f2ed2f45d8693ba126505cd376333c86
Author: Arkadiusz Miśkiewicz <arekm at maven.pl>
Date: Thu Nov 19 18:59:11 2026 +0100
Reject unknown git commands before fanning out to all repos
A typo like 'git pld inti' printed the same "not a git command" error
in every repository. Check the command once, in the first matching
repo, and if git doesn't know it, let git report the error there once.
slug.py | 36 +++++++++++++++++++++++++++--
tests/test_passthrough.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 93 insertions(+), 2 deletions(-)
---
diff --git a/slug.py b/slug.py
index 94c8ef6..46e78fa 100755
--- a/slug.py
+++ b/slug.py
@@ -456,6 +456,31 @@ def get_matching_repos(options):
return find_git_repos(options.packagesdir, options.pattern)
+def is_known_git_command(command, repo_dir):
+ """Return False only if git positively does not know 'command'.
+
+ Checked once before fanning out, so a typo like 'git pld inti' fails
+ immediately instead of printing the same error in every repo.
+ Covers builtins, git-* programs in PATH and aliases. Runs in a target
+ repo so repo-local and includeIf aliases are seen as they will be when
+ the command actually runs. If the list cannot be obtained (git < 2.18
+ lacks --list-cmds), we can't tell, so let the per-repo git run decide.
+ """
+ try:
+ result = subprocess.run(
+ ['git', '-C', repo_dir, '--list-cmds=main,others,alias'],
+ capture_output=True, text=True)
+ except OSError:
+ return True
+ if result.returncode != 0:
+ return True
+ # Alias names are case-insensitive and listed lowercased. Folding
+ # builtins too errs toward "known": 'git pld STATUS' fans out and fails
+ # per repo as before, rather than an alias being misjudged as a typo.
+ known = {name.lower() for name in result.stdout.split()}
+ return command.lower() in known
+
+
def build_git_cmd(repo_dir, git_command, git_args, config_pairs):
"""Assemble the full git command line.
@@ -555,8 +580,9 @@ def passthrough_command(options, git_command, git_args):
1. Resolve per-command -c config overrides (built-in or from gitconfig).
2. Find all repos matching --pattern in --packagesdir.
- 3. Run git in each repo via the worker pool.
- 4. Report failures and exit non-zero if any repo failed.
+ 3. Reject a command git doesn't know (checked once, in the first repo).
+ 4. Run git in each repo via the worker pool.
+ 5. Report failures and exit non-zero if any repo failed.
"""
config_pairs = get_command_config(git_command)
@@ -565,6 +591,12 @@ def passthrough_command(options, git_command, git_args):
print("fatal: no matching repositories found", file=sys.stderr)
sys.exit(1)
+ # Unknown command (typo): let git print its own error, with the "most
+ # similar command" hint, once. It runs in the same repo the check used,
+ # so git sees the same config and can only fail; args are dropped anyway.
+ if not git_command.startswith('-') and not is_known_git_command(git_command, repos[0]):
+ os.execvp('git', ['git', '-C', repos[0], git_command])
+
info("Running 'git {}' in {} repo(s)...".format(git_command, len(repos)),
options.quiet)
diff --git a/tests/test_passthrough.py b/tests/test_passthrough.py
index 4a28b13..4b6ab6e 100644
--- a/tests/test_passthrough.py
+++ b/tests/test_passthrough.py
@@ -115,6 +115,7 @@ def test_passthrough_command_exits_when_no_matching_repos(monkeypatch, make_opti
def test_passthrough_command_exits_when_any_repo_fails(monkeypatch, make_options, capsys):
monkeypatch.setattr(slug, "get_command_config", lambda command: [])
monkeypatch.setattr(slug, "get_matching_repos", lambda options: ["/repos/a", "/repos/b"])
+ monkeypatch.setattr(slug, "is_known_git_command", lambda command, repo_dir: True)
monkeypatch.setattr(slug, "run_worker", lambda function, options, args: ["/repos/b"])
with pytest.raises(SystemExit) as exc:
@@ -124,3 +125,61 @@ def test_passthrough_command_exits_when_any_repo_fails(monkeypatch, make_options
output = capsys.readouterr().err
assert "Running 'git fetch' in 2 repo(s)..." in output
assert "error: failed in 1 repo(s): b" in output
+
+
+def test_passthrough_command_unknown_command_execs_git_once_in_first_repo(monkeypatch, make_options):
+ checked = []
+ captured = {}
+
+ def fake_is_known(command, repo_dir):
+ checked.append((command, repo_dir))
+ return False
+
+ def fake_execvp(binary, argv):
+ captured["argv"] = argv
+ raise SystemExit(1)
+
+ monkeypatch.setattr(slug, "get_command_config", lambda command: [])
+ monkeypatch.setattr(slug, "get_matching_repos", lambda options: ["/repos/a", "/repos/b"])
+ monkeypatch.setattr(slug, "is_known_git_command", fake_is_known)
+ monkeypatch.setattr(slug.os, "execvp", fake_execvp)
+ monkeypatch.setattr(slug, "run_worker", lambda *a: pytest.fail("must not fan out"))
+
+ with pytest.raises(SystemExit):
+ slug.passthrough_command(make_options(), "inti", ["asdfad"])
+
+ assert checked == [("inti", "/repos/a")]
+ assert captured["argv"] == ["git", "-C", "/repos/a", "inti"]
+
+
+ at pytest.mark.parametrize(
+ ("command", "returncode", "stdout", "expected"),
+ [
+ ("status", 0, "add\nstatus\nst\n", True),
+ ("inti", 0, "add\ninit\nstatus\n", False),
+ # --list-cmds prints aliases lowercased; git resolves them case-insensitively
+ ("MixedCase", 0, "status\nmixedcase\n", True),
+ # git < 2.18 rejects --list-cmds: can't tell, so don't block
+ ("inti", 129, "", True),
+ ],
+)
+def test_is_known_git_command(monkeypatch, command, returncode, stdout, expected):
+ calls = []
+
+ def fake_run(cmd, **kwargs):
+ calls.append(cmd)
+ return SimpleNamespace(returncode=returncode, stdout=stdout, stderr="")
+
+ monkeypatch.setattr(slug.subprocess, "run", fake_run)
+
+ assert slug.is_known_git_command(command, "/repos/a") is expected
+ assert calls == [["git", "-C", "/repos/a", "--list-cmds=main,others,alias"]]
+
+
+def test_is_known_git_command_without_git_binary(monkeypatch):
+ def fake_run(cmd, **kwargs):
+ raise FileNotFoundError("git")
+
+ monkeypatch.setattr(slug.subprocess, "run", fake_run)
+
+ assert slug.is_known_git_command("inti", "/repos/a") is True
================================================================
---- gitweb:
http://git.pld-linux.org/gitweb.cgi/projects/git-slug.git/commitdiff/1f6fbfa9f2ed2f45d8693ba126505cd376333c86
More information about the pld-cvs-commit
mailing list