Skip to content

Instantly share code, notes, and snippets.

@argami
Created January 27, 2021 15:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save argami/78b12c9d46eb9a580fc69e0e54eebd75 to your computer and use it in GitHub Desktop.
Save argami/78b12c9d46eb9a580fc69e0e54eebd75 to your computer and use it in GitHub Desktop.
Adds the branch name to the beginning of the commit message if the branch name is something like DEV-123 (3 letters-3numbers)
#!/bin/bash
# Include any branches for which you wish to disable this script
if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(master develop staging test)
fi
# Get the current branch name and check if it is excluded
BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_NAME_VALID_FORMAT=$(echo $BRANCH_NAME | grep -c -E "^\w{3,}-\d{3,}$")
if ! [[ $BRANCH_NAME_VALID_FORMAT -eq 1 ]]; then
exit 0;
fi
BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
ALREADY_INCLUDED=$(cat "$1" | grep -c "\[$BRANCH_NAME\]")
# Trim it down to get the parts we're interested in
TRIMMED=$(echo $BRANCH_NAME | sed -e 's:^\([^-]*-[^-]*\)-.*:\1:' -e \
'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/')
# If it isn't excluded, preprend the trimmed branch identifier to the given message
if ! [[ $ALREADY_INCLUDED -eq 1 ]] && [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]]; then
sed -i.bak -e "1s/^/\[$BRANCH_NAME\] /" $1
fi
@argami
Copy link
Author

argami commented Jan 27, 2021

Original one was taken from https://medium.com/@nicklee1/prepending-your-git-commit-messages-with-user-story-ids-3bfea00eab5a

Only need to copy the file in your .git/hooks dir

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