2010-08-03 15:00:14 +00:00
|
|
|
#!/usr/bin/env bash
|
2010-08-27 20:43:57 +00:00
|
|
|
#=============================================================================
|
2012-02-29 21:26:33 +00:00
|
|
|
# Copyright 2010-2012 Kitware, Inc.
|
2010-08-27 20:43:57 +00:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
#=============================================================================
|
2010-03-31 21:39:00 +00:00
|
|
|
|
2012-02-29 21:26:33 +00:00
|
|
|
. "${BASH_SOURCE%/*}/hooks-config.bash"
|
|
|
|
|
2013-04-04 17:48:59 +00:00
|
|
|
# Start with project-specific hook.
|
|
|
|
hooks_start pre-commit "$@"
|
|
|
|
|
2010-03-31 21:39:00 +00:00
|
|
|
die() {
|
|
|
|
echo 'pre-commit hook failure' 1>&2
|
|
|
|
echo '-----------------------' 1>&2
|
|
|
|
echo '' 1>&2
|
|
|
|
echo "$@" 1>&2
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2010-06-07 17:56:33 +00:00
|
|
|
zero='0000000000000000000000000000000000000000'
|
|
|
|
|
2010-03-31 21:39:00 +00:00
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
# Check for committer identity.
|
2010-04-20 15:28:25 +00:00
|
|
|
advice='
|
|
|
|
Use the commands
|
2010-03-31 21:39:00 +00:00
|
|
|
|
|
|
|
git config --global user.name '\''Your Name'\''
|
|
|
|
git config --global user.email '\''you@yourdomain.com'\''
|
|
|
|
|
|
|
|
to introduce yourself to Git before committing.'
|
|
|
|
|
2010-04-20 15:28:25 +00:00
|
|
|
# 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"
|
|
|
|
|
2010-03-31 21:39:00 +00:00
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
# 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
|
|
|
|
|
2011-01-26 18:47:38 +00:00
|
|
|
# Merge ("git commit" after "git merge" with conflicts or --no-commit)
|
|
|
|
merge_head=$(git rev-parse -q --verify MERGE_HEAD) || merge_head=''
|
|
|
|
|
2010-03-31 21:39:00 +00:00
|
|
|
# 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
|
|
|
|
|
2010-05-20 15:42:57 +00:00
|
|
|
#-----------------------------------------------------------------------------
|
2010-03-31 21:39:00 +00:00
|
|
|
# Builtin whitespace checks.
|
|
|
|
bad=$(git diff-index --check --cached $against --) || die "$bad"
|
2010-04-23 19:49:01 +00:00
|
|
|
|
2010-08-30 14:02:42 +00:00
|
|
|
# Approximate whitespace=tab-in-indent check with Git < 1.7.2.
|
2010-12-30 14:08:10 +00:00
|
|
|
git --version | grep -q " \(1\.[0-6]\|1\.7\.[01]\)" &&
|
|
|
|
approx_tab_in_indent=true || approx_tab_in_indent=false
|
2010-04-23 19:49:01 +00:00
|
|
|
check_tab() {
|
2010-08-30 14:02:42 +00:00
|
|
|
lines=$(git diff-index -p --cached $against -- "$1" |
|
|
|
|
grep '^+ ') &&
|
|
|
|
echo "$lines" |
|
|
|
|
while read line; do
|
|
|
|
echo "$1: tab in indent." &&
|
|
|
|
echo "$line"
|
|
|
|
done
|
2010-04-23 19:49:01 +00:00
|
|
|
}
|
2010-12-30 14:08:10 +00:00
|
|
|
|
2010-12-30 14:11:38 +00:00
|
|
|
# Reject addition of a line without a newline at end-of-file.
|
|
|
|
check_no_lf_at_eof() {
|
|
|
|
lines=$(git diff-index -p --cached $against -- "$1" | tail -2)
|
|
|
|
if echo "$lines" | head -1 | grep -q '^+' &&
|
|
|
|
echo "$lines" | tail -1 | grep -q '^\\ No newline'; then
|
|
|
|
echo "$1: No newline at end of file"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2010-12-30 14:08:10 +00:00
|
|
|
# Custom whitespace checks.
|
|
|
|
check_whitespace() {
|
2010-08-30 14:02:42 +00:00
|
|
|
ws=$(git check-attr whitespace -- "$file" |
|
|
|
|
sed 's/^[^:]*: whitespace: //')
|
2010-12-30 14:08:10 +00:00
|
|
|
if $approx_tab_in_indent; then
|
|
|
|
case ",$ws," in
|
|
|
|
*,tab-in-indent,*) check_tab "$1" ;;
|
|
|
|
esac
|
|
|
|
fi
|
2010-12-30 14:11:38 +00:00
|
|
|
case ",$ws," in
|
|
|
|
*,no-lf-at-eof,*) check_no_lf_at_eof "$1" ;;
|
|
|
|
esac
|
2010-04-27 17:55:06 +00:00
|
|
|
}
|
2010-12-30 14:08:10 +00:00
|
|
|
bad=$(git diff-index --name-only --cached $against -- |
|
|
|
|
while read file; do
|
|
|
|
check_whitespace "$file"
|
|
|
|
done)
|
|
|
|
test -z "$bad" || die "$bad"
|
2010-05-20 15:42:57 +00:00
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
2010-06-07 17:56:33 +00:00
|
|
|
# Check file modes and sizes.
|
2010-06-07 17:27:19 +00:00
|
|
|
mode_looks_exe() {
|
2010-05-20 15:42:57 +00:00
|
|
|
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 () {
|
2015-04-15 14:38:39 +00:00
|
|
|
echo "The file '$file' has looks executable but does not have an executable mode.
|
|
|
|
On UNIX, run:
|
|
|
|
|
|
|
|
$ chmod u+x '$file' && git add -u '$file'
|
|
|
|
|
|
|
|
On Windows, run:
|
|
|
|
|
|
|
|
$ git update-index --chmod=+x '$file'
|
|
|
|
"
|
2010-05-20 15:42:57 +00:00
|
|
|
}
|
|
|
|
mode_bad_exe () {
|
2015-04-15 14:38:39 +00:00
|
|
|
echo "The file '$file' has executable mode but does not look executable.
|
|
|
|
On UNIX, run:
|
|
|
|
|
|
|
|
$ chmod u-x '$file' && git add -u '$file'
|
|
|
|
|
|
|
|
On Windows, run:
|
|
|
|
|
|
|
|
$ git update-index --chmod=-x '$file'
|
|
|
|
"
|
2010-05-20 15:42:57 +00:00
|
|
|
}
|
|
|
|
mode_non_file () {
|
|
|
|
echo "The path '$file' has a non-file mode."
|
|
|
|
}
|
|
|
|
check_mode() {
|
|
|
|
case "$dst_mode" in
|
2010-06-07 17:27:19 +00:00
|
|
|
100755) mode_looks_exe "$file" "$dst_obj" || mode_bad_exe ;;
|
|
|
|
100644) mode_looks_exe "$file" "$dst_obj" && mode_not_exe ;;
|
2010-05-20 15:42:57 +00:00
|
|
|
160000) ;;
|
|
|
|
*) mode_non_file ;;
|
|
|
|
esac
|
|
|
|
}
|
2010-06-07 17:56:33 +00:00
|
|
|
|
|
|
|
size_max_KiB=$(git config hooks.MaxObjectKiB)
|
|
|
|
test -n "$size_max_KiB" || size_max_KiB=1024
|
2010-08-25 15:50:53 +00:00
|
|
|
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).
|
|
|
|
'
|
|
|
|
}
|
2010-06-07 17:56:33 +00:00
|
|
|
size_too_large() {
|
2010-08-25 15:50:53 +00:00
|
|
|
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
|
2010-06-07 17:56:33 +00:00
|
|
|
}
|
|
|
|
check_size() {
|
2010-08-25 15:50:53 +00:00
|
|
|
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
|
2010-06-07 17:56:33 +00:00
|
|
|
file_KiB=$(expr '(' $(git cat-file -s "$dst_obj") + 1023 ')' / 1024)
|
2010-08-25 15:50:53 +00:00
|
|
|
test "$file_KiB" -le "$max_KiB" || size_too_large
|
2010-06-07 17:56:33 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2010-08-10 21:30:06 +00:00
|
|
|
short_commit() {
|
|
|
|
git rev-parse --short "$1" 2>/dev/null || echo "$1"
|
|
|
|
}
|
|
|
|
|
|
|
|
lookup_config_module_update() {
|
2010-09-15 13:22:03 +00:00
|
|
|
update=$(git config "hooks.$1.update")
|
|
|
|
|
|
|
|
# Special-case "true" to accept any update.
|
|
|
|
test "{$update}" = "{true}" && echo '.' && return
|
|
|
|
|
2010-08-10 21:30:06 +00:00
|
|
|
# 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"'\)$'
|
2010-09-15 13:22:03 +00:00
|
|
|
echo "$update" |
|
2010-08-10 21:30:06 +00:00
|
|
|
sed -n "/$regex/ {s/$regex/"'^\1[a-z0-9]* \2[a-z0-9]*$/;p;}' |
|
|
|
|
grep '.' # Return false if result is empty.
|
|
|
|
}
|
|
|
|
|
|
|
|
check_module() {
|
2010-09-15 13:22:03 +00:00
|
|
|
enabled=$(git config --get --bool hooks.submodule) || enabled=true
|
|
|
|
test "$enabled" = "false" && return
|
|
|
|
|
2011-01-26 18:47:38 +00:00
|
|
|
# Allow merged submodule updates.
|
|
|
|
test -n "$merge_head" &&
|
|
|
|
merge_obj=$(git rev-parse -q --verify "$merge_head:$file") &&
|
|
|
|
test "$merge_obj" = "$dst_obj" && return
|
|
|
|
|
2010-08-10 21:30:06 +00:00
|
|
|
# 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.
|
|
|
|
'
|
2011-01-26 18:56:06 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
check_module_rewind() {
|
|
|
|
parent_name="$1"
|
|
|
|
parent_commit="$2"
|
|
|
|
base=$(GIT_DIR="$file/.git" \
|
|
|
|
git merge-base $src_obj $dst_obj 2>/dev/null) || base=''
|
|
|
|
test "$base" != "$dst_obj" && return
|
|
|
|
parent_short=$(short_commit "$parent_commit")
|
|
|
|
src_short=$(GIT_DIR="$file/.git" short_commit "$src_obj")
|
|
|
|
dst_short=$(GIT_DIR="$file/.git" short_commit "$dst_obj")
|
|
|
|
echo 'This commit would rewind a submodule link:
|
|
|
|
|
|
|
|
"'"$file"'" '"$src_short => $dst_short"'
|
|
|
|
|
|
|
|
from the newer version in '"$parent_name"' ('"$parent_short"'). Run
|
|
|
|
|
|
|
|
git reset '"$parent_name"' -- "'"$file"'"
|
|
|
|
git submodule update -- "'"$file"'"
|
|
|
|
|
|
|
|
to checkout the newer version of the submodule in your work tree.
|
|
|
|
Then try the commit again.
|
|
|
|
'
|
|
|
|
return 1
|
2010-08-10 21:30:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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" |
|
2010-05-20 15:42:57 +00:00
|
|
|
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
|
2010-06-07 18:07:38 +00:00
|
|
|
fi
|
|
|
|
if test "$dst_mode" != "160000" -a "$dst_mode" != '000000'; then
|
2010-06-07 17:56:33 +00:00
|
|
|
check_size
|
2010-05-20 15:42:57 +00:00
|
|
|
fi
|
2010-08-10 21:30:06 +00:00
|
|
|
done
|
|
|
|
test -n "$diffs_module" && echo "$diffs_module" |
|
|
|
|
while read src_mode dst_mode src_obj dst_obj status file; do
|
2011-01-26 18:56:06 +00:00
|
|
|
check_module_rewind HEAD "$against" &&
|
|
|
|
check_module ||
|
|
|
|
break
|
|
|
|
done
|
|
|
|
)
|
|
|
|
test -z "$bad" || die "$bad"
|
|
|
|
|
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
# Merge checks.
|
|
|
|
if test -n "$merge_head"; then
|
|
|
|
merge_diffs=$(git diff-index --cached $merge_head -- |
|
|
|
|
sed -n '/^:[^:]/ {s/^://;p;}')
|
|
|
|
else
|
|
|
|
merge_diffs=''
|
|
|
|
fi
|
|
|
|
merge_diffs_normal=$(echo "$merge_diffs" | grep -v '^...... 160000')
|
|
|
|
merge_diffs_module=$(echo "$merge_diffs" | grep '^...... 160000')
|
|
|
|
bad=$(
|
|
|
|
test -n "$merge_diffs_module" && echo "$merge_diffs_module" |
|
|
|
|
while read src_mode dst_mode src_obj dst_obj status file; do
|
|
|
|
check_module_rewind MERGE_HEAD "$merge_head" ||
|
|
|
|
break
|
2010-08-10 21:30:06 +00:00
|
|
|
done
|
|
|
|
)
|
2010-05-20 15:42:57 +00:00
|
|
|
test -z "$bad" || die "$bad"
|
pre-commit: Apply uncrustify and KWStyle check for modified files.
uncrustify (uncrustify.sourceforge.net) is applied to changed files prior to
commit.
This feature is off by default. To enable this behavior, set
git config hooks.uncrustify true
By default, the behavior of git-mergetool is used to review the changes
uncrustify makes before they are added to the commit. For more information on
this behavior, see
git help mergetool
KWStyle is run on the changed C++ files and the commit is aborted if the files
do not pass the test. A file similar to the original is saved with a '*.kws'
extension so that line numbers referenced in the error message can be examined.
The test is off by default. To enable this behavoir, set
git config hooks.KWStyle true
Project specific uncrustify and KWStyle configuration files are set with 'git
config'. For example,
git config hooks.uncrustify.conf path/to/uncrustify.conf
git config hooks.KWStyle.conf path/to/KWStyle.conf
git config hooks.KWStyle.overwriteRulesConf path/to/overwrite.conf # optional
If the appropriate values have not been set, die() is called. An
optional KWStyle overwrite rules file can also been configured.
The files on which to run the style checks must also be identified in the
repository's '.gitattributes'. For example,
*.h hooks.style
*.cpp hooks.style
Or, to only enable a subset of style hooks,
*.h hooks.style=KWStyle
*.cpp hooks.style=KWStyle,uncrustify
Change-Id: Ia6b2d4136af3002eb0ec5d36f03c50df928917f4
2010-09-09 16:45:14 +00:00
|
|
|
|
2010-11-16 22:40:16 +00:00
|
|
|
#-----------------------------------------------------------------------------
|
|
|
|
# Chain to project-specific hook.
|
|
|
|
hooks_chain pre-commit "$@"
|
|
|
|
|
pre-commit: Apply uncrustify and KWStyle check for modified files.
uncrustify (uncrustify.sourceforge.net) is applied to changed files prior to
commit.
This feature is off by default. To enable this behavior, set
git config hooks.uncrustify true
By default, the behavior of git-mergetool is used to review the changes
uncrustify makes before they are added to the commit. For more information on
this behavior, see
git help mergetool
KWStyle is run on the changed C++ files and the commit is aborted if the files
do not pass the test. A file similar to the original is saved with a '*.kws'
extension so that line numbers referenced in the error message can be examined.
The test is off by default. To enable this behavoir, set
git config hooks.KWStyle true
Project specific uncrustify and KWStyle configuration files are set with 'git
config'. For example,
git config hooks.uncrustify.conf path/to/uncrustify.conf
git config hooks.KWStyle.conf path/to/KWStyle.conf
git config hooks.KWStyle.overwriteRulesConf path/to/overwrite.conf # optional
If the appropriate values have not been set, die() is called. An
optional KWStyle overwrite rules file can also been configured.
The files on which to run the style checks must also be identified in the
repository's '.gitattributes'. For example,
*.h hooks.style
*.cpp hooks.style
Or, to only enable a subset of style hooks,
*.h hooks.style=KWStyle
*.cpp hooks.style=KWStyle,uncrustify
Change-Id: Ia6b2d4136af3002eb0ec5d36f03c50df928917f4
2010-09-09 16:45:14 +00:00
|
|
|
# vim: set filetype=sh tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab :
|