Add a git hook to ensure that all commits adhere to the copyright header
authorWard Vandewege <wvandewege@veritasgenetics.com>
Wed, 23 May 2018 21:17:15 +0000 (17:17 -0400)
committerWard Vandewege <wvandewege@veritasgenetics.com>
Wed, 23 May 2018 21:17:56 +0000 (17:17 -0400)
convention for the Arvados project.

No issue #

Arvados-DCO-1.1-Signed-off-by: Ward Vandewege <wvandewege@veritasgenetics.com>

git/hooks/check-copyright-headers.sh [new file with mode: 0755]

diff --git a/git/hooks/check-copyright-headers.sh b/git/hooks/check-copyright-headers.sh
new file mode 100755 (executable)
index 0000000..f219532
--- /dev/null
@@ -0,0 +1,67 @@
+#!/bin/bash
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: AGPL-3.0
+
+# This script is intended to be run as a git update hook. It ensures that all
+# commits adhere to the copyright header convention for the Arvados project,
+# which is documented at
+#
+#    https://dev.arvados.org/projects/arvados/wiki/Coding_Standards#Copyright-headers
+
+REFNAME=$1
+OLDREV=$2
+NEWREV=$3
+
+EXITCODE=0
+
+# Load the .licenseignore file
+LICENSEIGNORE=`mktemp`
+git show ${NEWREV}:.licenseignore > "${LICENSEIGNORE}" 2>/dev/null
+if [[ "$?" != "0" ]]; then
+  # e.g., .licenseignore does not exist
+  ignores=()
+else
+  IFS=$'\n' read -a ignores -r -d $'\000' < "$LICENSEIGNORE" || true
+fi
+rm -f $LICENSEIGNORE
+
+oldIFS="$IFS"
+IFS=$'\n'
+for rev in $(git rev-list --objects $2..$3 | git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)'| sed -n 's/^blob //p'); do
+
+  IFS="$oldIFS" read -r -a array <<<"$rev"
+  sha=${array[0]}
+  fnm=${array[2]}
+
+  # Make sure to skip files that match patterns in .licenseignore
+  ignore=
+  for pattern in "${ignores[@]}"; do
+    if [[ ${fnm} == ${pattern} ]]; then
+      ignore=1
+    fi
+  done
+  if [[ ${ignore} = 1 ]]; then continue; fi
+
+  HEADER=`git show ${sha} | head -n20 | egrep -A3 -B1 'Copyright.*Arvados'`
+
+  if [[ ! "$HEADER" =~ "SPDX-License-Identifier:" ]]; then
+    if [[ "$EXITCODE" == "0" ]]; then
+      echo
+      echo "ERROR"
+      echo
+    fi
+    echo "missing or invalid copyright header in file ${fnm}"
+    EXITCODE=1
+  fi
+done
+IFS="$oldIFS"
+
+if [[ "$EXITCODE" != "0" ]]; then
+  echo
+  echo "[POLICY] all files must contain copyright headers, for more information see"
+  echo
+  echo "          https://dev.arvados.org/projects/arvados/wiki/Coding_Standards#Copyright-headers"
+  echo
+  exit $EXITCODE
+fi