Skip to content

Instantly share code, notes, and snippets.

@liuxd
Last active September 7, 2017 01:20
Show Gist options
  • Save liuxd/5c727305da2097fa55ee660ece989730 to your computer and use it in GitHub Desktop.
Save liuxd/5c727305da2097fa55ee660ece989730 to your computer and use it in GitHub Desktop.
[commit_without_chinese] #GitHook
<?php
/**
* git commit hook
* Check whether commit message include Chinese. If there is, stop the commit.
*/
function cecho($string, $style = 'notice') {
if (isset($_SERVER['REQUEST_URI'])){
echo $string, "\n";
return;
}
$colors = array(
'info' => '1',
'notice' => '32',
'error' => '31',
'system' => '34',
);
$string = addslashes($string);
$cmd = "echo \"\033[{$colors[$style]}m$string\033[0m\n\"";
$out = array();
exec($cmd, $out);
if (isset($out[0])){
echo $out[0], "\n";
}
}
$opts = ['commit-file:'];
$options = getopt('', $opts);
$cf = $options['commit-file'];
$commit_content = file($cf);
$msgs = array_filter($commit_content, function($v){
$v = trim($v);
if (substr($v, 0, 1) !== '#') {
return $v;
}
});
if (empty($msgs)) {
exit(1);
}
foreach ($msgs as $msg) {
$match = preg_match('/[\x{4e00}-\x{9fa5}]/u', $msg);
if ($match > 0) {
cecho('Chinese commit messages are forbidden!', 'error');
exit(1);
}
}
exit(0);
# end of this file
#!/bin/sh
cur=`pwd`
php $cur/.git/hooks/commit-check.php --commit-file=$1
ret_code=$?
if [ $ret_code -gt 0 ];then
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment