diff options
| author | mmoore <mmoore@anthropic.com> | 2026-09-25 12:17:32 -0700 |
|---|---|---|
| committer | mmoore <mmoore@anthropic.com> | 2026-09-25 12:17:32 -0700 |
| commit | 7de0de8eea27161166002620246a03ed68aed927 (patch) | |
| tree | 0f25456ef90b9bd55bd9ce836ef73e61d1c7825d /plugins/claude-security/scripts/lib/git.py | |
| parent | ad30d62cd52ad0d0af1beda1eab7f61c601045d0 (diff) | |
Claude Security Plugin - v0.12.0claude-security-v0.12.0
Diffstat (limited to 'plugins/claude-security/scripts/lib/git.py')
| -rw-r--r-- | plugins/claude-security/scripts/lib/git.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/plugins/claude-security/scripts/lib/git.py b/plugins/claude-security/scripts/lib/git.py new file mode 100644 index 00000000..ded163d3 --- /dev/null +++ b/plugins/claude-security/scripts/lib/git.py @@ -0,0 +1,27 @@ +"""Read-only git calls for the scripts: one runner, prompts suppressed, output byte-faithful.""" + +from __future__ import annotations + +import os +import subprocess + +REDIRECTS = frozenset({"GIT_DIR", "GIT_WORK_TREE", "GIT_INDEX_FILE", "GIT_COMMON_DIR"}) +ENV = {k: v for k, v in os.environ.items() if k not in REDIRECTS} | {"GIT_TERMINAL_PROMPT": "0"} + + +def run(cwd: str, *args: str) -> str | None: + """The stdout of `git -C <cwd> <args>` without trailing newlines; None on any failure.""" + try: + out = subprocess.run( + ["git", "-C", cwd, *args], + env=ENV, + stdout=subprocess.PIPE, + stderr=subprocess.DEVNULL, + timeout=30, + check=False, + ) + except (OSError, subprocess.SubprocessError): + return None + if out.returncode != 0: + return None + return out.stdout.decode("utf-8", "surrogateescape").rstrip("\r\n") |
