Skip to content

Instantly share code, notes, and snippets.

@s-mage
Last active November 20, 2024 16:33
Show Gist options
  • Save s-mage/6b4037301545b181a8923cec1742bf20 to your computer and use it in GitHub Desktop.
Save s-mage/6b4037301545b181a8923cec1742bf20 to your computer and use it in GitHub Desktop.
.git/hooks/prepare-commit-msg
#!/bin/bash
# usage:
# mkdir ~/.githooks
# nvim ~/.githooks/prepare-commit-msg # paste the content of this file
# git config --global core.hooksPath ~/.githooks/ # you don't have to do it globally but
COMMIT_MSG_FILE=$1
branch="$(git rev-parse --abbrev-ref HEAD)"
if [[ "$branch" =~ ^([0-9]+) ]]; then
prefix="[${BASH_REMATCH[1]}]"
echo -e "$prefix\n$(cat $COMMIT_MSG_FILE)" > $COMMIT_MSG_FILE
fi
if [[ "$branch" =~ ^([a-zA-Z]+)-([0-9]+) ]]; then
raw_prefix="${BASH_REMATCH[1]}"
id="${BASH_REMATCH[2]}"
# Convert the prefix to uppercase
prefix=$(echo "$raw_prefix" | tr '[:lower:]' '[:upper:]')
# Format the prefix and prepend it to the commit message
echo -e "[${prefix}-${id}]\n$(cat "$COMMIT_MSG_FILE")" > "$COMMIT_MSG_FILE"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment