forked from IntersectMBO/plutus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-pre-commit-hook
More file actions
executable file
·80 lines (65 loc) · 2.45 KB
/
git-pre-commit-hook
File metadata and controls
executable file
·80 lines (65 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/sh
# This is a git pre-commit hook that runs sytlish-haskell and hlint over the
# Haskell files you are about to commit. Sylish-haskell will modify the files
# that need modification in place. Hlinf warnings are reported as that and
# the commit is aborted, so you can fix it.
# Obviously you need stylish-haskell and hlint on your path. The easiest
# way to do this is to run:
#
# > stack install hlint stylish-haskell
#
# in the cardano-sl tree (to ensure the correct version of stylish-haskell is
# retrieved). The install command will copy them to ~/.local/bin/ which should
# be on your $PATH variable.
# To install this, copy it to '.git/hooks/pre-commit' in the cardano checkout
# amd make it executatble.
# It would be possible to symlink it but that is probably a bad idea security
# wise.
#-------------------------------------------------------------------------------
# This is really only needed it you are starting with an empty repo.
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
# Find all the files that are about to be commited.
files=$(git diff-index --name-status --cached HEAD | sed "s/^D.*$//;s/^[A-Z]+[ \t]*/ /")
# Redirect output to stderr.
exec 1>&2
#-------------------------------------------------------------------------------
# Prevent files with non-ascii filenames from being committed.
if test $(git diff --cached --name-only --diff-filter=A -z $against | LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0 ; then
echo "Error: Attempt to add a non-ascii file name."
echo "Commit aborted."
exit 1
fi
#-------------------------------------------------------------------------------
# Check the formatting of all HS files.
hsfiles=""
for f in $files ; do
if test $(echo $f | grep -c "\.hs$") -gt 0 ; then
if test $(echo $f | grep -c ^PatchedPkgs) -eq 0 ; then
hsfiles="$hsfiles $f"
fi
fi
done
if test -n "$hsfiles" ; then
stylish-haskell -i ${hsfiles}
if test $? -ne 0 ; then
echo
echo "Commit aborted. Stylish Haskell failed."
exit 1
fi
hlint ${hsfiles}
if test $? -ne 0 ; then
echo
echo "Commit aborted. Fix the above error before trying again."
exit 1
fi
fi
# Need to add them if we want stylish-haskell changes to be commited.
git add ${hsfiles}
#-------------------------------------------------------------------------------
# All ok!
exit 0