Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,10 @@ Sync PRs are squash-merged like every other PR, so there is no merge parent for
fork point and misreports the whole delta.

That makes `git merge v0.6.19` from `amd-integration` wrong: it resolves against
that stale base. Produce the merge from the recorded base instead:
a merge-base, which right after a squash-merged sync is the previous fork point,
and on a diverged release branch counts the recorded release's own commits as
ours. Produce the merge from the recorded base instead, the base
`upstream_canary.py` prices it with:

```bash
BASE=$(python3 -c "import sys; sys.path.insert(0, 'scripts'); import upstream_base as u; print(u.read_worktree('.').sha)")
Expand Down Expand Up @@ -408,8 +411,8 @@ Reachability is not free: it inflates any `git rev-list <tag>..HEAD` walk that
counts every parent. `git_describe_rocm.py` therefore measures `.devN` along
`--first-parent` — see `test_an_absorbed_upstream_branch_does_not_inflate_the_distance`
for why counting the merged side breaks version ordering outright. The canary's
`ours` commit count does rise by the release branch's length; that one is
cosmetic.
`ours` commit count still includes release-only commits on lines the record does
not contain (v0.5.3's today, v0.6.18's after the next sync); that one is cosmetic.

# Adding a Kernel

Expand Down
51 changes: 48 additions & 3 deletions scripts/upstream_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

Recorded in a file rather than read off ancestry: a squash-merged sync leaves no
merge parent, so ``merge-base(HEAD, <upstream tag>)`` silently returns the
*previous* fork point. Both callers must agree on the rule, so it lives here.
*previous* fork point. The rules live here so callers share them: ``select()``
for ownership (amd_coverage), ``sync_base()`` for pricing a sync (the canary).
"""

from __future__ import annotations
Expand Down Expand Up @@ -135,8 +136,8 @@ def select(
"""``(base_sha, source)`` — the anchor is the recorded commit, not our tip.

``merge-base(recorded, theirs)`` degrades to the recorded commit itself for a
release we have absorbed, and still finds the real divergence point for a
moving branch like ``upstream/main``.
release we have absorbed, and finds the divergence point for a moving branch
like ``upstream/main``. Pricing a merge needs :func:`sync_base` instead.
"""
if recorded is None:
anchor, source = ours, "ancestry"
Expand All @@ -153,6 +154,50 @@ def select(
return proc.stdout.strip(), source


def sync_base(
run: Runner,
repo: str,
ours: str,
theirs: str,
recorded: Optional[UpstreamBase],
) -> Tuple[str, str]:
"""``(base_sha, source)`` for pricing a sync: the base the sync itself uses.

The record wins wherever it applies: a ``theirs`` it contains is its own
base, and one descending from it -- or on a diverged line such as the next
release branch -- is priced against it. Only a diverged ``theirs`` that ours
reaches is its own base, and that one says so with ``"reachable"``.
"""
base, source = select(run, repo, ours, theirs, recorded)
if recorded is None:
return base, source
theirs_sha = _commit_sha(run, repo, theirs)
if base in (theirs_sha, recorded.sha):
return base, source
if _is_ancestor(run, repo, theirs_sha, ours):
return theirs_sha, "reachable"
return recorded.sha, source


def _commit_sha(run: Runner, repo: str, ref: str) -> str:
proc = run(
repo, "rev-parse", "--verify", "--quiet", f"{ref}^{{commit}}", check=False
)
if proc.returncode:
raise UpstreamBaseError(f"'{ref}' does not name a commit")
return proc.stdout.strip()


def _is_ancestor(run: Runner, repo: str, ancestor: str, descendant: str) -> bool:
proc = run(repo, "merge-base", "--is-ancestor", ancestor, descendant, check=False)
if proc.returncode not in (0, 1):
raise UpstreamBaseError(
f"cannot tell whether {ancestor[:12]} is an ancestor of "
f"{descendant[:12]}:\n{proc.stderr.strip()}"
)
return proc.returncode == 0


def multiple_bases(run: Runner, repo: str, base_anchor: str, theirs: str) -> bool:
proc = run(repo, "merge-base", "--all", base_anchor, theirs, check=False)
return not proc.returncode and len(proc.stdout.split()) > 1
Expand Down
16 changes: 11 additions & 5 deletions scripts/upstream_canary.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

"""Report what an upstream sync would cost, without performing one.

Prints how far the fork and upstream have moved from the merge base, the
Prints how far the fork and upstream have moved from the sync's base, the
conflicts a merge would raise, and upstream churn on the headers that
``rocm/`` forked -- those conflict with nothing and go stale silently.

Expand Down Expand Up @@ -241,7 +241,7 @@ def _report_position(
base_desc: str, ours: Tuple[int, int], theirs: Tuple[int, int]
) -> None:
print("== position ==")
print(f"merge base : {base_desc}")
print(f"base : {base_desc}")
print(f"ours : {ours[0]:>6} commits, {ours[1]:>5} files changed")
print(f"upstream : {theirs[0]:>6} commits, {theirs[1]:>5} files changed")
print()
Expand Down Expand Up @@ -358,10 +358,16 @@ def run(args: argparse.Namespace) -> int:
print(
upstream_base.missing_note(f"{ours[:12]}:{upstream_base.FILENAME}") + "\n"
)
base, _ = upstream_base.select(_run, repo, ours, theirs, recorded)
base, source = upstream_base.sync_base(_run, repo, ours, theirs, recorded)
if source == "reachable":
print("NOTE: ours already reaches upstream, so it is priced as absorbed.")
print(
" Reachability does not prove its content landed: an `-s ours`\n"
" merge adds ancestry only.\n"
)

anchor = recorded.sha if recorded else ours
if upstream_base.multiple_bases(_run, repo, anchor, theirs):
# A recorded base is used as-is, so only the ancestry fallback can be ambiguous.
if recorded is None and upstream_base.multiple_bases(_run, repo, ours, theirs):
print("NOTE: multiple merge bases -- churn and the merge below both use")
print(" the one shown; a real `git merge` will build a virtual base.\n")

Expand Down
50 changes: 50 additions & 0 deletions tests/rocm/test_upstream_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,56 @@ def test_a_ref_that_moved_away_from_the_recorded_sha_is_loud(self, forked):
ub.select(_run, str(forked.path), "HEAD", "v1.0", recorded)


class TestSyncBase:
"""The base a sync merges against -- what the canary must price with."""

def _sync_base(self, forked, theirs):
recorded = ub.read_worktree(str(forked.path))
return ub.sync_base(_run, str(forked.path), "HEAD", theirs, recorded)

def test_a_sibling_branch_is_priced_against_the_recorded_commit(self, forked):
"""merge-base gives R0, which would count the release-only R1 as ours."""
assert self._sync_base(forked, "main") == (forked.r1, "recorded")

def test_the_next_release_on_its_own_branch_is_priced_against_the_record(
self, forked
):
"""The v0.6.18 -> v0.7.3 shape: cut before R1, annotated tag."""
_git(forked.path, "checkout", "-q", "-b", "release-2", forked.r0)
_write(forked.path, "next.cu", "n1\n")
_commit(forked.path, "N1")
_git(forked.path, "tag", "-a", "-m", "v2.0", "v2.0")
_git(forked.path, "checkout", "-q", "ours")
assert self._sync_base(forked, "v2.0") == (forked.r1, "recorded")

def test_a_ref_the_recorded_commit_contains_is_its_own_base(self, forked):
assert self._sync_base(forked, forked.r0) == (forked.r0, "recorded")

def test_an_older_release_ours_reaches_is_its_own_base(self, forked):
"""v0.5.3's shape: its own release branch, reached from ours by a merge."""
_git(forked.path, "checkout", "-q", "-b", "old-release", forked.base)
_write(forked.path, "old.cu", "o\n")
_commit(forked.path, "OLD")
old = _rev(forked.path)
_git(forked.path, "checkout", "-q", "ours")
_git(forked.path, "merge", "-q", "-s", "ours", "--no-edit", old)
assert self._sync_base(forked, old) == (old, "reachable")

def test_an_ancestry_merge_cannot_override_the_record_on_its_own_line(self, forked):
"""`-s ours` of a release past the record adds reachability, not content."""
_git(forked.path, "checkout", "-q", "release")
_write(forked.path, "feat.cu", "r2\n")
_commit(forked.path, "R2")
r2 = _rev(forked.path)
_git(forked.path, "checkout", "-q", "ours")
_git(forked.path, "merge", "-q", "-s", "ours", "--no-edit", r2)
assert self._sync_base(forked, r2) == (forked.r1, "recorded")

def test_without_a_record_it_matches_select(self, forked):
got = ub.sync_base(_run, str(forked.path), "HEAD", "v1.0", None)
assert got == (forked.base, "ancestry")


class TestMultipleBases:
def test_single_base_is_not_announced(self, forked):
assert not ub.multiple_bases(_run, str(forked.path), forked.r1, "main")
Expand Down
22 changes: 22 additions & 0 deletions tests/rocm/test_upstream_canary.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,28 @@ def test_absorbed_upstream_is_not_reported_as_conflicts(
assert "clean merge -- nothing to do" in out
assert theirs[:7] in out, "position should name the recorded base"

def test_a_diverged_upstream_is_priced_against_the_record(
self, repo, monkeypatch, capsys
):
"""The v0.6.18 -> v0.7.3 shape. A merge-base would name `base` instead."""
_git(repo, "checkout", "-q", "-b", "release")
_write(repo, "release.cu", "release only\n")
_commit(repo, "release-only fix")
recorded = _rev(repo)
_git(repo, "checkout", "-q", "theirs")
_write(repo, "next.cu", "next release\n")
_commit(repo, "next release")
_git(repo, "checkout", "-q", "main")
_write(repo, "release.cu", "release only\n")
_write(repo, uc.upstream_base.FILENAME, f"release {recorded}\n")
_commit(repo, "squashed sync")
monkeypatch.chdir(repo)

uc.run(_args())

line = re.search(r"^base\s+:\s+(\S+)", capsys.readouterr().out, re.M)
assert line is not None and recorded.startswith(line.group(1))

def test_missing_record_announces_the_fallback(self, repo, monkeypatch, capsys):
_diverge(repo)
monkeypatch.chdir(repo)
Expand Down
Loading