Skip to content

Instantly share code, notes, and snippets.

@asuth
Created February 1, 2011 21:45
Show Gist options
  • Save asuth/806757 to your computer and use it in GitHub Desktop.
Save asuth/806757 to your computer and use it in GitHub Desktop.
We use this to check our puppet scripts as a git pre-commit hook.
#!/opt/local/bin/php
<?php
// Git pre-commit file to check Puppet for errors
$files = array();
exec('git diff-index --cached --name-only HEAD', $files);
$exitcode = 0;
foreach($files as $file) {
$output = '';
if (!preg_match('/\.pp$/i', $file))
continue;
// file might be being removed
if (!file_exists($file))
continue;
exec('puppet apply --parseonly '.escapeshellarg($file), $output, $return);
if (!$return) // gives a 0 error code if everything went well
continue;
$exitcode = 1; // abort the commit
echo "\n-------------- PUPPET PARSE ERRORS! ----------------\n";
echo implode("\n", $output)."\n"; // an extra newline to make it look good
echo "-------------- FIX BEFORE COMMIT ----------------\n\n";
}
exit($exitcode);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment