Skip to content

Instantly share code, notes, and snippets.

@rahulsprajapati
Forked from Ritesh-patel/pre-commit
Created June 17, 2016 20:40
Show Gist options
  • Save rahulsprajapati/e4599f496cb80872c869dab679d56bda to your computer and use it in GitHub Desktop.
Save rahulsprajapati/e4599f496cb80872c869dab679d56bda to your computer and use it in GitHub Desktop.
WordPress Plugin pre-commit
#!/bin/bash
# WordPress Plugin pre-commit hook
set -e
message="Checking staged changes..."
git_status_egrep='^[MARC].+'
for i; do
case "$i"
in
-m)
message="Checking any uncommitted changes..."
git_status_egrep='^.?[MARC].+'
shift;;
esac
done
echo $message
# Check for staged PHP files
IFS=$'\n' staged_php_files=( $(git status --porcelain | sed 's/[^ ]* -> *//g' | egrep $git_status_egrep'\.php$' | cut -c4-) )
if [ ${#staged_php_files[@]} != 0 ]; then
# PHP_CodeSniffer WordPress Coding Standards
echo "## phpcs"
if command -v phpcs >/dev/null 2>&1; then
phpcs_standard=WordPress-Core
phpcs -p -s -v --standard=$phpcs_standard "${staged_php_files[@]}"
else
echo "Skipping phpcs since not installed"
fi
fi
# Make sure the readme.md never gets out of sync with the readme.txt
generate_markdown_readme=$(find . -name generate-markdown-readme -print -quit)
if [ -n "$generate_markdown_readme" ]; then
markdown_readme_path=$($generate_markdown_readme)
git add $markdown_readme_path
fi
@rahulsprajapati
Copy link
Author

Pre-commit Hook

Create a build directory inside plugin and put pre-commit inside build directory.

Make pre-commit executable:

chmod +x build/pre-commit

Symlink to pre-commit from your project's .git/hooks/pre-commit:

cd .git/hooks && ln -s ../../build/pre-commit . && cd -

Originally from : https://gist.github.com/Ritesh-patel/7af384456816ee03aaf1f71466cef20c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment