|
| 1 | +# Copyright 2021 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +usage(){ |
| 16 | + echo "Usage: $0 [options] |
| 17 | + options: |
| 18 | + -g enable github log format" |
| 19 | +} |
| 20 | + |
| 21 | +github_log=0 |
| 22 | +IFS=',' # split options on ',' characters |
| 23 | +while getopts "hg" opt; do |
| 24 | + case $opt in |
| 25 | + h) |
| 26 | + usage |
| 27 | + exit 0 |
| 28 | + ;; |
| 29 | + g) |
| 30 | + github_log=1 |
| 31 | + ;; |
| 32 | + *) |
| 33 | + echo "unknown parameter" |
| 34 | + exit 2 |
| 35 | + ;; |
| 36 | + esac |
| 37 | +done |
| 38 | + |
| 39 | + |
| 40 | +# Check source files for copyright notices |
| 41 | + |
| 42 | +options=( |
| 43 | + -E # Use extended regexps |
| 44 | + -I # Exclude binary files |
| 45 | + -L # Show files that don't have a match |
| 46 | + 'Copyright.*[0-9]{4}.*Google' |
| 47 | +) |
| 48 | + |
| 49 | +result=$(git grep "${options[@]}" -- \ |
| 50 | + '*.'{c,cc,cmake,h,js,m,mm,py,rb,sh,swift} \ |
| 51 | + CMakeLists.txt '**/CMakeLists.txt' \ |
| 52 | + ':(exclude)**/third_party/**' \ |
| 53 | + ':(exclude)**/external/**') |
| 54 | + |
| 55 | +if [[ $result ]]; then |
| 56 | + if [[ $github_log -eq 1 ]]; then |
| 57 | + echo -n "::error ::Missing copyright notices in the following files:%0A" |
| 58 | + # Print all files with %0A instead of newline. |
| 59 | + echo "$result" | sed ':a;N;$!ba;s/\n/%0A/g' |
| 60 | + else |
| 61 | + echo "$result" |
| 62 | + echo "ERROR: Missing copyright notices in the files above. Please fix." |
| 63 | + fi |
| 64 | + exit 1 |
| 65 | +fi |
0 commit comments