GitLab pre-receive hook
The snippet can be accessed without any authentication.
Authored by
Lars With
Edited
pre-receive 1.44 KiB
#!/bin/bash
#https://git-scm.com/docs/githooks#pre-receive
set -Eeuo pipefail
TALISMANRC=".talismanrc"
fail() {
echo "$1"
exit 1
}
ensure_program() {
which $1 || fail "$1 not available"
}
ensure_programs() {
ensure_program talisman
ensure_program git
}
get_changed_content_and_scan() {
CUR_DIR=$(pwd)
TEMP_DIR=$(mktemp -d)
cd "${TEMP_DIR}"
git init
cd "${CUR_DIR}"
while read OLDREV NEWREV REFNAME; do
if [ "${OLDREV}" = "0000000000000000000000000000000000000000" ]; then
OLDREV="${NEWREV}"
fi
FILES=$(git diff --name-only ${OLDREV} ${NEWREV})
if [ -n "${FILES}" ]; then
for FILE in ${FILES}; do
mkdir -p "${TEMP_DIR}/$(dirname ${FILE})" &>/dev/null
git show ${NEWREV}:${FILE} > "${TEMP_DIR}/${FILE}"
done
cd "${TEMP_DIR}"
git add .
git commit -m "temp: ${REFNAME}"
if [ -n "$(git ls-files ${NEWREV}:.talismanrc)" ]; then
git show "${NEWREV}:${TALISMANRC}" > "${TEMP_DIR}/${TALISMANRC}"
fi
cd "${CUR_DIR}"
fi
done
cd "${TEMP_DIR}"
REPORTS_DIR="${TEMP_DIR}/talisman_reports"
mkdir -p "${REPORTS_DIR}"
talisman --scan --reportdirectory "${REPORTS_DIR}"
CHECK_RESULT=$?
if [ ${CHECK_RESULT} -ne 0 ]; then
cat "${REPORTS_DIR}/*"
fi
echo "TODO: We should run rm -rf \"${TEMP_DIR}\" now, but we don't just for testing."
exit ${CHECK_RESULT}
}
main() {
ensure_programs
get_changed_content_and_scan
}
main "$@"
Please register or sign in to comment