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