Andreas Wundsam | 698eeb8 | 2013-05-02 14:08:17 -0700 | [diff] [blame] | 1 | #!/bin/bash -e |
| 2 | |
| 3 | echo "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 |
| 7 | script=$(mktemp /tmp/check.XXXXXX) |
| 8 | trap "rm -f ${script}" EXIT |
| 9 | cat >${script} <<"EOF" |
| 10 | #!/bin/bash |
| 11 | diff --old-line-format= --unchanged-line-format= "$1" "$2" | grep -q $'\t' || exit |
| 12 | fn=$(basename "$2") |
| 13 | [ "$2" != "${2%%.java}" ] || [ "$2" != "${2%%.py}" ] || exit |
| 14 | echo "$2" |
| 15 | EOF |
| 16 | chmod +x ${script} |
| 17 | |
| 18 | # Run the script on all staged files |
| 19 | badfiles=$(git difftool --staged -y -x ${script}) |
| 20 | |
| 21 | if [ "${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 |
| 26 | fi |
| 27 | |
| 28 | if git rev-parse --verify HEAD >/dev/null 2>&1 |
| 29 | then |
| 30 | against=HEAD |
| 31 | else |
| 32 | # Initial commit: diff against an empty tree object |
| 33 | against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 |
| 34 | fi |
| 35 | |
| 36 | set +e |
| 37 | # If you want to allow non-ascii filenames set this variable to true. |
| 38 | allownonascii=$(git config hooks.allownonascii) |
| 39 | # Redirect output to stderr. |
| 40 | exec 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. |
| 45 | if [ "$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 |
| 51 | then |
| 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 |
| 65 | fi |
| 66 | |
| 67 | cfg_check_whitespace=$(git config hooks.checkwhitespace) |
| 68 | |
| 69 | if [ -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 |
| 76 | else |
| 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 |
| 83 | fi |
| 84 | |
| 85 | if ! 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 |
| 88 | fi |
| 89 | |
| 90 | if [[ $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 |
| 94 | fi |