Skip to content

Instantly share code, notes, and snippets.

@phstc
Last active August 29, 2015 14:00
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 phstc/b18aa5ff9616c9bf3bc2 to your computer and use it in GitHub Desktop.
Save phstc/b18aa5ff9616c9bf3bc2 to your computer and use it in GitHub Desktop.
Prepend branch name in the commit messages
#!/usr/bin/env ruby
msg_file = ARGV[0]
commit_msg = File.read(msg_file).to_s.strip
def empty_commit?(commit_msg)
return true if commit_msg.empty?
commit_msg.split("\n").each do |line|
# Lines starting with '#' will be ignored
return false if commit_msg.strip[0] != '#'
end
true
end
# if the commit was canceled (no message)
exit 0 if empty_commit?(commit_msg)
branch_name = `git rev-parse --abbrev-ref HEAD`.to_s
# remove line break added by the system call above
branch_name.chomp!
# remove git flow branch prefixes
branch_name.gsub!('feature/', '')
branch_name.gsub!('hotfix/', '')
# skip when develop or master branches
# or when the branch name is already in the beginning of the commit msg
if %w[develop master].include?(branch_name) || commit_msg.start_with?(branch_name)
File.open(msg_file, 'w') { |f| f.write commit_msg }
exit 0
end
# add the branch prefix following the Jira convention
# https://confluence.atlassian.com/display/AOD/Processing+JIRA+issues+with+commit+messages
commit_msg = "#{branch_name} #{commit_msg}"
File.open(msg_file, 'w') { |f| f.write commit_msg }
#!/bin/sh
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)
BRANCH_NAME_IN_MESSAGE="`cat $1 | egrep \"\[$BRANCH_NAME\] \"`"
if [ "$1" != "" ] && [ -n "$BRANCH_NAME" ] && [ "$BRANCH_NAME_IN_MESSAGE" == "" ] && [ "$BRANCH_NAME" != "master" ] && [ "$BRANCH_NAME" != "develop" ]; then
echo "[$BRANCH_NAME] $(cat $1)" > $1
fi
@phstc
Copy link
Author

phstc commented Apr 30, 2014

Copy to [project]/.git/hooks/commit-msg

Add permissions chmod +x [project]/.git/hooks/commit-msg

git checkout -b WOM-18
touch test.txt
git add test.txt
git commit -am "Adds test.txt"

=> [WOM-18 454d903] [WOM-18] Adds test.txt
=>  1 file changed, 0 insertions(+), 0 deletions(-)
=>  create mode 100644 test.txt

@buccolo
Copy link

buccolo commented May 15, 2014

I'm using this alternative script:

ticket=$(git symbolic-ref HEAD | awk -F'/' '{print $4}')
if [ -n "$ticket" ]; then
    sed -i '.bak' "1s/^/$ticket: /" $1
fi

The only difference as far as I can tell, is that it tags commits like:
WOM-18: Your commit msg here.

@phstc
Copy link
Author

phstc commented May 15, 2014

We can mix the solutions. Mine doesn't prepend master or developer branches and doesn't commit canceled commits, but prepends feature/ and hotfix/ - not sure if it is good or bad, because it can help to distinguish features from hot fixes.

Need to check if Jira accepts this format as an ISSUE_KEY.

@phstc
Copy link
Author

phstc commented Aug 8, 2014

No more feature/ or hotfix/ in the ruby version. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment