From fb7b2127f0886ccd9d70d24757482d98b4ff7eef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Thu, 30 Jun 2016 14:44:42 +0200 Subject: [PATCH 1/3] Makes sure git gets unglobbed patterns If bash does the globbing, it will be unaccurate and inefficient --- template/hooks/change_detector.sh | 4 ++-- template/hooks/php/cs-fixer/pre-commit | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/template/hooks/change_detector.sh b/template/hooks/change_detector.sh index ebabdba..397d90c 100755 --- a/template/hooks/change_detector.sh +++ b/template/hooks/change_detector.sh @@ -5,7 +5,7 @@ has_changed() { local hook_type=$1; shift - local monitored_paths=($@) + local monitored_paths=("$@") local against local changed @@ -39,7 +39,7 @@ has_changed() fi changed="$(git diff-index \ --name-status $against \ - -- ${monitored_paths[*]} | wc -l)" + -- "${monitored_paths[*]}" | wc -l)" ;; esac diff --git a/template/hooks/php/cs-fixer/pre-commit b/template/hooks/php/cs-fixer/pre-commit index fa880f5..95f2480 100755 --- a/template/hooks/php/cs-fixer/pre-commit +++ b/template/hooks/php/cs-fixer/pre-commit @@ -1,7 +1,7 @@ #!/bin/bash . "$GIT_DIR/hooks/change_detector.sh" -if has_changed pre-commit ./**/*.php ./*.php +if has_changed pre-commit '**/*.php' '*.php' then .git/hooks/php/cs-fixer/fix-cs fi From fb2fb3ea7f54482c156be9aab6120ca9bae0e198 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Thu, 30 Jun 2016 14:50:38 +0200 Subject: [PATCH 2/3] Fix indention --- template/hooks/change_detector.sh | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/template/hooks/change_detector.sh b/template/hooks/change_detector.sh index 397d90c..245390f 100755 --- a/template/hooks/change_detector.sh +++ b/template/hooks/change_detector.sh @@ -30,16 +30,16 @@ has_changed() -- ${monitored_paths[*]}| wc -l)" ;; pre-commit) - if git rev-parse --verify HEAD >/dev/null 2>&1 - then - against=HEAD - else - # Initial commit: diff against an empty tree object - against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 - fi - changed="$(git diff-index \ - --name-status $against \ - -- "${monitored_paths[*]}" | wc -l)" + if git rev-parse --verify HEAD >/dev/null 2>&1 + then + against=HEAD + else + # Initial commit: diff against an empty tree object + against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 + fi + changed="$(git diff-index \ + --name-status $against \ + -- "${monitored_paths[*]}" | wc -l)" ;; esac From c96ae700b4acae4db88fab5af6504164908ae58c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Paris?= Date: Thu, 30 Jun 2016 15:11:56 +0200 Subject: [PATCH 3/3] Test with a deep file --- tests/php/phpcs_fixer_test.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/php/phpcs_fixer_test.sh b/tests/php/phpcs_fixer_test.sh index afc5e50..854f6fa 100755 --- a/tests/php/phpcs_fixer_test.sh +++ b/tests/php/phpcs_fixer_test.sh @@ -8,9 +8,9 @@ testRunsPhpCsFixerOnPhpFileChange() export -f phpcsfixer export SHUNIT_TMPDIR initRepo - mkdir --parents a/b/c.php - echo "a" > c.php - git add c.php + mkdir --parents a/b + echo "a" > a/b/c.php + git add a/b/c.php git commit --quiet --message "first version of c.php" assertTrue 'PhpCsFixer was not run' "[ $(cat "${SHUNIT_TMPDIR}/phpCsFixerWasRun") == fix ]" rm "${SHUNIT_TMPDIR}/phpCsFixerWasRun"