Last active
August 29, 2015 14:06
-
-
Save paolomainardi/034808d5722a9814f62a to your computer and use it in GitHub Desktop.
dfq-pre-commit
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/php | |
<?php | |
/** | |
* @file | |
* place this file in your $GIT_DIR/hooks directory and call it pre-commit | |
* anyother scripts you want to execture on commit can then also be placed | |
* in $GIT_DIR/hooks with the correct naming convention.. ie: | |
* pre-commit_[whatevernameyouwant] make that file executable and this script | |
* finds it and runs it. | |
*/ | |
/** | |
* Get an array containing paths of all files in the specified tree. | |
*/ | |
function find_all_files($dir = '.') { | |
$root = scandir($dir); | |
// Ignore . and .. dirs. | |
foreach ($root as $value) { | |
if ($value === '.' || $value === '..') { | |
continue; | |
} | |
// Found a file? Store it. | |
if (is_file("$dir/$value")) { | |
$result[] = "$dir/$value"; | |
continue; | |
} | |
// Traverse the sub directories. | |
foreach (find_all_files("$dir/$value") as $value) { | |
$result[] = $value; | |
} | |
} | |
return $result; | |
} | |
// What is the .git directory for this project? | |
$git_dir = trim(`echo $(git rev-parse --git-dir 2>/dev/null)`); | |
if (empty($git_dir)) { | |
echo 'fatal: hooks/functions: GIT_DIR not set"'; | |
exit(1); | |
} | |
// Get a list of all the files in the git hooks directory. | |
$files = find_all_files($git_dir . '/hooks'); | |
// Search for correctly named files and execute them. | |
foreach ($files as $file) { | |
if (substr(basename($file), 0, 11) == 'pre-commit_') { | |
if (is_executable($file)) { | |
system($file, $ret); | |
if ($ret != 0) { | |
exit($ret); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment