Debian Codegraph @

scripts/gen-changelog.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 #
3 # Makes a GNU-Style ChangeLog from a git repository
4 import os
5 import sys
6 import subprocess
7 import re
8
9 meson_source_root = os.environ.get('MESON_SOURCE_ROOT')
10
11 meson_dist_root = os.environ.get('MESON_DIST_ROOT')
12 if meson_dist_root:
13 output_fn = os.path.join(meson_dist_root, 'ChangeLog')
14 else:
15 output_fn = sys.stdout.fileno()
16
17 # commit hash => release version tag string
18 release_refs = {}
19
20 # These are the pre-monorepo module beginnings
21 changelog_starts = {
22 'gstreamer': '70521179a75db0c7230cc47c6d7f9d63cf73d351',
23 'gst-plugins-base': '68746a38d5e48e6f7c220663dcc2f175ff55cb3c',
24 'gst-plugins-good': '81f63142d65b62b0971c19ceb79956c49ffc2f06',
25 'gst-plugins-ugly': '7d7c3e478e32b7b66c44cc4442d571fbab534740',
26 'gst-plugins-bad': 'ea6821e2934fe8d356ea89d5610f0630b3446877',
27 'gst-libav': '3c440154c60d1ec0a54186f0fad4aebfd2ecc3ea',
28 'gst-rtsp-server': '5029c85a46a8c366c4bf272d503e22bbcd624ece',
29 'gst-editing-services': 'ee8bf88ebf131cf7c7161356540efc20bf411e14',
30 'gst-python': 'b3e564eff577e2f577d795051bbcca85d47c89dc',
31 'gst-devtools': 'da962d096af9460502843e41b7d25fdece7ff1c2',
32 'gstreamer-sharp': 'b94528f8e7979df49fedf137dfa228d8fe475e1b',
33 }
34
35
36 def print_help():
37 print('', file=sys.stderr)
38 print('gen-changelog: generate GNU-style changelog from git history',
39 file=sys.stderr)
40 print('', file=sys.stderr)
41 print('Usage: {} [OPTIONS] GSTREAMER-MODULE [START-TAG] [HEAD-TAG]'.format(
42 sys.argv[0]), file=sys.stderr)
43 print('', file=sys.stderr)
44 sys.exit(1)
45
46
47 if len(sys.argv) < 2 or len(sys.argv) > 4 or '--help' in sys.argv:
48 print_help()
49
50 module = sys.argv[1]
51
52 if len(sys.argv) > 2:
53 start_tag = sys.argv[2]
54 else:
55 start_tag = None
56
57 if len(sys.argv) > 3:
58 head_tag = sys.argv[3]
59 else:
60 head_tag = None
61
62 if module not in changelog_starts:
63 print(f'Unknown module {module}', file=sys.stderr)
64 print_help()
65
66
67 def process_commit(lines, files, subtree_path=None):
68 # DATE NAME
69 # BLANK LINE
70 # Subject
71 # BLANK LINE
72 # ...
73 # FILES
74 fileincommit = False
75 lines = [x.strip() for x in lines if x.strip()
76 and not x.startswith('git-svn-id')]
77 files = [x.strip() for x in files if x.strip()]
78 for line in lines:
79 if line.startswith('* ') and ':' in line:
80 fileincommit = True
81 break
82
83 top_line = lines[0]
84 print(top_line.strip())
85 print()
86 if not fileincommit:
87 for f in files:
88 if subtree_path and f.startswith(subtree_path):
89 # requires Python 3.9
90 print('\t* %s:' % f.removeprefix(subtree_path))
91 else:
92 print('\t* %s:' % f)
93 for line in lines[1:]:
94 print('\t ', line)
95 print()
96
97
98 def output_commits(module, start_tag, end_tag, subtree_path=None):
99 # retrieve commit date for start tag so we can filter the log for commits
100 # after that date. That way we don't include commits from merged-in
101 # plugin-move branches that go back to the beginning of time.
102 start_date = get_commit_date_for_ref(start_tag)
103
104 cmd = ['git', 'log',
105 '--pretty=format:--START-COMMIT--%H%n%ai %an <%ae>%n%n%s%n%b%n--END-COMMIT--',
106 '--date=short',
107 '--name-only',
108 f'--since={start_date}',
109 f'{start_tag}..{end_tag}',
110 ]
111
112 if subtree_path:
113 cmd += ['--', '.']
114
115 p = subprocess.Popen(args=cmd, shell=False,
116 stdout=subprocess.PIPE, cwd=meson_source_root)
117 buf = []
118 files = []
119 filemode = False
120 for lin in [x.decode('utf8', errors='replace') for x in p.stdout.readlines()]:
121 if lin.startswith("--START-COMMIT--"):
122 commit_hash = lin[16:].strip()
123 if buf != []:
124 process_commit(buf, files, subtree_path)
125
126 if commit_hash in release_refs:
127 version_str = release_refs[commit_hash]
128 print(f'=== release {version_str} ===\n')
129
130 buf = []
131 files = []
132 filemode = False
133 elif lin.startswith("--END-COMMIT--"):
134 filemode = True
135 elif filemode is True:
136 files.append(lin)
137 else:
138 buf.append(lin)
139 if buf != []:
140 process_commit(buf, files, subtree_path)
141
142
143 def get_commit_date_for_ref(ref):
144 cmd = ['git', 'log', '--pretty=format:%cI', '-1', ref]
145 r = subprocess.run(cmd, capture_output=True, text=True,
146 check=True, cwd=meson_source_root)
147 commit_date = r.stdout.strip()
148 return commit_date
149
150
151 def populate_release_tags_for_premonorepo_module(module_tag_prefix):
152 if module_tag_prefix != '':
153 cmd = ['git', 'tag', '--list', f'{module_tag_prefix}*']
154 else:
155 cmd = ['git', 'tag', '--list', '1.*', 'RELEASE-*']
156
157 p = subprocess.Popen(args=cmd, shell=False,
158 stdout=subprocess.PIPE, cwd=meson_source_root)
159 for line in [x.decode('utf8') for x in p.stdout.readlines()]:
160 git_tag = line.strip()
161 version_str = git_tag.removeprefix(module_tag_prefix).removeprefix('RELEASE-').split('-')[0].replace('_', '.')
162 # might have been populated with post-monorepo tags already for gstreamer core
163 if version_str not in release_refs:
164 # find last commit before tag in module subdirectory
165 cmd = ['git', 'log', '--pretty=format:%H', '-1', git_tag]
166 r = subprocess.run(cmd, capture_output=True,
167 text=True, check=True, cwd=meson_source_root)
168 commit_hash = r.stdout.strip()
169 release_refs[commit_hash] = version_str
170
171 # print(f'{git_tag} => {version_str} => {commit_hash}')
172
173
174 def populate_release_tags_for_monorepo_subproject():
175 cmd = ['git', 'tag', '--list', '1.*']
176 p = subprocess.Popen(args=cmd, shell=False,
177 stdout=subprocess.PIPE, cwd=meson_source_root)
178 for line in [x.decode('utf8') for x in p.stdout.readlines()]:
179 version_str = line.strip()
180 version_arr = version_str.split('.')
181 major = int(version_arr[0])
182 minor = int(version_arr[1])
183 micro = int(version_arr[2])
184 # ignore pre-monorepo versions
185 if major < 1:
186 continue
187 if major == 1 and minor < 19:
188 continue
189 if major == 1 and minor == 19 and micro < 2:
190 continue
191 # find last commit before tag in module subdirectory
192 cmd = ['git', 'log', '--pretty=format:%H',
193 '-1', version_str, '--', '.']
194 r = subprocess.run(cmd, capture_output=True, text=True,
195 check=True, cwd=meson_source_root)
196 commit_hash = r.stdout.strip()
197 release_refs[commit_hash] = version_str
198
199
200 if __name__ == '__main__':
201 module_tag_prefix = '' if module == 'gstreamer' else f'{module}-'
202
203 populate_release_tags_for_monorepo_subproject()
204
205 with open(output_fn, 'w') as f:
206 sys.stdout = f
207
208 # Force writing of head tag
209 if head_tag and head_tag not in release_refs.values():
210 print(f'=== release {head_tag} ===\n')
211
212 # Output all commits from start_tag onwards, otherwise output full history.
213 # (We assume the start_tag is after the monorepo merge if it's specified.)
214 if start_tag and start_tag != 'start':
215 output_commits(module, start_tag, 'HEAD', f'subprojects/{module}/')
216 else:
217 # First output all post-monorepo commits or commits from start_tag if specified
218 output_commits(module, 'monorepo-start',
219 'HEAD', f'subprojects/{module}/')
220
221 populate_release_tags_for_premonorepo_module(module_tag_prefix)
222
223 # Next output all pre-monorepo commits (modules have their own root)
224 if not start_tag:
225 module_start = f'{module_tag_prefix}1.0.0'
226 elif start_tag == 'start':
227 module_start = changelog_starts[module]
228 else:
229 module_start = f'{module_tag_prefix}{start_tag}'
230
231 output_commits(module, module_start,
232 f'{module_tag_prefix}1.19.2', None)
233
234 # Write start tag at end for clarity
235 if not start_tag:
236 print(f'=== release 1.0.0 ===\n')
237 elif start_tag != 'start':
238 print(f'=== release {start_tag} ===\n')