Add commit hook and make the verification customizable.
[git/git.git] / templates / hooks--pre-commit
CommitLineData
89e2c5f1
JH
1#!/bin/sh
2#
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.
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.
13perl -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;
26 }
27 if ($reported_filename ne $filename) {
28 print "* In $filename\n";
29 $reported_filename = $filename;
30 }
31 print "* $why (line $lineno)\n$line\n";
32 }
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;
38 }
39 if (/^@@ -\S+ \+(\d+)/) {
40 $lineno = $1 - 1;
41 next;
42 }
43 if (/^ /) {
44 $lineno++;
45 next;
46 }
47 if (s/^\+//) {
48 $lineno++;
49 chomp;
50 if (/\s$/) {
51 bad_line("trailing whitespace", $_);
52 }
53 if (/^\s* /) {
54 bad_line("indent SP followed by a TAB", $_);
55 }
56 }
57 }
58 exit($found_bad);
59'
60