package / ocaml-base-compiler.4.10.0 / tools / pre-commit-githook
1 #!/usr/bin/env bash
2 #**************************************************************************
3 #* *
4 #* OCaml *
5 #* *
6 #* David Allsopp, MetaStack Solutions Ltd. *
7 #* *
8 #* Copyright 2017 MetaStack Solutions Ltd. *
9 #* *
10 #* All rights reserved. This file is distributed under the terms of *
11 #* the GNU Lesser General Public License version 2.1, with the *
12 #* special exception on linking described in the file LICENSE. *
13 #* *
14 #**************************************************************************
15
16 # Bump this on any changes. It's vital that HOOK_VERSION followed by equals
17 # appears nowhere else in these sources!
18 HOOK_VERSION=4
19
20 # For what it's worth, allow for empty trees!
21 if git rev-parse --verify HEAD >/dev/null 2>&1
22 then
23 against=HEAD
24 else
25 # Initial commit: diff against an empty tree object
26 against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
27 fi
28
29 # Redirect output to stderr.
30 exec 1>&2
31
32 # Check to see if the script's been updated
33 if git ls-files --error-unmatch tools/pre-commit-githook >/dev/null 2>&1 ; then
34 THEIR_VERSION=$(git cat-file --textconv HEAD:tools/pre-commit-githook \
35 | sed -ne 's/^HOOK_VERSION=//p')
36 if [[ -n $THEIR_VERSION && $THEIR_VERSION -gt $HOOK_VERSION ]] ; then
37 echo "Note: tools/pre-commit-githook is newer than .git/hooks/pre-commit"
38 echo " You may wish to update your local githook"
39 fi
40 fi
41
42 # Git's built-in mechanism for whitespace is neater than ours, so do it first.
43 # The strange construction below creates a list of files which have either
44 # white-at-eol or white-at-eof included in ocaml-typo in .gitattributes and by
45 # prefixing the names with :! causes git diff-index to skip over them.
46 if [[ -n $(git diff-index --cached --name-only $against) ]] ; then
47 FILES=$(git diff-index --cached --name-only $against \
48 | xargs git check-attr --cached ocaml-typo \
49 | sed -ne 's/\(.*\): ocaml-typo:.*[ ,]white-at-eo[fl]\(,\|$\)/:!\1/p')
50 if ! git diff-index --check --cached $against -- $FILES ; then
51 exit 1
52 fi
53 else
54 exit 0
55 fi
56
57 # Test to see if any part of the directory name has been marked prune
58 not_pruned () {
59 DIR=$(dirname "$1")
60 if [ "$DIR" = "." ] ; then
61 return 0
62 else
63 case ",$(git check-attr typo.prune "$DIR" | sed -e 's/.*: //')," in
64 ,set,)
65 return 1
66 ;;
67 *)
68
69 not_pruned "$DIR"
70 return $?
71 esac
72 fi
73 }
74
75 # Now run check-typo over all the files in the index
76 ERRORS=0
77 export OCAML_CT_PREFIX=:
78 export OCAML_CT_CAT="git cat-file --textconv"
79 export OCAML_CT_CA_FLAG=--cached
80 git diff --diff-filter=d --staged --name-only | (while IFS= read -r path
81 do
82 if not_pruned "$path" && ! tools/check-typo "./$path" ; then
83 ERRORS=1
84 fi
85 done; exit $ERRORS)
86