upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorkirigayakazima <46666711+kirigayakazima@users.noreply.github.com>2026-11-26 22:01:17 +0800
committerkirigayakazima <46666711+kirigayakazima@users.noreply.github.com>2026-11-26 22:01:17 +0800
commitd6bb80df42a2ebf3cf827ac5381b68df38aec20c (patch)
tree3e76b843169a0b97749ad90deb26ce9b6b7901e7 /scripts
parent73516a2902a9872e4d21d1bdbbde4e26059497c5 (diff)
merge_check: guard ref option injection, bounds, and git timeouts
Diffstat (limited to 'scripts')
-rw-r--r--scripts/merge_check.py47
1 files changed, 40 insertions, 7 deletions
diff --git a/scripts/merge_check.py b/scripts/merge_check.py
index e270cbe00..77cb7a917 100644
--- a/scripts/merge_check.py
+++ b/scripts/merge_check.py
@@ -35,15 +35,18 @@ REVERT_SUBJECT = re.compile(r"^Revert\b", re.IGNORECASE)
35# Where a doubled block is the repeated-merge damage. In code, three equal lines in two functions are ordinary, so a 35# Where a doubled block is the repeated-merge damage. In code, three equal lines in two functions are ordinary, so a
36# block there is listed but does not change the verdict. 36# block there is listed but does not change the verdict.
37TEXT_SUFFIXES = (".md", ".txt", ".rst") 37TEXT_SUFFIXES = (".md", ".txt", ".rst")
38GIT_TIMEOUT = 120
38 39
39 40
40class GitError(Exception): 41class GitError(Exception):
41 pass 42 pass
42 43
43 44
44def git(repo, *args, check=True): 45def git(repo, *args, check=True, timeout=GIT_TIMEOUT):
45 try: 46 try:
46 r = subprocess.run(["git", "-C", repo, *args], capture_output=True) 47 r = subprocess.run(["git", "-C", repo, *args], capture_output=True, timeout=timeout)
48 except subprocess.TimeoutExpired:
49 raise GitError(f"git {' '.join(args)}: timed out after {timeout}s") from None
47 except OSError as e: # git is not on PATH, or cannot be executed: a usage error (exit 2), not a traceback 50 except OSError as e: # git is not on PATH, or cannot be executed: a usage error (exit 2), not a traceback
48 raise GitError(f"cannot run git: {e.strerror or e}") from None 51 raise GitError(f"cannot run git: {e.strerror or e}") from None
49 # Decode without universal-newline translation: a bare CR inside a Git line is content. 52 # Decode without universal-newline translation: a bare CR inside a Git line is content.
@@ -177,20 +180,50 @@ def check(repo, main, number, ref, min_length, merges):
177 "duplicate_samples": {p: v[:3] for p, v in dups.items()}} 180 "duplicate_samples": {p: v[:3] for p, v in dups.items()}}
178 181
179 182
183def _positive_int(text):
184 try:
185 value = int(text)
186 except ValueError:
187 raise argparse.ArgumentTypeError(f"expected a positive integer, got {text!r}") from None
188 if value < 1:
189 raise argparse.ArgumentTypeError(f"must be at least 1, got {text!r}")
190 return value
191
192
193def _nonnegative_int(text):
194 try:
195 value = int(text)
196 except ValueError:
197 raise argparse.ArgumentTypeError(f"expected a non-negative integer, got {text!r}") from None
198 if value < 0:
199 raise argparse.ArgumentTypeError(f"must be non-negative, got {text!r}")
200 return value
201
202
203def _validate_ref(ref, label):
204 if not ref or ref != ref.strip() or ref.startswith("-"):
205 raise argparse.ArgumentTypeError(f"invalid {label} ref: {ref!r}")
206 return ref
207
208
180def parse_pair(text): 209def parse_pair(text):
181 number, sep, ref = text.partition("=") 210 number, sep, ref = text.partition("=")
182 if not sep or not number.lstrip("#").isdigit() or not ref: 211 raw_num = number[1:] if number.startswith("#") else number
212 if not sep or not raw_num.isascii() or not raw_num.isdigit() or int(raw_num) < 1:
183 raise argparse.ArgumentTypeError(f"expected PR=REF, got {text!r}") 213 raise argparse.ArgumentTypeError(f"expected PR=REF, got {text!r}")
184 return int(number.lstrip("#")), ref 214 return int(raw_num), _validate_ref(ref, "PR target")
185 215
186 216
187def main(argv=None): 217def main(argv=None):
188 ap = argparse.ArgumentParser(description=__doc__.strip().splitlines()[0]) 218 ap = argparse.ArgumentParser(description=__doc__.strip().splitlines()[0])
189 ap.add_argument("pairs", nargs="+", type=parse_pair, metavar="PR=REF") 219 ap.add_argument("pairs", nargs="+", type=parse_pair, metavar="PR=REF")
190 ap.add_argument("--main", default="origin/main", help="the branch the round merges into (default origin/main)") 220 ap.add_argument("--main", default="origin/main", type=lambda s: _validate_ref(s, "--main"),
221 help="the branch the round merges into (default origin/main)")
191 ap.add_argument("--repo", default=".", help="repository folder (default: the current one)") 222 ap.add_argument("--repo", default=".", help="repository folder (default: the current one)")
192 ap.add_argument("--min-length", type=int, default=40, help="shortest stripped line counted as a duplicate (40)") 223 ap.add_argument("--min-length", type=_nonnegative_int, default=40,
193 ap.add_argument("--depth", type=int, default=5000, help="first-parent commits searched for earlier merges (5000)") 224 help="shortest stripped line counted as a duplicate (40)")
225 ap.add_argument("--depth", type=_positive_int, default=5000,
226 help="first-parent commits searched for earlier merges (5000)")
194 ap.add_argument("--json", action="store_true", help="one JSON report instead of text") 227 ap.add_argument("--json", action="store_true", help="one JSON report instead of text")
195 args = ap.parse_args(argv) 228 args = ap.parse_args(argv)
196 try: 229 try: