config/git: add generic prepare-commit-msg hook

This commit is contained in:
xeruf 2022-02-12 09:49:31 +01:00
parent c1f1e22fdf
commit 9344a9a99c
2 changed files with 34 additions and 0 deletions

0
.config/git/ignore Executable file → Normal file
View File

34
.config/git/prepare-commit-msg Executable file
View File

@ -0,0 +1,34 @@
#!/bin/sh -e
#
# This hook adds a comment for guidance about the commit message
# format on top of the default commit message.
#
# Called by "git commit" with the name of the file that has the
# commit message, followed by the description of the commit
# message's source. The hook's purpose is to edit the commit
# message file. If the hook fails with a non-zero status,
# the commit is aborted.
#
# To enable this hook, set the hooksPath in git:
# git config core.hooksPath .dev/githooks
COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
SHA1=$3
beginswith() { case $2 in "$1"*) true;; *) false;; esac; }
original=$(cat "$COMMIT_MSG_FILE")
if beginswith $'\n#' "$original"; then
# Find common path prefix of changed files
path=$(while read file
do test -z "$count" && common="$file" && count=$(expr length "$file") ||
while expr substr "$file" $count 1 != substr "$common" $count 1 >/dev/null; do let count--; done
done <<<"$(git -P diff --cached --name-only -r)" &&
expr substr "${common%.}" 1 "$count")
case "$path" in ([0-9]*) path="${path#*/}";; esac
{
echo "${path%/}:"
echo "$original"
} > "$COMMIT_MSG_FILE"
fi