Add commit hook and make the verification customizable.
[git/jnareb-git.git] / templates / hooks--pre-commit
blobcb1c82d55db244fd0ffec151f433105472988be6
1 #!/bin/sh
3 # An example hook script to verify what is about to be committed.
4 # Called by git-commit-script 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.
8 # To enable this hook, make this file executable.
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 perl -e '
14 my $fh;
15 my $found_bad = 0;
16 my $filename;
17 my $reported_filename = "";
18 my $lineno;
19 sub bad_line {
20 my ($why, $line) = @_;
21 if (!$found_bad) {
22 print "*\n";
23 print "* You have some suspicious patch lines:\n";
24 print "*\n";
25 $found_bad = 1;
27 if ($reported_filename ne $filename) {
28 print "* In $filename\n";
29 $reported_filename = $filename;
31 print "* $why (line $lineno)\n$line\n";
33 open $fh, "-|", qw(git-diff-cache -p -M --cached HEAD);
34 while (<$fh>) {
35 if (m|^diff --git a/(.*) b/\1$|) {
36 $filename = $1;
37 next;
39 if (/^@@ -\S+ \+(\d+)/) {
40 $lineno = $1 - 1;
41 next;
43 if (/^ /) {
44 $lineno++;
45 next;
47 if (s/^\+//) {
48 $lineno++;
49 chomp;
50 if (/\s$/) {
51 bad_line("trailing whitespace", $_);
53 if (/^\s* /) {
54 bad_line("indent SP followed by a TAB", $_);
58 exit($found_bad);