Skip to content

Instantly share code, notes, and snippets.

@chrisjaure
Created October 21, 2011 20:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chrisjaure/1304921 to your computer and use it in GitHub Desktop.
Save chrisjaure/1304921 to your computer and use it in GitHub Desktop.
git pre-commit hook to block commits on matches of regular expressions
#!/usr/bin/env php
<?php
$unallowed = array(
);
exec('git diff --cached --', $output);
$file;
foreach($output as $line)
{
// match this type of thing: +++ b/filename
if (preg_match('/^\+\+\+ b\/(.+)$/', $line, $match))
{
$file = $match[1];
continue;
}
// match diff --git a/filename b/filename so we know we're done with prev file
if (preg_match('/^diff --git/', $line))
{
$file = null;
continue;
}
// match added lines
if ($file && !empty($line) && $line[0] == '+')
{
// search line for all unallowed content, exit if found
foreach($unallowed as $search)
{
if (preg_match($search, $line))
{
echo "Aborting commit! Found '$search' in '$file'\n";
exit(1);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment