When enforcing copyright headers, do not consider commits that are already part
[arvados-dev.git] / git / hooks / check-copyright-headers.sh
1 #!/bin/bash
2 # Copyright (C) The Arvados Authors. All rights reserved.
3 #
4 # SPDX-License-Identifier: AGPL-3.0
5
6 # This script is intended to be run as a git update hook. It ensures that all
7 # commits adhere to the copyright header convention for the Arvados project,
8 # which is documented at
9 #
10 #    https://dev.arvados.org/projects/arvados/wiki/Coding_Standards#Copyright-headers
11
12 REFNAME=$1
13 OLDREV=$2
14 NEWREV=$3
15
16 EXITCODE=0
17
18 echo "Enforcing copyright headers..."
19
20 # Load the .licenseignore file
21 LICENSEIGNORE=`mktemp`
22 git show ${NEWREV}:.licenseignore > "${LICENSEIGNORE}" 2>/dev/null
23 if [[ "$?" != "0" ]]; then
24   # e.g., .licenseignore does not exist
25   ignores=()
26 else
27   IFS=$'\n' read -a ignores -r -d $'\000' < "$LICENSEIGNORE" || true
28 fi
29 rm -f $LICENSEIGNORE
30
31 oldIFS="$IFS"
32 IFS=$'\n'
33 for rev in $(git rev-list --objects $OLDREV..$NEWREV --not --branches='*' | git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)'| sed -n 's/^blob //p'); do
34
35   IFS="$oldIFS" read -r -a array <<<"$rev"
36   sha=${array[0]}
37   fnm=${array[2]}
38
39   # Make sure to skip files that match patterns in .licenseignore
40   ignore=
41   for pattern in "${ignores[@]}"; do
42     if [[ ${fnm} == ${pattern} ]]; then
43       ignore=1
44     fi
45   done
46   if [[ ${ignore} = 1 ]]; then continue; fi
47
48   HEADER=`git show ${sha} | head -n20 | egrep -A3 -B1 'Copyright.*All rights reserved.'`
49
50   if [[ ! "$HEADER" =~ "SPDX-License-Identifier:" ]]; then
51     if [[ "$EXITCODE" == "0" ]]; then
52       echo
53       echo "ERROR"
54       echo
55     fi
56     echo "missing or invalid copyright header in file ${fnm}"
57     EXITCODE=1
58   fi
59 done
60 IFS="$oldIFS"
61
62 if [[ "$EXITCODE" != "0" ]]; then
63   echo
64   echo "[POLICY] all files must contain copyright headers, for more information see"
65   echo
66   echo "          https://dev.arvados.org/projects/arvados/wiki/Coding_Standards#Copyright-headers"
67   echo
68   exit $EXITCODE
69 fi