]> mj.ucw.cz Git - libucw.git/blob - build/git/pre-commit
5b112663dfb062be08359a05f02dd67eb54ae8ad
[libucw.git] / build / git / pre-commit
1 #!/bin/sh
2 #
3 # An example hook script to verify what is about to be committed.
4 # Called by git-commit with no arguments.  The hook should
5 # exit with non-zero status after issuing an appropriate message if
6 # it wants to stop the commit.
7 #
8 # To enable this hook, make this file executable.
9
10 # This is slightly modified from Andrew Morton's Perfect Patch.
11 # Lines you introduce should not have trailing whitespace.
12 # Also check for an indentation that has SP before a TAB.
13
14 if git-rev-parse --verify HEAD 2>/dev/null
15 then
16         git-diff-index -p -M --cached HEAD --
17 else
18         # NEEDSWORK: we should produce a diff with an empty tree here
19         # if we want to do the same verification for the initial import.
20         :
21 fi |
22 perl -e '
23     my $found_bad = 0;
24     my $filename;
25     my $reported_filename = "";
26     my $lineno;
27     my @sh_checks;
28     sub bad_line {
29         my ($why, $line) = @_;
30         if (!$found_bad) {
31             print STDERR "*\n";
32             print STDERR "* You have some suspicious patch lines:\n";
33             print STDERR "*\n";
34             $found_bad = 1;
35         }
36         if ($reported_filename ne $filename) {
37             print STDERR "* In $filename\n";
38             $reported_filename = $filename;
39         }
40         print STDERR "* $why ($filename:$lineno)\n";
41         print STDERR "$lineno:$line\n" if ($line);
42     }
43     sub sh_check {
44         open(IN, "$filename") || die ("Cannot open $filename");
45         my $has_loc = 0;
46         my $has_glob = 0;
47         my $has_copy = 0;
48         my $empty = 0;
49
50         $lineno = 0;
51         while(<IN>) {
52             if (/^\s*\#include\s+"/) {
53                     bad_line("sherlock includes after global includes", $_) if (!$has_loc && $has_glob);
54                     $has_loc++;
55             }
56             $has_glob++ if (/^\s*\#include\s+\</);
57             $has_copy++ if (/\(c\)\s*\w/);
58             $empty = $_ =~ /^\s*$/;
59             $lineno++;
60         }
61         bad_line("empty lines at end of input") if ($empty);
62         bad_line("missing copyright") if (!$has_copy);
63         close(IN);
64     }
65     while (<>) {
66         if (m|^diff --git a/(.*) b/\1$|) {
67             $filename = $1;
68             push (@sh_checks, $filename) if ($filename =~ /\.[ch]$/);
69             next;
70         }
71         if (/^@@ -\S+ \+(\d+)/) {
72             $lineno = $1 - 1;
73             next;
74         }
75         if (/^ /) {
76             $lineno++;
77             next;
78         }
79         if (s/^\+//) {
80             $lineno++;
81             chomp;
82             if (/\s$/) {
83                 bad_line("trailing whitespace", $_);
84             }
85             if (/^\s* \t/) {
86                 bad_line("indent SP followed by a TAB", $_);
87             }
88             #if (/^\s*\#define\s+LOCAL_DEBUG/) {
89             #    bad_line("LOCAL_DEBUG in commit", $_);
90             #}
91             if (/^([<>])\1{6} |^={7}$/) {
92                 bad_line("unresolved merge conflict", $_);
93             }
94         }
95     }
96     foreach $filename (@sh_checks) {
97         sh_check();
98     }
99     exit($found_bad);
100 '