Skip to content

Commit

Permalink
Merge pull request #18 from vardumper/feature/add-composer-hooks
Browse files Browse the repository at this point in the history
chore(git hooks): added brainmaestro/composer-git-hooks alpha release…
  • Loading branch information
holmey authored Sep 1, 2023
2 parents 70594c4 + 626d27b commit 6c04506
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 3 deletions.
43 changes: 43 additions & 0 deletions .githooks/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bash

# Commit message must be in correct format
# https://semver.org/
# https://www.conventionalcommits.org/
# https://youtu.be/nOVZxZX5dx8

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color

# commit headline must be in the correct format
commit_msg_file=$(git rev-parse --git-dir)/COMMIT_EDITMSG

# Read the commit message from the COMMIT_EDITMSG file
commit_msg=$(cat "$commit_msg_file")

# Regular expression pattern for conventional commit format
pattern="^(build|ci|docs|feat|fix|perf|refactor|style|test|chore|revert|release)(\(.+\))?: .{1,}"

# Check if the commit message matches the pattern
if [[ ! $commit_msg =~ $pattern ]]; then
printf "${RED}Aborting. ${YELLOW}Your commit message is invalid.${NC}
Syntax:
${YELLOW}<type>${NC}(${YELLOW}<scope>${NC}): ${YELLOW}<subject>${NC}
${YELLOW}<type>${NC} can be one of
build chore ci docs feat fix perf refactor revert style test
${YELLOW}<scope>${NC} is optional
${YELLOW}<subject>${NC} there must be a description of the change
Find more on this topic here:
- https://semver.org/
- https://www.conventionalcommits.org/
- https://youtu.be/nOVZxZX5dx8
"
exit 1
fi
33 changes: 33 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bash

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color

echo "pre commit hook start"
printf "committing as ${YELLOW}$(git config user.name) ${NC}/ ${YELLOW}$(git config user.email)${NC}\n"

PHP_CS_FIXER="./vendor/bin/php-cs-fixer"
PHP_CS_CONFIG=".php-cs-fixer.dist.php"
PHP_STAN="./vendor/bin/phpstan"
CHANGED_FILES=$(git diff --cached --name-only --diff-filter=ACM -- '*.php')

if [ -n "$CHANGED_FILES" ]; then
echo "php-cs-fixer start"
$PHP_CS_FIXER fix --config "$PHP_CS_CONFIG" $CHANGED_FILES;
echo "php-cs-fixer finish"

echo "php-stan start"
if $PHP_STAN analyse -c ./phpstan.neon --memory_limit=512M $CHANGED_FILES; then
echo "php-stan finish"
else
printf "${RED}Aborting. ${YELLOW}Please fix the errors php-stan found.${NC}\n"
exit 1
fi

git add $CHANGED_FILES;
fi

echo "pre commit hook finish"
21 changes: 20 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,25 @@
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.17",
"phpstan/phpstan": "^1.10.15"
"phpstan/phpstan": "^1.10.15",
"brainmaestro/composer-git-hooks": "v3.0.0-alpha.1"
},
"scripts": {
"post-install-cmd": [
"./vendor/bin/cghooks add --ignore-lock"
],
"post-update-cmd": [
"./vendor/bin/cghooks update"
]
},
"extra": {
"hooks": {
"pre-commit": [
"./.githooks/pre-commit"
],
"commit-msg": [
"./.githooks/commit-msg"
]
}
}
}
79 changes: 77 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6c04506

Please sign in to comment.