Skip to content

Add php-cs-fixer hook #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 2, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -16,13 +16,14 @@ Security - in case of vulnerabilities.

## [Unreleased]
### Added
- Added hooks `disallow-commits`
- Added hooks `conventional-commit`
- Added hook `disallow-commits`
- Added hook `conventional-commit`
- Added hook `php-cs-fixer`

## [1.1.0] 2019-03-01
### Added
- Added hooks `motivation`
- Added hooks `files-watcher`
- Added hook `motivation`
- Added hook `files-watcher`

## [1.0.0] 2019-03-01
Initial release.
64 changes: 64 additions & 0 deletions php-cs-fixer/php-cs-fixer
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/bin/sh
# Git `pre-commit` hook that runs `php-cs-fixer` against changed files.
#
# Note: you can use `commit --no-verify` to skip `pre-commit` hooks.
#
# Example of config:
# ~~~
# [php-cs-fixer]
# command = php-cs-fixer
# mode = check
# config-file = .php_cs
# files-pattern = .php$
# ~~~

FCR='\033[1;31m' # Red
FCG='\033[1;32m' # Green
FCY='\033[0;33m' # Yellow
FCS='\033[0;37m' # Light gray (silver)
NC='\033[0m'

report_error() {
echo "${FCR}$@${NC}"
}

report_done() {
[[ $# > 0 ]] && echo "${FCG}✓ $@${NC}" || echo "${FCG}✓ Done${NC}"
}

command=$(git config --default php-cs-fixer --get php-cs-fixer.command)
mode=$(git config --default check --get php-cs-fixer.mode)
prefix_pattern=$(git config --default .php_cs --get php-cs-fixer.config-file)
files_pattern=$(git config --default \.php$ --get php-cs-fixer.files-pattern)

diff_files() {
git diff --name-only --diff-filter=ACMRTUXB $1 | grep -i ${files_pattern}
}

cs() {
if [[ -n "$1" ]] ; then
case ${mode} in
"check" )
command ${command} fix --dry-run --config=${prefix_pattern} -v --using-cache=no --diff --diff-format=udiff --ansi $1
;;
"fix" )
command ${command} fix --config=${prefix_pattern} -v --using-cache=no --diff --diff-format=udiff --ansi $1
;;
* )
report_error "Invalid mode option"
esac
else
echo "Nothing to fix"
fi
}

cs "$(diff_files HEAD)"
status=$?

if [[ ${status} -gt 0 ]] ; then
report_error "Commit rejected due Coding Standards errors"
else
report_done
fi

exit ${status}