Skip to content

Instantly share code, notes, and snippets.

@nicojust
Created May 2, 2020 00:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nicojust/0621cd3272a61b1d77e315865b2de882 to your computer and use it in GitHub Desktop.
Save nicojust/0621cd3272a61b1d77e315865b2de882 to your computer and use it in GitHub Desktop.
Git Commit Hooks
#!/bin/bash
#
# 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".
COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
SHA1=$3
# Only add custom message when there is no commit source
# ($COMMIT_SOURCE is empty). Otherwise, keep the default message
# proposed by Git. Possible commit source: message, template,
# merge, squash or commit. See https://git-scm.com/docs/githooks
if [[ -z "$COMMIT_SOURCE" ]]; then
# https://regex101.com/r/jQgLYo/1
branch_name="$(git rev-parse --abbrev-ref HEAD)"
commit_prefix="$([[ $branch_name =~ ^(.*\/)?([a-zA-Z0-9]+-[0-9]+)(.+)?$ ]] && echo "${BASH_REMATCH[2]}")"
if [[ ! -z "$commit_prefix" ]]; then
hint=$(cat "$COMMIT_MSG_FILE")
ticket="[$commit_prefix] "
echo "$ticket" > "$COMMIT_MSG_FILE"
echo "$hint" >> "$COMMIT_MSG_FILE"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment