From 5b8d76e271151f163296472ba507fe36d0ff7773 Mon Sep 17 00:00:00 2001 From: bong-water-water-bong Date: Thu, 12 Nov 2026 13:54:31 -0400 Subject: [PATCH] copyright: refuse outside a work tree with a diagnostic, not a traceback (engine#2183) `listing()` ran `git ls-files` with `check=True`, so outside any work tree the tool died with `subprocess.CalledProcessError: Command '['git', 'ls-files']' returned non-zero exit status 128` -- a bare traceback whose only visible line is `File "tools/copyright.py", line 398`. That failure mode is not cosmetic. engine#2183 has pushed six heads and every one of them dies at the first build step, `Copyright notice on every file`; the offender count has gone 7 -> 8 -> 9 -> 10 rather than to zero. A traceback reads as "the tool is broken" rather than "run it from the repository root", and the root is the one thing the caller has to change. Now: a diagnostic naming the failed command, its exit status, its last stderr line and the working directory, and exit 2 (distinct from 1, which means offenders were found). Also fixes a landmine this change walked into: the tool is its own false positive one line away. It carries the notice as a `NOTICE` string rather than as a header, so `short_notice()` only saw a full block because "Licensed under the Apache License" happened to sit on line 20 -- exactly `NOTICE_WINDOW`. Adding any line above it pushed the phrase out of the window and the tool reported itself, which would fail CI for any PR that adds an import near the top. It now carries the real 16-line header, so the phrase is on line 6 and the window has 14 lines of margin. --- tests/copyright_gate_test.sh | 22 ++++++++++++++++++++++ tools/copyright.py | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/tests/copyright_gate_test.sh b/tests/copyright_gate_test.sh index 09d4e6423..2f1e3b410 100755 --- a/tests/copyright_gate_test.sh +++ b/tests/copyright_gate_test.sh @@ -180,5 +180,27 @@ check "...and exactly one SPDX line survives the repair" \ "[ \$(grep -c 'SPDX-License-Identifier' \"$tmp/doubled.py\") -eq 1 ]" \ "$(grep -c 'SPDX-License-Identifier' "$tmp/doubled.py") SPDX line(s)" +# engine#2183: outside a repository `git ls-files` exits 128, and `check=True` turned that into a bare +# Python traceback. Six pushes to one PR were made without the header the tool was asking for, because a +# traceback reads as "the tool is broken" rather than "run it from the repository root". A directory that +# is not inside any work tree must get a diagnostic naming the cause and the remedy, and its own exit code. +# +# The status is captured rather than piped: this file runs with `pipefail`, so `python3 ... | grep -q` fails +# on the tool's own non-zero exit even when the pattern matches, and an arm written that way tests nothing. +nogit=$(mktemp -d) +cp "$root/tools/copyright.py" "$nogit/copyright.py" +nogit_out=$( cd "$nogit" && python3 copyright.py --check 2>&1 ) +nogit_rc=$( cd "$nogit" && python3 copyright.py --check >/dev/null 2>&1; echo $? ) +check "outside any work tree: a diagnostic, not a traceback" \ + "[ \$(printf %s \"\$nogit_out\" | grep -c Traceback) -eq 0 ]" \ + "$(printf %s "$nogit_out" | tail -3)" +check "...it names the remedy (the repository root)" \ + "printf %s \"\$nogit_out\" | grep -q 'root of the repository'" \ + "$nogit_out" +check "...and exits 2, distinct from 1 (offenders found)" \ + "[ \"\$nogit_rc\" -eq 2 ]" \ + "exit $nogit_rc" +rm -rf "$nogit" + echo "$([ $fail -eq 0 ] && echo PASS || echo FAIL)" exit $((fail)) \ No newline at end of file diff --git a/tools/copyright.py b/tools/copyright.py index 95bfff724..6655268ce 100755 --- a/tools/copyright.py +++ b/tools/copyright.py @@ -1,4 +1,20 @@ #!/usr/bin/env python3 +# +# Copyright 2026 bong-water-water-bong +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# """Check or add the copyright and Apache-2.0 notice on every tracked file. usage: tools/copyright.py --check # exit 1 and list files missing the notice @@ -10,6 +26,7 @@ """ import argparse import fnmatch +import os import re import subprocess import sys @@ -329,8 +346,22 @@ def main(): # notice (this happened: onnx/tests/ort_run_generic.sh passed --check and failed CI on the same # head). Ignored files stay out: a build artifact is not source. def listing(*args): - return subprocess.run(["git", "ls-files", *args], capture_output=True, text=True, - check=True).stdout.split("\n") + # engine#2183: outside a repository `git ls-files` exits 128, and with `check=True` that + # surfaced as a bare Python traceback. Six pushes to that PR were made without the header + # fix the tool was asking for, and a traceback reads as "the tool is broken" rather than + # "run it from the repository root" -- which is the one thing the caller has to change. + try: + return subprocess.run(["git", "ls-files", *args], capture_output=True, text=True, + check=True).stdout.split("\n") + except FileNotFoundError: + sys.exit("copyright: git is not on PATH, so the file set cannot be listed") + except subprocess.CalledProcessError as e: + detail = (e.stderr or "").strip().splitlines() + sys.stderr.write( + "copyright: `git ls-files` failed (exit %d)%s\n" + " run this from the root of the repository; the working directory is\n" + " %s\n" % (e.returncode, (": " + detail[-1]) if detail else "", os.getcwd())) + raise SystemExit(2) tracked = set(filter(None, listing())) untracked = set(filter(None, listing("--others", "--exclude-standard"))) files = sorted(tracked | untracked)