Make default pre-commit hook less noisy
[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.
215a7ad1 4# Called by git-commit with no arguments. The hook should
89e2c5f1
JH
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.
d37fd032
JH
13
14if git-rev-parse --verify HEAD 2>/dev/null
15then
7a3db75b 16 git-diff-index -p -M --cached HEAD --
d37fd032
JH
17else
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 :
21fi |
89e2c5f1 22perl -e '
89e2c5f1
JH
23 my $found_bad = 0;
24 my $filename;
25 my $reported_filename = "";
26 my $lineno;
27 sub bad_line {
28 my ($why, $line) = @_;
29 if (!$found_bad) {
b909a15e
MC
30 print STDERR "*\n";
31 print STDERR "* You have some suspicious patch lines:\n";
32 print STDERR "*\n";
89e2c5f1
JH
33 $found_bad = 1;
34 }
35 if ($reported_filename ne $filename) {
b909a15e 36 print STDERR "* In $filename\n";
89e2c5f1
JH
37 $reported_filename = $filename;
38 }
b909a15e
MC
39 print STDERR "* $why (line $lineno)\n";
40 print STDERR "$filename:$lineno:$line\n";
89e2c5f1 41 }
d37fd032 42 while (<>) {
89e2c5f1
JH
43 if (m|^diff --git a/(.*) b/\1$|) {
44 $filename = $1;
45 next;
46 }
47 if (/^@@ -\S+ \+(\d+)/) {
48 $lineno = $1 - 1;
49 next;
50 }
51 if (/^ /) {
52 $lineno++;
53 next;
54 }
55 if (s/^\+//) {
56 $lineno++;
57 chomp;
58 if (/\s$/) {
59 bad_line("trailing whitespace", $_);
60 }
4a21d13d 61 if (/^\s* \t/) {
89e2c5f1
JH
62 bad_line("indent SP followed by a TAB", $_);
63 }
c60b5282 64 if (/^([<>])\1{6} |^={7}$/) {
61c2bcbd
JH
65 bad_line("unresolved merge conflict", $_);
66 }
89e2c5f1
JH
67 }
68 }
69 exit($found_bad);
70'