]> mj.ucw.cz Git - libucw.git/blob - build/git/pre-commit
Merge branch 'master' into dev-lib
[libucw.git] / build / git / pre-commit
1 #!/usr/bin/perl
2
3 my $found_bad = 0;
4 my $filename;
5 my $reported_filename = "";
6 my $lineno;
7
8 if (scalar @ARGV > 0) {
9         my $f;
10         foreach $f (@ARGV) {
11                 check_file($f);
12         }
13 } else {
14         exit(0) if (system("git-rev-parse --verify HEAD 2>/dev/null"));
15         open(PATCH, "git-diff-index -p -M --cached HEAD --|") ||
16                 die("git-diff-index failed");
17         while (<PATCH>) {
18                 check_file($1) if (m|^diff --git a/(.*\.[ch]) b/\1$|);
19         }
20         close(PATCH);
21         
22 }
23 exit($found_bad);
24
25 sub bad_line {
26         my ($why, $line) = @_;
27         if (!$found_bad) {
28                 print STDERR "*\n";
29                 print STDERR "* You have some suspicious patch lines:\n";
30                 print STDERR "*\n";
31                 $found_bad = 1;
32         }
33         if ($reported_filename ne $filename) {
34                 print STDERR "* In $filename\n";
35                 $reported_filename = $filename;
36         }
37         print STDERR "* \t$why\n";
38         print STDERR "$lineno:$line\n" if ($line);
39 }
40
41
42 sub check_file {
43         ($filename) = @_;
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 = 1;
51         while(<IN>) {
52                 chomp;
53                 if (/^\s*\#include\s+"/) {
54                         bad_line("sherlock includes after global includes", $_) if (!$has_loc && $has_glob);
55                         $has_loc++;
56                 }
57                 if (/\s$/) {
58                         bad_line("trailing whitespace", $_);
59                 }
60                 if (/^\s* \t/) {
61                         bad_line("indent SP followed by a TAB", $_);
62                 }
63                 if (/^\s*\#define\s+LOCAL_DEBUG/) {
64                         bad_line("LOCAL_DEBUG left enabled", $_);
65                 }
66                 if (/^([<>])\1{6} |^={7}$/) {
67                         bad_line("unresolved merge conflict", $_);
68                 }
69
70                 $has_glob++ if (/^\s*\#include\s+\</);
71                 $has_copy++ if (/\([Cc]\)\s*\w/);
72                 $empty = $_ =~ /^\s*$/;
73                 $lineno++;
74         }
75         bad_line("empty lines at end of input") if ($empty);
76         bad_line("missing copyright") if (!$has_copy);
77         close(IN);
78 }
79
80