check-licence-header.sh 2.89 KB
Newer Older
Eoin Clerkin's avatar
Eoin Clerkin committed
1
2
3
4
5
#!/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

6
7
8
9
10
11

# In the array excludes all directories and/or files are listed which shouldn't be
# tested if they habe the proper license statement.
# For all the directories/files listed in the array an explanation should be given.

excludes=(
12
13
14
15
# The directory external is excluded from the check since this directory
# contains code which isn't part of CbmRoot but external dependencies so
# we don't have to test for our license header
external
16
17
18
19
20
21
# The directory is excluded since the files have a license header
# inherited from the ALICE experiment. The code was copied by the
# original author when he started working for CBM.
papaframework
)

22
23
RETURN_CODE="0"

Eoin Clerkin's avatar
Eoin Clerkin committed
24
licenceHeaderCheck () {
25
26
27

	FILE_CODE="0";
	sed -n '2p' $1 | grep -q '   SPDX-License-Identifier: GPL-3.0-only'
Eoin Clerkin's avatar
Eoin Clerkin committed
28
	if [ $? -ne 0 ]; then
29
30
31
		echo "[ERROR] $1, line 2 missing spdx licence header declaration."
		((RETURN_CODE++))
		FILE_CODE="1"
Eoin Clerkin's avatar
Eoin Clerkin committed
32
33
	fi

Administrator's avatar
Administrator committed
34
	head -n 1 $1 | grep -q '\/\* Copyright (C) [0-9-]* [a-zA-Z -/]*, [a-zA-Z/]*'
Eoin Clerkin's avatar
Eoin Clerkin committed
35
	if [ $? -ne 0 ]; then
36
37
38
		echo "[ERROR] $1; line 1 has syntax errors in its licence header."
		((RETURN_CODE++))
		FILE_CODE="1"
Eoin Clerkin's avatar
Eoin Clerkin committed
39
40
	fi

41
	sed -n '3p' $1 | grep -q '   Authors: .[^\*\/]*\*/'
Eoin Clerkin's avatar
Eoin Clerkin committed
42
	if [ $? -ne 0 ]; then
43
44
45
		echo "[ERROR] $1; line 3 has syntax errors in its licence header."
		((RETURN_CODE++))
		FILE_CODE="1"
Eoin Clerkin's avatar
Eoin Clerkin committed
46
47
	fi

48
	sed -n '3p' $1 | grep -q '\[committer\]'
Eoin Clerkin's avatar
Eoin Clerkin committed
49
	if [ $? -ne 0 ]; then
50
51
52
53
54
55
56
		echo "[ERROR] $1; line 3 has missing first commiter information."
		((RETURN_CODE++))
		FILE_CODE="1"
	fi

	if [[ ${FILE_CODE} -eq 0 ]]; then
		echo "[OK] File: $1 passes licence header check."
Eoin Clerkin's avatar
Eoin Clerkin committed
57
	fi
58
}
Eoin Clerkin's avatar
Eoin Clerkin committed
59
60
61


if [ $# -eq 1 ]; then
62
  UPSTREAM=$1
Eoin Clerkin's avatar
Eoin Clerkin committed
63
64
65
66
67
68
69
70
71
72
73
74
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

75
76
77
if [ -f $1 ]; then
  CHANGED_FILES="$1"
  echo "LICENCE HEADER CHECK FOR FILE: $1"
78
else
79
80
81
  echo "Upstream name is :" $UPSTREAM
  BASE_COMMIT=$UPSTREAM/master
  CHANGED_FILES=$(git diff --name-only $BASE_COMMIT | egrep '\.cxx$|\.h|\.C$')
82
83
fi

84
85
# Exclude files which shouldn't be tested
# Check the license header for all other files
Eoin Clerkin's avatar
Eoin Clerkin committed
86
for FILE in $CHANGED_FILES; do
87
88
89
  for EXCLUDE in "${excludes[@]}"; do
    FILE=$(echo $FILE | egrep -v "$EXCLUDE")
  done
90
  if [[ -n $FILE && -f $FILE ]]; then
91
92
    licenceHeaderCheck $FILE
  fi
Eoin Clerkin's avatar
Eoin Clerkin committed
93
94
done

95
96
97
98
99
100
101
if [ $RETURN_CODE -eq "0" ]; then
	echo "[OK] Licence header passes automatic checks."
else
	echo "[FAIL] Visit https://redmine.cbm.gsi.de/projects/cbmroot/wiki/Licence for information on correct syntax for licence header."
fi

exit $RETURN_CODE;