Skip to content

Instantly share code, notes, and snippets.

@jonathanfann
Last active April 29, 2024 13:22
Show Gist options
  • Save jonathanfann/dbbc6f559ab51b09a96f4d132008620b to your computer and use it in GitHub Desktop.
Save jonathanfann/dbbc6f559ab51b09a96f4d132008620b to your computer and use it in GitHub Desktop.
Get current ticket from branch name and return with commit message in ZSH
function doCommit() {
local branch=$(git symbolic-ref HEAD 2> /dev/null | awk 'BEGIN{FS="/"} {print $NF}')
local ticket=""
if [[ $branch != "" ]];
then
local parts=(${(@s[-])branch})
local part1=$parts[1]
local part2=$parts[2]
ticket="$part1-$part2"
fi
if [[ $ticket == "" ]];
then
echo "Unable to get ticket number.\nWhen getting the original branch, received: $branch"
else
if (( $# != 0 ));
then
git commit -m "$ticket: $1"
else
echo -n "Commit Message: "
read msg
if [[ $msg != "" ]];
then
git commit -m "$ticket: $msg"
else
echo "you have to type something... not commiting anything, try again"
fi
fi
fi
}

Usage in Terminal

doCommit "my commit message"

or

doCommit

and enter the message after the prompt

Note:

  • Assumes you format your branches after your ticket numbers

Example

  • Ticket: PROJECT-123
  • Branch: PROJECT-123-HEY-NOW
  • Your Text: Changed the color of everything to red
  • Commit Message Generated: PROJECT-123: Changed the color of everything to red
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment