summaryrefslogtreecommitdiff
path: root/pre-commit
blob: fafc3e2cc7c5dbadfe56c015b23e83eb104a916b (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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env bash
#
# 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
}

zero='0000000000000000000000000000000000000000'

#-----------------------------------------------------------------------------
# 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.'

#-----------------------------------------------------------------------------
# Check file modes and sizes.
mode_looks_exe() {
	case "$1" in
		*.bat) return 0 ;;
		*.cmd) return 0 ;;
		*.exe) return 0 ;;
		*.com) return 0 ;;
	esac
	git cat-file blob "$2" | head -1 | grep "^#!/" > /dev/null
}
mode_not_exe () {
	echo "The file '$file' has looks executable but does not have an executable mode."
}
mode_bad_exe () {
	echo "The file '$file' has executable mode but does not look executable."
}
mode_non_file () {
	echo "The path '$file' has a non-file mode."
}
check_mode() {
	case "$dst_mode" in
		100755) mode_looks_exe "$file" "$dst_obj" || mode_bad_exe ;;
		100644) mode_looks_exe "$file" "$dst_obj" && mode_not_exe ;;
		160000) ;;
		*)	mode_non_file ;;
	esac
}

size_max_KiB=$(git config hooks.MaxObjectKiB)
test -n "$size_max_KiB" || size_max_KiB=1024
size_too_large_once=""
size_too_large_once() {
	test -z "$size_too_large_once" || return ; size_too_large_once=done
	echo 'At least one file is staged for commit with size larger than its limit.
We prefer to keep large files out of the main source tree, especially
binary files that do not compress well.  This hook disallows large files
by default but can be configured.  A limit for specific files or patterns
may be set in ".gitattributes" with the "hooks.MaxObjectKiB" attribute.
For example, the line

  *.c              hooks.MaxObjectKiB=2048

sets a limit of 2048 KiB for C source files.  See "git help attributes"
for details on the .gitattributes format.  If no attribute has been set
for a given file then its size is limited by the local default.  Run

  git config hooks.MaxObjectKiB $KiB

to set the local default limit (0 to disable).
'
}
size_too_large() {
	size_too_large_once
	echo "The path '$file' has size $file_KiB KiB, greater than allowed $max_KiB KiB."
}
size_validate_max_KiB() {
	test "$max_KiB" -ge "0" 2>/dev/null && return 0
	echo "The path '$file' has invalid attribute \"hooks-MaxObjectKiB=$max_KiB\"."
	return 1
}
check_size() {
	test "$dst_obj" != "$zero" || return
	max_KiB=$(git check-attr hooks.MaxObjectKiB -- "$file" |
		  sed 's/^[^:]*: hooks.MaxObjectKiB: //')
	case "$max_KiB" in
		'unset')       return ;; # No maximum for this object.
		'set')         max_KiB="$size_max_KiB" ;; # Use local default.
		'unspecified') max_KiB="$size_max_KiB" ;; # Use local default.
		*) size_validate_max_KiB || return ;;
	esac
	if test "$max_KiB" -gt "0"; then
		file_KiB=$(expr '(' $(git cat-file -s "$dst_obj") + 1023 ')' / 1024)
		test "$file_KiB" -le "$max_KiB" || size_too_large
	fi
}

short_commit() {
	git rev-parse --short "$1" 2>/dev/null || echo "$1"
}

lookup_config_module_update() {
	# Format is "aaaaaa..bbbbbb" for update aaaaaa => bbbbbb.
	# Convert to regex "^aaaaaa[a-z0-9]* bbbbbb[a-z0-9]*$".
	sha1ex='[a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9]*'
	regex='^\('"$sha1ex"'\)\.\.\('"$sha1ex"'\)$'
	git config "hooks.$1.update" |
	sed -n "/$regex/ {s/$regex/"'^\1[a-z0-9]* \2[a-z0-9]*$/;p;}' |
	grep '.' # Return false if result is empty.
}

check_module() {
	# Allow module-only commits without extra work.
	test -z "$diffs_normal" && return

	# Check if module update is allowed with other changes.
	allow=$(lookup_config_module_update "$file") || allow='none'
	if echo "$src_obj $dst_obj" | grep "$allow" > /dev/null; then
		return
	fi
	src_short=$(short_commit "$src_obj")
	dst_short=$(short_commit "$dst_obj")
	echo 'A submodule link is staged for commit (among other changes):

  "'"$file"'"  '"$src_short => $dst_short"'

This may occur by accident so we require an extra step to commit.
If you intend to include this change in your commit, run

  git config "hooks.'"$file"'.update" '"$src_short..$dst_short"'

to explicitly allow the change and try the commit again.  Otherwise run

  git reset HEAD -- "'"$file"'"

to unstage the change.  Furthermore, if you did not intend to modify
the submodule at all, also run

  git submodule update -- "'"$file"'"

to checkout the current version of the submodule in your work tree.
Test your changes again to see if they still work with the module.
Finally, try the commit again.
'
}

diffs=$(git diff-index --cached $against -- |
        sed -n '/^:[^:]/ {s/^://;p;}')
diffs_normal=$(echo "$diffs" | grep -v '^...... 160000')
diffs_module=$(echo "$diffs" | grep    '^...... 160000')
bad=$(
test -n "$diffs_normal" && echo "$diffs_normal" |
while read src_mode dst_mode src_obj dst_obj status file; do
	if test "$src_mode" != "$dst_mode" -a "$dst_mode" != "000000"; then
		check_mode
	fi
	if test "$dst_mode" != "160000" -a "$dst_mode" != '000000'; then
		check_size
	fi
done
test -n "$diffs_module" && echo "$diffs_module" |
while read src_mode dst_mode src_obj dst_obj status file; do
	check_module
done
)
test -z "$bad" || die "$bad"