-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_patch.py
More file actions
43 lines (36 loc) · 1.23 KB
/
validate_patch.py
File metadata and controls
43 lines (36 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import re
import sys
from pathlib import Path
p = Path(sys.argv[1]) if len(sys.argv) > 1 else None
if not p or not p.exists():
print("Usage: python validate_patch.py <patchfile>")
sys.exit(2)
text = p.read_text(encoding="utf-8", errors="replace")
# Reject markdown fences
if "```" in text:
print("FAIL: patch contains markdown fences ```")
sys.exit(1)
# Must have git headers
if "diff --git " not in text:
print("FAIL: patch missing 'diff --git' header")
sys.exit(1)
# Must not contain absolute home paths
if "/home/" in text:
print("FAIL: patch contains absolute paths (/home/...)")
sys.exit(1)
# Detect naked (unprefixed) lines inside hunks
in_hunk = False
for i, line in enumerate(text.splitlines(True), start=1):
if line.startswith("@@ "):
in_hunk = True
continue
if in_hunk:
if line.startswith(("diff --git ", "index ", "--- ", "+++ ")):
in_hunk = False
continue
if not line.startswith(("+", "-", " ", "\\")):
# This catches your exact issue (blank/whitespace lines with no prefix)
preview = line.replace("\n", "\\n")
print(f"FAIL: unprefixed line inside hunk at {i}: {preview!r}")
sys.exit(1)
print("OK")