Skip to content

Commit 884d8ba

Browse files
authored
chore: update the copyright header at a specific line (#17)
Signed-off-by: behnazh-w <[email protected]>
1 parent 7a1f222 commit 884d8ba

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

scripts/dev_scripts/copyright-checker.sh

+31-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22

3-
# Copyright (c) 2022 - 2022, Oracle and/or its affiliates. All rights reserved.
3+
# Copyright (c) 2022 - 2023, Oracle and/or its affiliates. All rights reserved.
44
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
55

66
#
@@ -10,6 +10,7 @@
1010
files=$(git diff --cached --name-only)
1111
currentyear=$(date +"%Y")
1212
missing_copyright_files=()
13+
license_note="Licensed under the Universal Permissive License v 1.0 as shown at https:\/\/oss\.oracle\.com\/licenses\/upl\/\."
1314

1415

1516
for f in $files; do
@@ -29,25 +30,46 @@ done
2930

3031
if [ ${#missing_copyright_files[@]} -ne 0 ]; then
3132
for f in "${missing_copyright_files[@]}"; do
33+
34+
# Don't allow this script to run on itself.
35+
if [[ $0 == $f ]];then
36+
echo "Cannot run the $0 on itself. Please fix the headers in this file manually."
37+
exit 1
38+
fi
39+
missing_license_note=$(grep -i "$license_note" "$f")
3240
startyear=$(git log --format=%ad --date=format:%Y "$f" | tail -1)
3341
if [[ -z "${startyear// }" ]]; then
3442
startyear=$currentyear
3543
fi
3644
if [[ $f =~ .*\.(js$|java$|go$|dl$) ]]; then
3745
expected="\/\* Copyright \(c\) $startyear - $currentyear, Oracle and\/or its affiliates\. All rights reserved\. \*\/"
38-
expected="$expected\n\/\* Licensed under the Universal Permissive License v 1.0 as shown at https:\/\/oss\.oracle\.com\/licenses\/upl\/\. \*\/"
46+
if [ ${#missing_license_note} -eq 0 ]; then
47+
expected="$expected\n\/\* $license_note \*\/"
48+
fi
3949
elif [[ $f =~ .*\.(py$|tf$|sh$|yaml$) ]] || [[ "${f##*/}" = "Dockerfile" ]]; then
4050
expected="# Copyright \(c\) $startyear - $currentyear, Oracle and\/or its affiliates\. All rights reserved\."
41-
expected="$expected\n# Licensed under the Universal Permissive License v 1.0 as shown at https:\/\/oss\.oracle\.com\/licenses\/upl\/\."
42-
51+
if [ ${#missing_license_note} -eq 0 ]; then
52+
expected="$expected\n# $license_note"
53+
fi
4354
fi
4455

45-
if ! grep -i -e "Copyright (c) .* Oracle and/or its affiliates. All rights reserved" "$f" 1>/dev/null;then
46-
echo "Copyright header missing for $f"
47-
sed -i "1s/^/$expected\n\n/" "$f"
56+
# Find the first matching copyright line.
57+
line_number=$(grep -m 1 -n -i -e "Copyright (c) .* Oracle and/or its affiliates. All rights reserved" "$f" | cut -d : -f 1)
58+
if [[ -z "$line_number" ]]; then
59+
echo "Copyright header missing for $f."
60+
61+
# Check for executable scripts and don't replace the first line starting with shebang.
62+
shebang_line=$(grep -m 1 -n "#!" "$f")
63+
if [[ -z "$shebang_line" ]];then
64+
# If there is no shebang, insert at the first line.
65+
sed -i "1s/^/$expected\n\n/" "$f"
66+
else
67+
# If there is a shebang, append to the end of the line.
68+
sed -i "$(echo $shebang_line | cut -d : -f 1)""s/$/\n\n$expected/" "$f"
69+
fi
4870
else
49-
echo "Copyright header needs update for $f"
50-
sed -i "1s/^.*/$expected/" "$f"
71+
echo "Copyright header needs update for $f."
72+
sed -i "$line_number""s/^.*/$expected/" "$f"
5173
fi
5274
done
5375
echo "Copyright headers have been automatically added/updated. Please review and stage the changes before running git commit again."

0 commit comments

Comments
 (0)