Skip to content

Instantly share code, notes, and snippets.

@zhangpengGenedock
Created December 8, 2017 03:15
Show Gist options
  • Save zhangpengGenedock/14ad33edd084c76879e2119bfb00143e to your computer and use it in GitHub Desktop.
Save zhangpengGenedock/14ad33edd084c76879e2119bfb00143e to your computer and use it in GitHub Desktop.
在提交信息之前自动加上branch的名字, 待修改
#!/usr/bin/env python
import sys, os, re
from subprocess import check_output
# 收集参数
commit_msg_filepath = sys.argv[1]
if len(sys.argv) > 2:
commit_type = sys.argv[2]
else:
commit_type = ''
if len(sys.argv) > 3:
commit_hash = sys.argv[3]
else:
commit_hash = ''
print "prepare-commit-msg: File: %s\nType: %s\nHash: %s" % (commit_msg_filepath, commit_type, commit_hash)
# 检测我们所在的分支
branch = check_output(['git', 'symbolic-ref', '--short', 'HEAD']).strip()
print "prepare-commit-msg: On branch '%s'" % branch
# 用issue编号生成提交信息
if branch.startswith('issue-'):
print "prepare-commit-msg: Oh hey, it's an issue branch."
result = re.match('issue-(.*)', branch)
issue_number = result.group(1)
with open(commit_msg_filepath, 'r+') as f:
content = f.read()
f.seek(0, 0)
f.write("ISSUE-%s %s" % (issue_number, content))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment