-
Notifications
You must be signed in to change notification settings - Fork 1
/
.pre-commit.hook
58 lines (51 loc) · 2.07 KB
/
.pre-commit.hook
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
#!/bin/sh
#
# Verifies that all files in the worktree follow our codestyle standards.
#
# Note that this script can't check that they're actually committing the nicely
# formatted code. It just checks that the worktree is clean. So if they've fixed
# all files but haven't added the codestyle fixes to their commit, they'll still
# pass this check. But it's still a great protection against most mistakes.
#
# To install this hook, just run the following in the project's root folder:
# ln -fs "../../.pre-commit.hook" .git/hooks/pre-commit
#
# Redirect output to stderr.
exec 1>&2
# Git ensures that CWD is always the root of the project folder, so we can just
# run all tests without verifying what folder we are in...
failed="no"
echo "[pre-commit] Checking work-tree codestyle..."
# Check if we need updated LazyJsonMapper class documentation.
echo "> Verifying class documentation..."
./vendor/bin/lazydoctor -c composer.json -pfo --validate-only >/dev/null
if [ $? -ne 0 ]; then
failed="yes"
fi
# Check the general codestyle format.
echo "> Verifying php-cs-fixer..."
./vendor/bin/php-cs-fixer fix --config=.php_cs.dist --allow-risky yes --dry-run
if [ $? -ne 0 ]; then
failed="yes"
fi
# Look for specific problems with the style, related to our project.
echo "> Verifying checkStyle..."
/usr/bin/env php devtools/checkStyle.php x
if [ $? -ne 0 ]; then
failed="yes"
fi
# Refuse to commit if there were problems. Instruct the user about solving it.
if [ "${failed}" = "yes" ]; then
# Yes there are lots of "echo" commands, because "\n" is not cross-platform.
echo "[commit failed] There are problems with your code..."
echo ""
echo "Run 'composer codestyle' to fix the code in your worktree."
echo ""
echo "But beware that the process is automatic, and that the result"
echo "isn't always perfect and won't be automatically staged."
echo ""
echo "So remember to manually read through the changes, then further"
echo "fix them if necessary, and finally stage the updated code"
echo "afterwards so that the fixed code gets committed."
exit 1
fi