Merge branch 'main' into 15814-wb2-secrets
[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|<\?xml.*?>\n)\n?)?}{${2}$ENV{want}\n\n}ms
47 EOF
48 }
49
50 IFS=$'\n' read -a ignores -r -d $'\000' <.licenseignore || true
51 result=0
52
53 coproc git ls-files -z ${@} </dev/null
54 while read -rd $'\000' fnm
55 do
56     grepAfter=2
57     grepBefore=0
58     cs=
59     cc=
60     ce=
61     fixer=
62     if [[ ! -f ${fnm} ]] || [[ -L ${fnm} ]] || [[ ! -s ${fnm} ]]
63     then
64         continue
65     fi
66
67     ignore=
68     for pattern in "${ignores[@]}"
69     do
70         if [[ ${fnm} == ${pattern} ]]
71         then
72             ignore=1
73         fi
74     done
75     if [[ ${ignore} = 1 ]]; then continue; fi
76
77     case ${fnm} in
78         Makefile | */Makefile \
79             | *.dockerfile | */Dockerfile.* | */Dockerfile | *.dockerignore \
80             | */MANIFEST.in | */fuse.conf | */gitolite.rc \
81             | *.pl | *.pm | *.PL \
82             | *.rb | *.rb.example | *.rake | *.ru \
83             | *.gemspec | */Gemfile | */Rakefile \
84             | services/login-sync/bin/* \
85             | sdk/cli/bin/* \
86             | *.py \
87             | sdk/python/bin/arv-* \
88             | sdk/cwl/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             | */nginx.conf \
96             | build/build.list | *.R)
97             fixer=fixer
98             cc="#"
99             ;;
100         *.md)
101             fixer=fixer
102             cc="[//]: #"
103             ;;
104         *.rst)
105             fixer=fixer
106             cc=".."
107             ;;
108         *.erb)
109             fixer=fixer
110             cs="<%# "
111             cc=""
112             ce=" %>"
113             ;;
114         *.liquid)
115             fixer=fixer
116             cs=$'{% comment %}\n'
117             cc=""
118             ce=$'\n{% endcomment %}'
119             grepAfter=3
120             grepBefore=1
121             ;;
122         *.textile)
123             fixer=fixer
124             cs="###. "
125             cc="...."
126             ce=
127             ;;
128         *.css)
129             fixer=fixer
130             cs="/* "
131             cc=""
132             ce=" */"
133             ;;
134         *.coffee)
135             fixer=fixer
136             cs="### "
137             cc=""
138             ce=" ###"
139             ;;
140         *.go | *.scss | *.java | *.js)
141             fixer=fixer
142             cc="//"
143             ;;
144         *.sql)
145             fixer=fixer
146             cc="--"
147             ;;
148         *.html | *.svg)
149             fixer=fixer
150             cs="<!-- "
151             cc=""
152             ce=" -->"
153             ;;
154         *)
155             cc="#"
156             hashbang=$(head -n1 ${fnm})
157             if [[ ${hashbang} = "#!/bin/sh" ]] ||  [[ ${hashbang} = "#!/bin/bash" ]]
158             then
159                 fixer=fixer
160             fi
161             ;;
162     esac
163     wantGPL="${cs:-${cc}${cc:+ }}Copyright (C) The Arvados Authors. All rights reserved.
164 ${cc}
165 ${cc}${cc:+ }SPDX-License-Identifier: AGPL-3.0${ce}"
166     wantApache="${cs:-${cc}${cc:+ }}Copyright (C) The Arvados Authors. All rights reserved.
167 ${cc}
168 ${cc}${cc:+ }SPDX-License-Identifier: Apache-2.0${ce}"
169     wantBYSA="${cs:-${cc}${cc:+ }}Copyright (C) The Arvados Authors. All rights reserved.
170 ${cc}
171 ${cc}${cc:+ }SPDX-License-Identifier: CC-BY-SA-3.0${ce}"
172     wantBYSAmd="[comment]: # (Copyright © The Arvados Authors. All rights reserved.)
173 [comment]: # ()
174 [comment]: # (SPDX-License-Identifier: CC-BY-SA-3.0)"
175     found=$(head -n20 "$fnm" | egrep -A${grepAfter} -B${grepBefore} 'Copyright.*All rights reserved.' || true)
176     case ${fnm} in
177         Makefile | build/* | lib/* | tools/* | apps/* | services/* | sdk/cli/bin/crunch-job)
178             want=${wantGPL}
179             ;;
180         crunch_scripts/* | docker/* | sdk/*)
181             want=${wantApache}
182             ;;
183         doc/*)
184             want=${wantBYSA}
185             ;;
186         README.md)
187             want=${wantBYSAmd}
188             ;;
189         *)
190             want=
191             ;;
192     esac
193     case "$found" in
194         "$wantGPL")
195             ;;
196         "$wantApache")
197             ;;
198         "$wantBYSA")
199             ;;
200         "$wantBYSAmd")
201             ;;
202         "")
203             if [[ -z ${found} ]] && [[ -n ${want} ]] && [[ $fix = true ]] && [[ $fixer != "" ]]
204             then
205                 ${fixer} ${fnm}
206             else
207                 echo "missing copyright notice: $fnm"
208                 result=1
209             fi
210             ;;
211         *)
212             echo "nonstandard copyright notice: $fnm '${found}'"
213             result=1
214             ;;
215     esac
216 done <&${COPROC[0]}
217 exit $result