Skip to content

Instantly share code, notes, and snippets.

@omega8cc
Forked from skwashd/README.md
Created August 17, 2016 12:45
Show Gist options
  • Save omega8cc/a53642ce44d485a9311d663c3708f8fb to your computer and use it in GitHub Desktop.
Save omega8cc/a53642ce44d485a9311d663c3708f8fb to your computer and use it in GitHub Desktop.
Drupal git pre-commit hook

This pre-commit hook is designed to be used for Drupal 7 and 8 sites.

Download the pre-commit file. Save it as .git/hook/pre-commit in your git repo. You need to ensure that the file is executable.

If you want this to be added to all new projects automatically, add it to your git init templates.

To install and PHP CodeSniffer for Drupal, please read the official documentation.

To see this working checking out this short YouTube video.

#!/bin/sh
#
# Git pre-commit hook that prevents really broken Drupal from code being committed.
#
# Created by Dave Hall http://davehall.com.au
#
# Fetch the list of files that have changed.
FILES="$(git diff --staged --name-only HEAD --diff-filter=ACMRTUXB)"
echo "Running PHP lint check" >&2
LINT_FAIL=0
for filename in $FILES; do
if ! php -l $filename; then
LINT_FAIL=1
fi
done;
# There is no point in running PHP Code Sniffer on broken files.
if [ $LINT_FAIL -gt 0 ]; then
exit 1
fi
echo "Running PHP Code Sniffer Checks" >&2
echo $FILES | xargs phpcs --standard=Drupal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment