aboutsummaryrefslogtreecommitdiff
path: root/hooks/_common.py
diff options
context:
space:
mode:
Diffstat (limited to 'hooks/_common.py')
-rw-r--r--hooks/_common.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/hooks/_common.py b/hooks/_common.py
index d4bf520..e82f7ed 100644
--- a/hooks/_common.py
+++ b/hooks/_common.py
@@ -16,6 +16,7 @@ the tools themselves.
"""
import json
+import os
import re
import sys
from typing import Optional
@@ -64,6 +65,25 @@ def respond_ask(reason: str, system_message: Optional[str] = None) -> None:
print(json.dumps(output))
+def read_referenced_file(path: str, max_bytes: int = 1_000_000) -> Optional[str]:
+ """Read a local file referenced by -F/--file/--body-file so its text can be
+ attribution-scanned. Return the text, or None if it can't be safely read
+ (missing, not a regular file, larger than max_bytes, or not valid UTF-8).
+ None means 'could not inspect', never 'clean'."""
+ if not path:
+ return None
+ try:
+ expanded = os.path.expanduser(path)
+ if not os.path.isfile(expanded):
+ return None
+ if os.path.getsize(expanded) > max_bytes:
+ return None
+ with open(expanded, "r", encoding="utf-8", errors="strict") as fh:
+ return fh.read()
+ except (OSError, UnicodeDecodeError):
+ return None
+
+
def scan_attribution(text: str) -> list[str]:
"""Return human-readable descriptions of any AI-attribution hits."""
if not text: