Skip to content

Instantly share code, notes, and snippets.

@polonskiy
Last active March 20, 2020 07:39
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save polonskiy/8037997 to your computer and use it in GitHub Desktop.
Save polonskiy/8037997 to your computer and use it in GitHub Desktop.
PHPLint pre-commit git hook
#!/usr/bin/php
<?php
exec('git diff --name-only --diff-filter=ACM --cached', $files);
$status = 0;
foreach ($files as $file) {
if (pathinfo($file, PATHINFO_EXTENSION) != 'php') continue;
$file = escapeshellarg($file);
$message = array();
exec("git show :$file | php -l 2>&1", $message, $code);
if (!$code) continue;
fwrite(STDERR, str_replace('in - on', "in $file on", $message[0])."\n");
$status = 1;
}
die($status);
#!/bin/bash
staged="$(git diff --name-only --diff-filter=ACM --cached)"
phpfiles="$(grep '\.php$' <<< "$staged")"
for file in $phpfiles; do
php -l $file > /dev/null || exit 1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment