Skip to content
This repository has been archived by the owner on May 17, 2021. It is now read-only.

Commit

Permalink
Supply a Git pre-commit hook for tools/check-typo
Browse files Browse the repository at this point in the history
Automatically runs tools/check-typo and rejects the commit if they don't
pass.
  • Loading branch information
dra27 committed Jun 30, 2018
1 parent 18bba74 commit 546dd12
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Expand Up @@ -135,6 +135,7 @@ tools/make-package-macosx text eol=lf
tools/ocaml-objcopy-macosx text eol=lf
tools/ocamlmktop.tpl text eol=lf
tools/ocamlsize text eol=lf
tools/pre-commit-githook text eol=lf

# These two are cat scripts, so may not actually require this
config/auto-aux/sharpbang text eol=lf
Expand Down
3 changes: 3 additions & 0 deletions CONTRIBUTING.md
Expand Up @@ -68,6 +68,9 @@ columns, not use tab characters (spaces only), and not use non-ASCII
characters. These typographical rules can be checked with the script
`tools/check-typo`.

If you are working from a Git clone, you can automate this process by
copying the file `tools/pre-commit-githook` to `.git/hooks/pre-commit`.

Otherwise, there are no strongly enforced guidelines specific to the
compiler -- and, as a result, the style may differ in the different
parts of the compiler. The general [OCaml Programming
Expand Down
12 changes: 8 additions & 4 deletions tools/check-typo
Expand Up @@ -91,7 +91,7 @@ export LC_ALL=C
# Special case for recursive call from the find command (see IGNORE_DIRS).
case "$1" in
--check-prune)
case `git check-attr ocaml-typo "$2" 2>/dev/null` in
case `git check-attr $OCAML_CT_CA_FLAG ocaml-typo "$2" 2>/dev/null` in
*prune*) echo "INFO: pruned directory $2 (ocaml-typo=prune)" >&2; exit 0;;
*) exit 3;;
esac;;
Expand All @@ -113,6 +113,8 @@ while : ; do
esac
done

OCAML_CT_CAT=${OCAML_CT_CAT:-cat}

IGNORE_DIRS="
-name .git -prune -o
-type d -exec $0 --check-prune {} ; -prune -o
Expand Down Expand Up @@ -149,7 +151,8 @@ IGNORE_DIRS="
EMPTY=`git hash-object -t tree /dev/null`
git diff-tree --numstat $EMPTY HEAD -- "$f" | grep -q "^-[[:blank:]]-" \
&& continue
svnrules=`git check-attr ocaml-typo "$f" | sed -e 's/.*: //'`
svnrules=`git check-attr $OCAML_CT_CA_FLAG ocaml-typo "$f" \
| sed -e 's/.*: //'`
case $svnrules in unspecified) svnrules= ;; esac
fi
rules="$userrules"
Expand All @@ -169,14 +172,15 @@ IGNORE_DIRS="
# requires the entire line to match. This specifically detects the
# presence of lines containing malformed UTF-8. It may be tested using
# https://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
if LC_ALL=en_US.UTF8 grep -qaxv '.*' "$f" ; then
if $OCAML_CT_CAT "$OCAML_CT_PREFIX$f" \
| LC_ALL=en_US.UTF8 grep -qaxv '.*' ; then
echo File "$f" is not correctly encoded in UTF-8
exit 2
fi
;;
esac

(cat "$f" | tr -d '\r'; echo) \
($OCAML_CT_CAT "$OCAML_CT_PREFIX$f" | tr -d '\r'; echo) \
| awk -v rules="$rules" -v svnrules="$svnrules" -v file="$f" \
'
function is_err(name) {
Expand Down
49 changes: 49 additions & 0 deletions tools/pre-commit-githook
@@ -0,0 +1,49 @@
#!/bin/sh
#**************************************************************************
#* *
#* OCaml *
#* *
#* David Allsopp, MetaStack Solutions Ltd. *
#* *
#* Copyright 2017 MetaStack Solutions Ltd. *
#* *
#* All rights reserved. This file is distributed under the terms of *
#* the GNU Lesser General Public License version 2.1, with the *
#* special exception on linking described in the file LICENSE. *
#* *
#**************************************************************************

# For what it's worth, allow for empty trees!
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

# Redirect output to stderr.
exec 1>&2

# Git's built-in mechanism for whitespace is neater than ours, so do it first.
# The strange construction below creates a list of files which have either
# white-at-eol or white-at-eof included in ocaml-typo in .gitattributes and by
# prefixing the names with :! causes git diff-index to skip over them.
FILES=$(git diff-index --cached --name-only $against \
| xargs git check-attr --cached ocaml-typo \
| sed -ne 's/\(.*\): ocaml-typo:.*[ ,]white-at-eo[fl]\(,\|$\)/:!\1/p')
if ! git diff-index --check --cached $against -- $FILES ; then
exit 1
fi

# Now run check-typo over all the files in the index
ERRORS=0
export OCAML_CT_PREFIX=:
export OCAML_CT_CAT="git cat-file --textconv"
export OCAML_CT_CA_FLAG=--cached
git diff --staged --name-only | (while IFS= read -r path
do
if ! tools/check-typo $path ; then
ERRORS=1
fi
done; exit $ERRORS)

0 comments on commit 546dd12

Please sign in to comment.