Remove all trailing whitespace from source files
[gpgme.git] / build-aux / git-hooks / commit-msg
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   # Limit line length to allow for the ChangeLog's leading TAB.
90   foreach my $line (@line)
91     {
92       72 < length $line && $line =~ /^[^#]/
93         and return 'line longer than 72 characters';
94     }
95
96   return '';
97 }
98
99 {
100   @ARGV == 1
101     or die;
102
103   my $log_file = $ARGV[0];
104
105   while (1)
106     {
107       my @line;
108       my $err = check_msg $log_file, \@line;
109       $err eq ''
110         and last;
111       $err = "$ME: $err\n";
112       warn $err;
113       exit 1;
114
115       # Insert the diagnostic as a comment on the first line of $log_file.
116       #rewrite $log_file, $err, \@line;
117       #re_edit $log_file;
118       #
119       ## Stop if our parent is killed.
120       #getppid() == 1
121       #  and last;
122     }
123 }
124
125 # Local Variables:
126 # mode: perl
127 # End: