aboutsummaryrefslogtreecommitdiffstats
path: root/commit-check
blob: c7880368256487135e5e9c153b834c12fa4eaa45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env perl

## commit-check --- Simple git commit style checker
# Copyright (C) 2014  Tom Willemse <tom@ryuslash.org>

# commit-check is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# commit-check is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with commit-ccheck. If not, see <http://www.gnu.org/licenses/>.

=head1 Commit check

This is a simple commit style checker. It is made to be used as a git
C<commit-msg> hook. It expects the name of the file to check as the
first command-line argument. The style that's checked is based on
tpope's L<A Note About Git Commit
Messages|http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html>.

Currently there are 4 style errors that are checked for.

=over

=item 1

The first character of the first line of the commit message should be
capitalized.

=item 2

The first line of the commit message should be no longer than 50
characters.

=item 3

The second line of the commit message should be empty.

=item 4

No line should be longer than 72 characters.

=back

All comments are skipped and so is all whitespace at the beginning of
the file.

If any of the error conditions are encountered the program will exit
with a non-C<0> status. This has the effect of stopping git from
committing the message when this program is used as a C<commit-msg>
hook. Each error condition encountered also prints a message to the
standard error stream.

=cut

use strict;
use warnings;

my $lineno = 0;
my $status = 0;

sub err {
    my ($msg) = @_;

    print STDERR "Error on line $lineno (actual line $.): ". $msg ."\n";
    $status = 1;
}

open(my $commitfile, "<", $ARGV[0]) or die "Couldn't open $ARGV[0]";

while (<$commitfile>) {
    next if /^#/;                 # Discard comments
    next if $lineno == 0 && /^$/; # Discard leading empty lines
    $lineno++;                    # Start at 1, so increment first

    if ($lineno == 1) {
        err "Not capitalized" if /^[^[:upper:]]/;
        err "Longer than 50 characters" if /^.{51,}/;
        next;
    }

    err "Should be empty" if $lineno == 2 && /.+/;
    err "Longer than 72 characters" if /^.{73,}/;
}

close $commitfile;
exit $status;