Created
February 27, 2018 03:18
-
-
Save JonyFang/dfa14ccebeb1aeddf160066705412095 to your computer and use it in GitHub Desktop.
使用 commit-msg 脚本文件对于对应格式进行校验,来保证 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/env python3 | |
import sys, os, re | |
from subprocess import check_output | |
class bcolors: | |
SUCCESS = '\033[92m' | |
WARNING = '\033[93m' | |
FAILURE = '\033[91m' | |
ENDC = '\033[0m' | |
def check_commit_message_content(content): | |
lines = content.split("\n") | |
for index, line in enumerate(lines): | |
trim_line = line.strip() | |
# 过滤空行、注释行以及 title | |
if trim_line == "" or trim_line[0] == '#' or index == 0: | |
continue | |
true_text = " - 正确 √" | |
false_text = " - 错误 ✘" | |
# 检查提交项 | |
regax = r'\[[a-zA-Z]+\].+' | |
pattern = re.compile(regax) | |
if pattern.match(line): | |
log = line + true_text | |
print(bcolors.SUCCESS + log + bcolors.ENDC) | |
continue | |
# 检查签名 | |
regax = r'(Signed-off-by).+' | |
pattern = re.compile(regax) | |
if pattern.match(line): | |
log = line + true_text | |
print(bcolors.SUCCESS + log + bcolors.ENDC) | |
continue | |
# 标红处理 | |
log = line + false_text | |
print(bcolors.FAILURE + log + bcolors.ENDC) | |
print('\n') | |
sys.exit(1) | |
commit_msg_filepath = sys.argv[1] | |
branch = check_output(['git', 'symbolic-ref', '--short', 'HEAD']).strip() | |
print('\n') | |
with open(commit_msg_filepath, 'r') as f: | |
content = f.read() | |
check_commit_message_content(content) | |
print("\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment