forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_diff_hygiene.py
More file actions
executable file
·48 lines (40 loc) · 1.6 KB
/
Copy pathcheck_diff_hygiene.py
File metadata and controls
executable file
·48 lines (40 loc) · 1.6 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
44
45
46
47
48
#!/usr/bin/env python3
"""Check changed files for whitespace errors and unresolved conflict markers."""
from __future__ import annotations
import argparse
import subprocess
import sys
from pathlib import Path
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--changed-files", required=True, type=Path)
parser.add_argument("--base", required=True)
parser.add_argument("--head", default="HEAD")
args = parser.parse_args()
diff = subprocess.run(["git", "diff", "--check", args.base, args.head], check=False)
if args.head == "HEAD":
worktree_diff = subprocess.run(["git", "diff", "--check", "HEAD"], check=False)
if worktree_diff.returncode:
return worktree_diff.returncode
if diff.returncode:
return diff.returncode
failed = False
for path in args.changed_files.read_text(encoding="utf-8").splitlines():
candidate = Path(path)
if not candidate.is_file():
continue
try:
lines = candidate.read_text(encoding="utf-8").splitlines()
except (OSError, UnicodeDecodeError):
continue
for lineno, line in enumerate(lines, start=1):
if line.startswith(("<<<<<<<", ">>>>>>>")):
print(f"{path}:{lineno}: unresolved merge conflict marker", file=sys.stderr)
failed = True
if failed:
print("FAIL: unresolved merge conflict markers found in changed files", file=sys.stderr)
return 1
print("Diff hygiene checks passed.")
return 0
if __name__ == "__main__":
raise SystemExit(main())