Skip to content

Commit

Permalink
chore: optimize/harden the license script
Browse files Browse the repository at this point in the history
This script is still a little awkward, but it's now 30x faster and more
correct:

1. It will auto-update the copyright year.
2. It will apply the correct author (Filecoin Core Devs).
3. It will work without committing files first.
4. It will insert lines in the correct order.
  • Loading branch information
Stebalien committed Oct 22, 2024
1 parent 4cb7342 commit 226a7c0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 33 deletions.
57 changes: 26 additions & 31 deletions scripts/add_license.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,35 @@
#
# Checks if the source code contains required license and adds it if necessary.
# Returns 1 if there was a missing license, 0 otherwise.
COPYRIGHT_TXT=$(dirname $0)/copyright.txt

# Any year is fine. We can update the year as a single PR in all files that have it up to last year.
PAT_PL=".*// Copyright 2021-202\d Protocol Labs.*"
# Files that were taken from ChainSafe and not modified can be left as-is.
PAT_CS=".*// Copyright 2019-2022 ChainSafe Systems.*"
PAT_SPDX="/*// SPDX-License-Identifier: Apache-2.0, MIT.*"

# Look at enough lines so that we can include multiple copyright holders.
LINES=4

YEAR="$(date '+%Y')"
ret=0
set -e -x

# Look for files without headers.
for file in $(git grep --cached -Il '' -- '*.rs'); do
header=$(head -$LINES "$file")
if ! echo "$header" | grep -q -P "$PAT_SPDX"; then
echo "$file was missing header"
cat $COPYRIGHT_TXT "$file" > temp
mv temp "$file"
ret=1
fi
done
while read -r file; do
echo "$file was missing header"
sed -i "$file" -f - <<EOF
1i\
// SPDX-License-Identifier: Apache-2.0, MIT'
EOF
ret=1
done < <(git grep -IL "^// SPDX-License-Identifier:" -- '*.rs')

# Look for changes that don't have the new copyright holder.
for file in $(git diff --diff-filter=d --name-only master -- '*.rs'); do
header=$(head -$LINES "$file")
if ! echo "$header" | grep -q -P "$PAT_PL"; then
echo "$file was missing Protocol Labs"
head -1 $COPYRIGHT_TXT > temp
cat "$file" >> temp
mv temp "$file"
ret=1
fi
done
while read -r file; do
if grep -q "^// Copyright \([0-9]\+-\)\?${YEAR} Filecoin Core Devs" "$file"; then
continue
fi
ret=1
if grep -q "^// Copyright [0-9]\+\(-[0-9]\+\)\? Filecoin Core Devs" "$file"; then
# Update the copyright line if available.
script="s|^// Copyright \([0-9]\+\)\(-[0-9]\+\)\? Filecoin Core Devs|// Copyright \1-${YEAR} Filecoin Core Devs|"
else
# Otherwise, add the new one _AFTER_ the SDPX header.
script="/^\/\/ SPDX-License-Identifier: /a\
// Copyright ${YEAR} Filecoin Core Devs
"
fi
sed -i "$file" -e "$script"
done < <(git diff --diff-filter=DM --name-only --merge-base master -- '*.rs')

exit $ret
2 changes: 0 additions & 2 deletions scripts/copyright.txt

This file was deleted.

0 comments on commit 226a7c0

Please sign in to comment.