| 1 |
eval '(exit $?0)' && eval 'exec perl -w "$0" ${1+"$@"}' |
| 2 |
& eval 'exec perl -w "$0" $argv:q' |
| 3 |
if 0; |
| 4 |
|
| 5 |
# An hook script to check the commit log message. |
| 6 |
# Called by "git commit" with one argument, the name of the file |
| 7 |
# that has the commit message. The hook should exit with non-zero |
| 8 |
# status after issuing an appropriate message if it wants to stop the |
| 9 |
# commit. The hook is allowed to edit the commit message file. |
| 10 |
# |
| 11 |
# To enable this hook, copy it to "~/.git/hooks/commit-msg". |
| 12 |
# |
| 13 |
# This script is based on the one from GNU coreutils. |
| 14 |
|
| 15 |
use strict; |
| 16 |
use warnings; |
| 17 |
(my $ME = $0) =~ s|.*/||; |
| 18 |
|
| 19 |
my $editor = $ENV{EDITOR} || 'vi'; |
| 20 |
$ENV{PATH} = '/bin:/usr/bin'; |
| 21 |
|
| 22 |
# Rewrite the $LOG_FILE (old contents in @$LINE_REF) with an additional |
| 23 |
# commented diagnostic "# $ERR" line at the top. |
| 24 |
sub rewrite($$$) |
| 25 |
{ |
| 26 |
my ($log_file, $err, $line_ref) = @_; |
| 27 |
local *LOG; |
| 28 |
open LOG, '>', $log_file |
| 29 |
or die "$ME: $log_file: failed to open for writing: $!"; |
| 30 |
print LOG "# $err"; |
| 31 |
print LOG @$line_ref; |
| 32 |
close LOG |
| 33 |
or die "$ME: $log_file: failed to rewrite: $!\n"; |
| 34 |
} |
| 35 |
|
| 36 |
sub re_edit($) |
| 37 |
{ |
| 38 |
my ($log_file) = @_; |
| 39 |
|
| 40 |
warn "Interrupt (Ctrl-C) to abort...\n"; |
| 41 |
|
| 42 |
system 'sh', '-c', "$editor $log_file"; |
| 43 |
($? & 127) || ($? >> 8) |
| 44 |
and die "$ME: $log_file: the editor ($editor) failed, aborting\n"; |
| 45 |
} |
| 46 |
|
| 47 |
# Given a $LOG_FILE name and a \@LINE buffer, |
| 48 |
# read the contents of the file into the buffer and analyze it. |
| 49 |
# If the log message passes muster, return the empty string. |
| 50 |
# If not, return a diagnostic. |
| 51 |
sub check_msg($$) |
| 52 |
{ |
| 53 |
my ($log_file, $line_ref) = @_; |
| 54 |
|
| 55 |
local *LOG; |
| 56 |
open LOG, '<', $log_file |
| 57 |
or return "failed to open for reading: $!"; |
| 58 |
@$line_ref = <LOG>; |
| 59 |
close LOG; |
| 60 |
|
| 61 |
my @line = @$line_ref; |
| 62 |
chomp @line; |
| 63 |
|
| 64 |
# Don't filter out blank or comment lines; git does that already, |
| 65 |
# and if we were to ignore them here, it could lead to committing |
| 66 |
# with lines that start with "#" in the log. |
| 67 |
|
| 68 |
# Filter out leading blank and comment lines. |
| 69 |
# while (@line && $line[0] =~ /^(?:#.*|[ \t]*)$/) { shift @line; } |
| 70 |
|
| 71 |
# Filter out blank and comment lines at EOF. |
| 72 |
# while (@line && $line[$#line] =~ /^(?:#.*|[ \t]*)$/) { pop @line; } |
| 73 |
|
| 74 |
@line == 0 |
| 75 |
and return 'no log message'; |
| 76 |
|
| 77 |
# The first line should not be too short |
| 78 |
8 < length $line[0] || return 'summary line too short'; |
| 79 |
|
| 80 |
# The first line should not start with an asterisk or a hash sign. |
| 81 |
# An asterisk might indicate that a change entry was started right |
| 82 |
# at the first line. |
| 83 |
$line[0] =~ /^[*#]/ && return "summary line starts with an * or #"; |
| 84 |
|
| 85 |
# Second line should be blank or not present. |
| 86 |
2 <= @line && length $line[1] |
| 87 |
and return 'second line must be empty'; |
| 88 |
|
| 89 |
# See git-commit(1), this is the --cleanup=scissors option. Everything |
| 90 |
# after and including this line gets ignored. |
| 91 |
my $marker = '# ------------------------ >8 ------------------------'; |
| 92 |
|
| 93 |
# Limit line length to allow for the ChangeLog's leading TAB. |
| 94 |
foreach my $line (@line) |
| 95 |
{ |
| 96 |
72 < length $line && $line =~ /^[^#]/ |
| 97 |
and return 'line longer than 72 characters'; |
| 98 |
|
| 99 |
last if $line eq $marker; |
| 100 |
} |
| 101 |
|
| 102 |
return ''; |
| 103 |
} |
| 104 |
|
| 105 |
{ |
| 106 |
@ARGV == 1 |
| 107 |
or die; |
| 108 |
|
| 109 |
my $log_file = $ARGV[0]; |
| 110 |
|
| 111 |
while (1) |
| 112 |
{ |
| 113 |
my @line; |
| 114 |
my $err = check_msg $log_file, \@line; |
| 115 |
$err eq '' |
| 116 |
and last; |
| 117 |
$err = "$ME: $err\n"; |
| 118 |
warn $err; |
| 119 |
exit 1; |
| 120 |
|
| 121 |
# Insert the diagnostic as a comment on the first line of $log_file. |
| 122 |
#rewrite $log_file, $err, \@line; |
| 123 |
#re_edit $log_file; |
| 124 |
# |
| 125 |
## Stop if our parent is killed. |
| 126 |
#getppid() == 1 |
| 127 |
# and last; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
# Local Variables: |
| 132 |
# mode: perl |
| 133 |
# End: |