Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sometime, it's useful to have the last 5 commits to name it's own commit. This hook will add this information in comment at the end of the text editor
#!/bin/sh
#
# An example hook script to prepare the commit log 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, rename this file to "prepare-commit-msg".
# This hook will display your five last commit msg in one line format as below
#
# GIT LOG:
#
# 324a081 (HEAD -> master) [xxx] implement the method to refreshNormalization
# 047011b (origin/master) [xxx] add venv generation in one command
# 92f6416 [xxx] add instruction to package a distribution
# 9e7144e [xxx] design and define the command to implement
# run this part of the code only when
# the prepare-commit-msg script is invoke by git commit,
# not git commit -m or git commit --amend
if [ x = x${2} ]; then
commit_message_file=$1
echo "# GIT LOG:" >> ${commit_message_file}
echo "# " >> ${commit_message_file}
git log --pretty="tformat:%C(auto) %h %d %s %x00" --abbrev-commit --decorate -n 5 |\
tr -d '\n' |\
xargs -0 -I '{}' echo "# {}" >> ${commit_message_file}
fi
.
├── [...]
└── .git
├── branches
├── COMMIT_EDITMSG
├── config
├── description
├── HEAD
├── hooks
│   ├── applypatch-msg.sample
│   ├── commit-msg.sample
│   ├── post-update.sample
│   ├── pre-applypatch.sample
│   ├── pre-commit.sample
│   ├── prepare-commit-msg <- we will modify this file
│   ├── pre-push.sample
│   ├── pre-rebase.sample
│   ├── pre-receive.sample
│   └── update.sample
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.