From d2cbcc087f19093fd3341538492c804079fcfa93 Mon Sep 17 00:00:00 2001 From: PatternCatalyst Date: Tue, 3 Nov 2026 22:55:16 -0500 Subject: [PATCH] chore(host-ids): re-vendor host_ids.py (filter --repo) Brings in lgtm-skills#90: filter now takes --repo and, without it, uses the repo holding the working directory, so captured logs scrub the repo path to instead of ~/... --- scripts/host_ids.py | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/scripts/host_ids.py b/scripts/host_ids.py index c433b24..6ff133c 100755 --- a/scripts/host_ids.py +++ b/scripts/host_ids.py @@ -10,7 +10,9 @@ host_ids.py check [--repo DIR] tracked files; exit 1 on a hit (CI-safe) host_ids.py scrub [--repo DIR] [PATH...] rewrite files in place (default: tracked) host_ids.py scrub --dry-run ... change nothing; exit 1 if a file would change - host_ids.py filter scrub stdin to stdout (captured logs) + host_ids.py filter [--repo DIR] scrub stdin to stdout (captured logs); the + repo path becomes (default: the git + repo holding the working directory, if any) host_ids.py history [--repo DIR] [--emit-rules FILE] scan every blob and message in all refs; optionally write git filter-repo rules @@ -554,6 +556,18 @@ def cmd_scrub( return 1 if (dry_run and changed) else 0 +def filter_rules(repo_arg: str | None) -> list[Rule]: + """Rules for `filter`: the repo path becomes `` whenever a repo is known. + + An explicit --repo must be a git repository. Without one, the repo holding the + working directory is used, so a log captured inside a project scrubs to `` + rather than `~/...`; outside any repo only the home-path rules apply. + """ + if repo_arg is None and not _run("git", "rev-parse", "--show-toplevel"): + return all_rules(gather(None)) + return setup(repo_arg)[2] + + def cmd_filter(rules: list[Rule]) -> int: sys.stdout.write(scrub_text(sys.stdin.read(), rules, Config(), "-")) return 0 @@ -816,6 +830,22 @@ def expect(cond: bool, what: str) -> None: expect( cmd_scrub(repo, cfg, rules, [], dry_run=True) == 0, "scrub is idempotent" ) + planted_repo = "/home/planted-acct/Dev/proj" + saved_repo, fake.repo_path = fake.repo_path, planted_repo + log = f"rootdir: {planted_repo}\n{planted_repo}/.venv/bin/python /home/planted-acct/x\n" + out = scrub_text(log, runtime_rules(fake), Config(), "-") + want = "rootdir: \n/.venv/bin/python ~/x\n" + expect(out == want, f"filter repo path -> : {out!r}") + fake.repo_path = saved_repo + cwd = os.getcwd() + try: + os.chdir(repo) + expect( + Path(gather(repo_root(Path("."))).repo_path or "") == repo.resolve(), + "filter finds the repo holding the working directory", + ) + finally: + os.chdir(cwd) lines, dropped = format_rules( { "kernel\n`8.4.2-200.fc98": "kernel\n`8.4", @@ -866,7 +896,8 @@ def main() -> int: p.add_argument("--repo") p.add_argument("--dry-run", action="store_true") p.add_argument("paths", nargs="*") - sub.add_parser("filter", help="scrub stdin to stdout") + p = sub.add_parser("filter", help="scrub stdin to stdout") + p.add_argument("--repo") p = sub.add_parser("history", help="scan all reachable blobs and messages") p.add_argument("--repo") p.add_argument("--emit-rules", metavar="FILE") @@ -877,8 +908,7 @@ def main() -> int: if args.cmd == "self-test": return self_test() if args.cmd == "filter": - rt = gather(None) - return cmd_filter(all_rules(rt)) + return cmd_filter(filter_rules(args.repo)) repo, cfg, rules = setup(getattr(args, "repo", None)) if args.cmd == "check": return cmd_check(repo, cfg, rules)