forked from jlebar/pre-commit-hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_do_not_submit.py
More file actions
executable file
·34 lines (26 loc) · 989 Bytes
/
check_do_not_submit.py
File metadata and controls
executable file
·34 lines (26 loc) · 989 Bytes
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
#!/usr/bin/env python3
"""Checks each file in sys.argv for the string "DO NOT SUBMIT"."""
from __future__ import annotations
import os
import subprocess
import sys
def err(s: str) -> None:
print(s, file=sys.stderr)
# There are many ways we could search for the string "DO NOT SUBMIT", but `git
# grep --no-index` is nice because
# - it's very fast (as compared to iterating over the file in Python)
# - we can reasonably assume it's available on all machines
# - unlike plain grep, which is slower and has different flags on MacOS versus
# Linux, git grep is always the same.
res = subprocess.run(
["git", "grep", "-Hn", "--no-index", "DO NOT SUBMIT", *sys.argv[1:]],
capture_output=True,
)
if res.returncode == 0:
err('Error: The string "DO NOT SUBMIT" was found!')
err(res.stdout.decode("utf-8"))
sys.exit(1)
elif res.returncode == 2:
err(f"Error invoking grep on {', '.join(sys.argv[1:])}:")
err(res.stderr.decode("utf-8"))
sys.exit(2)