Last active
March 28, 2024 01:14
-
-
Save PeloNZ/0c6b994c4c27ea52e365f4ee17473286 to your computer and use it in GitHub Desktop.
git hooks to run linter and coding standards in docker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# before pushing code | |
# Run sniffer and fixer. | |
DOCKER_EXEC_COMMAND="docker exec --workdir /path/to/project container-name" | |
# Exclude warnings for now. | |
PHPCBF_COMMAND="vendor/bin/phpcbf --report=summary --standard=.phpcs.xml --warning-severity=0 --colors" | |
# Get a list of all files to be pushed | |
# PUSH_FILES=$(git diff --name-only @{push}.. | grep ".php\{0,1\}$" ) | |
# Ignore files that only have whitespace changes, useful when using rector | |
PUSH_FILES=$(git diff -w --ignore-all-space --ignore-blank-lines -b --ignore-space-change --ignore-cr-at-eol @{push}.. | grep '.php\{0,1\}$' | grep 'diff' | cut -d ' ' -f 3 | cut -d '/' -f 2- ) | |
# If there are no PHP files to be pushed, skip the script | |
if [ "$PUSH_FILES" = "" ]; then | |
exit 0 | |
fi | |
echo "Running coding standards fixer..." | |
# run the beautifier | |
$DOCKER_EXEC_COMMAND $PHPCBF_COMMAND $PUSH_FILES | |
# Stage the fixed files | |
git add $PUSH_FILES | |
# allow push if nothing has been fixed | |
git diff --cached --quiet | |
if [ $? -ne 0 ]; then | |
echo "Fixes have been applied, please commit then try again" | |
exit 1 | |
fi | |
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# before committing code, run linter | |
DOCKER_EXEC_COMMAND="docker exec --workdir /path/to/project container-name" | |
LINT_COMMAND="vendor/bin/parallel-lint --show-deprecated" | |
# Get a list of all staged files | |
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep ".php\{0,1\}$") | |
# If there are no PHP files, skip the script | |
if [ "$STAGED_FILES" = "" ]; then | |
echo "No staged files to lint" | |
exit 0 | |
fi | |
echo "Running php linter..." | |
$DOCKER_EXEC_COMMAND $LINT_COMMAND $STAGED_FILES | |
if [ $? -ne 0 ]; then | |
exit 1 | |
fi | |
exit 0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment