Created
November 11, 2019 09:36
-
-
Save floydawong/db05a7962f38b89d39353aa3a5ffe38e to your computer and use it in GitHub Desktop.
post-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 python | |
# _*_ coding:utf-8 _*_ | |
# author: Floyda | |
import os, re | |
import subprocess | |
import sys | |
reload(sys) | |
sys.setdefaultencoding("utf8") | |
CHANGELOG_PATH = "CHANGELOG.md" | |
if not os.path.exists(CHANGELOG_PATH): | |
with open(CHANGELOG_PATH, "w") as fp: | |
fp.write("") | |
# process = subprocess.Popen( | |
# ["git", "update-index", "--assume-unchanged", "CHANGELOG.md"] | |
# ) | |
def add_tag(tag): | |
with open(CHANGELOG_PATH, "r+") as fp: | |
content = fp.read() | |
fp.seek(0, 0) | |
fp.write("\n# " + tag + "\n" + content) | |
def add_changelog(log): | |
with open(CHANGELOG_PATH, "r+") as fp: | |
content = fp.read() | |
fp.seek(0, 0) | |
fp.write(" - " + log + "\n" + content) | |
def check_changelog(content): | |
result = re.match(r"(^\s*\[.*\].*)", content) | |
if result: | |
return result.group(0).strip() | |
msg_path = ".git/COMMIT_EDITMSG" | |
with open(msg_path, "r") as fp: | |
content = fp.readlines() | |
for c in content: | |
log = check_changelog(c) | |
if log: | |
add_changelog(log) | |
branch = subprocess.check_output(["git", "symbolic-ref", "--short", "HEAD"]).strip() | |
print("-------------------------- branch", branch) | |
if branch is "master": | |
pass | |
# git diff HEAD^ HEAD -U0 | |
diff_content = subprocess.check_output(["git", "diff", "HEAD^", "HEAD", "-U0"]).decode( | |
"utf-8" | |
) | |
rg = re.compile(r"\+AOBI_VERSION\s?\=\s?\'(.*)\'\n") | |
m = rg.search(diff_content) | |
if m: | |
tag = m.group(1) | |
print("-------------------------- tag", tag) | |
add_tag(tag) | |
process = subprocess.Popen(["git", "tag", "-f", tag]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment