Last active
January 8, 2018 02:16
-
-
Save Desgard/354c28251dae78d1b886cbe339d88cd1 to your computer and use it in GitHub Desktop.
Sepicat git logs commit-msg [Githook]
This file contains hidden or 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 | |
| # 在项目根目录下执行即可: | |
| # wget -O ./.git/hooks/commit-msg https://gist.githubusercontent.com/Desgard/354c28251dae78d1b886cbe339d88cd1/raw/6d0bedce72371090d3b07e6f8c975b2f787a437f/commit-msg.py | |
| # 权限修改:sudo chmod -x .git/hooks/commit-msg | |
| 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