1

We are using SourceTree on Windows and Jira, and our rule is that the commit message always has to include the ticket ID from Jira, like HAD-4234.

It feels like this is something that can be automated. Also, sometimes people are forgetting this.

I have now tried and failed to come up with a way to do this. The main thing I tried was setting up a prepare-commit-msg hook in .git\hooks of the project, but that one didn't seem to have any effect at all (aka, it looks like SourceTree simply ignored it). I have tried a number of variations such as the following:

#!C:/Programs/Git/bin/sh.exe
#
# Inspects branch name and checks if it contains a Jira ticket number (i.e. ABC-123).
# If yes, commit message will be automatically prepended with [ABC-123].
#
# Useful for looking through git history and relating a commit or group of commits
# back to a user story.
#

BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)

# Ensure BRANCH_NAME is not empty and is not in a detached HEAD state (i.e. rebase).
# SKIP_PREPARE_COMMIT_MSG may be used as an escape hatch to disable this hook,
# while still allowing other githooks to run.
if [ ! -z "$BRANCH_NAME" ] && [ "$BRANCH_NAME" != "HEAD" ] && [ "$SKIP_PREPARE_COMMIT_MSG" != 1 ]; then

  PREFIX_PATTERN='[A-Z]{2,5}-[0-9]{1,4}'

  [[ $BRANCH_NAME =~ $PREFIX_PATTERN ]]

  PREFIX=${BASH_REMATCH[0]}

  PREFIX_IN_COMMIT=$(grep -c "\[$PREFIX\]" $1)

  # Ensure PREFIX exists in BRANCH_NAME and is not already present in the commit message
  if [[ -n "$PREFIX" ]] && ! [[ $PREFIX_IN_COMMIT -ge 1 ]]; then
    sed -i.bak -e "1s~^~[$PREFIX] ~" $1
  fi
fi

In the end, I am still left with a blank commit message box. I did manage to configure SourceTree to automatically link the commit ID to the Jira ticket when it is typed in, but that is not what I want.

What I want is that every developer on my team, when he opens the commit dialogue, is already provided with the ticket ID in the commit message box, like "HAD-4234: "

I have read about something called commit message templates, but my latest status is that those are not implemented yet for SourceTree on Windows.

So, is there a way to achieve this on SourceTree for Windows? Or is this simply impossible as of yet?

Kira Resari
  • 1,718
  • 4
  • 19
  • 50

1 Answers1

1

Okay, so I figured out that the above actually does work. The only thing to keep in mind that it does not actually display the Jira ID in the commit message when typing it into the Commit Message box in SourceTree. However, once you hit commit, the Ticket ID is automatically prepended.

So, here's a step by step instruction for how to get it to work:

  1. In your project, go to the folder .git/hooks (create the hooks folder if it is not present)
  2. In that folder, create a file named prepare-commit-msg
  3. Copy and paste the following into that file, then save it:
#!C:/Program\ Files/Git/usr/bin/sh.exe
#
# Inspects branch name and checks if it contains a Jira ticket number (i.e. ABC-123).
# If yes, commit message will be automatically prepended with [ABC-123].
#
# Useful for looking through git history and relating a commit or group of commits
# back to a user story.
#
 
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
 
# Ensure BRANCH_NAME is not empty and is not in a detached HEAD state (i.e. rebase).
# SKIP_PREPARE_COMMIT_MSG may be used as an escape hatch to disable this hook,
# while still allowing other githooks to run.
if [ ! -z "$BRANCH_NAME" ] && [ "$BRANCH_NAME" != "HEAD" ] && [ "$SKIP_PREPARE_COMMIT_MSG" != 1 ]; then
 
  PREFIX_PATTERN='[A-Z]{2,5}-[0-9]{1,4}'
 
  [[ $BRANCH_NAME =~ $PREFIX_PATTERN ]]
 
  PREFIX=${BASH_REMATCH[0]}
 
  PREFIX_IN_COMMIT=$(grep -c "\[$PREFIX\]" $1)
 
  # Ensure PREFIX exists in BRANCH_NAME and is not already present in the commit message
  if [[ -n "$PREFIX" ]] && ! [[ $PREFIX_IN_COMMIT -ge 1 ]]; then
    sed -i.bak -e "1s~^~$PREFIX: ~" $1
  fi
 
fi
  • If you don't have Git installed under C:/Program Files/Git, you have to adjust the first line accordingly
  • The above will prepend the ID in the format "HAD-234: "
    • If you want to have it displayed in another format, adjust the $PREFIX: part in the prepare-commit-msg-File (between the two ~ in the bottommost line before the two fis)
      • e.g.: [$PREFIX] will instead format it as "[HAD-234] "
Kira Resari
  • 1,718
  • 4
  • 19
  • 50