Skip to content

Instantly share code, notes, and snippets.

@tylernhoward
Last active December 11, 2018 18:38
Show Gist options
  • Save tylernhoward/0593a706d07b59e4bdd65373ac6bf8b3 to your computer and use it in GitHub Desktop.
Save tylernhoward/0593a706d07b59e4bdd65373ac6bf8b3 to your computer and use it in GitHub Desktop.
Utility script to parse ticket number out of branch name
#!/bin/bash
### Contrast specific utility script
### - Automatically formats a commit message based on the Jira ticket found in the branch name
### ARG1 = "commit text", ARG2 = "-skip" flag
# get commit message from arg
if [[ $1 == "" ]]; then
echo "No commit message given. Try again: -> commit \"message\""
exit 1;
else
message=$1;
fi
# store if skip flag
skipValidation=$2
# get branch name
branch=$(git symbolic-ref --short HEAD)
# rip out 'CONTRAST-#####'
regex="^.[A-Z]{7}(-)[0-9]*"
# if we find a ticket in the branch name, try to commit
if [[ $branch =~ $regex ]]; then
ticket=$BASH_REMATCH
commit="$ticket $message"
if [[ $skipValidation != "-skip" ]]; then
read -p "Does this look good to commit? (y/n) -> \"$commit\"" confirm && [[ $confirm == [yY] ]] || exit 1;
fi
git commit -m "$commit"
else
echo "Branch with name: '$branch' is invalid."
exit 1;
fi
@tylernhoward
Copy link
Author

alias commit='bash ${CONTRAST_DIR}/contrast-commit.sh'

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