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"
If the commit only exists in your local repository and has not been pushed to GitHub.com, you can amend the commit message with the git commit --amend command. On the command line, navigate to the repository that contains the commit you want to amend. Type git commit --amend and press Enter.
The commit type subject line should be all lowercase with a character limit to encourage succinct descriptions. The optional commit body should be used to provide further detail that cannot fit within the character limitations of the subject line description.
Commit filesClick Commit in the toolbar. Select the files to commit in the Pending files panel. Enter a commit message. Click Commit at the bottom of the window.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With