From d9bd14349fafb739428a8d324596f32b583d9243 Mon Sep 17 00:00:00 2001 From: Wolfvin Date: Sun, 12 Jul 2026 09:42:40 +0700 Subject: [PATCH] fix(pre-commit-hook): same UnicodeDecodeError encoding bug as ownership_engine.py (PR #216) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Systematic grep for the same bug class (subprocess.run text=True without explicit encoding) found 2 more occurrences in pre_commit_hook.py — git diff --name-only and the codelens check subprocess invocation, both would crash on non-cp1252 output (accented filenames, non-ASCII finding text) same as the ownership crash fixed in PR #216. Fix: add encoding='utf-8', errors='replace' to both calls. --- scripts/pre_commit_hook.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/scripts/pre_commit_hook.py b/scripts/pre_commit_hook.py index 7b7df9b0..61f274dc 100644 --- a/scripts/pre_commit_hook.py +++ b/scripts/pre_commit_hook.py @@ -21,6 +21,12 @@ import subprocess import json +# subprocess.run(..., text=True) below also passes encoding='utf-8', +# errors='replace' — without it, Windows decodes with cp1252 (platform +# default) and crashes on git/codelens output containing non-cp1252 bytes +# (accented file/author names, non-ASCII findings text). Same class of bug +# fixed in ownership_engine.py (PR #216). + # Add scripts directory to path SCRIPT_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..', 'scripts') CODELENS = os.path.join(SCRIPT_DIR, 'codelens.py') @@ -33,7 +39,8 @@ def main(): try: result = subprocess.run( ['git', 'diff', '--cached', '--name-only', '--diff-filter=ACMR'], - capture_output=True, text=True, cwd=workspace + capture_output=True, text=True, encoding='utf-8', errors='replace', + cwd=workspace ) staged_files = [f.strip() for f in result.stdout.strip().split('\n') if f.strip()] except Exception: @@ -67,7 +74,10 @@ def main(): '--max-findings', str(max_findings), '--commands'] + commands - result = subprocess.run(cmd, capture_output=True, text=True, cwd=workspace, timeout=120) + result = subprocess.run( + cmd, capture_output=True, text=True, encoding='utf-8', errors='replace', + cwd=workspace, timeout=120 + ) if result.returncode != 0: # Gate failed