Skip to content

Instantly share code, notes, and snippets.

@mgrebenets
Last active August 29, 2015 14:22
Show Gist options
  • Save mgrebenets/b8789d195ff67a67d9bd to your computer and use it in GitHub Desktop.
Save mgrebenets/b8789d195ff67a67d9bd to your computer and use it in GitHub Desktop.
Git Hooks

Git Hooks

Use git hooks to automatically add JIRA ID at the start of each commit message.

Main Project Repo

Navigate to the root of the main project repository.

Create a copy of .git/hooks/commit-msg.sample and name it commit-msg.

cp .git/hooks/commit-msg.sample .git/hooks/commit-msg

Remove the example code from it.

# Remove this
test "" = "$(grep '^Signed-off-by: ' "$1" |
	 sort | uniq -c | sed -e '/^[ 	]*1[ 	]/d')" || {
	echo >&2 Duplicate Signed-off-by lines.
	exit 1
}

Add the following bash script code at the end of the file.

# If commit message is a fixup message, ignore it
[[ -n "$(cat $1 | grep 'fixup!')" ]] && FIXUP="YES"

TICKET=$(git symbolic-ref HEAD | rev | cut -d/ -f1 | rev | grep -o -E "[A-Z]+-[0-9]+" | head -n1)
if [[ -n "${TICKET}" && -z "${FIXUP}" ]]; then
	sed -i.bak -e "1s/^/[${TICKET}] /" $1
fi

Submodules

Git hooks for submodules are located in .git/modules/<Submodule/Path>/hooks/. For example .git/modules/Libraries/SomeLib/hooks.

Run this script to use the same gi thooks for all submodules.

for smh in $(find .git/modules -name hooks); do cp -f .git/hooks/commit-msg ${smh}/; done

Make sure you re-run it every time you change the hooks for main repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment