From 4d728ee2dd3f19acf1b1fe7e2d527d1bb2cfbf4d Mon Sep 17 00:00:00 2001 From: bong-water-water-bong Date: Wed, 11 Nov 2026 19:15:16 -0400 Subject: [PATCH] copyright: --check's duplicate report names the command that repairs it, instead of telling the reader to edit by hand before: copyright notice duplicated inside the header window (delete the second copy): doubled.py after: copyright notice duplicated inside the header window (--fix keeps the first copy and drops the rest): doubled.py That advice was written when `--fix` genuinely could not repair a doubled block: it printed `repaired:` and left the copy, which #2192 fixed. So the reader was being told to do by hand the one thing the tool had just learned to do, and the tool's own summary line already says `run: python3 tools/copyright.py --fix` two lines further down -- two pieces of advice that disagreed about whether a person should edit the file. One arm pins the wording, and it is worth having because the wording is now a claim about the tool's behaviour rather than a suggestion: with the old text the arm fails, with this text it passes. Two traps met while writing that arm, both recorded in the test: * `python copyright.py --check | grep -q ...` always fails here: the file runs under `set -o pipefail` and `--check` exits 1 BY DESIGN for the duplicate it just reported, so the pipeline's status is the checker's, not grep's -- the same trap as `cmd | tail` making `$?` the tail's. * `\"` inside `$( )` inside a double-quoted argument is not unescaped by bash, so a nested `case "$( cd \"$tmp\" ... )"` fed the quote characters into the path and `cd` failed on `"/tmp/tmp.XXXX"`. The output is captured into a variable first and the arm compares that. --- tests/copyright_gate_test.sh | 11 +++++++++++ tools/copyright.py | 4 ++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/copyright_gate_test.sh b/tests/copyright_gate_test.sh index f73a8d4a2..09d4e6423 100755 --- a/tests/copyright_gate_test.sh +++ b/tests/copyright_gate_test.sh @@ -161,6 +161,17 @@ check "...and exactly one SPDX line is left in the window" \ # left the copy in place, so the file it had just written still failed the very check that sent it there. printf '%s%s\nx = 1\n' "$NOTICE_BLOCK" "$NOTICE_BLOCK" > "$tmp/doubled.py" git -C "$tmp" add doubled.py +# (checked with `case`, not `python --check | grep -q`: this file runs under `set -o pipefail` and `--check` exits 1 BY DESIGN +# for the duplicate it just reported, so the pipeline would return 1 even when grep matched -- the same `cmd | grep` trap that +# makes `$?` the pipeline's rather than the command's.) +# the report must name the command that repairs it: the old text said "delete the second copy", i.e. hand-edit a file that +# `--fix` had been failing on and now repairs (engine#2192), so a reader was told to do by hand what the tool does. +# assigned first, not nested: `\"` inside `$( )` inside a double-quoted argument is NOT unescaped by bash, so a nested form +# comparing the captured output passes the quote characters into the path (`cd: "/tmp/..."`). +dup_report=$( cd "$tmp" && python3 copyright.py --check 2>&1 ) +check "the duplicate report names --fix rather than telling the reader to edit by hand" \ + "case \"\$dup_report\" in *'keeps the first copy'*) true ;; *) false ;; esac" \ + "$( cd "$tmp" && python3 copyright.py --check 2>&1 | grep duplicated )" ( cd "$tmp" && python3 copyright.py --fix >/dev/null 2>&1 ) check "--fix on a doubled block leaves a file that its own --check accepts" \ "! ( cd \"$tmp\" && python3 copyright.py --check 2>&1 | grep -q duplicated )" \ diff --git a/tools/copyright.py b/tools/copyright.py index f705f0b89..65e993989 100755 --- a/tools/copyright.py +++ b/tools/copyright.py @@ -278,7 +278,7 @@ def repair_notice(text, block): # A SECOND canonical notice is the same defect as a near-miss beside the first: the block pasted twice. `--check` # has always reported it (`duplicated_notice`); `--fix` used to print "repaired" and leave the copy in place, so the # file it had just written still failed the very check that sent it there. The first occurrence is kept and later - # ones dropped, which is what "delete the second copy" means. + # ones dropped, which is what the report line now tells the reader --fix will do. if canonical_notice_line(line): key = re.sub(r"^\W+", "", line).strip() if key in seen_canonical: @@ -363,7 +363,7 @@ def listing(*args): missing.append(path) for path in duplicated: - print("copyright notice duplicated inside the header window (delete the second copy):", path) + print("copyright notice duplicated inside the header window (--fix keeps the first copy and drops the rest):", path) for path in unknown: print("no comment style known (add one to LINE_STYLES/BLOCK_STYLES or EXEMPT):", path) for path in missing: