Commit 5f418145 authored by Adrien Oliva's avatar Adrien Oliva

feat(script): Git hooks

- Three hooks added in repository:
        - commit-msg: that add Signed-off-by footer in every commit
          message
        - prepare-commit-msg: Add a commit message template in comment
        - pre-commit: check for non ascii filename and for whitespace
          errors

- Add script to install hook in a given git repository
Signed-off-by: Adrien Oliva's avatarAdrien Oliva <olivaa+gitlab@yapbreak.fr>
parent 1b69a9f1
#!/bin/bash
usage()
{
echo "$0 [OPTIONS]"
echo ""
echo "Install hooks in git repository"
echo ""
echo "Options"
echo " -s, -m soft install hooks as symbolic links"
echo " -m hard, install hooks with file copy (default)"
exit 1
}
test -d ".git/hooks" || {
echo "Please run this script in top level of a git working tree"
exit 1
}
SYMBOLIC=0
while getopts "sm:" o; do
case "${o}" in
s)
SYMBOLIC=1
;;
m)
MODE=${OPTARG}
case "$MODE" in
soft)
SYMBOLIC=1
;;
hard)
SYMBOLIC=0
;;
*)
usage
esac
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
installfile()
{
local src
local dst
src="$1"
dst="$2"
if [ $SYMBOLIC -eq 0 ]; then
cp -f "$src" "$dst"
else
ln -s "$src" "$dst"
fi
}
HOOKS_FOLDER=$(readlink -f "$(dirname "$0")/../hooks")
for hook in "$HOOKS_FOLDER"/*; do
hookname="$(basename "$hook")"
installfile "$hook" ".git/hooks/$hookname"
done
#!/bin/sh
# Add signed-off commit footer
SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
grep -qs "^$SOB" "$1" || cat >> "$1" <<HERE
$SOB
HERE
test "" = "$(grep '^Signed-off-by: ' "$1" | sort | uniq -c | sed -e '/^[ \t]*1[ \t]/d')" || {
echo >&2 "Duplicate Signed-off-by lines."
exit 1
}
#!/bin/bash
if git rev-parse --verify HEAD >/dev/null 2>&1; then
against=HEAD
else
exit 0
fi
allownonascii="$(git config --bool hooks.allownonascii)"
exec 1>&2
if [ "$allownonascii" != "true" ]; then
if [ "$(git diff --cached --name-only --diff-filter=A -z $against | LC_ALL=C tr -d '[ -~]\0' | wc -c)" != 0 ]; then
cat <<HERE
Error: Attempt to add a non-ASCII file name.
This can cause problems if you wont to work with people on other platforms.
To be protable it is advisable to rename the file.
If you know what you are doing, you can disable this check using:
git config hooks.allownonascii true
HERE
exit 1
fi
fi
# Check whitespace errors
exec git diff-index --check --cached $against --
#!/bin/bash
cat >> "$1" <<HERE
# Commit message template:
#
# <type>(<scope>): <subject>
#
# <description>
#
# <footer>
#
# With the following values:
# - <type>: build, ci, docs, feat, fix, perf, refactor, style, test
# - <footer>: reference to gitlab issue with tag "GTLAB #6"
#
#
HERE
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment