Last active
December 17, 2015 15:18
-
-
Save geschke/5630249 to your computer and use it in GitHub Desktop.
git post-commit hook to simulate revision numbers in current branch and replace $Revision$ with current number in all *.sql files. Please rename to "pre-commit" and place it into .git/hooks/ directory.
This file contains hidden or 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/php | |
<?php | |
$output = array(); | |
$rootDirCurrent = getcwd(); | |
exec('git rev-parse --show-toplevel',$rootDir); | |
$rootDir = implode($rootDir); | |
chdir($rootDir); | |
exec('git rev-list HEAD | wc -l',$revision); | |
$newRevision = intval(implode($revision)) + 1; | |
exec('git diff-index --cached --name-only HEAD',$output); | |
$needle = '/(.sql)$/'; | |
$exit_status = 0; | |
foreach ($output as $file) { | |
if (!preg_match($needle, $file)) { | |
// only check .sql files | |
continue; | |
} | |
$inputFile = file_get_contents($file); | |
$outputFile = preg_replace("/\\\$Revision\\\$/", "\$Revision: " . $newRevision . "\\\$", $inputFile); | |
file_put_contents($file, $outputFile); | |
exec('git add ' . $file); | |
} | |
chdir($rootDirCurrent); | |
exit($exit_status); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment