Skip to content

Instantly share code, notes, and snippets.

@tjtjtj
Last active December 16, 2015 06:35
Show Gist options
  • Save tjtjtj/56e331f3791a90eddca1 to your computer and use it in GitHub Desktop.
Save tjtjtj/56e331f3791a90eddca1 to your computer and use it in GitHub Desktop.
git プレコミットフック
#!/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
<?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