2014-05-13 16:41:33 +02:00
|
|
|
#!/bin/sh
|
|
|
|
|
2015-09-27 18:58:30 +02:00
|
|
|
# Copyright 2014 Ludwig Knüpfer <ludwig.knuepfer@fu-berlin.de>
|
2014-11-28 18:03:38 +01:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2014-05-13 16:41:33 +02:00
|
|
|
# customizable
|
|
|
|
CHECKROOT=$(dirname "${0}")
|
|
|
|
LICENSEDIR="${CHECKROOT}/patterns"
|
|
|
|
OUTPUT="${CHECKROOT}/out"
|
|
|
|
UNKNOWN="${OUTPUT}/unknown"
|
|
|
|
TMP="${CHECKROOT}/.tmp"
|
|
|
|
|
2015-01-12 03:20:09 +01:00
|
|
|
# Needed for compatibility with BSD sed
|
|
|
|
TAB_CHAR="$(printf '\t')"
|
|
|
|
|
2014-05-13 16:41:33 +02:00
|
|
|
# prepare
|
|
|
|
ROOT=$(git rev-parse --show-toplevel)
|
|
|
|
LICENSES=$(ls "${LICENSEDIR}")
|
|
|
|
EXIT_CODE=0
|
2014-11-23 10:14:44 +01:00
|
|
|
ERROR_EXIT_CODE="1"
|
2014-05-13 16:41:33 +02:00
|
|
|
|
|
|
|
# reset output dir
|
|
|
|
rm -fr "${OUTPUT}"
|
|
|
|
mkdir -p "${OUTPUT}"
|
|
|
|
for LICENSE in ${LICENSES}; do
|
|
|
|
echo -n '' > "${OUTPUT}/${LICENSE}"
|
|
|
|
done
|
|
|
|
|
2014-11-22 16:35:16 +01:00
|
|
|
# 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
|
|
|
|
|
2014-11-23 10:14:44 +01:00
|
|
|
# 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
|
|
|
|
|
2014-06-20 17:57:34 +02:00
|
|
|
# select files to check
|
|
|
|
if [ -z "${BRANCH}" ]; then
|
2014-08-23 16:13:05 +02:00
|
|
|
FILES="$(git ls-tree -r --full-tree --name-only HEAD | grep -E '\.([sSch]|cpp)$')"
|
2014-06-20 17:57:34 +02:00
|
|
|
else
|
2014-11-22 16:35:16 +01:00
|
|
|
FILES="$(git diff ${DIFFFILTER} --name-only ${BRANCH} | grep -E '\.([sSchp]|cpp)$')"
|
2014-06-20 17:57:34 +02:00
|
|
|
fi
|
|
|
|
|
2014-05-13 16:41:33 +02:00
|
|
|
# categorize files
|
2014-06-20 17:57:34 +02:00
|
|
|
for FILE in ${FILES}; do
|
2014-05-13 16:41:33 +02:00
|
|
|
FAIL=1
|
2015-01-12 03:20:09 +01:00
|
|
|
head -100 "${ROOT}/${FILE}" | sed -e 's/[\/\*'"${TAB_CHAR}"']/ /g' -e 's/$/ /' | tr -d '\r\n' | sed -e 's/ */ /g' > "${TMP}"
|
2014-05-13 16:41:33 +02:00
|
|
|
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}"
|
2014-06-20 17:57:34 +02:00
|
|
|
echo "file has an unknown license header: '${FILE}'"
|
2014-11-23 10:14:44 +01:00
|
|
|
EXIT_CODE=${ERROR_EXIT_CODE}
|
2014-05-13 16:41:33 +02:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
|
|
|
exit ${EXIT_CODE}
|