Skip to content

Instantly share code, notes, and snippets.

@geschke
Last active December 17, 2015 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geschke/5630249 to your computer and use it in GitHub Desktop.
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.
#!/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