Skip to content

Commit c1a4468

Browse files
committed
tools/versions: add command to commit wrap changes with automatic log
For version bumps, autogenerate the commit message. For other wrap changes, require a manually-specified commit message but prepend the wrap name. (Less useful, but perhaps has some value.) In all cases, commit only those files that might be relevant.
1 parent 9153229 commit c1a4468

File tree

1 file changed

+71
-5
lines changed

1 file changed

+71
-5
lines changed

tools/versions.py

Lines changed: 71 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python3
22

3-
# Copyright 2024 Benjamin Gilbert <[email protected]>
3+
# Copyright 2024-2025 Benjamin Gilbert <[email protected]>
44

55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
import json
2424
import os
2525
import re
26+
import subprocess
2627
import sys
2728
import time
2829
from typing import TypedDict
@@ -104,10 +105,17 @@ def sub(name, old, new):
104105

105106

106107
@cache
107-
def get_releases() -> dict[str, WrapInfo]:
108-
'''Parse and return releases.json.'''
109-
with open('releases.json') as f:
110-
return json.load(f)
108+
def get_releases(commit=None) -> dict[str, WrapInfo]:
109+
'''Parse and return releases.json for the specified commit or the working
110+
tree.'''
111+
if commit is not None:
112+
data = subprocess.check_output(
113+
['git', 'cat-file', 'blob', f'{commit}:releases.json'], text=True
114+
)
115+
else:
116+
with open('releases.json') as f:
117+
data = f.read()
118+
return json.loads(data)
111119

112120

113121
def get_wrap_versions() -> dict[str, str]:
@@ -262,6 +270,52 @@ def do_autoupdate(args: Namespace) -> None:
262270
raise Exception(f"Couldn't update {failures} wraps")
263271

264272

273+
def do_commit(args: Namespace) -> None:
274+
old_releases = get_releases('HEAD')
275+
new_releases = get_releases()
276+
277+
# we don't validate any invariants checked by sanity_checks.py
278+
changed_wraps = [
279+
name for name in new_releases
280+
if old_releases.get(name) != new_releases[name]
281+
]
282+
if not changed_wraps:
283+
raise ValueError('Found no changes to releases.json')
284+
if len(changed_wraps) != 1:
285+
raise ValueError(f'Can only commit one wrap at a time: {changed_wraps}')
286+
name = changed_wraps[0]
287+
288+
if not args.message:
289+
new_ver = new_releases[name]['versions'][0]
290+
try:
291+
old_ver = old_releases[name]['versions'][0]
292+
except KeyError:
293+
old_ver = None
294+
if old_ver is None:
295+
args.message = 'add'
296+
elif old_ver != new_ver and new_ver.endswith('-1'):
297+
args.message = f'update from {old_ver.split("-")[0]} to {new_ver.split("-")[0]}'
298+
else:
299+
raise ValueError("Can't autogenerate commit message; specify -m")
300+
301+
commit_files = [
302+
'ci_config.json', 'releases.json', f'subprojects/{name}.wrap'
303+
]
304+
patch_dir = get_wrap_contents(name).get(
305+
'wrap-file', 'patch_directory', fallback=None
306+
)
307+
if patch_dir:
308+
commit_files.append(f'subprojects/packagefiles/{patch_dir}')
309+
310+
# suppress Git summary output and recreate it ourselves so we can also
311+
# show the diffstat, to confirm we've committed the right files
312+
subprocess.check_call(
313+
['git', 'commit', '-m', f'{name}: {args.message}', '-q'] + commit_files
314+
)
315+
subprocess.check_call(['git', 'log', '-1', '--format=[%h] %s'])
316+
subprocess.check_call(['git', 'diff', '--stat', 'HEAD^..HEAD'])
317+
318+
265319
def do_libtool_ver(args: Namespace) -> None:
266320
components = args.info.split(':', 2)
267321
components += ['0'] * (3 - len(components))
@@ -388,6 +442,18 @@ def main() -> None:
388442
)
389443
autoupdate.set_defaults(func=do_autoupdate)
390444

445+
commit = subparsers.add_parser(
446+
'commit',
447+
aliases=['com'],
448+
help='commit a wrap update to Git',
449+
description='Commit a wrap update to Git.',
450+
)
451+
commit.add_argument(
452+
'-m', '--message', metavar='text',
453+
help='commit message, without wrap name prefix'
454+
)
455+
commit.set_defaults(func=do_commit)
456+
391457
libtool_ver = subparsers.add_parser(
392458
'libtool-ver',
393459
aliases=['ltv'],

0 commit comments

Comments
 (0)