Skip to content

Instantly share code, notes, and snippets.

@andrewtremblay
Last active September 30, 2019 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewtremblay/30f5faa605db5d0de9b68c2e47a25229 to your computer and use it in GitHub Desktop.
Save andrewtremblay/30f5faa605db5d0de9b68c2e47a25229 to your computer and use it in GitHub Desktop.
Smart Commit git hooks and scripts

These git hooks and scripts provide a more seamless experience linking git branches and commits to certain jira tickets.

This uses the Smart Commits feature https://confluence.atlassian.com/bitbucket/use-smart-commits-298979931.html

How to set up:

  1. Enable Smart Commits https://confluence.atlassian.com/fisheye/enabling-smart-commits-960155641.html

  2. Add prepare-commit-msg to your .git/hooks directory in your git repo

  3. Re-initialize your hooks with git init

  4. Append your ~/.bash_profile file with the content in bash_profile_additions

  5. Re-initialize bash: source ~/.bash_profile

How to use:

  1. Copy a link to a jira ticket (https://peartx.atlassian.net/browse/EXAMPLE-2019)
  2. In your console run one of your bash functions, for example:

createFeatureBranch https://peartx.atlassian.net/browse/EXAMPLE-2019 new-login-screen

Creates a branch named feature/EXAMPLE-2019-new-login-screen with one empty commit: "[EXAMPLE-2019] starting work"

  1. All commmits within properly named branches will prepend the jira ticket to the commit subject, for example:

git commit -m "another empty commit" --allow-empty

In feature/EXAMPLE-2019-new-login-screen becomes commit message "[EXAMPLE-2019] another empty commit"

Caveats

Adding this githook to existing repos will break your commit commands on branches that do not match the given regex.

# assumes you're clean on master
create-empty-jira-branch() { # JIRA_TICKET=$1 BRANCH_NAME=$2
git checkout master
git pull
git branch $1
git checkout $1
# add [$1] to commit msg if prepare-commit-msg hook is missing
git commit -m "starting work" --allow-empty
git push --set-upstream origin $2
git status
}
# most branches come with prefixes, for example feature/EXAMPLE-1-login-page
createFeatureBranch(){ # JIRA_URL=$1 BRANCH_EXTRA_NAME=$2
local JIRA_TICKET=$(echo $1| cut -d'/' -f 5)
create-empty-jira-branch $JIRA_TICKET "feature/$JIRA_TICKET-$2"
}
# most branches come with prefixes, for example bugfix/EXAMPLE-1-fix-spelling
createBugfixBranch(){ # JIRA_URL=$1 BRANCH_EXTRA_NAME=$2
local JIRA_TICKET=$(echo $1| cut -d'/' -f 5)
create-empty-jira-branch $JIRA_TICKET "bugfix/$JIRA_TICKET-$2"
}
!/bin/bash
Include any branches for which you wish to disable this script
if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(master develop staging test)
fi
# Get the current branch name and check if it is excluded
BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
# Trim it down to get the parts we're interested in
TRIMMED=$(echo $BRANCH_NAME | sed -e 's:^.*\/\([^-]*-[^-]*\)-.*:\1:' -e \
'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/')
# If it isn't excluded, preprend the trimmed branch identifier to the given message
if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]]; then
sed -i.bak -e "1s/^/[$TRIMMED] /" $1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment