check.sh
2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/bin/sh
# Copyright 2014 Ludwig Knüpfer <ludwig.knuepfer@fu-berlin.de>
#
# This file is subject to the terms and conditions of the GNU Lesser
# General Public License v2.1. See the file LICENSE in the top level
# directory for more details.
# customizable
CHECKROOT=$(dirname "${0}")
LICENSEDIR="${CHECKROOT}/patterns"
OUTPUT="${CHECKROOT}/out"
UNKNOWN="${OUTPUT}/unknown"
TMP="${CHECKROOT}/.tmp"
# Needed for compatibility with BSD sed
TAB_CHAR="$(printf '\t')"
# prepare
ROOT=$(git rev-parse --show-toplevel)
LICENSES=$(ls "${LICENSEDIR}")
EXIT_CODE=0
ERROR_EXIT_CODE="1"
# reset output dir
rm -fr "${OUTPUT}"
mkdir -p "${OUTPUT}"
for LICENSE in ${LICENSES}; do
echo -n '' > "${OUTPUT}/${LICENSE}"
done
# If no branch but an option is given, unset BRANCH.
# Otherwise, consume this parameter.
BRANCH="${1}"
if echo "${BRANCH}" | grep -q '^-'; then
BRANCH=""
else
if [ -n "${BRANCH}" ]; then
shift 1
fi
fi
# If the --diff-filter option is given, consume this parameter.
# Set the default DIFFFILTER option otherwise.
DIFFFILTER="${1}"
if echo "${DIFFFILTER}" | grep -q '^--diff-filter='; then
shift 1
else
DIFFFILTER="--diff-filter=ACMR"
fi
# If the --error-exitcode option is given, consume this parameter
# and overwrite the default ERROR_EXIT_CODE.
if echo "${1}" | grep -q '^--error-exitcode='; then
ERROR_EXIT_CODE=$(echo ${1} | sed -e 's/--error-exitcode=//')
shift 1
fi
# select files to check
if [ -z "${BRANCH}" ]; then
FILES="$(git ls-tree -r --full-tree --name-only HEAD | grep -E '\.([sSch]|cpp)$')"
else
FILES="$(git diff ${DIFFFILTER} --name-only ${BRANCH} | grep -E '\.([sSchp]|cpp)$')"
fi
# categorize files
for FILE in ${FILES}; do
FAIL=1
head -100 "${ROOT}/${FILE}" | sed -e 's/[\/\*'"${TAB_CHAR}"']/ /g' -e 's/$/ /' | tr -d '\r\n' | sed -e 's/ */ /g' > "${TMP}"
for LICENSE in ${LICENSES}; do
if pcregrep -q -f "${LICENSEDIR}/${LICENSE}" "${TMP}"; then
echo "${FILE}" >> "${OUTPUT}/${LICENSE}"
FAIL=0
break
fi
done
if [ ${FAIL} = 1 ]; then
echo "${FILE}" >> "${UNKNOWN}"
echo "file has an unknown license header: '${FILE}'"
EXIT_CODE=${ERROR_EXIT_CODE}
fi
done
exit ${EXIT_CODE}