-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate.py
executable file
·147 lines (111 loc) · 3.44 KB
/
generate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python3
import sys
import os
import argparse
import re
from dataclasses import dataclass
from subprocess import check_output
RE_MERGE = re.compile("Merge branch '(.+)' into .+$")
RE_PR = re.compile("Merge pull request .+ from (.+)$")
@dataclass
class Merge:
cid: str
date: str
branch: str
@property
def filename(self):
branch = self.branch.replace("/", "-")
return f"pr-texts/{self.date}-{branch}.md"
def extract_branch(msg):
if (m := RE_MERGE.match(msg)):
return m.group(1)
elif (m := RE_PR.match(msg)):
return m.group(1)
else:
raise ValueError(f"Could not parse msg: {msg}")
class Git:
def __init__(self, git_dir):
self.git_dir = git_dir
def run(self, args):
return check_output([
"git",
"-C", self.git_dir,
*args,
], encoding="utf8")
def log_to_pr_names(self, commits):
output = self.run([
"log",
"--merges",
"--date-order",
"--format=format:%H|%ci|%s",
commits
])
res = []
for line in output.splitlines():
cid, datetime, msg = line.split("|", 2)
date = datetime.split()[0]
branch = extract_branch(msg)
branch = branch.replace("chrivers/chrivers/", "chrivers/")
res.append(Merge(cid, date, branch))
return res
def log_msg(self, commit):
return self.run([
"show",
"--no-patch",
"--format=format:%b",
commit
]).strip()
def load_pr_texts(names):
res = []
for merge in names:
try:
with open(merge.filename) as f:
data = f.read().strip()
if data:
res.append(data)
except:
print(f"ERROR: could not open {merge.filename} for commit {merge.cid}", file=sys.stderr)
raise
return res
TEMPLATE = """### {merge.date}: `{merge.branch}`
{body}
"""
def fill_missing_texts(git, names):
for merge in names:
if not os.path.exists(merge.filename):
text = TEMPLATE.format(merge=merge, body=git.log_msg(merge.cid))
with open(merge.filename, "w") as f:
f.write(text)
print(f"Generated {merge.filename}", file=sys.stderr)
def changes(texts, limit=None):
delim = "\n\n****************************************\n\n"
if limit:
return delim.join(texts[:limit])
else:
return delim.join(texts)
def main(args):
from jinja2 import Environment, FileSystemLoader, select_autoescape
env = Environment(
loader=FileSystemLoader("templates/"),
autoescape=select_autoescape(),
)
git = Git(args.git_dir)
names = git.log_to_pr_names(args.commits)
fill_missing_texts(git, names)
try:
pr_texts = load_pr_texts(names)
except:
return 1
template = env.get_template(args.template.removeprefix("templates/"))
context = {
"changes": lambda *args: changes(pr_texts, *args),
"has_changes": len(pr_texts) > 0,
}
print(template.render(**context), end="")
return 0
if __name__ == "__main__":
args = argparse.ArgumentParser()
args.add_argument("git_dir", metavar="<git-dir>")
args.add_argument("template", metavar="<template.jinja>")
args.add_argument("commits", metavar="<git-commit-range>")
sys.exit(main(args.parse_args()))