Last active
December 30, 2015 05:49
-
-
Save ciscou/7785511 to your computer and use it in GitHub Desktop.
commit-msg hook to ensure we're adding the Pivotal Tracker story id
This file contains 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 | |
# This is a git hook that checks commit messages for validity | |
# This file must live in your project's .git/hooks/ folder | |
# and have execution permission | |
message_file = ARGV[0] | |
message = File.read(message_file) | |
def valid_commit_message?(message) | |
lines = message.lines.map(&:chomp) | |
line_is_not_empty?(lines.shift) && | |
line_is_empty?(lines.shift) && | |
lines_contain_link_to_trello?(lines) | |
end | |
def line_is_empty?(line) | |
line && line.empty? | |
end | |
def line_is_not_empty?(line) | |
line && !line.empty? | |
end | |
def lines_contain_link_to_trello?(lines) | |
regex = %r{https://trello\.com/c/(\w+)(?:\/[\w\-]+)?|\W#(\w+)} | |
lines.any? { |line| regex.match(line) } | |
end | |
unless valid_commit_message? message | |
puts "Did you forget to reference the Trello card this commit refers to?" | |
puts "More info: https://gist.github.com/ciscou/8d755a68d0ddc7dc7895" | |
puts "If you have a very good reason not to add a Trello card link, use --no-verify" | |
exit 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment