Skip to content

Instantly share code, notes, and snippets.

@keopx
Forked from skwashd/README.md
Created November 13, 2016 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keopx/9bfdc753317d60a88cd8b6a0a7ea5dd0 to your computer and use it in GitHub Desktop.
Save keopx/9bfdc753317d60a88cd8b6a0a7ea5dd0 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=ACMRUXB)"
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