| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# Ensure script is run at the root of a git repository |
| 4 |
git rev-parse --is-inside-work-tree &>/dev/null || { echo "Not inside a git repository"; exit 1; } |
| 5 |
|
| 6 |
# Find all .go files tracked by git |
| 7 |
git ls-files "*.go" | while read -r file; do |
| 8 |
# Skip files that don't have a copyright belonging to "The NATS Authors" |
| 9 |
current_copyright=$(grep -oE "^// Copyright [0-9]{4}(-[0-9]{4})? The NATS Authors" "$file" || echo "") |
| 10 |
[[ -z "$current_copyright" ]] && continue |
| 11 |
|
| 12 |
# Get the last commit year for the file, ignore commit messages containing the word "copyright" |
| 13 |
last_year=$(git log --follow --format="%ad" --date=format:%Y --grep="(C|c)opyright" --invert-grep -n 1 -- "$file") |
| 14 |
existing_years=$(echo "$current_copyright" | grep -oE "[0-9]{4}(-[0-9]{4})?") |
| 15 |
|
| 16 |
# Determine the new copyright range |
| 17 |
if [[ "$existing_years" =~ ^([0-9]{4})-([0-9]{4})$ ]]; then |
| 18 |
first_year=${BASH_REMATCH[1]} |
| 19 |
new_copyright="// Copyright $first_year-$last_year The NATS Authors" |
| 20 |
elif [[ "$existing_years" =~ ^([0-9]{4})$ ]]; then |
| 21 |
first_year=${BASH_REMATCH[1]} |
| 22 |
if [[ "$first_year" == "$last_year" ]]; then |
| 23 |
new_copyright="// Copyright $first_year The NATS Authors" |
| 24 |
else |
| 25 |
new_copyright="// Copyright $first_year-$last_year The NATS Authors" |
| 26 |
fi |
| 27 |
else |
| 28 |
continue # If the format is somehow incorrect, skip the file |
| 29 |
fi |
| 30 |
|
| 31 |
# Update the first line |
| 32 |
if sed --version &>/dev/null; then |
| 33 |
# Linux sed |
| 34 |
sed -i "1s|^// Copyright.*|$new_copyright|" "$file" |
| 35 |
else |
| 36 |
# BSD/macOS sed, needs -i '' |
| 37 |
sed -i '' "1s|^// Copyright.*|$new_copyright|" "$file" |
| 38 |
fi |
| 39 |
done |