|
2 | 2 | # Usage: ./set_copyright_year.sh [PATHS] |
3 | 3 | # |
4 | 4 | # 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. |
9 | 11 | # |
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. |
12 | 16 | # |
13 | 17 | # Run this script with --check to have it raise an error if it |
14 | 18 | # would change anything. |
|
17 | 21 | EXIT_CODE=0 |
18 | 22 |
|
19 | 23 | # 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="" |
25 | 32 |
|
| 33 | +# Validate the copyright year of each source file |
26 | 34 | while read -rd $'\0' year file; do |
27 | 35 |
|
| 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 | + |
28 | 41 | # Extract the first year from the copyright notice |
29 | 42 | current_year=$(sed -n '1,2s/^\(# Copyright (c) \)\([[:digit:]]\{4,\}\).*/\2/p' "$file") |
30 | 43 |
|
31 | | - # Skip the file if no year is found |
| 44 | + # Skip the source file if no year is found |
32 | 45 | if [[ -z "$current_year" ]]; then |
33 | 46 | continue |
34 | 47 | fi |
35 | 48 |
|
| 49 | + # If in check mode, report the incorrect copyright year |
36 | 50 | if $CHECK_MODE && [[ "$current_year" != "$year" ]]; then |
37 | 51 | 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 |
40 | 54 | fi |
41 | 55 |
|
| 56 | + # Otherwise rewrite the incorrect copyright year |
42 | 57 | if ! $CHECK_MODE && [[ "$current_year" != "$year" ]]; then |
43 | 58 | sed -i "1,2s/^\(# Copyright (c) \)[[:digit:]]\{4,\}/\1$year/" "$file" |
44 | 59 | echo "Updated copyright year in $file" |
45 | 60 | fi |
46 | 61 | done < <(git ls-files -z "$@" | xargs -0I{} git log -1 -z --format="%cd {}" --date="format:%Y" -- "{}") |
47 | 62 |
|
| 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 | + |
48 | 76 | exit $EXIT_CODE |
0 commit comments