-
-
Save thomasheller/69184f47a0c25e1f0b9699fff167144f to your computer and use it in GitHub Desktop.
Git Commit Hooks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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