Last active
December 16, 2015 06:35
-
-
Save tjtjtj/56e331f3791a90eddca1 to your computer and use it in GitHub Desktop.
git プレコミットフック
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
#!/bin/sh | |
# .git/hooks/pre-commit | |
php ./.git/hooks/rejection_pattern_check.php | |
if test $? -ne 0 | |
then | |
echo "COMMIT REJECTED. Please remove them before commiting OR use --no-verify" | |
exit 1 | |
fi |
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
<?php | |
// 拒絶パターン | |
$rejection_pattern = "/@@@/"; | |
// 検査対象のファイル拡張子 | |
$filename_pattern = '/\.cs$|\.java$|\.html$|\.sql$|\.php$|\.txt$|\.md$/'; | |
// 終了コード | |
$exit_status = 0; | |
// ステージング中のファイルを取得 | |
$output = array(); | |
$return = 0; | |
exec('git diff --cached --name-only', $output, $return); | |
foreach ($output as $file) { | |
// ファイル拡張子検査 | |
if (!preg_match($filename_pattern, $file)) { | |
continue; | |
} | |
// ファイル内容検査 | |
$rp = realpath(__DIR__.'/../../'.$file); | |
$filecontents = file_get_contents($rp); | |
$return = preg_match($rejection_pattern, $filecontents); | |
if ($return == 0) { | |
continue; | |
} | |
echo "- {$file} has {$rejection_pattern}\n"; | |
$exit_status = 1; | |
} | |
exit($exit_status); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment