From 19e300ed030f7fa1cc50913ecb4d42e50f397ed7 Mon Sep 17 00:00:00 2001 From: Ward Vandewege Date: Wed, 23 May 2018 17:17:15 -0400 Subject: [PATCH] Add a git hook to ensure that all commits adhere to the copyright header convention for the Arvados project. No issue # Arvados-DCO-1.1-Signed-off-by: Ward Vandewege --- git/hooks/check-copyright-headers.sh | 67 ++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100755 git/hooks/check-copyright-headers.sh diff --git a/git/hooks/check-copyright-headers.sh b/git/hooks/check-copyright-headers.sh new file mode 100755 index 0000000..f219532 --- /dev/null +++ b/git/hooks/check-copyright-headers.sh @@ -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 -- 2.30.2