diff options
author | Laria Carolin Chabowski <laria@laria.me> | 2020-04-30 09:38:05 +0200 |
---|---|---|
committer | Laria Carolin Chabowski <laria@laria.me> | 2020-04-30 09:38:25 +0200 |
commit | 2381c52c690342f773a7cfa500387b6bfad76952 (patch) | |
tree | 340296ea529e40b81012c936703429a86481788e /git-hooks/hooks | |
parent | 2d26169b367af60cbc03d417c09918f50aa3b882 (diff) | |
download | ste-2381c52c690342f773a7cfa500387b6bfad76952.tar.gz ste-2381c52c690342f773a7cfa500387b6bfad76952.tar.bz2 ste-2381c52c690342f773a7cfa500387b6bfad76952.zip |
Automatic code formatting
Also add git hooks that checks formatting
Diffstat (limited to 'git-hooks/hooks')
-rwxr-xr-x | git-hooks/hooks/pre-commit | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/git-hooks/hooks/pre-commit b/git-hooks/hooks/pre-commit new file mode 100755 index 0000000..5666e93 --- /dev/null +++ b/git-hooks/hooks/pre-commit @@ -0,0 +1,52 @@ +#!/bin/sh + +set -e + +gitdir="$(git rev-parse --git-dir)" + +changed_files() { + git diff --name-status --cached HEAD | tr '\011' '\012' | awk ' + /^[ADMTUXB]/ { n = 1; next } + /^[CR]/ { n = 2; next } + n > 0 { + print $0 + n-- + } + ' +} + +staged_file_hash() { + git ls-files -s "$1" | cut -d' ' -f2 +} + +lint_php() { + php -l >/dev/null +} + +lint_php_cs_fixer() { + php-cs-fixer fix --config="$gitdir/../.php_cs.dist" -v --dry-run --using-cache=no --show-progress=none - >&2 +} + +lint_changes() { + linter_name="$1" + grep_pattern="$2" + linter_func="$3" + + failcount="$(changed_files | grep "$grep_pattern" | while read -r f; do + hash="$(staged_file_hash "$f")" + if [ -n "$hash" ]; then + if ! git cat-file blob "$hash" | "$linter_func"; then + echo >&2 + echo "Linter $linter_name reported error for: $f" >&2 + echo >&2 + + echo failed + fi + fi + done | wc -l)" + + [ "$failcount" -eq 0 ] || return 1 +} + +lint_changes "php -l" '\.php$' lint_php || exit 1 +lint_changes "php_cs_fixer" '\.php$' lint_php_cs_fixer || exit 1 |