summaryrefslogtreecommitdiff
path: root/plugins/security-guidance/hooks/gitutil.py
diff options
context:
space:
mode:
authorOctavian Guzu <oct@anthropic.com>2026-09-09 19:05:41 +0100
committerGitHub <noreply@github.com>2026-09-09 19:05:41 +0100
commit3ea32df27be71d78775e627df2382fc26203577f (patch)
tree849075bd62d15d2e938bc7938434fd4e1a893b88 /plugins/security-guidance/hooks/gitutil.py
parent517b2fcd1b60fa2181ac52dcf8492361ba341180 (diff)
parentdebbe686ab74ec095436a499b1a59864037e46cf (diff)
Merge pull request #5982 from anthropics/sg-resolve-repo-root
security-guidance: resolve repository from command and edited paths
Diffstat (limited to 'plugins/security-guidance/hooks/gitutil.py')
-rw-r--r--plugins/security-guidance/hooks/gitutil.py27
1 files changed, 25 insertions, 2 deletions
diff --git a/plugins/security-guidance/hooks/gitutil.py b/plugins/security-guidance/hooks/gitutil.py
index 4d771b42..b6e8cd54 100644
--- a/plugins/security-guidance/hooks/gitutil.py
+++ b/plugins/security-guidance/hooks/gitutil.py
@@ -39,6 +39,29 @@ GIT_CMD = [
"-c", "core.quotePath=false",
]
+SAFE_GIT_CONFIG = (
+ ("core.fsmonitor", "false"),
+ ("core.hooksPath", "/dev/null"),
+)
+
+
+def git_config_env(pairs, base=None):
+ base = os.environ if base is None else base
+ try:
+ n = max(0, int(base.get("GIT_CONFIG_COUNT") or 0))
+ except (TypeError, ValueError):
+ n = 0
+ env = {}
+ for i, (k, v) in enumerate(pairs, start=n):
+ env[f"GIT_CONFIG_KEY_{i}"] = k
+ env[f"GIT_CONFIG_VALUE_{i}"] = v
+ env["GIT_CONFIG_COUNT"] = str(n + len(pairs))
+ return env
+
+
+def apply_safe_git_env():
+ os.environ.update(git_config_env(SAFE_GIT_CONFIG))
+
def _git_rev_parse_head(cwd):
"""Return the current HEAD SHA, or None if not a git repo / no commits."""
@@ -238,7 +261,7 @@ def _git_diff_range(repo_root, base, head="HEAD"):
# raw UTF-8, not C-quoted. Required by the downstream
# parse_diff_into_files / extract_file_paths_from_diff regex.
r = subprocess.run(
- [*GIT_CMD, "diff", "-p", "--no-color", "--no-ext-diff", base, head],
+ [*GIT_CMD, "diff", "-p", "--no-color", "--no-ext-diff", "--no-textconv", base, head],
cwd=repo_root, capture_output=True, timeout=30,
)
if r.returncode != 0:
@@ -481,7 +504,7 @@ def get_git_diff(cwd, baseline_sha, full_context=False, paths=None, untracked_pa
return ""
# core.quotePath=false comes from GIT_CMD globally (see definition).
- cmd = [*GIT_CMD, "diff", "--no-color", "--no-ext-diff", baseline_sha] + (["--unified=99999"] if full_context else []) + pathspec
+ cmd = [*GIT_CMD, "diff", "--no-color", "--no-ext-diff", "--no-textconv", baseline_sha] + (["--unified=99999"] if full_context else []) + pathspec
try:
with _temp_index(cwd, untracked_paths) as env:
# env is None when no index could be found (bare repo / not a