summaryrefslogtreecommitdiff
path: root/pre-commit
blob: 14e03ab59385e3f19cd17dd25b059d9277e0a29d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/sh
#
# Copy or link this file as ".git/hooks/pre-commit".

die() {
	echo 'pre-commit hook failure' 1>&2
	echo '-----------------------' 1>&2
	echo '' 1>&2
	echo "$@" 1>&2
	exit 1
}

#-----------------------------------------------------------------------------
# Check for committer identity.
advice='
Use the commands

	git config --global user.name '\''Your Name'\''
	git config --global user.email '\''you@yourdomain.com'\''

to introduce yourself to Git before committing.'

# Ensure name and email are available.
git config --get user.name > /dev/null &&
git config --get user.email > /dev/null ||
die 'Identity not configured!' "$advice"

# Validate the name and email.
git config --get user.name | grep ' ' > /dev/null ||
die 'Set user.name to your Real Name (with a space), not a userid.' "$advice"
git config --get user.email | grep '^[^@]*@[^@]*$' > /dev/null ||
die 'Set user.email to an email address (userid@validdomain.com).' "$advice"

#-----------------------------------------------------------------------------
# Check content that will be added by this commit.

if git rev-parse --verify -q HEAD > /dev/null; then
	against=HEAD
else
	# Initial commit: diff against an empty tree object
	against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

# Disallow non-ascii file names.  The printable range starts at the
# space character and ends with tilde.
if test "$(git diff --cached --name-only --diff-filter=A -z $against |
	   LC_ALL=C tr -d '[ -~]\0')"; then
	die 'Non-ascii file names may not be added:
'"$(git diff --cached --name-only --diff-filter=A $against)"
fi

# Builtin whitespace checks.
bad=$(git diff-index --check --cached $against --) || die "$bad"

# Reject leading TABs.
check_tab() {
	git diff-index -p --cached $against -- "$1" |
	grep '^+	' > /dev/null &&
	echo "  $1"
}
check_file() {
	case "$1" in
		*.c)		check_tab "$1" ;;
		*.h)		check_tab "$1" ;;
		*.cxx)		check_tab "$1" ;;
		*.txx)		check_tab "$1" ;;
		*.hxx)		check_tab "$1" ;;
		*.htm)		check_tab "$1" ;;
		*.html)		check_tab "$1" ;;
		*.txt)		check_tab "$1" ;;
		*.cmake)	check_tab "$1" ;;
	esac
}
bad=$(git diff-index --name-only --cached $against -- |
while read file; do
	check_file "$file"
done)
test -z "$bad" || die 'Leading TABs added in
'"$bad"'
Convert them to spaces (2 per TAB) before commit.'