Debian Codegraph @

script/gitlog2changelog.py.in

raw
1 #!@PYTHON@
2 # Copyright 2008 Marcus D. Hanwell <[email protected]>
3 # Minor changes for NUT by Charles Lepple
4 # Subsequent maintenance for NUT by Jim Klimov (since 2021)
5 # Distributed under the terms of the GNU General Public License v2 or later
6
7 import re
8 import os
9 from textwrap import TextWrapper
10 import sys
11 import subprocess
12
13 # Python 3 compatibility hack
14 try:
15 try:
16 import unicode
17 except:
18 # Maybe built-in?
19 pass
20 unicode('')
21 except NameError as ex:
22 #DEBUG# sys.stderr.write("Using 'str' as 'unicode': %s\n" % str(ex))
23 #DEBUG# sys.stderr.flush()
24 unicode = str
25
26 try:
27 import unicodedata
28 except:
29 pass
30
31 rev_range = "HEAD"
32
33 if len(sys.argv) > 1:
34 base = sys.argv[1]
35 rev_range = "%s..HEAD" % base
36
37 # Execute git log with the desired command line options.
38 # Support Python2 and Python3 (esp. 3.6 and earlier) semantics
39 # with regard to utf-8 content support (avois ascii decoding in Py3)
40 fin_mode = 0
41 # Remove trailing end of line? spitlines() in py3 variant takes care of them
42 fin_chop = 0
43 try:
44 p = subprocess.Popen(
45 [
46 "git",
47 "log",
48 "--pretty=medium",
49 "--summary",
50 "--stat",
51 "--no-merges",
52 "--date=short",
53 ("%s" % rev_range),
54 ],
55 encoding="utf-8",
56 close_fds=True,
57 stdout=subprocess.PIPE,
58 stderr=subprocess.PIPE,
59 )
60 fin, ferr = p.communicate()
61 if p.wait() != 0:
62 print("ERROR getting git changelog")
63 sys.exit(1)
64 fin = fin.splitlines()
65 fin_mode = 3
66 except TypeError:
67 fin = os.popen(
68 "git log --pretty=medium --summary --stat --no-merges --date=short %s"
69 % rev_range,
70 "r",
71 )
72 fin_mode = 2
73 fin_chop = 1
74
75 # Create a ChangeLog file in the current directory by default.
76 CHANGELOG_FILE = "ChangeLog"
77 try:
78 # e.g. point from Makefile to a builddir (caller ensures it exists)
79 if os.environ.get("CHANGELOG_FILE", None) is not None:
80 CHANGELOG_FILE = os.environ.get("CHANGELOG_FILE")
81 except Exception as ignored:
82 pass
83
84 if CHANGELOG_FILE == "-":
85 fout = sys.stdout
86 else:
87 if fin_mode == 3:
88 fout = open(CHANGELOG_FILE, "w", encoding="UTF-8")
89 else:
90 fout = open(CHANGELOG_FILE, "w")
91
92 # By default we collect information from a commit and output it as soon as
93 # we have enough. Part of it is best-effort grouping of a series of commits
94 # made by the same author on the same day, if they follow each other.
95 # The alternative is to expend memory to collect all git log entries into a
96 # dictionary first (key = date+author, value = list of entries) and only
97 # print the output in the end of processing. This costs more resources, so
98 # is not default behavior.
99 requireGroupByDateAuthor = False
100 try:
101 tmpEnvVar = os.environ.get("CHANGELOG_REQUIRE_GROUP_BY_DATE_AUTHOR", None)
102 if str(tmpEnvVar).lower() == "true":
103 requireGroupByDateAuthor = True
104 except Exception as ignored:
105 pass
106
107 cachedContent = None
108 if requireGroupByDateAuthor:
109 try:
110 from collections import defaultdict
111 cachedContent = defaultdict(list)
112 except Exception as x:
113 print("Failed to init requireGroupByDateAuthor processing as defaultdict(list), trying simple dict(): " + str(x))
114 requireGroupByDateAuthor = False
115 cachedContent = dict()
116
117 # Set up the loop variables in order to locate the blocks we want
118 authorFound = False
119 dateFound = False
120 messageFound = False
121 filesFound = False
122 message = ""
123 messageNL = False
124 files = ""
125 prevAuthorLine = ""
126
127 # Legacy default: keep as is
128 authorMustBeASCII = False
129 authorMustBeASCII_inverse_setting = str(os.environ.get("WITH_PDF_NONASCII_TITLES", "")).upper()
130 if authorMustBeASCII_inverse_setting in ["YES", "TRUE"]:
131 authorMustBeASCII = False
132 elif authorMustBeASCII_inverse_setting in ["NO", "FALSE"]:
133 authorMustBeASCII = True
134
135 # See also: https://github.com/python/cpython/blob/main/Lib/textwrap.py
136 wrapper = TextWrapper(initial_indent=" ", subsequent_indent=" ", width=78, break_on_hyphens=False)
137
138 # The main part of the loop
139 for line in fin:
140 # The commit line marks the start of a new commit object.
141 if line.startswith("commit"):
142 # Start all over again...
143 authorFound = False
144 dateFound = False
145 messageFound = False
146 messageNL = False
147 message = ""
148 filesFound = False
149 files = ""
150 continue
151 # Match the author line and extract the part we want
152 # (Don't use startswith to allow Author override inside commit message.)
153 elif "Author:" in line:
154 if sys.version_info >= (3, 13, ):
155 authorList = re.split(": ", line, maxsplit=1)
156 else:
157 authorList = re.split(": ", line, 1)
158
159 try:
160 author = authorList[1]
161 author = author[0 : len(author) - fin_chop]
162 if authorMustBeASCII:
163 try:
164 if isinstance(author, str) and unicode != str:
165 author = unicodedata.normalize(u'NFKD', unicode(author, "utf-8")).encode('ascii', 'ignore').decode('utf8')
166 else:
167 author = unicodedata.normalize(u'NFKD', author).encode('ascii', 'ignore').decode('utf8')
168 except Exception as e:
169 print("Could not unicodedata.normalize() author '%s': %s" % (author, str(e)))
170 authorFound = True
171 except:
172 print("Could not parse authorList = '%s'" % (line))
173
174 # Match the date line
175 elif line.startswith("Date:"):
176 if sys.version_info >= (3, 13, ):
177 dateList = re.split(": ", line, maxsplit=1)
178 else:
179 dateList = re.split(": ", line, 1)
180
181 try:
182 date = dateList[1]
183 date = date[0 : len(date) - fin_chop]
184 dateFound = True
185 except:
186 print("Could not parse dateList = '%s'" % (line))
187 # The Fossil-IDs are ignored:
188 elif line.startswith(" Fossil-ID:") or line.startswith(" [[SVN:"):
189 continue
190 # The svn-id lines are ignored
191 elif " git-svn-id:" in line:
192 continue
193 # The sign off line is ignored too
194 elif "Signed-off-by" in line:
195 continue
196 # Extract the actual commit message for this commit
197 elif authorFound and dateFound and messageFound is False:
198 # Find the commit message if we can (including the optional
199 # details after the title and a blank line)
200 # FIXME: Detect end of message by /^#/ to allow for longer essays
201 # in the detailed comments part?
202 # FIXME: Some such comments include asciidoc-ish markup, notably
203 # bullet lists - do not concatenate those into one block but do
204 # actually pass them as sub-lists (indented, and perhaps not
205 # starting with an asterisk which we use for this document).
206 if len(line) == fin_chop:
207 if messageNL:
208 messageFound = True
209 else:
210 messageNL = True
211 elif len(line) == 3 + fin_chop:
212 messageFound = True
213 else:
214 if len(message) == 0:
215 message = message + line.strip()
216 else:
217 message = message + " " + line.strip()
218 # If this line is hit all of the files have been stored for this commit
219 elif re.search("files? changed", line):
220 filesFound = True
221 continue
222 # Collect the files for this commit. FIXME: Still need to add +/- to files
223 elif authorFound and dateFound and messageFound:
224 if sys.version_info >= (3, 13, ):
225 fileList = re.split(r' \| ', line, maxsplit=2)
226 else:
227 fileList = re.split(r' \| ', line, 2)
228
229 if len(fileList) > 1:
230 if len(files) > 0:
231 files = files + ", " + fileList[0].strip()
232 else:
233 files = fileList[0].strip()
234
235 # All of the parts of the commit have been found - write out the entry
236 if authorFound and dateFound and messageFound and filesFound:
237 # First the author line, only outputted if it is the first for that
238 # author on this day.
239 # WARNING: In case of git rebase commit shuffling, merges of dormant
240 # branches, etc. we are not guaranteed to have all dates in the list
241 # nicely ordered. In fact, the same date+author can be repeated if
242 # there were commits with other metadata in git history between those
243 # (e.g. many PRs from a few authors merged during one day). While we
244 # could cache each section by authorLine and only output in the end,
245 # it can require a lot of memory - so by default we do not.
246 authorLine = date + " " + author
247 if requireGroupByDateAuthor:
248 if authorLine not in cachedContent:
249 cachedContent[authorLine] = list()
250 else:
251 if len(prevAuthorLine) == 0:
252 fout.write(authorLine + "\n\n")
253 elif authorLine == prevAuthorLine:
254 pass
255 else:
256 fout.write("\n" + authorLine + "\n\n")
257
258 # Assemble the actual commit message line(s) and limit the line length
259 # to 80 characters.
260 # Avoid printing same (or equivalent) filename lists twice, if commit
261 # message starts with them.
262 if message.startswith(files + ":"):
263 commitLine = "* " + message
264 else:
265 namesF = None
266 namesM = None
267 try:
268 namesM = sorted(re.split(r"[ ,]", message.split(":")[0]))
269 namesF = sorted(re.split(r"[ ,]", files))
270 except:
271 pass
272
273 if namesM is not None and namesM == namesF:
274 commitLine = "* " + message
275 else:
276 commitLine = "* " + files + ": " + message
277
278 if requireGroupByDateAuthor:
279 cachedContent[authorLine].append(commitLine)
280 else:
281 # Write out the commit line, wrapped for length
282 fout.write(wrapper.fill(commitLine) + "\n")
283
284 # Now reset all the variables ready for a new commit block.
285 authorFound = False
286 dateFound = False
287 messageFound = False
288 messageNL = False
289 message = ""
290 filesFound = False
291 files = ""
292 prevAuthorLine = authorLine
293
294 if requireGroupByDateAuthor:
295 # We did not print anything before, flush it out now;
296 # most recent date first (alphanumerically reverse)
297 counter = 0
298 for authorLine in sorted(cachedContent, reverse=True):
299 if counter == 0:
300 fout.write(authorLine + "\n\n")
301 else:
302 fout.write("\n" + authorLine + "\n\n")
303
304 # Use original list append order
305 for commitLine in cachedContent[authorLine]:
306 fout.write(wrapper.fill(commitLine) + "\n")
307
308 counter = counter + 1
309
310 # Close the input and output lines now that we are finished.
311 if fin_mode == 3:
312 p.stdout.close()
313 p.stderr.close()
314 else:
315 fin.close()
316 fout.close()