github.com/vmware/go-vcloud-director/v2@v2.24.0/scripts/copyright_check.sh (about) 1 #!/usr/bin/env bash 2 # This script will find code files that don't have a copyright notice 3 # or the ones with an outdated copyright. 4 # 5 # The checks will fail if: 6 # a) the source file does not have a copyright header 7 # b) the source file has a copyright header from last year, but it was modified this year 8 9 10 # This check will find files with a copyright for any year 11 vmware_any_copyright='Copyright \d\d\d\d VMware' 12 13 this_year=$(date +%Y) 14 last_year=$((this_year-1)) 15 16 # This check will find files with a copyright for the current year 17 vmware_latest_copyright="Copyright $this_year VMware" 18 19 # This check will find files with a copyright for last year 20 vmware_last_year_copyright="Copyright $last_year VMware" 21 exit_code=0 22 23 modified_files=$(git status -uno | grep "modified:" | awk '{print $2}') 24 25 function is_modified { 26 fname=$1 27 for fn in $modified_files 28 do 29 if [ "$fname" == "$fn" -o "$fname" == "./$fn" ] 30 then 31 echo yes 32 fi 33 done 34 } 35 36 for F in $(find . -name '*.go' | grep -v '/vendor/' ) 37 do 38 modified_not_committed_yet=$(is_modified $F) 39 copyright_found="" 40 for line_num in 1 2 3 41 do 42 # Looks for copyright in the Nth line of the file 43 has_any_copyright=$(head -n $line_num $F | tail -n 1 | grep "$vmware_any_copyright" ) 44 has_latest_copyright=$(head -n $line_num $F | tail -n 1 | grep "$vmware_latest_copyright" ) 45 has_last_year_copyright=$(head -n $line_num $F | tail -n 1 | grep "$vmware_last_year_copyright" ) 46 47 if [ -n "$has_latest_copyright" ] 48 then 49 if [ "$1" == "-v" -o "$1" == "--verbose" ] 50 then 51 echo "$F: latest copyright found in line $line_num" 52 fi 53 copyright_found=$line_num 54 elif [ -n "$has_last_year_copyright" ] 55 then 56 commit_date=$(git log -1 --format="%cd" $F) 57 update_label=committed 58 59 # The file is updated this year if the commit date contains the current year 60 committed_this_year=$(echo "$commit_date" | grep -w $this_year) 61 62 # The file is also updated this year if it is in the list of modified files 63 # (not committed yet) 64 if [ -n "$modified_not_committed_yet" ] 65 then 66 update_label=modified 67 fi 68 69 if [ -n "$modified_not_committed_yet" -o "$committed_this_year" ] 70 then 71 echo "$F $update_label this year, but has last year's copyright" 72 exit_code=1 73 fi 74 copyright_found=$line_num 75 elif [ -n "$has_any_copyright" ] 76 then 77 echo "$F: older copyright found in line $line_num" 78 echo "$has_any_copyright" 79 copyright_found=$line_num 80 fi 81 done 82 if [ -z "$copyright_found" ] 83 then 84 echo "File $F has no valid copyright" 85 exit_code=1 86 fi 87 done 88 exit $exit_code