git hook for legit commit message (#123 good message)
Asked Answered
S

3

5

I need to make sure commit messages are some what legit else reject it. The commit message should be like "#123 fixing missing bracket"

I want to make sure it begins with hash, there is an integer (no 123a), and the message is at least 10 words.

Nice to have: the message would not be the exact same in a row

I am using this Trac plugin for change set, it describes the commit message format in more detail http://trac-hacks.org/wiki/TracTicketChangelogPlugin

Thanks,

Sheerness answered 22/8, 2011 at 21:39 Comment(1)
Does this answer your question? How do I set a pattern for git commit messages?Gillies
S
5

I created a commit-msg hook with:

#!/usr/bin/env ruby
message_file = ARGV[0]
message = File.read(message_file)

#starts with # then number, space, and at least 5 words no more than 200
$regex = /(^#[0-9]+ \W*(\w+(\W+|$)){5,200})/

if !$regex.match(message)
puts "Your message is not formatted correctly (example: #XXX at least 5 words)"
exit 1
end

I borrowed from this blog post http://fhopf.blogspot.com/2011/01/git-hook-for-redmine-messages.html

Sheerness answered 23/8, 2011 at 14:14 Comment(2)
Ah, it wasn't clear to me from your question that you wanted to prevent commits of the wrong format even being created locally. My answer addresses refusing pushes that contain commits with the wrong format, which means you don't have to organize distributing your hook to every contributor - you may want both.Derry
Your answer helped me get a good understanding so I was able to do a hook.Sheerness
D
5

You can create a pre-receive hook that refuses commits based on any criteria you like - you just have to print an error to standard output and exit with a non-zero status. For example, something along the lines of:

#!/bin/sh
while read rev_old rev_new ref
do
    MALFORMED="$(git rev-list --oneline $rev_old..$rev_new | egrep -v '^[a-f0-9]+ #[0-9]+ ')"
    if [ x"$MALFORMED" != x ]
    then
        echo Some commits had a malformed subject line
        exit 1
    fi
done

(I haven't tested that, but you get the idea...)

Derry answered 22/8, 2011 at 22:2 Comment(0)
S
5

I created a commit-msg hook with:

#!/usr/bin/env ruby
message_file = ARGV[0]
message = File.read(message_file)

#starts with # then number, space, and at least 5 words no more than 200
$regex = /(^#[0-9]+ \W*(\w+(\W+|$)){5,200})/

if !$regex.match(message)
puts "Your message is not formatted correctly (example: #XXX at least 5 words)"
exit 1
end

I borrowed from this blog post http://fhopf.blogspot.com/2011/01/git-hook-for-redmine-messages.html

Sheerness answered 23/8, 2011 at 14:14 Comment(2)
Ah, it wasn't clear to me from your question that you wanted to prevent commits of the wrong format even being created locally. My answer addresses refusing pushes that contain commits with the wrong format, which means you don't have to organize distributing your hook to every contributor - you may want both.Derry
Your answer helped me get a good understanding so I was able to do a hook.Sheerness
B
0

Mike,

Our user group, the Central Florida Web Developers User Group, recently had a discussion on GitHooks which can be implemented to solve the problem you have presented. The discussion was led by Rick Osborne of Full Sail University. You can access the meeting here http://www.meetup.com/florida-web-developers/events/20352661/ and the recording here http://www.meetup.com/florida-web-developers/messages/boards/thread/12465891/#45223031.

If you take the time to go through the entire recorded discussion you will see Rick walk you through this process step-by-step including downloadable examples.

Hope this helps.

Barrack answered 22/8, 2011 at 21:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.