Skip to content

Commit fdc5144

Browse files
committed
etc/scripts/set_copyright_year.sh: Fix exit bug and check LICENSE
Previously, the `EXIT_CODE` of the copyright year check was hardcoded to the value 0, causing the CI to pass unconditionally. This fixes the bug by coupling the `EXIT_CODE` to the result of the copyright year check. Moreover, the check has been extended to additionally verify the copyright year of the `LICENSE` file. Fixes #458
1 parent 10ff4e4 commit fdc5144

1 file changed

Lines changed: 42 additions & 14 deletions

File tree

etc/scripts/set_copyright_year.sh

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22
# Usage: ./set_copyright_year.sh [PATHS]
33
#
44
# This is a small script for setting the correct copyright year
5-
# for each given file (i.e. the year the file was last changed).
6-
# Instead of file paths you can also specify directories, in which
7-
# case the script will attempt to set the copyright year for all
8-
# files in the given directories. Globbing is also possible.
5+
# for each given source file (i.e. the year the file was last
6+
# changed) and LICENSE file (i.e. the latest modification year
7+
# across all files). Instead of file paths you can also specify
8+
# directories, in which case the script will attempt to set the
9+
# copyright year for all files in the given directories.
10+
# Globbing is also possible.
911
#
10-
# The script will check the first two lines for a copyright
11-
# notice (in case the first line is a shebang).
12+
# In source files, the script checks the first two lines for a
13+
# copyright notice (in case the first line is a shebang).
14+
# In the LICENSE file, it checks the first three lines for a
15+
# copyright notice containing a year range.
1216
#
1317
# Run this script with --check to have it raise an error if it
1418
# would change anything.
@@ -17,32 +21,56 @@
1721
EXIT_CODE=0
1822

1923
# Set CHECK_MODE based on whether --check is passed
20-
CHECK_MODE=false
21-
if [[ "$1" == "--check" ]]; then
22-
CHECK_MODE=true
23-
shift # Remove --check from the arguments
24-
fi
24+
CHECK_MODE=false
25+
if [[ "$1" == "--check" ]]; then
26+
CHECK_MODE=true
27+
shift # Remove --check from the arguments
28+
fi
29+
30+
# Initialise a variable to track the latest modification year across all files
31+
max_year=""
2532

33+
# Validate the copyright year of each source file
2634
while read -rd $'\0' year file; do
2735

36+
# Extract the current modification year and update variable
37+
if [[ -z "$max_year" || "$year" -gt "$max_year" ]]; then
38+
max_year="$year"
39+
fi
40+
2841
# Extract the first year from the copyright notice
2942
current_year=$(sed -n '1,2s/^\(# Copyright (c) \)\([[:digit:]]\{4,\}\).*/\2/p' "$file")
3043

31-
# Skip the file if no year is found
44+
# Skip the source file if no year is found
3245
if [[ -z "$current_year" ]]; then
3346
continue
3447
fi
3548

49+
# If in check mode, report the incorrect copyright year
3650
if $CHECK_MODE && [[ "$current_year" != "$year" ]]; then
3751
echo "Error: Copyright year mismatch in file $file. Expected $year, found $current_year."
38-
# Set ERROR_CODE to 1 to indicate mismatch
39-
ERROR_CODE=1
52+
# Set EXIT_CODE to 1 to indicate mismatch
53+
EXIT_CODE=1
4054
fi
4155

56+
# Otherwise rewrite the incorrect copyright year
4257
if ! $CHECK_MODE && [[ "$current_year" != "$year" ]]; then
4358
sed -i "1,2s/^\(# Copyright (c) \)[[:digit:]]\{4,\}/\1$year/" "$file"
4459
echo "Updated copyright year in $file"
4560
fi
4661
done < <(git ls-files -z "$@" | xargs -0I{} git log -1 -z --format="%cd {}" --date="format:%Y" -- "{}")
4762

63+
# Validate the copyright year of the LICENSE file
64+
license_current_year=$(sed -n '1,3{s/^Copyright (c) [[:digit:]]\{4\}-\([[:digit:]]\{4\}\).*/\1/p}' LICENSE)
65+
66+
if $CHECK_MODE && [[ -n "$license_current_year" && "$license_current_year" != "$max_year" ]]; then
67+
echo "Error: Copyright year mismatch in file LICENSE. Expected $max_year, found $license_current_year."
68+
EXIT_CODE=1
69+
fi
70+
71+
if ! $CHECK_MODE && [[ -n "$license_current_year" && "$license_current_year" != "$max_year" ]]; then
72+
sed -i "1,3s/^\(Copyright (c) [[:digit:]]\{4\}-\)[[:digit:]]\{4\}/\1$max_year/" LICENSE
73+
echo "Updated copyright year in LICENSE"
74+
fi
75+
4876
exit $EXIT_CODE

0 commit comments

Comments
 (0)