fix: let bump_commit return its refusal reason

This commit is contained in:
Abdessamad Derraz committed 2026-11-21 16:43:46 +01:00
1 parent 8780398518
commit beefb32cc8
4 files changed
+86 -74

No files matched your search

+25 -30
View File
@@ -5,7 +5,7 @@ from __future__ import annotations
import functools
import os
import re
import sys
from dataclasses import dataclass
from pathlib import Path
from core.paths import PLATFORMS_DIR
@@ -105,11 +105,19 @@ def shipping_platforms(
]
@dataclass(frozen=True)
class Bump:
"""Whether the pin moved, and the reason it was refused when one applies."""
moved: bool
refusal: str | None = None
def bump_commit(
path: Path, report: ProfileReport, accept_changed: bool = False,
cache_dir: str = DEFAULT_CACHE, offline: bool = False,
platforms_dir: str = PLATFORMS_DIR,
) -> bool:
) -> Bump:
"""Advance source_commit to HEAD when nothing needs a read again.
core_version moves with the pin, read again from version_basis at the
@@ -117,22 +125,20 @@ def bump_commit(
advance whose version cannot be read is refused.
"""
if not report.head or _needs_review(report, accept_changed):
return False
return Bump(False)
text = path.read_text(encoding="utf-8")
if pending_recale(report, accept_changed, text):
return False
return Bump(False)
document = yaml.safe_load(text)
if document.get("built_by") and not document.get("pin_follows"):
# The pin is the commit a platform builds (recalbox gsplus.mk, the
# mame0258 its package fetches), not a point on a moving branch:
# HEAD is a build nobody ships under this name. A build of the
# branch tip (RetroPie's advmame) is HEAD, and follows it.
print(
f"{path.stem}: pinned to the build {', '.join(document['built_by'])} "
"ships, advanced by hand",
file=sys.stderr,
)
return False
return Bump(False, (
f"pinned to the build {', '.join(document['built_by'])} "
"ships, advanced by hand"
))
if not (document.get("pin_frozen") or document.get("pin_follows")):
shipped = shipping_platforms(path.stem, str(path.parent), platforms_dir)
if len(shipped) == 1:
@@ -140,22 +146,16 @@ def bump_commit(
# builds: a bulk advance moved fheroes2, supertux and
# supermariowar past what EmuELEC compiles, and rigelengine is
# already one commit ahead of its package.mk.
print(
f"{path.stem}: only {shipped[0]} ships it. Read the revision its "
return Bump(False, (
f"only {shipped[0]} ships it. Read the revision its "
"build compiles and set pin_frozen, or pin_follows when it "
"builds the branch tip",
file=sys.stderr,
)
return False
"builds the branch tip"
))
if any(isinstance(document.get(f), dict) for f in ("source_commit", "upstream_commit")):
# A pin per build mode names one revision per repository, and the
# report heads only the primary one: rewriting str(dict) was a
# refused write on mesence, supermodel and xemu.
print(
f"{path.stem}: pins keyed by build mode are advanced by hand",
file=sys.stderr,
)
return False
return Bump(False, "pins keyed by build mode are advanced by hand")
# An upstream pinned on a release tag is the base the port was cut from:
# its HEAD is another program, so the bump leaves it on the tag.
# A profile citing nothing compared nothing upstream: writing that HEAD
@@ -169,7 +169,7 @@ def bump_commit(
if document.get("source_commit") == report.head and not upstream_moves:
# Rewriting the pin to the value it already holds is not a bump, and
# announcing it buries the profiles that did move.
return False
return Bump(False)
if (
document.get("source_commit") != report.head
and not cites_primary(report)
@@ -177,11 +177,7 @@ def bump_commit(
# Every ref anchored in another repository: HEAD of this one was
# never compared with anything. gam4980's pin moved to the fork's
# HEAD while its bare libretro.c refs still described upstream.
print(
f"{path.stem}: no ref resolves in {report.repo}, its pin is not advanced",
file=sys.stderr,
)
return False
return Bump(False, f"no ref resolves in {report.repo}, its pin is not advanced")
expected = dict(document)
expected["source_commit"] = report.head
new_text = _set_pin(text, document, "source_commit", report.head, "profiled_date")
@@ -195,8 +191,7 @@ def bump_commit(
readings = version_basis.check(expected, cache_dir, offline, recompute=True)
refused = version_refusal(readings)
if refused:
print(f"{path.stem}: {refused}, its pin is not advanced", file=sys.stderr)
return False
return Bump(False, f"{refused}, its pin is not advanced")
version, basis = apply_readings(expected, readings)
if version != expected.get("core_version") or basis != expected.get("version_basis"):
new_text = set_version_fields(new_text, version, basis)
@@ -204,7 +199,7 @@ def bump_commit(
if basis is not None:
expected["version_basis"] = basis
apply_edit(path, new_text, expected)
return True
return Bump(True)
def version_refusal(readings: list) -> str | None:
+8 -7
View File
@@ -172,20 +172,21 @@ def _apply_writes(
applied = []
if args.rebase_refs and not report.skipped:
applied = rebase_refs(target, report, args.accept_changed)
moved = bump_commit(target, report, args.accept_changed,
args.cache_dir, args.offline)
bump = bump_commit(target, report, args.accept_changed,
args.cache_dir, args.offline)
moved = bump.moved
if args.rebase_refs and applied and not moved:
keyed = any(isinstance(profile.get(f), dict) for f in ("source_commit", "upstream_commit"))
reason = (
"its pins are keyed by build mode and are set by hand"
if keyed else "the prose or an annotated ref has to move first"
)
# A refusal bump_commit did not name is a ref the recale left
# for a reader: the prose guard above has already passed.
reason = bump.refusal or "the prose or an annotated ref has to move first"
print(
f"{name}: refs recaled but the pin will not follow, so nothing "
f"was written: {reason}",
file=sys.stderr,
)
return
if bump.refusal:
print(f"{name}: {bump.refusal}", file=sys.stderr)
messages = [f"{name}: {recale}{line}" for line in applied]
if moved:
# Each pin the write changes, as written: a bump that only moved
+49 -33
View File
@@ -2745,6 +2745,21 @@ class TestWriteDryRun(unittest.TestCase):
self.assertIn("p: not written: o/n@pin:a.c is not in the offline cache", err.getvalue())
self.assertEqual(self.path.read_bytes(), before)
def test_a_recale_held_by_the_bump_names_the_bump_refusal(self):
"""The pass guessed why the pin would not follow and named the prose
on a profile bump_commit had refused for the build a platform ships."""
sample = SAMPLE + "built_by: [recalbox]\n"
self.path.write_text(sample, encoding="utf-8")
with contextlib.redirect_stderr(io.StringIO()) as err:
self._run(self._args(rebase_refs=True, dry_run=False), self._shifted(), sample)
self.assertEqual(self.path.read_text(), sample)
self.assertIn(
"p: refs recaled but the pin will not follow, so nothing was written: "
"pinned to the build recalbox ships, advanced by hand",
err.getvalue(),
)
self.assertNotIn("prose", err.getvalue())
def test_rebase_states_the_move_and_leaves_the_file(self):
before = self.path.read_text()
output = self._run(self._args(rebase_refs=True), self._shifted())
@@ -2771,7 +2786,7 @@ class TestWriteDryRun(unittest.TestCase):
entries=[EntryReport("a.bin", "a.c:10-12", "ANCHORED", [part])],
counts={"ANCHORED": 1},
)
self.assertFalse(bump_commit(self.path, report))
self.assertFalse(bump_commit(self.path, report).moved)
self.assertEqual(self._run(self._args(bump_commit=True), report), "")
def test_two_writing_pins_give_two_readings(self):
@@ -3068,7 +3083,7 @@ class TestDeliberatelyFrozenPin(TestBuildReport):
path = Path(self.dir) / "p.yml"
path.write_text(SAMPLE, encoding="utf-8")
self.assertEqual(rebase_refs(path, report), [])
self.assertFalse(bump_commit(path, report))
self.assertFalse(bump_commit(path, report).moved)
def test_the_builds_platforms_hold_say_so(self):
# reicast is the commit Recalbox's reicast.mk builds, 789 commits
@@ -3324,7 +3339,7 @@ class TestBumpCommit(unittest.TestCase):
counts={"SHIFTED": 1},
)
self.assertEqual(rebase.pending_recale(report), 1)
self.assertFalse(bump_commit(self.path, report))
self.assertFalse(bump_commit(self.path, report).moved)
def test_a_profile_citing_nothing_advances(self):
# anarch's pin sat behind HEAD through every bulk advance: the
@@ -3334,7 +3349,7 @@ class TestBumpCommit(unittest.TestCase):
skipped=resolver.NO_SOURCE_REF,
)
report.upstream_head, report.upstream_pin = "uphead", "uppin"
self.assertTrue(bump_commit(self.path, report))
self.assertTrue(bump_commit(self.path, report).moved)
text = self.path.read_text(encoding="utf-8")
self.assertIn("newhead", text)
# Nothing was compared upstream, so its pin is not written.
@@ -3361,22 +3376,22 @@ class TestBumpCommit(unittest.TestCase):
bump.shipping_platforms("advanced_drastic", self.tmp.name, platforms),
["knulli"],
)
with contextlib.redirect_stderr(io.StringIO()) as err:
self.assertFalse(bump_commit(path, report, platforms_dir=platforms))
self.assertIn("only knulli ships it", err.getvalue())
refused = bump_commit(path, report, platforms_dir=platforms)
self.assertFalse(refused.moved)
self.assertIn("only knulli ships it", refused.refusal or "")
path.write_text(
path.read_text(encoding="utf-8")
+ 'pin_follows: "KNULLI builds the branch tip"\n',
encoding="utf-8",
)
self.assertTrue(bump_commit(path, report, platforms_dir=platforms))
self.assertTrue(bump_commit(path, report, platforms_dir=platforms).moved)
def test_any_other_skip_still_holds_the_pin(self):
report = ProfileReport(
name="p", repo="o/n", pin="pin", head="newhead", entries=[], counts={},
skipped="rate limited: gitlab.com",
)
self.assertFalse(bump_commit(self.path, report))
self.assertFalse(bump_commit(self.path, report).moved)
def test_accepted_when_nothing_had_to_move(self):
part = PartResult(
@@ -3390,7 +3405,7 @@ class TestBumpCommit(unittest.TestCase):
counts={"ANCHORED": 1},
)
self.assertEqual(rebase.pending_recale(report), 0)
self.assertTrue(bump_commit(self.path, report))
self.assertTrue(bump_commit(self.path, report).moved)
def test_refused_on_a_build_a_platform_compiles(self):
# mame0258 documents the MAME Recalbox builds: libretro/mame HEAD
@@ -3406,8 +3421,9 @@ class TestBumpCommit(unittest.TestCase):
],
counts={"ANCHORED": 1},
)
with contextlib.redirect_stderr(io.StringIO()):
self.assertFalse(bump_commit(self.path, report))
refused = bump_commit(self.path, report)
self.assertFalse(refused.moved)
self.assertIn("recalbox ships, advanced by hand", refused.refusal or "")
self.assertNotIn("newhead", self.path.read_text())
def test_a_build_of_the_branch_tip_follows_it(self):
@@ -3428,7 +3444,7 @@ class TestBumpCommit(unittest.TestCase):
],
counts={"ANCHORED": 1},
)
self.assertTrue(bump_commit(self.path, report))
self.assertTrue(bump_commit(self.path, report).moved)
self.assertIn("newhead", self.path.read_text())
def test_a_config_read_at_the_pin_follows_it(self):
@@ -3460,7 +3476,7 @@ class TestBumpCommit(unittest.TestCase):
entries=[EntryReport("a.bin", "a.c:10-12", "ANCHORED", [part])],
counts={"ANCHORED": 1},
)
self.assertTrue(bump_commit(self.path, report))
self.assertTrue(bump_commit(self.path, report).moved)
document = yaml.safe_load(self.path.read_text(encoding="utf-8"))
self.assertEqual(document["source_commit"], head)
self.assertEqual([r["revision"] for r in document["config_reads"]], [head, "b" * 40])
@@ -3479,9 +3495,9 @@ class TestBumpCommit(unittest.TestCase):
entries=[EntryReport("a.bin", "libretro.c:10-12", "ANCHORED", [part])],
counts={"ANCHORED": 1},
)
self.assertFalse(bump_commit(self.path, report("up/n")))
self.assertFalse(bump_commit(self.path, report("up/n")).moved)
self.assertNotIn("newhead", self.path.read_text())
self.assertTrue(bump_commit(self.path, report(None)))
self.assertTrue(bump_commit(self.path, report(None)).moved)
def test_a_bump_alone_waits_for_the_recale(self):
"""lrps2 at 0 to review with two SHIFTED parts: --bump-commit run
@@ -3492,10 +3508,10 @@ class TestBumpCommit(unittest.TestCase):
entries=[EntryReport("a.bin", "a.c:10-12", "SHIFTED", [part])],
counts={"SHIFTED": 1},
)
self.assertFalse(bump_commit(self.path, report))
self.assertFalse(bump_commit(self.path, report).moved)
self.assertNotIn("newhead", self.path.read_text())
self.assertEqual(rebase_refs(self.path, report), ["a.c:10-12 -> a.c:30-32"])
self.assertTrue(bump_commit(self.path, report))
self.assertTrue(bump_commit(self.path, report).moved)
def test_an_upstream_on_its_release_tag_stays_there(self):
"""GroovyMAME's upstream_commit is the mame0289 tag it merged: a bump
@@ -3510,7 +3526,7 @@ class TestBumpCommit(unittest.TestCase):
upstream_pin="uptag", upstream_head="uphead",
upstream_origin="upstream_commit", upstream_tag="mame0289",
)
self.assertTrue(bump_commit(self.path, report))
self.assertTrue(bump_commit(self.path, report).moved)
written = yaml.safe_load(self.path.read_text())
self.assertEqual(written["source_commit"], "newhead")
self.assertNotIn("upstream_commit", written)
@@ -3526,7 +3542,7 @@ class TestBumpCommit(unittest.TestCase):
counts={"ANCHORED": 1},
upstream_pin="uppin", upstream_head="uphead", upstream_origin="upstream_commit",
)
self.assertTrue(bump_commit(self.path, report))
self.assertTrue(bump_commit(self.path, report).moved)
written = yaml.safe_load(self.path.read_text())
self.assertEqual(written["source_commit"], "newhead")
self.assertEqual(written["upstream_commit"], "uphead")
@@ -3548,7 +3564,7 @@ class TestBumpCommit(unittest.TestCase):
)
before = self.path.read_text(encoding="utf-8")
with contextlib.redirect_stderr(io.StringIO()):
self.assertFalse(bump_commit(self.path, report))
self.assertFalse(bump_commit(self.path, report).moved)
self.assertEqual(self.path.read_text(encoding="utf-8"), before)
def test_refused_when_pinned_to_a_superseded_tag(self):
@@ -3556,21 +3572,21 @@ class TestBumpCommit(unittest.TestCase):
name="p", repo="o/n", pin="pin", head="newhead",
entries=[], counts={"ANCHORED": 3}, pinned_tag="v1.6.0",
)
self.assertFalse(bump_commit(self.path, report))
self.assertFalse(bump_commit(self.path, report).moved)
def test_refused_while_a_changed_remains(self):
report = ProfileReport(
name="p", repo="o/n", pin="pin", head="newhead",
entries=[], counts={"CHANGED": 1},
)
self.assertFalse(bump_commit(self.path, report))
self.assertFalse(bump_commit(self.path, report).moved)
def test_accepted_when_everything_anchors(self):
report = ProfileReport(
name="p", repo="o/n", pin="pin", head="newhead",
entries=[], counts={"ANCHORED": 3, "SHIFTED": 1},
)
self.assertTrue(bump_commit(self.path, report))
self.assertTrue(bump_commit(self.path, report).moved)
self.assertEqual(
yaml.safe_load(self.path.read_text())["source_commit"], "newhead"
)
@@ -4425,7 +4441,7 @@ class TestRebaseProse(unittest.TestCase):
written = self.path.read_text()
self.assertEqual(written, text.replace("(a.c:10)", "(a.c:14)"))
self.assertEqual(rebase.pending_recale(report, text=written), 0)
self.assertTrue(bump_commit(self.path, report))
self.assertTrue(bump_commit(self.path, report).moved)
def test_a_refused_recale_names_the_citation_and_writes_nothing(self):
# The structured ref could move, the prose could not: the run
@@ -4534,7 +4550,7 @@ class TestPendingProse(unittest.TestCase):
report = self._report([part])
text = self.path.read_text()
self.assertEqual(rebase.pending_recale(report, text=text), 1)
self.assertFalse(bump_commit(self.path, report))
self.assertFalse(bump_commit(self.path, report).moved)
self.assertEqual(
yaml.safe_load(self.path.read_text())["source_commit"], "pin"
)
@@ -4543,7 +4559,7 @@ class TestPendingProse(unittest.TestCase):
part = PartResult(RefPart("a.c", 10, 10, "10"), "SHIFTED", None, 14, 14, [])
report = self._report([part])
rebase_refs(self.path, report)
self.assertTrue(bump_commit(self.path, report))
self.assertTrue(bump_commit(self.path, report).moved)
self.assertEqual(
yaml.safe_load(self.path.read_text())["source_commit"], "newhead"
)
@@ -4554,7 +4570,7 @@ class TestPendingProse(unittest.TestCase):
self.assertEqual(
rebase.pending_recale(report, text=self.path.read_text()), 0
)
self.assertTrue(bump_commit(self.path, report))
self.assertTrue(bump_commit(self.path, report).moved)
def test_unwritable_prose_blocks_the_pin(self):
self.path.write_text(FOLDED_TWICE, encoding="utf-8")
@@ -4564,7 +4580,7 @@ class TestPendingProse(unittest.TestCase):
"twice", "q.c:7", "SHIFTED", [part], "prose", "twice"
)
self.assertEqual(rebase_refs(self.path, report), [])
self.assertFalse(bump_commit(self.path, report))
self.assertFalse(bump_commit(self.path, report).moved)
class TestWrappedProse(unittest.TestCase):
@@ -4631,7 +4647,7 @@ class TestWrappedProse(unittest.TestCase):
self.assertEqual(len(rebase_refs(self.path, report)), 1)
text = self.path.read_text()
self.assertIn("(libretro.cpp:\n 2381-2382, 2438-2442) and by", text)
self.assertTrue(bump_commit(self.path, report))
self.assertTrue(bump_commit(self.path, report).moved)
def test_a_break_before_the_colon_moves_the_next_line(self):
# playdiaemu wrote `.../src/content.rs` and `:111-199` one line apart.
@@ -4650,7 +4666,7 @@ class TestWrappedProse(unittest.TestCase):
["adam: src/content.rs:111-199 -> src/content.rs:111-204"],
)
self.assertIn("(src/content.rs\n :111-204). No", self.path.read_text())
self.assertTrue(bump_commit(self.path, report, accept_changed=True))
self.assertTrue(bump_commit(self.path, report, accept_changed=True).moved)
class TestOpcodeCache(unittest.TestCase):
@@ -4726,7 +4742,7 @@ class TestCommentCitations(unittest.TestCase):
self.path.read_text(),
self.TEXT.replace("src/cpu.c:3212-3451", "src/cpu.c:3268-3507"),
)
self.assertTrue(bump_commit(self.path, report))
self.assertTrue(bump_commit(self.path, report).moved)
def test_a_renamed_comment_path_is_rewritten(self):
# bennugd `libretro.c:887-910` in a comment: RENAMED to
@@ -4737,7 +4753,7 @@ class TestCommentCitations(unittest.TestCase):
rebase_refs(self.path, report), ["comment line 16: z.c:9 -> src/z.c:9"]
)
self.assertIn('"a.c:10-12" # see src/z.c:9', self.path.read_text())
self.assertTrue(bump_commit(self.path, report))
self.assertTrue(bump_commit(self.path, report).moved)
class TestRealignProse(unittest.TestCase):
+4 -4
View File
@@ -277,7 +277,7 @@ class AnAdvanceReadsTheVersionAgain(unittest.TestCase):
return [self._reading(version_basis.DRIFT, "2.0")]
with mock.patch.object(version_basis, "check", check):
self.assertTrue(bump_commit(self.path, self.report))
self.assertTrue(bump_commit(self.path, self.report).moved)
written = yaml.safe_load(self.path.read_text(encoding="utf-8"))
self.assertEqual(written["source_commit"], "bbbbbbb")
self.assertEqual(written["core_version"], "2.0")
@@ -287,20 +287,20 @@ class AnAdvanceReadsTheVersionAgain(unittest.TestCase):
def test_an_unreadable_version_holds_the_pin(self):
with mock.patch.object(version_basis, "check",
return_value=[self._reading(version_basis.UNREADABLE, None)]):
self.assertFalse(bump_commit(self.path, self.report))
self.assertFalse(bump_commit(self.path, self.report).moved)
self.assertEqual(self.path.read_text(encoding="utf-8"), SAMPLE)
def test_a_profile_without_basis_is_not_advanced(self):
self.path.write_text(SAMPLE.split("version_basis:")[0] + "files: []\n", encoding="utf-8")
before = self.path.read_text(encoding="utf-8")
self.assertFalse(bump_commit(self.path, self.report))
self.assertFalse(bump_commit(self.path, self.report).moved)
self.assertEqual(self.path.read_text(encoding="utf-8"), before)
def test_a_package_version_is_advanced_by_hand(self):
reading = version_basis.Reading("", "package", "1.0", None, "read from x.apk",
version_basis.UNCHECKED)
with mock.patch.object(version_basis, "check", return_value=[reading]):
self.assertFalse(bump_commit(self.path, self.report))
self.assertFalse(bump_commit(self.path, self.report).moved)
class TheWriterKeepsTheDocument(unittest.TestCase):