Skip to content

Commit 28e4525

Browse files
committed
toolshed: fix copyright-year check to work outside staged-file workflows
Replace is_staged() with last_modified_year(), which consults git status for working-tree changes and git log -1 for committed files. The old is_staged() gate used git diff --staged as a proxy for 'was this file changed recently', which only holds during a git commit. Under pre-commit run --all-files or CI nothing is staged, so the gate short-circuited to 'return True' and the year was never checked. Also change fetch-depth from 1 to 0 in the pre-commit CI workflow. git log -1 needs enough history to find when each file was last touched; a depth-1 shallow clone returns empty output for files not changed in the tip commit, causing silent false passes on stale copyright years. Fixes NVIDIA#2273.
1 parent 526b119 commit 28e4525

2 files changed

Lines changed: 21 additions & 7 deletions

File tree

‎.github/workflows/pre-commit.yml‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,14 @@ jobs:
5858

5959
# PR workflows run from the merge revision; check out that same revision
6060
# so their helper scripts and configuration stay in sync.
61+
# Full history is required so that check_spdx.py can call `git log -1`
62+
# to determine when each file was last modified (used for copyright-year
63+
# enforcement). A shallow clone would return no history for files not
64+
# touched in the tip commit, causing false passes on stale years.
6165
- name: Checkout repository
6266
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
6367
with:
64-
fetch-depth: 1
68+
fetch-depth: 0
6569
persist-credentials: false
6670

6771
- name: Set up Python

‎toolshed/check_spdx.py‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,24 @@ def load_spdx_ignore():
7171
CURRENT_YEAR = str(datetime.datetime.now(tz=datetime.UTC).year)
7272

7373

74-
def is_staged(filepath):
75-
# If the file is staged, we need to update it to the current year
76-
process = subprocess.run( # noqa: S603
77-
["git", "diff", "--staged", "--", filepath], # noqa: S607
74+
def last_modified_year(filepath):
75+
# Staged or unstaged working-tree changes mean the file is being modified now.
76+
status = subprocess.run( # noqa: S603
77+
["git", "status", "--porcelain", "--", filepath], # noqa: S607
7878
capture_output=True,
7979
text=True,
8080
)
81-
return process.stdout.strip() != ""
81+
if status.stdout.strip():
82+
return int(CURRENT_YEAR)
83+
# Find the most recent commit that touched this file.
84+
log = subprocess.run( # noqa: S603
85+
["git", "log", "-1", "--format=%cd", "--date=format:%Y", "--", filepath], # noqa: S607
86+
capture_output=True,
87+
text=True,
88+
)
89+
year_str = log.stdout.strip()
90+
# Fall back to current year for brand-new untracked files with no git history.
91+
return int(year_str) if year_str else int(CURRENT_YEAR)
8292

8393

8494
def normalize_repo_path(filepath):
@@ -165,7 +175,7 @@ def validate_or_fix_copyright(filepath, blob, fix):
165175
else:
166176
start_year = end_year = years
167177

168-
if not is_staged(filepath) or int(end_year) >= int(CURRENT_YEAR):
178+
if int(end_year) >= last_modified_year(filepath):
169179
return True, blob
170180

171181
print(f"OUTDATED copyright {years!r} (expected {CURRENT_YEAR!r}) in {filepath!r}")

0 commit comments

Comments
 (0)