1 from __future__
import annotations
3 # Allow direct execution
7 sys
.path
.insert(0, os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
))))
14 from collections
import defaultdict
15 from dataclasses
import dataclass
16 from functools
import lru_cache
17 from pathlib
import Path
19 from devscripts
.utils
import read_file
, run_process
, write_file
21 BASE_URL
= 'https://github.com'
22 LOCATION_PATH
= Path(__file__
).parent
25 logger
= logging
.getLogger(__name__
)
28 class CommitGroup(enum
.Enum
):
30 PRIORITY
= 'Important'
32 EXTRACTOR
= 'Extractor'
33 DOWNLOADER
= 'Downloader'
34 POSTPROCESSOR
= 'Postprocessor'
39 def commit_lookup(cls
):
44 cls
.UPSTREAM
: {'upstream'}
,
67 cls
.EXTRACTOR
: {'extractor', 'extractors'}
,
68 cls
.DOWNLOADER
: {'downloader'}
,
69 cls
.POSTPROCESSOR
: {'postprocessor'}
,
76 result
= cls
.commit_lookup().get(value
)
78 logger
.debug(f
'Mapped {value!r} => {result.name}')
89 result
= f
'{self.short!r}'
92 result
+= f
' ({self.hash[:HASH_LENGTH]})'
95 authors
= ', '.join(self
.authors
)
96 result
+= f
' by {authors}'
104 sub_details
: tuple[str, ...]
111 return ((self
.details
or '').lower(), self
.sub_details
, self
.message
)
115 MISC_RE
= re
.compile(r
'(?:^|\b)(?:lint(?:ing)?|misc|format(?:ting)?|fixes)(?:\b|$)', re
.IGNORECASE
)
117 def __init__(self
, groups
, repo
):
118 self
._groups
= groups
122 return '\n'.join(self
._format
_groups
(self
._groups
)).replace('\t', ' ')
124 def _format_groups(self
, groups
):
125 for item
in CommitGroup
:
128 yield self
.format_module(item
.value
, group
)
130 def format_module(self
, name
, group
):
131 result
= f
'\n#### {name} changes\n' if name
else '\n'
132 return result
+ '\n'.join(self
._format
_group
(group
))
134 def _format_group(self
, group
):
135 sorted_group
= sorted(group
, key
=CommitInfo
.key
)
136 detail_groups
= itertools
.groupby(sorted_group
, lambda item
: (item
.details
or '').lower())
137 for _
, items
in detail_groups
:
139 details
= items
[0].details
146 if details
== 'cleanup':
147 items
, cleanup_misc_items
= self
._filter
_cleanup
_misc
_items
(items
)
149 sub_detail_groups
= itertools
.groupby(items
, lambda item
: tuple(map(str.lower
, item
.sub_details
)))
150 for sub_details
, entries
in sub_detail_groups
:
152 for entry
in entries
:
153 yield f
'{indent}- {self.format_single_change(entry)}'
156 entries
= list(entries
)
157 prefix
= f
'{indent}- {", ".join(entries[0].sub_details)}'
158 if len(entries
) == 1:
159 yield f
'{prefix}: {self.format_single_change(entries[0])}'
163 for entry
in entries
:
164 yield f
'{indent}\t- {self.format_single_change(entry)}'
166 if details
== 'cleanup' and cleanup_misc_items
:
167 yield from self
._format
_cleanup
_misc
_sub
_group
(cleanup_misc_items
)
169 def _filter_cleanup_misc_items(self
, items
):
170 cleanup_misc_items
= defaultdict(list)
173 if self
.MISC_RE
.search(item
.message
):
174 cleanup_misc_items
[tuple(item
.commit
.authors
)].append(item
)
176 non_misc_items
.append(item
)
178 return non_misc_items
, cleanup_misc_items
180 def _format_cleanup_misc_sub_group(self
, group
):
181 prefix
= '\t- Miscellaneous'
183 yield f
'{prefix}: {next(self._format_cleanup_misc_items(group))}'
187 for message
in self
._format
_cleanup
_misc
_items
(group
):
188 yield f
'\t\t- {message}'
190 def _format_cleanup_misc_items(self
, group
):
191 for authors
, infos
in group
.items():
193 self
._format
_message
_link
(None, info
.commit
.hash)
194 for info
in sorted(infos
, key
=lambda item
: item
.commit
.hash or ''))
195 yield f
'{message} by {self._format_authors(authors)}'
197 def format_single_change(self
, info
):
198 message
= self
._format
_message
_link
(info
.message
, info
.commit
.hash)
200 message
= f
'{message} ({self._format_issues(info.issues)})'
202 if info
.commit
.authors
:
203 message
= f
'{message} by {self._format_authors(info.commit.authors)}'
206 fix_message
= ', '.join(f
'{self._format_message_link(None, fix.hash)}' for fix
in info
.fixes
)
208 authors
= sorted({author for fix in info.fixes for author in fix.authors}
, key
=str.casefold
)
209 if authors
!= info
.commit
.authors
:
210 fix_message
= f
'{fix_message} by {self._format_authors(authors)}'
212 message
= f
'{message} (With fixes in {fix_message})'
216 def _format_message_link(self
, message
, hash):
217 assert message
or hash, 'Improperly defined commit message or override'
218 message
= message
if message
else hash[:HASH_LENGTH
]
219 return f
'[{message}]({self.repo_url}/commit/{hash})' if hash else message
221 def _format_issues(self
, issues
):
222 return ', '.join(f
'[#{issue}]({self.repo_url}/issues/{issue})' for issue
in issues
)
225 def _format_authors(authors
):
226 return ', '.join(f
'[{author}]({BASE_URL}/{author})' for author
in authors
)
230 return f
'{BASE_URL}/{self._repo}'
235 COMMIT_SEPARATOR
= '-----'
237 AUTHOR_INDICATOR_RE
= re
.compile(r
'Authored by:? ', re
.IGNORECASE
)
238 MESSAGE_RE
= re
.compile(r
'''
240 (?P<prefix>[^\]\/:,]+)
241 (?:/(?P<details>[^\]:,]+))?
242 (?:[:,](?P<sub_details>[^\]]+))?
244 (?:(?P<sub_details_alt>`?[^:`]+`?): )?
246 (?:\ \((?P<issues>\#\d+(?:,\ \#\d+)*)\))?
247 ''', re
.VERBOSE | re
.DOTALL
)
248 EXTRACTOR_INDICATOR_RE
= re
.compile(r
'(?:Fix|Add)\s+Extractors?', re
.IGNORECASE
)
249 FIXES_RE
= re
.compile(r
'(?i:Fix(?:es)?(?:\s+bugs?)?(?:\s+in|\s+for)?|Revert)\s+([\da-f]{40})')
250 UPSTREAM_MERGE_RE
= re
.compile(r
'Update to ytdl-commit-([\da-f]+)')
252 def __init__(self
, start
, end
, default_author
=None):
253 self
._start
, self
._end
= start
, end
254 self
._commits
, self
._fixes
= self
._get
_commits
_and
_fixes
(default_author
)
255 self
._commits
_added
= []
258 return iter(itertools
.chain(self
._commits
.values(), self
._commits
_added
))
261 return len(self
._commits
) + len(self
._commits
_added
)
263 def __contains__(self
, commit
):
264 if isinstance(commit
, Commit
):
269 return commit
in self
._commits
271 def _get_commits_and_fixes(self
, default_author
):
272 result
= run_process(
273 self
.COMMAND
, 'log', f
'--format=%H%n%s%n%b%n{self.COMMIT_SEPARATOR}',
274 f
'{self._start}..{self._end}' if self
._start
else self
._end
).stdout
277 fixes
= defaultdict(list)
278 lines
= iter(result
.splitlines(False))
279 for i
, commit_hash
in enumerate(lines
):
281 skip
= short
.startswith('Release ') or short
== '[version] update'
283 authors
= [default_author
] if default_author
else []
284 for line
in iter(lambda: next(lines
), self
.COMMIT_SEPARATOR
):
285 match
= self
.AUTHOR_INDICATOR_RE
.match(line
)
287 authors
= sorted(map(str.strip
, line
[match
.end():].split(',')), key
=str.casefold
)
289 commit
= Commit(commit_hash
, short
, authors
)
290 if skip
and (self
._start
or not i
):
291 logger
.debug(f
'Skipped commit: {commit}')
294 logger
.debug(f
'Reached Release commit, breaking: {commit}')
297 fix_match
= self
.FIXES_RE
.search(commit
.short
)
299 commitish
= fix_match
.group(1)
300 fixes
[commitish
].append(commit
)
302 commits
[commit
.hash] = commit
304 for commitish
, fix_commits
in fixes
.items():
305 if commitish
in commits
:
306 hashes
= ', '.join(commit
.hash[:HASH_LENGTH
] for commit
in fix_commits
)
307 logger
.info(f
'Found fix(es) for {commitish[:HASH_LENGTH]}: {hashes}')
308 for fix_commit
in fix_commits
:
309 del commits
[fix_commit
.hash]
311 logger
.debug(f
'Commit with fixes not in changes: {commitish[:HASH_LENGTH]}')
313 return commits
, fixes
315 def apply_overrides(self
, overrides
):
316 for override
in overrides
:
317 when
= override
.get('when')
318 if when
and when
not in self
and when
!= self
._start
:
319 logger
.debug(f
'Ignored {when!r}, not in commits {self._start!r}')
322 override_hash
= override
.get('hash')
323 if override
['action'] == 'add':
324 commit
= Commit(override
.get('hash'), override
['short'], override
.get('authors') or [])
325 logger
.info(f
'ADD {commit}')
326 self
._commits
_added
.append(commit
)
328 elif override
['action'] == 'remove':
329 if override_hash
in self
._commits
:
330 logger
.info(f
'REMOVE {self._commits[override_hash]}')
331 del self
._commits
[override_hash
]
333 elif override
['action'] == 'change':
334 if override_hash
not in self
._commits
:
336 commit
= Commit(override_hash
, override
['short'], override
['authors'])
337 logger
.info(f
'CHANGE {self._commits[commit.hash]} -> {commit}')
338 self
._commits
[commit
.hash] = commit
340 self
._commits
= {key: value for key, value in reversed(self._commits.items())}
343 groups
= defaultdict(list)
345 upstream_re
= self
.UPSTREAM_MERGE_RE
.match(commit
.short
)
347 commit
.short
= f
'[upstream] Merge up to youtube-dl {upstream_re.group(1)}'
349 match
= self
.MESSAGE_RE
.fullmatch(commit
.short
)
351 logger
.error(f
'Error parsing short commit message: {commit.short!r}')
354 prefix
, details
, sub_details
, sub_details_alt
, message
, issues
= match
.groups()
357 if prefix
== 'priority':
358 prefix
, _
, details
= (details
or '').partition('/')
359 logger
.debug(f
'Priority: {message!r}')
360 group
= CommitGroup
.PRIORITY
362 if not details
and prefix
:
363 if prefix
not in ('core', 'downloader', 'extractor', 'misc', 'postprocessor', 'upstream'):
364 logger
.debug(f
'Replaced details with {prefix!r}')
365 details
= prefix
or None
367 if details
== 'common':
371 details
= details
.strip()
374 group
= CommitGroup
.CORE
376 sub_details
= f
'{sub_details or ""},{sub_details_alt or ""}'.replace(':', ',')
377 sub_details
= tuple(filter(None, map(str.strip
, sub_details
.split(','))))
379 issues
= [issue
.strip()[1:] for issue
in issues
.split(',')] if issues
else []
382 group
= CommitGroup
.get(prefix
.lower())
384 if self
.EXTRACTOR_INDICATOR_RE
.search(commit
.short
):
385 group
= CommitGroup
.EXTRACTOR
387 group
= CommitGroup
.POSTPROCESSOR
388 logger
.warning(f
'Failed to map {commit.short!r}, selected {group.name}')
390 commit_info
= CommitInfo(
391 details
, sub_details
, message
.strip(),
392 issues
, commit
, self
._fixes
[commit
.hash])
393 logger
.debug(f
'Resolved {commit.short!r} to {commit_info!r}')
394 groups
[group
].append(commit_info
)
399 def get_new_contributors(contributors_path
, commits
):
401 if contributors_path
.exists():
402 for line
in read_file(contributors_path
).splitlines():
403 author
, _
, _
= line
.strip().partition(' (')
404 authors
= author
.split('/')
405 contributors
.update(map(str.casefold
, authors
))
407 new_contributors
= set()
408 for commit
in commits
:
409 for author
in commit
.authors
:
410 author_folded
= author
.casefold()
411 if author_folded
not in contributors
:
412 contributors
.add(author_folded
)
413 new_contributors
.add(author
)
415 return sorted(new_contributors
, key
=str.casefold
)
418 if __name__
== '__main__':
421 parser
= argparse
.ArgumentParser(
422 description
='Create a changelog markdown from a git commit range')
424 'commitish', default
='HEAD', nargs
='?',
425 help='The commitish to create the range from (default: %(default)s)')
427 '-v', '--verbosity', action
='count', default
=0,
428 help='increase verbosity (can be used twice)')
430 '-c', '--contributors', action
='store_true',
431 help='update CONTRIBUTORS file (default: %(default)s)')
433 '--contributors-path', type=Path
, default
=LOCATION_PATH
.parent
/ 'CONTRIBUTORS',
434 help='path to the CONTRIBUTORS file')
436 '--no-override', action
='store_true',
437 help='skip override json in commit generation (default: %(default)s)')
439 '--override-path', type=Path
, default
=LOCATION_PATH
/ 'changelog_override.json',
440 help='path to the changelog_override.json file')
442 '--default-author', default
='pukkandan',
443 help='the author to use without a author indicator (default: %(default)s)')
445 '--repo', default
='yt-dlp/yt-dlp',
446 help='the github repository to use for the operations (default: %(default)s)')
447 args
= parser
.parse_args()
450 datefmt
='%Y-%m-%d %H-%M-%S', format
='{asctime} | {levelname:<8} | {message}',
451 level
=logging
.WARNING
- 10 * args
.verbosity
, style
='{', stream
=sys
.stderr
)
453 commits
= CommitRange(None, args
.commitish
, args
.default_author
)
455 if not args
.no_override
:
456 if args
.override_path
.exists():
457 overrides
= json
.loads(read_file(args
.override_path
))
458 commits
.apply_overrides(overrides
)
460 logger
.warning(f
'File {args.override_path.as_posix()} does not exist')
462 logger
.info(f
'Loaded {len(commits)} commits')
464 new_contributors
= get_new_contributors(args
.contributors_path
, commits
)
466 if args
.contributors
:
467 write_file(args
.contributors_path
, '\n'.join(new_contributors
) + '\n', mode
='a')
468 logger
.info(f
'New contributors: {", ".join(new_contributors)}')
470 print(Changelog(commits
.groups(), args
.repo
))