forked from aparajita/capp_lint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pre-commit
executable file
·107 lines (83 loc) · 3.24 KB
/
pre-commit
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".
die() {
echo '-------------------------' 1>&2
echo 'pre-commit check failure' 1>&2
echo '-------------------------' 1>&2
echo "$@" 1>&2
if [ -f /tmp/capp_lint.tmp ]; then
rm -f /tmp/capp_lint.tmp
fi
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 'Please set user.name to your Real Name (with a space), not a userid.' "$advice"
git config --get user.email | grep '^[^@]*@[^@]*$' > /dev/null ||
die 'Please 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
# Check for trailing whitespaces, indents that uses a space before a tab and leftover conflict markers
if test "$(git diff-index --check --cached $against --)"; then
die 'The following whitespace errors or unresolved conflicts have been found:
'"$(git diff-index --check --cached $against --)"
fi
#-----------------------------------------------------------------------------
# Check for inadvertent globals and dangling variable declarations.
fileCount=`git diff --cached --name-only --diff-filter=AM $against | wc -l`
if [[ fileCount -ne "0" ]]; then
lintPath=''
if [[ `which capp_lint.py` ]]; then
lintPath="`which capp_lint.py`"
fi
if [[ -z "$lintPath" && `which capp_lint` ]]; then
lintPath="`which capp_lint`"
fi
if [[ -z "$lintPath" ]]; then
path=`dirname $0`/capp_lint.py
if [[ -x "$path" ]]; then
lintPath="$path"
fi
fi
if [[ -z "$lintPath" ]]; then
die 'Could not find capp_lint or capp_lint.py in your PATH or in .git/hooks.'
fi
git diff --cached --name-only --diff-filter=AM $against | "$lintPath" - > /tmp/capp_lint.tmp
if [ -s /tmp/capp_lint.tmp ]; then
die 'The following lint errors were found:
'"$(cat /tmp/capp_lint.tmp)"
else
rm -f /tmp/capp_lint.tmp
fi
fi
exit 0