Skip to content

Commit 726ffcb

Browse files
committed
[dev] githooks without external dependencies
1 parent f7f0c6d commit 726ffcb

File tree

6 files changed

+83
-36
lines changed

6 files changed

+83
-36
lines changed

.gitattributes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/.github/ export-ignore
2+
/.githooks/ export-ignore
23
/docs/ export-ignore
34
/tests/ export-ignore
45
/.gitattributes export-ignore
56
/.gitignore export-ignore
67
/.php-cs-fixer.dist.php export-ignore
78
/compose.yaml export-ignore
89
/Dockerfile export-ignore
9-
/pre-commit.config.yaml export-ignore
1010
/mkdocs.yml export-ignore
1111
/phpmd.baseline.xml export-ignore
1212
/phpmd.xml export-ignore

.githooks/pre-commit

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/bin/sh
2+
3+
DOCKER_COMPOSE_RUN="docker compose run -T --rm --no-deps php"
4+
5+
run_command () {
6+
COMMAND_NAME=$1
7+
COMMAND_RUN=$2
8+
9+
(
10+
OUTPUT=$(eval "$DOCKER_COMPOSE_RUN $COMMAND_RUN" 2>&1)
11+
status_code=$?
12+
13+
if [ $status_code -ne 0 ]; then
14+
printf "\r\033[K❌ %s \033[31m%s\033[0m\n" "$COMMAND_NAME" "failed" >&2
15+
echo "$OUTPUT" > "/tmp/${COMMAND_NAME}_pre-commit"
16+
exit 1
17+
else
18+
printf "\r\033[K✅ %s \033[32m%s\033[0m\n" "$COMMAND_NAME" "passed" >&2
19+
fi
20+
) &
21+
}
22+
23+
printf "🐚 \033[33m%s\033[0m\n\n" "Executing pre-commit hook..."
24+
25+
# Staged files
26+
STAGED_FILES=$(git diff --cached --name-only --diff-filter=AMR | grep -E '\.php$|\.php-cs-fixer(\.dist)?\.php|composer\.lock|phpunit\.xml|phpstan(-baseline)?\.neon' | tr '\n' ' ')
27+
28+
HOOK_EXIT_CODE=0
29+
if [ "$STAGED_FILES" ]; then
30+
# PHP-CS-Fixer; run on changed files or on whole project if .php-cs-fixer.dist.php/composer.lock has changed
31+
EXTRA_ARGS=''
32+
IFS='
33+
'
34+
if ! echo "${STAGED_FILES}" | grep -qE '\.php-cs-fixer(\.dist)?\.php|composer\.lock'; then
35+
EXTRA_ARGS=$(printf -- '--path-mode=intersection %s' "${STAGED_FILES}")
36+
fi
37+
38+
run_command "PHP-CS-Fixer" "vendor/bin/php-cs-fixer fix --dry-run -v --diff --config=.php-cs-fixer.dist.php --show-progress=none $EXTRA_ARGS"
39+
run_command "PHPStan" "vendor/bin/phpstan --memory-limit=1G --error-format=table"
40+
run_command "PHPMD" "vendor/bin/phpmd src/ text phpmd.xml"
41+
run_command "PHPUnit" "vendor/bin/phpunit"
42+
43+
wait
44+
45+
for OUTPUT_FILE in /tmp/*_pre-commit; do
46+
if [ ! -f "$OUTPUT_FILE" ]; then
47+
continue
48+
fi
49+
50+
HOOK_EXIT_CODE=1
51+
printf "\n----------------\n❌ %s\n\n" $(basename $OUTPUT_FILE '_output')
52+
cat "$OUTPUT_FILE"
53+
rm "$OUTPUT_FILE"
54+
done
55+
56+
else
57+
printf "ℹ️ No staged PHP files\n"
58+
HOOK_EXIT_CODE=0
59+
fi
60+
61+
exit $HOOK_EXIT_CODE

.pre-commit-config.yaml

Lines changed: 0 additions & 26 deletions
This file was deleted.

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ Features and bugfixes should be based on the `master` branch.
4242
### Prerequisites
4343

4444
* [Docker Compose](https://docs.docker.com/compose/install/)
45-
* [Pre-commit](https://pre-commit.com/#install)
4645
* [Task (optional)](https://taskfile.dev/installation/)
4746

4847
### Install dependencies
@@ -51,10 +50,10 @@ Features and bugfixes should be based on the `master` branch.
5150
task composer:install
5251
```
5352

54-
### Enable pre-commit
53+
### Enable pre-commit hook
5554

5655
```shell
57-
task pre-commit:init
56+
task git:hooks
5857
```
5958

6059
> Note: Checks for `phpcs`, `phpstan`, `phpmd` and `phpunit` are executed when committing.

Taskfile.yml

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,26 @@ tasks:
6363
cmds:
6464
- docker compose run --rm $TTY php composer update {{.PACKAGE}} {{.CLI_ARGS | default "--no-cache"}}
6565

66-
# Tools
67-
pre-commit:init:
68-
desc: Init pre-commit
66+
# Git
67+
git:hooks:
68+
desc: Setup git hooks
6969
silent: true
7070
cmds:
71-
- pre-commit install -f
71+
- |
72+
APP_HOOKS_DIR="{{.ROOT_DIR}}/.githooks"
73+
GIT_HOOKS_DIR="{{.ROOT_DIR}}/.git/hooks"
74+
75+
# Create the hooks directory if it does not exist
76+
mkdir -p "$GIT_HOOKS_DIR"
77+
78+
# Symlink hooks
79+
for hook in $(ls "$APP_HOOKS_DIR"); do
80+
ln -sf "$APP_HOOKS_DIR/$hook" "$GIT_HOOKS_DIR/$hook"
81+
done
7282
83+
printf "\033[32m%s\033[0m\n" "Git hooks installed."
84+
85+
# Tools
7386
phpcs:
7487
desc: PHPCS dry run
7588
cmds:

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@
3939
},
4040
"require-dev": {
4141
"rector/rector": "^1.0",
42-
"phpmd/phpmd": "^2.13",
42+
"phpmd/phpmd": "^2.15",
4343
"friendsofphp/php-cs-fixer": "^3.64",
44-
"phpstan/phpstan": "^1.9",
44+
"phpstan/phpstan": "^1.12",
4545
"phpunit/phpunit": "^10.0"
4646
},
4747
"config": {

0 commit comments

Comments
 (0)