Skip to content

Instantly share code, notes, and snippets.

@Rankarusu
Last active October 30, 2023 17:06
Show Gist options
  • Save Rankarusu/0b0ceebc896a15f0e512ca4102d5ec11 to your computer and use it in GitHub Desktop.
Save Rankarusu/0b0ceebc896a15f0e512ca4102d5ec11 to your computer and use it in GitHub Desktop.
Prepend git commit messages with ticket number from branch
#!/bin/bash
# parse ticket number (if available) and prepend commit messge with it
# as we have a pound sign (#) at the start of our messages, we should probably edit the git comment character
# git config core.commentChar ';'
branchName=$(git branch --show-current)
# sed -n : quiet, s: substitute, /: delimiter
# match everything that is not a number before a number that is at least 5 digits long
# and everything after
# replace everything with capture group 1 which is our number
ticketId=$(echo "$branchName" | sed -n 's/[^0-9]*\([0-9]\{5,\}\).*/\1/p')
# we dont want to add the ticket number if it's already there (amends)
# grep -c gets the count of occurrences
isTicketIdAlreadyThere=$(grep -c "$ticketId" "$1")
# -n check if variable is not empty (null or 0 leangth)
if [[ -n $ticketId ]] && [[ $isTicketIdAlreadyThere -eq 0 ]]; then
# -e interprets backslash escape sequences
# $1 is our commit file. We prepend it with our ticket id
echo -e "#$ticketId:" "$(cat "$1")" >"$1"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment