aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaria Carolin Chabowski <laria@laria.me>2020-04-26 21:50:04 +0200
committerLaria Carolin Chabowski <laria@laria.me>2020-04-26 21:50:04 +0200
commitf9d7a587a23a964be76883f1bd6ef2eee08e756c (patch)
treea3f030566459e73ae632873aaa3ca8ddd2ad3a38
parentade9edeb849803082856f7633194913ad4df93a0 (diff)
downloadratatoeskr-cms-f9d7a587a23a964be76883f1bd6ef2eee08e756c.tar.gz
ratatoeskr-cms-f9d7a587a23a964be76883f1bd6ef2eee08e756c.tar.bz2
ratatoeskr-cms-f9d7a587a23a964be76883f1bd6ef2eee08e756c.zip
Add pre-commit hooks to check for syntax errors and coding style
Just run the git-hooks/install.sh script to install the pre-commit hook
-rwxr-xr-xgit-hooks/hooks/pre-commit52
-rwxr-xr-xgit-hooks/install.sh16
-rwxr-xr-xgit-hooks/loader.sh10
3 files changed, 78 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
diff --git a/git-hooks/install.sh b/git-hooks/install.sh
new file mode 100755
index 0000000..17d59bc
--- /dev/null
+++ b/git-hooks/install.sh
@@ -0,0 +1,16 @@
+#!/bin/sh
+
+set -e
+
+githooks_dir="$(git rev-parse --git-dir)"
+for hook in hooks/*; do
+ hook="${hook#hooks/}"
+ if [ -f "$githooks_dir/hooks/$hook" ]; then
+ mv "$githooks_dir/hooks/$hook" "$githooks_dir/hooks/own_$hook"
+ fi
+
+ echo '#!/bin/sh
+ exec "$(git rev-parse --git-dir)/../git-hooks/loader.sh" '"$hook"' "$@"
+ ' > "$githooks_dir/hooks/$hook"
+ chmod +x "$githooks_dir/hooks/$hook"
+done
diff --git a/git-hooks/loader.sh b/git-hooks/loader.sh
new file mode 100755
index 0000000..eadafa6
--- /dev/null
+++ b/git-hooks/loader.sh
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+set -e
+
+hook="$1"
+shift
+
+gitdir="$(git rev-parse --git-dir)"
+[ -x "$gitdir/hooks/own_$hook" ] && "$gitdir/hooks/own_$hook" "$@"
+[ -x "$gitdir/../git-hooks/hooks/$hook" ] && "$gitdir/../git-hooks/hooks/$hook" "$@"