Git Hooks¶
Hooks are little scripts you can place in $GIT_DIR/hooks directory
refs: githooks, Pro Git: Git Hooks
prepare_commit_msg¶
Ref: gitdoc: prepare_commit_msg
This hook is invoked by git commit after preparing the default message, and before the editor is started. It takes one to three parameters.
- The name of the file that contains the prepared message.
- The second is
messageif a-mor-Foption was given,templatewith a-toption or configurationcommit.template,commitif a-c,-Cor--amendoption was given,mergefor a merge commit or with a.git/MERGE_MSGfile; andsquashif a.git/SQUASH_MSGfile exists. - The third is only for a
commitand is the commit SHA-1.
A non-zero exit means a failure of the hook and aborts the commit.
#!/bin/sh
num_messages=5
format="# %h %s [%an]"
log="$(git log --pretty="${format}" -${num_messages})"
header="#
# last ${num_messages} messages
# ----------------------"
template() {
echo "${header}"
echo "${log}"
}
case "$2" in
merge|template|squash) ;;
""|commit) template >> $1;;
*) echo "error in prepare-commit-msg hook" >&2
exit 1
;;
esac