Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@marwahaha
Last active March 2, 2018 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marwahaha/deb752493230f4785d32562d7aed8493 to your computer and use it in GitHub Desktop.
Save marwahaha/deb752493230f4785d32562d7aed8493 to your computer and use it in GitHub Desktop.
make changelog gist for moment
# coding: utf-8
import sys
if len(sys.argv) < 3:
print("Must specify before and after tags. Example: 'python moment-gist.py 2.19.2 2.19.3'")
sys.exit(1)
BEFORE = sys.argv[1]
AFTER = sys.argv[2]
import re
BEFORE_MATCH = re.compile(".*"+BEFORE+"$")
AFTER_MATCH = re.compile(".*"+AFTER+"$")
import subprocess
git_logs = subprocess.check_output(["git", "log", "--oneline"]).decode('utf-8').split('\n')
BEFORE_NUM = len(git_logs) - 1
AFTER_NUM = -1
for idx,log in enumerate(git_logs):
if AFTER_MATCH.match(log):
AFTER_NUM = max(AFTER_NUM, idx)
elif BEFORE_MATCH.match(log):
BEFORE_NUM = min(BEFORE_NUM, idx)
OTHER = "__OTHER__"
## converts header -> [changelog header, ordering, commits in this bucket]
CONVERT_CHART = {
"[bugfix]": ["Bugfixes", 1, []],
"[feature]": ["Features", 2, []],
"[critical]": ["Critical", 0, []],
"[new locale]": ["New locales", 3, []],
"[locale]": ["Locale improvements", 4, []],
"[misc]": ["Misc", 5, []],
"[tests]": ["Tests", 7, []],
"[pkg]": ["Packaging", 6, []],
OTHER: ["Other", 8, []]
}
def linkify(entry):
return re.sub(r'(#)(\d+)', r'[\1\2](https://github.com/moment/moment/pull/\2)', entry)
def make_entry(split_log):
return linkify("* "
+ split_log[-1][1:-1]
+ " "
+ " ".join(split_log[1:-1]))
for log in git_logs[AFTER_NUM+1:BEFORE_NUM]:
split_log = log.split(" ")
one_word_prefix = split_log[1]
two_word_prefix = " ".join(split_log[1:3])
if one_word_prefix in CONVERT_CHART:
CONVERT_CHART[one_word_prefix][2].append(make_entry(split_log))
elif two_word_prefix in CONVERT_CHART:
CONVERT_CHART[two_word_prefix][2].append(make_entry(split_log))
else:
CONVERT_CHART[OTHER][2].append(make_entry(split_log))
sorted_keys = sorted(CONVERT_CHART, key=lambda x: CONVERT_CHART[x][1])
used_keys = list(filter(lambda key: 0 != len(CONVERT_CHART[key][2]), sorted_keys))
print("# moment %s changelog\n" % AFTER)
for key in used_keys:
print("## %s\n" % CONVERT_CHART[key][0])
print("\n".join(CONVERT_CHART[key][2]) + "\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment