Skip to content

Instantly share code, notes, and snippets.

@jackbentley
Last active November 19, 2018 12:40
Show Gist options
  • Save jackbentley/094c1e8f9e1156ae77874a89ca285723 to your computer and use it in GitHub Desktop.
Save jackbentley/094c1e8f9e1156ae77874a89ca285723 to your computer and use it in GitHub Desktop.
Post commit hook for PHP CS Fixer. This will only run CS fixer on the changes made in the last commit.
#!/usr/bin/env bash
if [[ -z "${IN_HOOK}" ]]; then
export IN_HOOK='yes'
REQUIRES_UNSTASH=''
if [[ ! -z "$(git diff --no-commit-id --name-only)" ]]; then
REQUIRES_UNSTASH='yes'
echo '[] stash'
git stash
fi
echo '[] fix'
./vendor/bin/php-cs-fixer fix
echo '[] add'
git diff-tree --no-commit-id -m --name-only -r @{0} | while read line; do
git add "$line";
done
echo '[] commit'
git commit --amend --no-edit --allow-empty
echo '[] reset'
git reset --hard
if [[ -z "$(git diff-tree --no-commit-id --name-only -r @\{0\})" ]]; then
echo '[] prune empty'
git reset HEAD^
fi
if [[ ! -z "${REQUIRES_UNSTASH}" ]]; then
echo '[] stash apply'
git stash pop
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment