Last active
October 31, 2021 23:57
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 theTICKET_BRANCH_PREFIX
variable with your preferred username/prefix. It won't work until you create a new shell or usesource ~/.zshrc
to load the changes.Setting up a branch
.zshrc
again with~/.zshrc
)newticket
Making commits
If you haven't run
newticket
in the same shell you may be asked for the ticket name and description againticketcommit
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.