From 43f19846a0156bca4d99720a75574ff84480c48a Mon Sep 17 00:00:00 2001
From: Eoin Clerkin <e.clerkin@gsi.de>
Date: Thu, 2 Sep 2021 20:53:57 +0200
Subject: [PATCH] Automated licence header check

Ensures that modified .cpp, .C and .h files follow basic licence header guidelines.

Check line 1 for a copyright declaration with correct numerial and character placements for a date followed by institute and city.
Check line 2 for SPDX licence identifier.
Check line 3 for author and committer information.
---
 .gitlab-ci.yml                  | 20 +++++++++++
 scripts/check-licence-header.sh | 59 +++++++++++++++++++++++++++++++++
 2 files changed, 79 insertions(+)
 create mode 100755 scripts/check-licence-header.sh

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index b0abe768e9..35b25059a3 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -130,6 +130,26 @@ FileEndCheck:
     - git fetch upstream
     - scripts/check-file-ending.sh upstream
 
+
+FileLicenceCheck:
+  stage: checkFormat
+  image: alpine
+  tags:
+    - docker
+  only:
+    refs:
+      - merge_requests
+    variables:
+      - $CI_MERGE_REQUEST_PROJECT_PATH == "computing/cbmroot" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master"
+  script:
+    # Get the upstream repository manually. I did not find any other way to have it for
+    # comparison
+    - apk update && apk add git bash file
+    - scripts/connect_upstream_repo.sh $CI_MERGE_REQUEST_PROJECT_URL
+    - git fetch upstream
+    - scripts/check-licence-header.sh upstream
+
+
 #
 # job template
 #
diff --git a/scripts/check-licence-header.sh b/scripts/check-licence-header.sh
new file mode 100755
index 0000000000..784bf0e76f
--- /dev/null
+++ b/scripts/check-licence-header.sh
@@ -0,0 +1,59 @@
+#!/bin/bash
+# Copyright (C) 2021 Facility for Antiproton and Ion Research in Europe, Darmstadt
+# SPDX-License-Identifier: GPL-3.0-only
+# First commited by Eoin Clerkin
+
+licenceHeaderCheck () {
+	sed -n '2p' $1 | grep -L '   SPDX-License-Identifier: GPL-3.0-only'
+	if [ $? -ne 0 ]; then
+		echo "[ERROR] Missing or Malformated licence header for file $FILE"
+		exit 11
+	fi
+
+	head -n 1 $1 | grep -L '\/\* Copyright (C) [0-9-]* .[a-zA-Z ]*, [a-zA-Z]*'
+	if [ $? -ne 0 ]; then
+		echo "[ERROR] $FILE; line 2 has syntax errors in its licence header."
+		exit 12
+	fi
+
+	sed -n '3p' $1 | grep -L '   Authors: .[^\*\/]*\*/'
+	if [ $? -ne 0 ]; then
+		echo "[ERROR] $FILE; line 3 has syntax errors in its licence header."
+		exit 13
+	fi
+
+	sed -n '3p' $FILE | grep -L '\[committer\]'
+	if [ $? -ne 0 ]; then
+		echo "[ERROR] $FILE; line 3 has missing first commiter information."
+	exit 14
+	fi
+	echo "[OK] File: $1 passes licence header check.";
+	}
+
+
+if [ $# -eq 1 ]; then
+  UPSTREAM=$1
+else
+  if [ -z $UPSTREAM ]; then
+    UPSTREAM=$(git remote -v | grep git.cbm.gsi.de[:/]computing/cbmroot | cut -f1 | uniq)
+    if [ -z $UPSTREAM ]; then
+      echo "Error: Name of upstream repository not provided and not found by automatic means"
+      echo 'Please provide if by checking your remotes with "git remote -v" and exporting UPSTREAM'
+      echo "or passing as an argument"
+      exit -1
+    fi
+  fi
+fi
+
+echo "See wikipage https://redmine.cbm.gsi.de/projects/cbmroot/wiki/Licence for information regarding the correct syntax used in the licence header."
+
+echo "Upstream name is :" $UPSTREAM
+
+BASE_COMMIT=$UPSTREAM/master
+CHANGED_FILES=$(git diff --name-only $BASE_COMMIT | egrep '\.cxx$|\.h|\.C$')
+for FILE in $CHANGED_FILES; do
+licenceHeaderCheck $FILE
+done
+
+echo "[OK] Licence header passes automatic checks."
+exit 0;
-- 
GitLab