Created
October 21, 2011 20:50
-
-
Save chrisjaure/1304921 to your computer and use it in GitHub Desktop.
git pre-commit hook to block commits on matches of regular expressions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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