Skip to content

Instantly share code, notes, and snippets.

@jwalsh
Forked from jasonrobertfox/commit-msg
Last active August 29, 2015 14:08
Show Gist options
  • Save jwalsh/0a9403db5b53258b47ca to your computer and use it in GitHub Desktop.
Save jwalsh/0a9403db5b53258b47ca to your computer and use it in GitHub Desktop.

Background

If you don’t want to remember this each tiem you could also integrate a change for hooks/prepare-commit-msg in a manner like

https://gist.github.com/bartoszmajsak/1396344#file-prepare-commit-msg-sh or https://bitbucket.org/tpettersen/prepare-commit-msg-jira

This could then be used in conjunction with a hook/commit-msg that enforces standard behavior on commit messages:

Basic Setup

#!/bin/sh

grep -q 'BOOS-' $1 || {
        echo "Add BOOS ticket"
        echo "Commit aborted"
        false
}

Advanced Installation

mkdir -p ~/.git_template/hooks
git config --global init.templatedir '~/.git_template'
cp commit-msg validate_commit.rb ~/.git_template/hooks
git init
echo "git commit -m 'No ticket in first line should fail'"
#!/bin/sh
exec < /dev/tty
./validate_commit.rb $1
# GIT COMMIT MESSAGE FORMAT ERRORS:
# Error 8: No line should be over 72 characters long.
BOOS-1234: Sample title
This implements
- Sub 1
- Sub 2
01234567890123456789012345678901234567890123456789012345670123456789012345678901234567890123456789012345678901234567
# GIT COMMIT MESSAGE FORMAT ERRORS:
# Error 1: First line should be less than 50 characters in length.
TITTLE TOO LONG 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789 BOOS-1234
# GIT COMMIT MESSAGE FORMAT ERRORS:
# Error 1: First line should be less than 50 characters in length.
0123456789012345678901234567890123456789012345678901234567
#!/bin/sh
echo Test harness for valid and invalid commit messages
echo
echo Valid
for T in valid*.txt
do
echo $T
./validate_commit.rb $T
done
echo
echo Invalid
for T in invalid*.txt
do
echo $T
./validate_commit.rb $T
done
BOOS-1234: Sample title
This implements
- Sub 1
- Sub 2
#!/usr/bin/ruby
# Encoding: utf-8
editor = ENV['EDITOR'] != 'none' ? ENV['EDITOR'] : 'vim'
message_file = ARGV[0]
def check_format_rules(line_number, line)
real_line_number = line_number + 1
return "Error #{real_line_number}: First line should be less than 50 characters in length." if line_number == 0 && line.length > 50
return "Error #{real_line_number}: First line should include ticket." if line_number == 0 && line["BOOS-d"]
return "Error #{real_line_number}: Second line should be empty." if line_number == 1 && line.length > 0
(return "Error #{real_line_number}: No line should be over 72 characters long." if line.length > 72) unless line[0,1] == '#'
false
end
while true
commit_msg = []
errors = []
File.open(message_file, 'r').each_with_index do |line, line_number|
commit_msg.push line
e = check_format_rules line_number, line.strip
errors.push e if e
end
unless errors.empty?
File.open(message_file, 'w') do |file|
file.puts "\n# GIT COMMIT MESSAGE FORMAT ERRORS:"
errors.each { |error| file.puts "# #{error}" }
file.puts "\n"
commit_msg.each { |line| file.puts line }
end
puts 'Invalid git commit message format. Press y to edit and n to cancel the commit. [y/n]'
choice = $stdin.gets.chomp
exit 1 if %w(no n).include?(choice.downcase)
next if `#{editor} #{message_file}`
end
break
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment