Last active
August 29, 2015 14:00
-
-
Save phstc/b18aa5ff9616c9bf3bc2 to your computer and use it in GitHub Desktop.
Prepend branch name in the commit messages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
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.
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.
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
Copy to
[project]/.git/hooks/commit-msg
Add permissions
chmod +x [project]/.git/hooks/commit-msg