Skip to content

Instantly share code, notes, and snippets.

@voxpelli
Created August 10, 2011 19:32
Show Gist options
  • Save voxpelli/1137906 to your computer and use it in GitHub Desktop.
Save voxpelli/1137906 to your computer and use it in GitHub Desktop.
PHPMD Git pre-commit hook with parallell checks thanks to Node.js
#!/usr/bin/env node
var child_process = require('child_process'),
spawn = child_process.spawn,
exec = child_process.exec,
quitOnError = null, rules;
function checkFiles() {
if (quitOnError === null || !rules) {
return;
}
var gitPhpmd = spawn('git', ['phpmd', rules])
gitPhpmd.stdout.on('data', function (data) {
process.stdout.write(data);
});
gitPhpmd.on('exit', function (code) {
if (quitOnError) {
setTimeout(function () {
process.exit(code);
}, 10);
}
});
}
exec('git config hooks.phpmdstoponerror', function (error, stdout) {
if (error === null) {
quitOnError = (stdout.trim() != '0');
checkFiles();
}
else {
quitOnError = false;
}
});
exec('git config hooks.phpmd', function (error, stdout) {
if (error === null) {
rules = stdout.trim();
checkFiles();
}
});
#!/bin/bash
# Uses multiphpmd from https://github.com/voxpelli/node-multiphpmd
git diff --name-only --staged | multiphpmd $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment