2411: Fix liquid comment template and skip top matter in docs.
[arvados.git] / build / check-copyright-notices
1 #!/bin/bash
2 # Copyright (C) The Arvados Authors. All rights reserved.
3 #
4 # SPDX-License-Identifier: AGPL-3.0
5
6 set -e
7
8 fix=false
9 while [[ "${@}" != "" ]]
10 do
11     arg=${1}
12     shift
13     case ${arg} in
14         --help)
15             cat <<EOF
16 Usage: $0 [--fix] [-- git-ls-args...]
17
18 Options:
19
20 --fix   Insert missing copyright notices where possible.
21
22 Git arguments:
23
24 Arguments after "--" are passed to \`git ls-files\`; this can be used to
25 restrict the set of files to check.
26
27 EOF
28             exit 2
29             ;;
30         --fix)
31             fix=true
32             ;;
33         --)
34             break
35             ;;
36         *)
37             echo >&2 "Unrecognized argument '${arg}'. Try $0 --help"
38             exit 2
39             ;;
40     esac
41 done
42
43 fixer() {
44     want="${want}" perl -pi~ - "${1}" <<'EOF'
45 BEGIN { undef $/ }
46 s{^(\#\!.*?\n|\n*---\n.*?\n\.\.\.\n\n?)?}{${1}$ENV{want}\n\n}ms
47 EOF
48 }
49
50 IFS=$'\n' read -a ignores -r -d $'\000' <.licenseignore || true
51 result=0
52 git ls-files -z ${@} | \
53     while read -rd $'\000' fnm
54     do
55         grepAfter=2
56         grepBefore=0
57         cs=
58         cc=
59         ce=
60         fixer=
61         if [[ ! -f ${fnm} ]] || [[ -L ${fnm} ]] || [[ ! -s ${fnm} ]]
62         then
63             continue
64         fi
65
66         ignore=
67         for pattern in "${ignores[@]}"
68         do
69             if [[ ${fnm} == ${pattern} ]]
70             then
71                 ignore=1
72             fi
73         done
74         if [[ ${ignore} = 1 ]]; then continue; fi
75
76         case ${fnm} in
77             Makefile | */Makefile \
78                 | *.dockerfile | */Dockerfile.* | */Dockerfile | *.dockerignore \
79                 | */MANIFEST.in | */fuse.conf | */gitolite.rc \
80                 | *.pl | *.pm | *.PL \
81                 | *.rb | *.rb.example | *.rake | *.ru \
82                 | *.gemspec | */Gemfile | */Rakefile \
83                 | services/login-sync/bin/* \
84                 | sdk/cli/bin/* \
85                 | *.py \
86                 | sdk/python/bin/arv-* \
87                 | sdk/cwl/bin/* \
88                 | services/nodemanager/bin/* \
89                 | services/fuse/bin/* \
90                 | tools/crunchstat-summary/bin/* \
91                 | crunch_scripts/* \
92                 | *.yaml | *.yml | *.yml.example | *.cwl \
93                 | *.sh | *.service \
94                 | */run | */run-service | */restart-dns-server \
95                 | */nodemanager/doc/*.cfg \
96                 | */nginx.conf \
97                 | build/build.list)
98                 fixer=fixer
99                 cc="#"
100                 ;;
101             *.md)
102                 fixer=fixer
103                 cc="[//]: #"
104                 ;;
105             *.rst)
106                 fixer=fixer
107                 cc=".."
108                 ;;
109             *.erb)
110                 fixer=fixer
111                 cs="<%#"
112                 cc=""
113                 ce=" %>"
114                 ;;
115             *.liquid)
116                 fixer=fixer
117                 cs=$'{% comment %}\n'
118                 cc=
119                 ce=$'\n{% endcomment %}'
120                 grepAfter=3
121                 grepBefore=1
122                 ;;
123             *.textile)
124                 fixer=fixer
125                 cs="###."
126                 cc="...."
127                 ce=
128                 ;;
129             *.css)
130                 fixer=fixer
131                 cs="/*"
132                 cc=""
133                 ce=" */"
134                 ;;
135             *.go | *.scss | *.java | *.js | *.coffee)
136                 fixer=fixer
137                 cc="//"
138                 ;;
139             *.sql)
140                 fixer=fixer
141                 cc="--"
142                 ;;
143             *.html | *.svg)
144                 fixer=fixer
145                 cs="<!-- "
146                 cc=
147                 ce=" -->"
148                 ;;
149             *)
150                 cc="#"
151                 hashbang=$(head -n1 ${fnm})
152                 if [[ ${hashbang} = "#!/bin/sh" ]] ||  [[ ${hashbang} = "#!/bin/bash" ]]
153                 then
154                     fixer=fixer
155                 fi
156                 ;;
157         esac
158         wantGPL="${cs:-${cc}} Copyright (C) The Arvados Authors. All rights reserved.
159 ${cc}
160 ${cc}${cc:+ }SPDX-License-Identifier: AGPL-3.0${ce}"
161         wantApache="${cs:-${cc}} Copyright (C) The Arvados Authors. All rights reserved.
162 ${cc}
163 ${cc}${cc:+ }SPDX-License-Identifier: Apache-2.0${ce}"
164         wantBYSA="${cs:-${cc}} Copyright (C) The Arvados Authors. All rights reserved.
165 ${cc}
166 ${cc}${cc:+ }SPDX-License-Identifier: CC-BY-SA-3.0${ce}"
167         found=$(head "$fnm" | egrep -A${grepAfter} -B${grepBefore} 'Copyright.*Arvados' || true)
168         case ${fnm} in
169             Makefile | build/* | lib/* | tools/* | apps/* | services/*)
170                 want=${wantGPL}
171                 ;;
172             crunch_scripts/* | backports/* | docker/* | sdk/*)
173                 want=${wantApache}
174                 ;;
175             README.md | doc/*)
176                 want=${wantBYSA}
177                 ;;
178             *)
179                 want=
180                 ;;
181         esac
182         case "$found" in
183             "$wantGPL")
184                 ;;
185             "$wantApache")
186                 ;;
187             "$wantBYSA")
188                 ;;
189             "")
190                 if [[ -z ${found} ]] && [[ -n ${want} ]] && [[ $fix = true ]] && [[ $fixer != "" ]]
191                 then
192                     ${fixer} ${fnm}
193                 else
194                     echo "missing copyright notice: $fnm"
195                     result=1
196                 fi
197                 ;;
198             *)
199                 echo "nonstandard copyright notice: $fnm '${found}'"
200                 result=1
201                 ;;
202         esac
203     done
204 exit $result