8

I usually create branches from JIRA issue site. So let's say my JIRA ticket name is "SOMEBUG-356: Bug in software" then a branch name will be: "feature/SOMEBUG-356-bug-in-software".

Is it possible to set some kind of template to SourceTree which would add a prefix to commit message with name of JIRA ticket of a branch that I am currently on? (It would add SOMEBUG-356 prefix if I were on branch "feature/SOMEBUG-356-bug-in-software"

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Mariusz
  • 1,825
  • 5
  • 22
  • 36

2 Answers2

4

I'm not too sure if this is the right way to go about it, at my company we use the native gitflow when creating branches etc. However on all commits we enforce a regex as part of the commit. You can do this by going to the .git folder of the project and opening the hooks folder, there you will see a commit-msg.sample file remove the .sample so it says commit-msg. In this file add something like.

#!/usr/bin/env bash


# regex to validate in commit msg
commit_regex='(SOMEBUG|SOMEOTHERBUG)-[0-9]{0,6}\w+'
error_msg="Aborting commit. Your commit message is missing a valid JIRA Issue key and number. An example commit would be SOMEBUG-1234"

if ! grep -iqE "$commit_regex" "$1"; then
    echo "$error_msg" >&2
    exit 1
fi

What this does is enforces the regex on every commit, and because it’s in your project .git folder you can have custom hooks for each project. Its then a matter of adding the origin to jira and jira will sync you commit to the jira ticket. If you then use FishEye + Crucible it can become an incredibly powerful relationship.

FullStack
  • 437
  • 5
  • 15
  • Your solution is to validate the message, not to set a template, so it is not the right answer. You can use git hooks to add validations, behaviours, etc to the git lifecycle. You will find some examples in the same folder for each type of hook. There exist one called `prepare-commit-msg.sample`. – voiski Feb 16 '18 at 20:30
  • I agree it isn't the correct answer, this enforces a regex check on commit. The answer can be found here https://stackoverflow.com/questions/5894946/how-to-add-gits-branch-name-to-the-commit-message – FullStack Feb 20 '18 at 10:46
2

I could not find the final answer to sourcetree, but I know that it uses the git hooks from the repository. This is not the final solution but cold help.

Create the file .git/hooks/prepare-commit-msg with execution rights chmod +x .git/hooks/prepare-commit-msg and use the follow code as example based on this one. You can find more examples on internet.

#!/bin/bash

COMMIT_MSG_FILE=$1
BRANCH_NAME=$(git symbolic-ref --short HEAD | sed 's/\(.*-[0-9]*\).*/\1/')
BRANCH_NAME="${BRANCH_NAME##*/}"
if [ -n "$BRANCH_NAME" ] &&
   [ $(head -1 ${COMMIT_MSG_FILE}|grep -c "${BRANCH_NAME}" ) = 0 ]
then
  sed -i.bak -e "1s/^/${BRANCH_NAME} /" ${COMMIT_MSG_FILE}
fi

This will work perfect in the terminal, but sadly Sourcetree will not show it on the commit message input. Sourcetree will use the hook only after you hit the commit button that will result in a message with the jira card in prefix.

You can go further and configure the global templates, but it will take effect only for new git clones/git init. You will still need to copy the hook for the already existing clones. Here more one script that will help you in this journey, with some help from this link:

# Creating file on your home folder
mkdir -p ~/.git-templates/hooks
cat << 'EOF' > ~/.git-templates/hooks/prepare-commit-msg
#!/bin/bash
COMMIT_MSG_FILE=$1
BRANCH_NAME=$(git symbolic-ref --short HEAD | sed 's/\(.*-[0-9]*\).*/\1/')
BRANCH_NAME="${BRANCH_NAME##*/}"
if [ -n "$BRANCH_NAME" ] &&
   [ $(head -1 ${COMMIT_MSG_FILE}|grep -c "${BRANCH_NAME}" ) = 0 ]
then
  sed -i.bak -e "1s/^/${BRANCH_NAME} /" ${COMMIT_MSG_FILE}
fi
EOF
chmod +x ~/.git-templates/hooks/prepare-commit-msg

# Use this line to config as default for all new git clones/init
git config --global init.templatedir '~/.git-templates'

# Use this line to create a alias to install this hook on existing local git repos
echo "
alias git_install_commit_template_hook='ln -s ~/.git-templates/hooks/prepare-commit-msg .git/hooks/prepare-commit-msg'
" >> ~/.bash_aliases
source ~/.bash_aliases

Why don't sourcetree executes the pre-hook when I open the message input? Yes, this is bad because it sounds more like a post-commit-message. Sourcetree makes an interface with git and has own lifecycle. In this case, the message fields will not interact with git until you hit commit button. They can improve it, it should not be hard, but maybe it has more work than I know =P

voiski
  • 437
  • 5
  • 10