Skip to content

Instantly share code, notes, and snippets.

@jacklynrose
Last active October 31, 2021 23:57
Show Gist options
  • Save jacklynrose/9cd8777a7e1cd399f737964b8e7d4f34 to your computer and use it in GitHub Desktop.
Save jacklynrose/9cd8777a7e1cd399f737964b8e7d4f34 to your computer and use it in GitHub Desktop.
Generate a branch name with ZSH prompts that has the ticket name and description
# Other zushy stuff
sluggify() {
# 1. Convert upper to lower
# 2. Remove all non-alphanumeric chars or non-space char
# 3. Convert spaces to dashes
# 4. Convert multiple dashes to single dash
echo $1 | tr '[:upper:]' '[:lower:]' | tr -dc '[:alnum:] ' | tr '[:space:]' '-' | sed -E 's/-+/-/g'
}
export TICKET_BRANCH_PREFIX='jrose' # replace with your username/prefix
newticket() {
export TicketName="" # clear out previous entry
export TicketDescription="" # clear out previous entry
vared -p "Ticket name: " -r "" -c TicketName # prompt that sets env var for ticket name
vared -p "Ticket description: " -r "" -c TicketDescription # prompt that sets env var for ticket description
# Create a new branch that is PREFIX/TICKET NAME/SLUGGIFIED TICKET DESCRIPTION but on the first 50 bytes
git checkout -b "$(head -c 50 <<<"$TICKET_BRANCH_PREFIX/$TicketName/$(sluggify $TicketDescription)")"
}
ticketcommit() {
export ChangeScope="" # reset change scope
export ChangeShortDescription="" # reset change short description
export ChangeLog="" # reset change log
# If the ticket name is no longer there (new session) ask for it
if [ -z $TicketName ]; then
vared -p "Ticket name: " -r "" -c TicketName
fi
# If the ticket description is no longer there (new session) ask for it
if [ -z $TicketDescription ]; then
vared -p "Ticket description: " -r "" -c TicketDescription
fi
vared -p "Change scope: " -r "" -c ChangeScope # ask for change scope
vared -p "Change short description: " -r "" -c ChangeShortDescription # ask for change description
# Prompt for the change log because we're not using vared
echo 'Write the changes one by one pressing enter between (leave blank to finish)'
# Wait for input for each change
while read change; do
# If no input then we're done collecting changes
if [ -z $change ]; then
break
fi
if [ -z $ChangeLog ]; then # first entry
ChangeLog="- $change"
else # all other entries get a new line and added to the end
ChangeLog="$ChangeLog
- $change"
fi
done
# Do the commit
git commit --allow-empty -m "feat($ChangeScope): $ChangeShortDescription" -m "$TicketName - $TicketDescription" -m $ChangeLog
}
@jacklynrose
Copy link
Author

jacklynrose commented Oct 31, 2021

How to use

Why

Consistency and reviewer help. Providing all these details gives a better log of why something changed and what you were thinking about at the time. All code is written in a given context and that context will move as fast as your team does. Adding notes and details like the ticket will make a huge difference to reviewers and people (even yourself) coming back to the code.

Installation

Copy and paste everything and add to the end of your ~/.zshrc file. Edit the TICKET_BRANCH_PREFIX variable with your preferred username/prefix. It won't work until you create a new shell or use source ~/.zshrc to load the changes.

Setting up a branch

  1. After installing (mask sure you start a new shell or source your .zshrc again with ~/.zshrc)
  2. Run newticket
  3. Add the ticket name/number at the prompt
  4. Add the ticket description at the prompt
  5. You'll now be in your new change

Making commits

If you haven't run newticket in the same shell you may be asked for the ticket name and description again

  1. Run ticketcommit
  2. Enter the ticket name prompt if you get it
  3. Enter the ticket description prompt if you get it
  4. Enter the change scope at the prompt - what area of the application you changed
  5. Enter the change short description at the prompt - a very concise summary of the changes
  6. Enter a description of each change you made to each of the files including why and any notes for reviewers
  7. Once done adding changes leave the prompt empty and hit Enter/Return to finish adding changes
  8. A new commit will be created formatted for you with all the details

Creating a PR

Copy and paste the git log into your PR and you've already got all the thoughts and description of why changes were made in your PR written as you went.

You can use this git alias to quickly get your log formatted in a way that's easy to copy paste.

[alias]
  messages = log --pretty=format:%B --reverse master..HEAD

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