blob: 3f0aa09356b7bddbdec2c9d97a9367204e7e27da [file] [log] [blame]
Andreas Wundsam698eeb82013-05-02 14:08:17 -07001#!/bin/bash -e
2
3echo "Running ${PWD}/$0"
4
5# Create a script that diffs a staged file vs. the original, and looks for illegal
6# tabs in new or modified lines
7script=$(mktemp /tmp/check.XXXXXX)
8trap "rm -f ${script}" EXIT
9cat >${script} <<"EOF"
10#!/bin/bash
11diff --old-line-format= --unchanged-line-format= "$1" "$2" | grep -q $'\t' || exit
12fn=$(basename "$2")
13[ "$2" != "${2%%.java}" ] || [ "$2" != "${2%%.py}" ] || exit
14echo "$2"
15EOF
16chmod +x ${script}
17
18# Run the script on all staged files
19badfiles=$(git difftool --staged -y -x ${script})
20
21if [ "${badfiles}" ]; then
22 echo "New or modified lines in the following files contain tab characters:"
23 echo "${badfiles}" | sed "s/^/ /"
24 echo "Please correct these problems and retry the commit."
25 exit 1
26fi
27
28if git rev-parse --verify HEAD >/dev/null 2>&1
29then
30 against=HEAD
31else
32 # Initial commit: diff against an empty tree object
33 against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
34fi
35
36set +e
37# If you want to allow non-ascii filenames set this variable to true.
38allownonascii=$(git config hooks.allownonascii)
39# Redirect output to stderr.
40exec 1>&2
41
42# Cross platform projects tend to avoid non-ascii filenames; prevent
43# them from being added to the repository. We exploit the fact that the
44# printable range starts at the space character and ends with tilde.
45if [ "$allownonascii" != "true" ] &&
46 # Note that the use of brackets around a tr range is ok here, (it's
47 # even required, for portability to Solaris 10's /usr/bin/tr), since
48 # the square bracket bytes happen to fall in the designated range.
49 test $(git diff --cached --name-only --diff-filter=A -z $against |
50 LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
51then
52 echo "Error: Attempt to add a non-ascii file name."
53 echo
54 echo "This can cause problems if you want to work"
55 echo "with people on other platforms."
56 echo
57 echo "To be portable it is advisable to rename the file ..."
58 echo
59 echo "If you know what you are doing you can disable this"
60 echo "check using:"
61 echo
62 echo " git config hooks.allownonascii true"
63 echo
64 exit 1
65fi
66
67cfg_check_whitespace=$(git config hooks.checkwhitespace)
68
69if [ -e .git/MERGE_MSG ]; then
70 # this is a merge commit - default to no check
71 if [[ $cfg_check_whitespace == "true" ]]; then
72 check_whitespace=true
73 else
74 check_whitespace=false
75 fi
76else
77 # this is not merge commit - default to check
78 if [[ $cfg_check_whitespace != "false" ]]; then
79 check_whitespace=true
80 else
81 check_whitespace=false
82 fi
83fi
84
85if ! git diff --cached --name-only | grep -q -E '\.(java|c|h|yaml|yang|json|sh|xsl|py)$'; then
86 echo "No source files found in commit - skipping whitespace check"
87 check_whitespace=false
88fi
89
90if [[ $check_whitespace == "true" ]]; then
91 # If there are whitespace errors, print the offending file names and fail.
92 rv=0
93 exec git diff-index --check --cached $against
94fi