Debian Codegraph @

source/util/checkcommit

raw
1 #!/usr/bin/env perl
2
3 use warnings;
4 use strict;
5 use Encode;
6
7 ## Checks the commit diff for suspicious lines not covered by util/checkwhite
8 ## and util/unbrace, namely unresolved merges.
9
10 open DIFF, "git diff-index -p -M --cached HEAD --|"
11 or die "pre-commit: Can't obtain the diff. Run git commit -n to ignore.\n";
12
13 my $found_bad = 0;
14 my $filename;
15 my $reported_filename = "";
16 my $lineno;
17 my $lastadd = 0; # a terrible hack
18 my $skip = 0;
19
20 sub bad_line
21 {
22 return if $skip;
23 my ($why, $noquote) = @_;
24 if (!$found_bad)
25 {
26 print STDERR "*\n";
27 print STDERR "* You have some suspicious patch lines:\n";
28 print STDERR "*\n";
29 $found_bad = 1;
30 }
31 if ($reported_filename ne $filename)
32 {
33 print STDERR "* $filename\n";
34 $reported_filename = $filename;
35 }
36 print STDERR " + $why (line $lineno)\n";
37 #print STDERR "$_\n" unless $noquote;
38 }
39
40 while (<DIFF>)
41 {
42 if (m|^diff --git a/(.*) b/\1$|)
43 {
44 $filename = $1;
45
46 $skip = $filename =~ /\.(png|gif|ttf|ico|icns|fig|tex|eps|pdf)$/i
47 || $filename =~ /\.(sln|vim|pbxproj|vsprops|plist|csproj|config|cs)$/i
48 || $filename =~ /\.(vcproj|vcproj\.user|vcxproj|vcxproj\.filters)$/i
49 || $filename =~ /^\.gitmodules$/
50 || $filename =~ /\.(lex|tab)\./
51 || $filename !~ /\./;
52 next;
53 }
54 if (/^@@ -\S+ \+(\d+)/)
55 {
56 $lineno = $1 - 1;
57 next;
58 }
59 if (/^ /)
60 {
61 $lineno++;
62 next;
63 }
64 if (s/^\+//)
65 {
66 $lineno++;
67 chomp;
68 bad_line("unresolved merge conflict") if /^([<>])\1{6} |^={7}$/;
69 }
70 $lastadd = $lineno if $_ eq "";
71 }
72
73 if ($found_bad)
74 {
75 print STDERR "\nPlease correct or, if it's a false positive,\n".
76 "commit -n to ignore. In that case, please report it as a bug.\n";
77 exit 1;
78 }