|
1 | 1 | #!/usr/bin/env bash
|
2 | 2 |
|
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. |
4 | 4 | # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/.
|
5 | 5 |
|
6 | 6 | #
|
|
10 | 10 | files=$(git diff --cached --name-only)
|
11 | 11 | currentyear=$(date +"%Y")
|
12 | 12 | missing_copyright_files=()
|
| 13 | +license_note="Licensed under the Universal Permissive License v 1.0 as shown at https:\/\/oss\.oracle\.com\/licenses\/upl\/\." |
13 | 14 |
|
14 | 15 |
|
15 | 16 | for f in $files; do
|
|
29 | 30 |
|
30 | 31 | if [ ${#missing_copyright_files[@]} -ne 0 ]; then
|
31 | 32 | 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") |
32 | 40 | startyear=$(git log --format=%ad --date=format:%Y "$f" | tail -1)
|
33 | 41 | if [[ -z "${startyear// }" ]]; then
|
34 | 42 | startyear=$currentyear
|
35 | 43 | fi
|
36 | 44 | if [[ $f =~ .*\.(js$|java$|go$|dl$) ]]; then
|
37 | 45 | 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 |
39 | 49 | elif [[ $f =~ .*\.(py$|tf$|sh$|yaml$) ]] || [[ "${f##*/}" = "Dockerfile" ]]; then
|
40 | 50 | 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 |
43 | 54 | fi
|
44 | 55 |
|
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 |
48 | 70 | 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" |
51 | 73 | fi
|
52 | 74 | done
|
53 | 75 | echo "Copyright headers have been automatically added/updated. Please review and stage the changes before running git commit again."
|
|
0 commit comments