Skip to content

Commit d6a3800

Browse files
committed
Setup PHP style check in git pre-commit hook
1 parent e69a08e commit d6a3800

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

.git-hooks-matomo/pre-commit

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/bin/bash
2+
3+
### Check we're running in the context of a plugin and get helpful dir variables ###
4+
5+
REPO_DIR="$(git rev-parse --show-toplevel)"
6+
echo "Running pre-commit hook in repo: $REPO_DIR"
7+
8+
if [[ "$REPO_DIR" =~ /plugins/(.*) ]]; then
9+
PLUGIN_PATH="plugins/${BASH_REMATCH[1]}/"
10+
else
11+
echo "Not a plugin, not running any further checks"
12+
exit 1
13+
fi
14+
MATOMO_DIR=$(echo "$REPO_DIR" | sed -E 's|/plugins/.*$||')
15+
16+
17+
18+
# Ensure that PHPCBF command is available
19+
PHPCBF_COMMAND=""
20+
if [ -f "${MATOMO_DIR}/vendor/bin/phpcbf" ]; then
21+
PHPCBF_COMMAND="${MATOMO_DIR}/vendor/bin/phpcbf ${REPO_DIR} --standard=${REPO_DIR}/phpcs.xml"
22+
fi
23+
# If no command, exit
24+
if [[ -z "PHPCBF_COMMAND" ]]; then
25+
echo "No way to run phpcbf found. It should be installed when running Matomo in dev mode."
26+
exit 1
27+
fi
28+
29+
echo "Running PHPCBF to fix any style issues which can be corrected automatically..."
30+
echo "Command being run: ${PHPCBF_COMMAND}"
31+
$PHPCBF_COMMAND
32+
33+
34+
35+
echo ""
36+
# Ensure that PHPCS command is available
37+
PHPCS_COMMAND=""
38+
if [ -f "${MATOMO_DIR}/vendor/bin/phpcs" ]; then
39+
PHPCS_COMMAND="${MATOMO_DIR}/vendor/bin/phpcs --report-full --standard=${REPO_DIR}/phpcs.xml ${REPO_DIR}"
40+
fi
41+
# If no command, exit
42+
if [[ -z "PHPCS_COMMAND" ]]; then
43+
echo "No way to run phpcs found. It should be installed when running Matomo in dev mode."
44+
exit 1
45+
fi
46+
47+
echo "Running PHPCS check on repo: ${PHPCS_COMMAND}"
48+
if ! $PHPCS_COMMAND; then
49+
echo "Commit cancelled!"
50+
echo "There was an issue found during the PHPCS check. Please fix any issues from the report above and commit again."
51+
exit 1
52+
fi
53+
54+
echo ""
55+
echo "No issues found during style check. Continuing with commit..."
56+
echo ""
57+
58+
exit 0

0 commit comments

Comments
 (0)