Skip to content

Instantly share code, notes, and snippets.

Last active May 27, 2020
What would you like to do?
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.

# Git pre-commit hook that prevents really broken Drupal from code being committed.
# Created by Dave Hall
# Fetch the list of files that have changed.
FILES="$(git diff --staged --name-only HEAD --diff-filter=ACMRUXB)"
echo "Running PHP lint check" >&2
for filename in $FILES; do
if ! php -l $filename; then
# There is no point in running PHP Code Sniffer on broken files.
if [ $LINT_FAIL -gt 0 ]; then
exit 1
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