Skip to content

Commit 265a5e2

Browse files
committed
Fix creation year lookup; use POSIX pathspec
1 parent b7709c9 commit 265a5e2

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

template/tools/add_spdx.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,25 @@ def get_file_creation_year(file_path: Union[str, Path]) -> str:
4646
root = Path(repo.working_tree_dir).resolve()
4747
rel_path = file_path.resolve().relative_to(root)
4848

49-
output = repo.git.log(
49+
rel_path_git = rel_path.as_posix() # IMPORTANT for git pathspec
50+
51+
# Get the year when the file was first added to Git history.
52+
# NOTE: Do not combine `--reverse` with `--max-count=1` here, as it can
53+
# yield an empty result with some Git versions. Instead, get the full
54+
# filtered output and take the first line.
55+
log_output = repo.git.log(
5056
'--follow',
5157
'--diff-filter=A',
5258
'--reverse',
53-
'--max-count=1',
5459
'--format=%ad',
5560
'--date=format:%Y',
5661
'--',
57-
str(rel_path),
62+
rel_path_git,
5863
).strip()
5964

60-
return output or str(datetime.now().year)
65+
year = log_output.splitlines()[0].strip() if log_output else ''
66+
67+
return year or str(datetime.now().year)
6168

6269

6370
def get_org_url(repo_path: Union[str, Path]) -> str:

0 commit comments

Comments
 (0)