From 33b2647cb05b459a702f4084a1132a4b9d03ee16 Mon Sep 17 00:00:00 2001 From: Lucas Koontz Date: Mon, 16 Nov 2026 21:44:51 -0600 Subject: [PATCH 1/2] fix(sync): land a hotfix staging already holds instead of failing on its conflict (ENG-4379) --- .github/workflows/release-unfreeze.yml | 5 +- .github/workflows/sync-main-to-staging.yml | 10 +- README.md | 2 + scripts/sync_staging.py | 111 +++++++++++++ tests/test_sync_staging.py | 178 +++++++++++++++++++++ 5 files changed, 302 insertions(+), 4 deletions(-) create mode 100644 scripts/sync_staging.py create mode 100644 tests/test_sync_staging.py diff --git a/.github/workflows/release-unfreeze.yml b/.github/workflows/release-unfreeze.yml index 4fdfd4d..3802876 100644 --- a/.github/workflows/release-unfreeze.yml +++ b/.github/workflows/release-unfreeze.yml @@ -159,7 +159,10 @@ jobs: echo "${STAGING} already contains ${BASE} — nothing to sync." exit 0 fi - git merge --no-ff "origin/${BASE}" -m "Sync ${BASE} back into ${STAGING} after release" + # The same merge as sync-main-to-staging.yml: a conflict only on changes + # staging already holds lands, and any other conflict fails naming them. + python3 ../.ci-shared/scripts/sync_staging.py \ + --base "origin/${BASE}" --message "Sync ${BASE} back into ${STAGING} after release" git push origin "HEAD:${STAGING}" # The alert lives here rather than in each repo's wrapper, which is what diff --git a/.github/workflows/sync-main-to-staging.yml b/.github/workflows/sync-main-to-staging.yml index 8f5fc02..b274469 100644 --- a/.github/workflows/sync-main-to-staging.yml +++ b/.github/workflows/sync-main-to-staging.yml @@ -88,7 +88,7 @@ jobs: # After the checkout above, which would otherwise clean this back out of # the workspace. - - name: Check out the shared alert implementation + - name: Check out the shared alert and sync scripts uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1 with: repository: mindsdb/github-actions @@ -124,8 +124,12 @@ jobs: exit 0 fi - git merge --no-ff "origin/${BASE}" \ - -m "Sync ${BASE} into ${STAGING}" + # A plain merge, or, when it conflicts only on changes staging already + # holds (a hotfix that landed on staging first), a merge that keeps + # staging's content. Any other conflict fails here, naming the files + # and the commits staging lacks. + python3 .ci-shared/scripts/sync_staging.py \ + --base "origin/${BASE}" --message "Sync ${BASE} into ${STAGING}" if git push origin "HEAD:${STAGING}"; then echo "Pushed ${BASE} -> ${STAGING} on attempt ${attempt}." diff --git a/README.md b/README.md index 52fae24..587a63e 100644 --- a/README.md +++ b/README.md @@ -124,6 +124,8 @@ concurrency: The sync is idempotent (it exits 0 when `staging` already contains `main`), so whichever run loses the race no-ops. +**A hotfix staging already holds lands on its own.** Both syncs merge through `scripts/sync_staging.py`. A hotfix lands on staging first and is then cherry-picked onto main, so staging usually holds the change before main's push fires the sync. A plain `git merge` still conflicts when staging has since edited a neighboring line. On a conflict, the script asks `git cherry` whether every commit main gained since the merge base has an equal patch on staging. If so, it records the merge with `-s ours`, which keeps staging's content and makes `main` an ancestor of `staging`; the run log carries a notice naming the held commits. If main brings any change staging lacks, or a merge commit `git cherry` cannot compare, the run fails and its error names the conflicting files and the missing commits. A person then resolves it. Staging takes squash merges only, so a hand-made merge PR into staging does not make `main` an ancestor. Instead, open a PR on `main` that brings the conflicting lines in line with staging, and the next sync merges cleanly. + The chain is event-driven: `Staging Freeze` finishing fires the release-PR workflow via `workflow_run`; merging that PR fires `Staging Unfreeze`. The `workflow_run` link matches on the **caller** workflow's name, so callers must diff --git a/scripts/sync_staging.py b/scripts/sync_staging.py new file mode 100644 index 0000000..14f99e8 --- /dev/null +++ b/scripts/sync_staging.py @@ -0,0 +1,111 @@ +"""Merge main into the checked-out staging branch, landing hotfixes staging already holds. + +A hotfix lands on staging first and is then cherry-picked onto main, so by the +time main's push fires the sync, staging usually holds the same change already. +`git merge` still conflicts when staging has since edited a neighboring line, +which is what stopped cowork's sync on 17 Nov: both sides add the same import, +and the line under it differs. Nobody can land that merge by hand on a +squash-only staging, so the train stays red until someone works around it. + +So on a conflict this checks whether every commit main gained since the merge +base already has an equal patch on staging (`git cherry` marks each one `-`). If +so, it records main as merged and keeps staging's content (`merge -s ours`): +staging holds every change main brings, so nothing is lost. If main holds any +change staging lacks, or a merge commit whose content `git cherry` cannot +compare, it fails as before, and names the conflicting files and the commits +staging lacks. + +Run it inside the staging checkout, with the merge identity configured. Exit 0 +means HEAD is a new merge commit ready to push; exit 1 means a person must merge. +""" + +from __future__ import annotations + +import argparse +import subprocess +import sys +from dataclasses import dataclass +from typing import Callable, Sequence + +Runner = Callable[[Sequence[str]], "subprocess.CompletedProcess[str]"] + +MERGED = "merged" # an ordinary merge, no conflict +MERGED_AS_HELD = "merged-as-held" # a conflict on changes staging already holds; staging's content kept +NEEDS_A_PERSON = "needs-a-person" # a conflict on a change staging lacks + + +@dataclass(frozen=True) +class SyncResult: + outcome: str + conflicts: tuple[str, ...] = () # files the plain merge conflicted on + held: tuple[str, ...] = () # " " of main commits staging already holds + lacking: tuple[str, ...] = () # " " of main commits staging lacks + + +def _run(argv: Sequence[str]) -> "subprocess.CompletedProcess[str]": + return subprocess.run(list(argv), capture_output=True, text=True, check=False) + + +def _git(runner: Runner, *args: str) -> str: + result = runner(["git", *args]) + if result.returncode != 0: + raise RuntimeError(f"git {' '.join(args)} failed: {result.stderr.strip() or result.stdout.strip()}") + return result.stdout + + +def _subject(runner: Runner, sha: str) -> str: + return f"{sha[:12]} {_git(runner, 'log', '-1', '--format=%s', sha).strip()}" + + +def sync(runner: Runner, base_ref: str, message: str) -> SyncResult: + merge = runner(["git", "merge", "--no-ff", base_ref, "-m", message]) + if merge.returncode == 0: + return SyncResult(MERGED) + + conflicts = tuple(_git(runner, "diff", "--name-only", "--diff-filter=U").split()) + runner(["git", "merge", "--abort"]) + if not conflicts: + # The merge failed for some other reason (a dirty tree, a missing ref): not ours to judge. + raise RuntimeError(f"git merge {base_ref} failed: {merge.stderr.strip() or merge.stdout.strip()}") + + merge_base = _git(runner, "merge-base", "HEAD", base_ref).strip() + merges = _git(runner, "rev-list", "--merges", f"{merge_base}..{base_ref}").split() + # `+ `: main commit with no equal patch on staging; `- `: staging holds it. + marks = [line.split() for line in _git(runner, "cherry", "HEAD", base_ref, merge_base).splitlines() if line] + held = tuple(_subject(runner, sha) for mark, sha in marks if mark == "-") + lacking = tuple(_subject(runner, sha) for mark, sha in marks if mark == "+") + lacking += tuple(f"{_subject(runner, sha)} (a merge commit, which cannot be compared)" for sha in merges) + + if lacking or not held: + return SyncResult(NEEDS_A_PERSON, conflicts=conflicts, held=held, lacking=lacking) + + _git(runner, "merge", "--no-ff", "-s", "ours", base_ref, "-m", message) + return SyncResult(MERGED_AS_HELD, conflicts=conflicts, held=held) + + +def main(argv: Sequence[str] | None = None, runner: Runner = _run) -> int: + parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) + parser.add_argument("--base", required=True, help="the ref to merge in, e.g. origin/main") + parser.add_argument("--message", required=True, help="the merge commit's message") + args = parser.parse_args(argv) + + result = sync(runner, args.base, args.message) + if result.outcome == MERGED: + print(f"Merged {args.base}.") + return 0 + if result.outcome == MERGED_AS_HELD: + print( + f"::notice::{args.base} conflicted on {', '.join(result.conflicts)}, but staging already holds " + f"every change it brings ({'; '.join(result.held)}). Recorded the merge and kept staging's content." + ) + return 0 + print( + f"::error::{args.base} conflicts with staging on {', '.join(result.conflicts)}. " + f"Staging lacks {'; '.join(result.lacking) or 'no commit git cherry can name'}. " + "A person resolves it: see the README's sync section." + ) + return 1 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tests/test_sync_staging.py b/tests/test_sync_staging.py new file mode 100644 index 0000000..0eebc02 --- /dev/null +++ b/tests/test_sync_staging.py @@ -0,0 +1,178 @@ +"""Unit tests for the main-to-staging merge (``scripts/sync_staging.py``). + +Each test builds the history in a scratch repository and runs real git. The case +that matters is 17 Nov's: a hotfix lands on staging first, the same change is +cherry-picked onto main, and staging then edits the line next to it. A plain +`git merge` conflicts there, and the old step stopped on it. A merge that keeps +staging's content is only safe when staging holds every change main brings, so +the mixed case must still fail. +""" + +import importlib.util +import subprocess +import sys +from pathlib import Path + +import pytest + +_PATH = Path(__file__).resolve().parents[1] / "scripts" / "sync_staging.py" +_spec = importlib.util.spec_from_file_location("sync_staging", _PATH) +sync_staging = importlib.util.module_from_spec(_spec) +# A dataclass looks its module up in sys.modules while it is defined. +sys.modules["sync_staging"] = sync_staging +_spec.loader.exec_module(sync_staging) + +IMPORTS = "import a\nimport urls_v1\nimport b\n" + + +class Repo: + def __init__(self, path: Path): + self.path = path + self.git("init", "-q", "-b", "main") + self.git("config", "user.email", "test@example.com") + self.git("config", "user.name", "Release Bot") + self.git("config", "commit.gpgsign", "false") + self.write("settings.js", IMPORTS) + self.write("other.js", "one\n") + self.commit("initial") + self.git("branch", "staging") + + def git(self, *args: str, check: bool = True) -> str: + result = subprocess.run(["git", *args], cwd=self.path, capture_output=True, text=True, check=check) + return result.stdout.strip() + + def write(self, name: str, text: str) -> None: + (self.path / name).write_text(text) + + def read(self, name: str) -> str: + return (self.path / name).read_text() + + def commit(self, subject: str) -> str: + self.git("add", "-A") + self.git("commit", "-q", "-m", subject) + return self.git("rev-parse", "HEAD") + + def checkout(self, branch: str) -> None: + self.git("checkout", "-q", branch) + + def cherry_pick_to_main(self, sha: str) -> None: + self.checkout("main") + self.git("cherry-pick", "-x", sha) + + def runner(self, argv): + return subprocess.run(list(argv), cwd=self.path, capture_output=True, text=True, check=False) + + +def add_greeting_on_staging(repo: Repo) -> str: + """The hotfix's staging PR: add an import above the urls line.""" + repo.checkout("staging") + repo.write("settings.js", "import a\nimport greeting\nimport urls_v1\nimport b\n") + return repo.commit("fix: translate the greeting") + + +def edit_next_line_on_staging(repo: Repo) -> None: + """A later staging PR rewrites the line under the hotfix's import.""" + repo.checkout("staging") + repo.write("settings.js", "import a\nimport greeting\nimport urls_v2\nimport b\n") + repo.commit("feat: api keys in the menu") + + +@pytest.fixture +def repo(tmp_path): + return Repo(tmp_path) + + +def test_a_hotfix_staging_already_holds_lands_and_keeps_staging_content(repo): + fix = add_greeting_on_staging(repo) + repo.cherry_pick_to_main(fix) + edit_next_line_on_staging(repo) + repo.checkout("staging") + staging_tree = repo.git("rev-parse", "HEAD^{tree}") + + # The step before this change: a plain merge, which conflicts. + assert subprocess.run( + ["git", "merge", "--no-ff", "main", "-m", "x"], cwd=repo.path, capture_output=True + ).returncode != 0 + repo.git("merge", "--abort") + + result = sync_staging.sync(repo.runner, "main", "Sync main into staging") + + assert result.outcome == sync_staging.MERGED_AS_HELD + assert result.conflicts == ("settings.js",) + assert [line.split(" ", 1)[1] for line in result.held] == ["fix: translate the greeting"] + assert repo.git("rev-parse", "HEAD^{tree}") == staging_tree + assert subprocess.run(["git", "merge-base", "--is-ancestor", "main", "HEAD"], cwd=repo.path).returncode == 0 + assert repo.git("log", "-1", "--format=%P").count(" ") == 1 # a real two-parent merge + + +def test_a_conflict_on_a_change_staging_lacks_fails_and_leaves_staging_unchanged(repo): + fix = add_greeting_on_staging(repo) + repo.cherry_pick_to_main(fix) + # Main also gets a change staging never saw, on the same lines. + repo.write("settings.js", "import a\nimport greeting\nimport urls_main_only\nimport b\n") + repo.commit("fix: main-only change") + edit_next_line_on_staging(repo) + repo.checkout("staging") + before = repo.git("rev-parse", "HEAD") + + result = sync_staging.sync(repo.runner, "main", "Sync main into staging") + + assert result.outcome == sync_staging.NEEDS_A_PERSON + assert result.conflicts == ("settings.js",) + assert [line.split(" ", 1)[1] for line in result.lacking] == ["fix: main-only change"] + assert repo.git("rev-parse", "HEAD") == before + assert repo.git("status", "--porcelain") == "" + + +def test_a_conflict_with_a_merge_commit_on_main_fails(repo): + fix = add_greeting_on_staging(repo) + repo.checkout("main") + repo.git("checkout", "-q", "-b", "hotfix") + repo.git("cherry-pick", "-x", fix) + repo.checkout("main") + repo.git("merge", "-q", "--no-ff", "hotfix", "-m", "Merge hotfix") + edit_next_line_on_staging(repo) + repo.checkout("staging") + + result = sync_staging.sync(repo.runner, "main", "Sync main into staging") + + assert result.outcome == sync_staging.NEEDS_A_PERSON + assert any("merge commit" in line for line in result.lacking) + + +def test_a_clean_merge_is_an_ordinary_merge(repo): + repo.checkout("main") + repo.write("other.js", "one\ntwo\n") + repo.commit("fix: other file") + edit_next_line_on_staging(repo) + repo.checkout("staging") + + result = sync_staging.sync(repo.runner, "main", "Sync main into staging") + + assert result.outcome == sync_staging.MERGED + assert repo.read("other.js") == "one\ntwo\n" + assert repo.read("settings.js") == "import a\nimport greeting\nimport urls_v2\nimport b\n" + + +def test_main_exits_1_with_an_error_naming_files_and_commits(repo, capsys): + fix = add_greeting_on_staging(repo) + repo.cherry_pick_to_main(fix) + repo.write("settings.js", "import a\nimport greeting\nimport urls_main_only\nimport b\n") + repo.commit("fix: main-only change") + edit_next_line_on_staging(repo) + repo.checkout("staging") + + code = sync_staging.main(["--base", "main", "--message", "Sync main into staging"], runner=repo.runner) + + out = capsys.readouterr().out + assert code == 1 + assert out.startswith("::error::") + assert "settings.js" in out + assert "fix: main-only change" in out + + +def test_a_merge_that_fails_without_conflicts_raises(repo): + repo.checkout("staging") + + with pytest.raises(RuntimeError, match="no-such-ref"): + sync_staging.sync(repo.runner, "no-such-ref", "Sync") From bde6ae8082bbe71c157e0dda30f5689d0dde8412 Mon Sep 17 00:00:00 2001 From: Lucas Koontz Date: Mon, 16 Nov 2026 23:44:55 -0600 Subject: [PATCH 2/2] fix(sync): match only staging's commits since the last sync, and keep staging's content only when main's clean changes are already there (ENG-4379) --- README.md | 2 +- scripts/sync_staging.py | 129 +++++++++++++++++------- tests/test_sync_staging.py | 195 ++++++++++++++++++++++++++++++++++--- 3 files changed, 278 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index 587a63e..f191c48 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,7 @@ concurrency: The sync is idempotent (it exits 0 when `staging` already contains `main`), so whichever run loses the race no-ops. -**A hotfix staging already holds lands on its own.** Both syncs merge through `scripts/sync_staging.py`. A hotfix lands on staging first and is then cherry-picked onto main, so staging usually holds the change before main's push fires the sync. A plain `git merge` still conflicts when staging has since edited a neighboring line. On a conflict, the script asks `git cherry` whether every commit main gained since the merge base has an equal patch on staging. If so, it records the merge with `-s ours`, which keeps staging's content and makes `main` an ancestor of `staging`; the run log carries a notice naming the held commits. If main brings any change staging lacks, or a merge commit `git cherry` cannot compare, the run fails and its error names the conflicting files and the missing commits. A person then resolves it. Staging takes squash merges only, so a hand-made merge PR into staging does not make `main` an ancestor. Instead, open a PR on `main` that brings the conflicting lines in line with staging, and the next sync merges cleanly. +**A hotfix staging already holds lands on its own.** Both syncs merge through `scripts/sync_staging.py`. A hotfix lands on staging first and is then cherry-picked onto main, so staging usually holds the change before main's push fires the sync. A plain `git merge` still conflicts when staging has since edited a neighboring line. On a conflict, the script records the merge with `-s ours`, which keeps staging's content and makes `main` an ancestor of `staging`, only when two checks pass. First, every commit main gained since the merge base has an equal patch among the staging commits made since main last came in. Releases squash staging into main, so older staging commits never join main's history, and one from releases ago can't vouch for a change staging has since undone. Second, main's changes that merge cleanly leave staging's tree as it is, which catches a change staging reverted. The run log then carries a notice naming the held commits, and the merge commit's message names them too. If main brings a change staging lacks, a merge commit with no patch to compare, or a clean change staging's tree doesn't have, the run fails. Its error names the conflicting files, the missing commits and the files that differ, and a person resolves it. Staging takes squash merges only, so a hand-made merge PR into staging does not make `main` an ancestor. First bring each commit staging lacks onto staging: cherry-pick it onto a branch off `staging` and squash-merge that PR. When the cherry-pick applies with no edits, the next sync finds the equal patch and lands on its own. When it needed edits, the sync still conflicts. Then open a PR on `main` that sets only the conflicting lines to staging's version, after checking that those lines use nothing that exists only on staging, and the next sync merges cleanly. For a file the error says differs, decide which side is right: land main's change on staging again, or revert it on `main` too. The chain is event-driven: `Staging Freeze` finishing fires the release-PR workflow via `workflow_run`; merging that PR fires `Staging Unfreeze`. The diff --git a/scripts/sync_staging.py b/scripts/sync_staging.py index 14f99e8..e5d1fd7 100644 --- a/scripts/sync_staging.py +++ b/scripts/sync_staging.py @@ -7,16 +7,26 @@ and the line under it differs. Nobody can land that merge by hand on a squash-only staging, so the train stays red until someone works around it. -So on a conflict this checks whether every commit main gained since the merge -base already has an equal patch on staging (`git cherry` marks each one `-`). If -so, it records main as merged and keeps staging's content (`merge -s ours`): -staging holds every change main brings, so nothing is lost. If main holds any -change staging lacks, or a merge commit whose content `git cherry` cannot -compare, it fails as before, and names the conflicting files and the commits -staging lacks. - -Run it inside the staging checkout, with the merge identity configured. Exit 0 -means HEAD is a new merge commit ready to push; exit 1 means a person must merge. +So on a conflict this records main as merged and keeps staging's content +(`merge -s ours`), but only when two checks show staging holds every change +main brings: + +1. Every commit main gained since the merge base has an equal patch among the + staging commits made since main last came in. Releases squash staging into + main, so older staging commits never join main's history. Comparing with + them would let a commit from releases ago vouch for a change staging has + since undone. +2. Main's changes that merge cleanly change nothing: with each conflict + resolved to staging's side, the merge gives staging's own tree. This catches + a change staging applied and then reverted, which still has its equal patch. + +If main holds a change staging lacks, a merge commit with no patch to compare, +or a clean change staging's tree doesn't have, it fails as before, and names the +conflicting files, the commits staging lacks and the files that differ. + +Run it inside the staging checkout, with the merge identity configured and git +2.43 or later (`merge-tree -X`). Exit 0 means HEAD is a new merge commit ready +to push; exit 1 means a person must merge. """ from __future__ import annotations @@ -25,21 +35,35 @@ import subprocess import sys from dataclasses import dataclass +from enum import Enum from typing import Callable, Sequence Runner = Callable[[Sequence[str]], "subprocess.CompletedProcess[str]"] -MERGED = "merged" # an ordinary merge, no conflict -MERGED_AS_HELD = "merged-as-held" # a conflict on changes staging already holds; staging's content kept -NEEDS_A_PERSON = "needs-a-person" # a conflict on a change staging lacks + +class Outcome(Enum): + MERGED = "merged" # an ordinary merge, no conflict + MERGED_AS_HELD = "merged-as-held" # a conflict on changes staging already holds; staging's content kept + NEEDS_A_PERSON = "needs-a-person" # a conflict on a change staging lacks + + +@dataclass(frozen=True) +class Commit: + sha: str + subject: str + + def __str__(self) -> str: + return f"{self.sha[:12]} {self.subject}" @dataclass(frozen=True) class SyncResult: - outcome: str + outcome: Outcome conflicts: tuple[str, ...] = () # files the plain merge conflicted on - held: tuple[str, ...] = () # " " of main commits staging already holds - lacking: tuple[str, ...] = () # " " of main commits staging lacks + held: tuple[Commit, ...] = () # main commits with an equal patch among staging's commits since the last sync + lacking: tuple[Commit, ...] = () # main commits with none + merges: tuple[Commit, ...] = () # merge commits on main, which have no patch to compare + differs: tuple[str, ...] = () # files main's side would still change with every conflict resolved to staging's def _run(argv: Sequence[str]) -> "subprocess.CompletedProcess[str]": @@ -53,34 +77,58 @@ def _git(runner: Runner, *args: str) -> str: return result.stdout -def _subject(runner: Runner, sha: str) -> str: - return f"{sha[:12]} {_git(runner, 'log', '-1', '--format=%s', sha).strip()}" +def _commit(runner: Runner, sha: str) -> Commit: + return Commit(sha, _git(runner, "log", "-1", "--format=%s", sha).strip()) + + +def _files_main_would_change(runner: Runner, base_ref: str) -> tuple[str, ...]: + """Files whose content changes when main merges in with every conflict resolved to staging's side. + + `-s ours` drops all of main's side, its clean hunks included, so only an + empty answer lets it keep staging's content without losing anything. + """ + merged = runner(["git", "merge-tree", "--write-tree", "--name-only", "-X", "ours", "HEAD", base_ref]) + if merged.returncode not in (0, 1): + raise RuntimeError(f"git merge-tree HEAD {base_ref} failed: {merged.stderr.strip() or merged.stdout.strip()}") + tree, _, rest = merged.stdout.partition("\n") + if merged.returncode == 1: + # Conflicts `-X ours` can't settle, such as modify/delete: staging's side is not a whole answer. + return tuple(rest.split("\n\n", 1)[0].splitlines()) + return tuple(_git(runner, "diff", "--name-only", "HEAD", tree.strip()).splitlines()) def sync(runner: Runner, base_ref: str, message: str) -> SyncResult: merge = runner(["git", "merge", "--no-ff", base_ref, "-m", message]) if merge.returncode == 0: - return SyncResult(MERGED) + return SyncResult(Outcome.MERGED) - conflicts = tuple(_git(runner, "diff", "--name-only", "--diff-filter=U").split()) + conflicts = tuple(_git(runner, "diff", "--name-only", "--diff-filter=U").splitlines()) runner(["git", "merge", "--abort"]) if not conflicts: # The merge failed for some other reason (a dirty tree, a missing ref): not ours to judge. raise RuntimeError(f"git merge {base_ref} failed: {merge.stderr.strip() or merge.stdout.strip()}") merge_base = _git(runner, "merge-base", "HEAD", base_ref).strip() - merges = _git(runner, "rev-list", "--merges", f"{merge_base}..{base_ref}").split() - # `+ `: main commit with no equal patch on staging; `- `: staging holds it. - marks = [line.split() for line in _git(runner, "cherry", "HEAD", base_ref, merge_base).splitlines() if line] - held = tuple(_subject(runner, sha) for mark, sha in marks if mark == "-") - lacking = tuple(_subject(runner, sha) for mark, sha in marks if mark == "+") - lacking += tuple(f"{_subject(runner, sha)} (a merge commit, which cannot be compared)" for sha in merges) - - if lacking or not held: - return SyncResult(NEEDS_A_PERSON, conflicts=conflicts, held=held, lacking=lacking) + merges = tuple(_commit(runner, sha) for sha in _git(runner, "rev-list", "--merges", f"{merge_base}..{base_ref}").split()) + # Compare only with staging commits made since main last came in: those after the oldest merge + # on staging's first-parent line that contains the merge base, or after the merge base itself + # when staging has never merged main. + synced = _git(runner, "rev-list", "--first-parent", "--ancestry-path", "--merges", f"{merge_base}..HEAD").split() + since = synced[-1] if synced else merge_base + # `=`: a main commit with an equal patch on staging; `+`: one without. + marks = _git(runner, "rev-list", "--cherry-mark", "--right-only", "--no-merges", f"HEAD...{base_ref}", f"^{since}").split() + held = tuple(_commit(runner, mark[1:]) for mark in marks if mark[0] == "=") + lacking = tuple(_commit(runner, mark[1:]) for mark in marks if mark[0] == "+") + differs = _files_main_would_change(runner, base_ref) + + if lacking or merges or differs or not held: + return SyncResult( + Outcome.NEEDS_A_PERSON, conflicts=conflicts, held=held, lacking=lacking, merges=merges, differs=differs + ) - _git(runner, "merge", "--no-ff", "-s", "ours", base_ref, "-m", message) - return SyncResult(MERGED_AS_HELD, conflicts=conflicts, held=held) + kept = "Kept staging's content: staging already holds " + "; ".join(str(commit) for commit in held) + "." + _git(runner, "merge", "--no-ff", "-s", "ours", base_ref, "-m", message, "-m", kept) + return SyncResult(Outcome.MERGED_AS_HELD, conflicts=conflicts, held=held) def main(argv: Sequence[str] | None = None, runner: Runner = _run) -> int: @@ -90,18 +138,27 @@ def main(argv: Sequence[str] | None = None, runner: Runner = _run) -> int: args = parser.parse_args(argv) result = sync(runner, args.base, args.message) - if result.outcome == MERGED: + if result.outcome is Outcome.MERGED: print(f"Merged {args.base}.") return 0 - if result.outcome == MERGED_AS_HELD: + if result.outcome is Outcome.MERGED_AS_HELD: print( f"::notice::{args.base} conflicted on {', '.join(result.conflicts)}, but staging already holds " - f"every change it brings ({'; '.join(result.held)}). Recorded the merge and kept staging's content." + f"every change it brings ({'; '.join(map(str, result.held))}). Recorded the merge and kept staging's content." ) return 0 + + reasons = [] + if result.lacking: + reasons.append(f"Staging lacks {'; '.join(map(str, result.lacking))}.") + if result.merges: + reasons.append(f"Main has merge commits, which have no patch to compare: {'; '.join(map(str, result.merges))}.") + if result.differs: + reasons.append(f"Main's side would still change staging's {', '.join(result.differs)}.") + if not reasons: + reasons.append("Staging has no patch equal to any of main's commits.") print( - f"::error::{args.base} conflicts with staging on {', '.join(result.conflicts)}. " - f"Staging lacks {'; '.join(result.lacking) or 'no commit git cherry can name'}. " + f"::error::{args.base} conflicts with staging on {', '.join(result.conflicts)}. {' '.join(reasons)} " "A person resolves it: see the README's sync section." ) return 1 diff --git a/tests/test_sync_staging.py b/tests/test_sync_staging.py index 0eebc02..5afa8b7 100644 --- a/tests/test_sync_staging.py +++ b/tests/test_sync_staging.py @@ -5,7 +5,8 @@ cherry-picked onto main, and staging then edits the line next to it. A plain `git merge` conflicts there, and the old step stopped on it. A merge that keeps staging's content is only safe when staging holds every change main brings, so -the mixed case must still fail. +the mixed case must still fail, and so must a change whose equal patch staging +made releases ago or has since reverted. """ import importlib.util @@ -22,6 +23,8 @@ sys.modules["sync_staging"] = sync_staging _spec.loader.exec_module(sync_staging) +Outcome = sync_staging.Outcome + IMPORTS = "import a\nimport urls_v1\nimport b\n" @@ -34,6 +37,7 @@ def __init__(self, path: Path): self.git("config", "commit.gpgsign", "false") self.write("settings.js", IMPORTS) self.write("other.js", "one\n") + self.write("flags.js", "NEW_BILLING=true\n") self.commit("initial") self.git("branch", "staging") @@ -59,6 +63,14 @@ def cherry_pick_to_main(self, sha: str) -> None: self.checkout("main") self.git("cherry-pick", "-x", sha) + def release(self) -> None: + """Squash staging into main, as the release PR does, then sync main back into staging.""" + self.checkout("main") + self.git("merge", "-q", "--squash", "staging") + self.commit("release: staging -> main") + self.checkout("staging") + self.git("merge", "-q", "--no-ff", "main", "-m", "Sync main back into staging after release") + def runner(self, argv): return subprocess.run(list(argv), cwd=self.path, capture_output=True, text=True, check=False) @@ -77,15 +89,39 @@ def edit_next_line_on_staging(repo: Repo) -> None: repo.commit("feat: api keys in the menu") +def hold_a_conflicting_hotfix(repo: Repo) -> None: + """17 Nov's shape: the hotfix on staging, its cherry-pick on main, then staging edits the next line.""" + fix = add_greeting_on_staging(repo) + repo.cherry_pick_to_main(fix) + edit_next_line_on_staging(repo) + + +def turn_billing_off_and_on_across_two_releases(repo: Repo) -> None: + """Staging turns a flag off, ships it, turns it back on, and ships that too.""" + repo.checkout("staging") + repo.write("flags.js", "NEW_BILLING=false\n") + repo.commit("chore: turn new billing off") + repo.release() + repo.checkout("staging") + repo.write("flags.js", "NEW_BILLING=true\n") + repo.commit("feat: new billing back on") + repo.release() + + +def turn_billing_off_on_main(repo: Repo) -> None: + """A production hotfix with the same patch as the old staging commit.""" + repo.checkout("main") + repo.write("flags.js", "NEW_BILLING=false\n") + repo.commit("hotfix: turn new billing off") + + @pytest.fixture def repo(tmp_path): return Repo(tmp_path) def test_a_hotfix_staging_already_holds_lands_and_keeps_staging_content(repo): - fix = add_greeting_on_staging(repo) - repo.cherry_pick_to_main(fix) - edit_next_line_on_staging(repo) + hold_a_conflicting_hotfix(repo) repo.checkout("staging") staging_tree = repo.git("rev-parse", "HEAD^{tree}") @@ -97,12 +133,29 @@ def test_a_hotfix_staging_already_holds_lands_and_keeps_staging_content(repo): result = sync_staging.sync(repo.runner, "main", "Sync main into staging") - assert result.outcome == sync_staging.MERGED_AS_HELD + assert result.outcome is Outcome.MERGED_AS_HELD assert result.conflicts == ("settings.js",) - assert [line.split(" ", 1)[1] for line in result.held] == ["fix: translate the greeting"] + assert [commit.subject for commit in result.held] == ["fix: translate the greeting"] assert repo.git("rev-parse", "HEAD^{tree}") == staging_tree assert subprocess.run(["git", "merge-base", "--is-ancestor", "main", "HEAD"], cwd=repo.path).returncode == 0 assert repo.git("log", "-1", "--format=%P").count(" ") == 1 # a real two-parent merge + assert repo.git("log", "-1", "--format=%s") == "Sync main into staging" + assert "fix: translate the greeting" in repo.git("log", "-1", "--format=%b") + + +def test_a_held_hotfix_lands_after_a_release(repo): + repo.checkout("staging") + repo.write("other.js", "one\ntwo\n") + repo.commit("feat: other file") + repo.release() + hold_a_conflicting_hotfix(repo) + repo.checkout("staging") + staging_tree = repo.git("rev-parse", "HEAD^{tree}") + + result = sync_staging.sync(repo.runner, "main", "Sync main into staging") + + assert result.outcome is Outcome.MERGED_AS_HELD + assert repo.git("rev-parse", "HEAD^{tree}") == staging_tree def test_a_conflict_on_a_change_staging_lacks_fails_and_leaves_staging_unchanged(repo): @@ -117,13 +170,101 @@ def test_a_conflict_on_a_change_staging_lacks_fails_and_leaves_staging_unchanged result = sync_staging.sync(repo.runner, "main", "Sync main into staging") - assert result.outcome == sync_staging.NEEDS_A_PERSON + assert result.outcome is Outcome.NEEDS_A_PERSON assert result.conflicts == ("settings.js",) - assert [line.split(" ", 1)[1] for line in result.lacking] == ["fix: main-only change"] + assert [commit.subject for commit in result.lacking] == ["fix: main-only change"] assert repo.git("rev-parse", "HEAD") == before assert repo.git("status", "--porcelain") == "" +def test_an_old_staging_commit_with_the_same_patch_does_not_vouch_for_a_main_change(repo): + turn_billing_off_and_on_across_two_releases(repo) + hold_a_conflicting_hotfix(repo) + turn_billing_off_on_main(repo) + repo.checkout("staging") + before = repo.git("rev-parse", "HEAD") + + result = sync_staging.sync(repo.runner, "main", "Sync main into staging") + + assert result.outcome is Outcome.NEEDS_A_PERSON + assert [commit.subject for commit in result.lacking] == ["hotfix: turn new billing off"] + assert repo.git("rev-parse", "HEAD") == before + + +def test_an_old_staging_commit_does_not_vouch_for_a_main_change_that_conflicts_too(repo): + turn_billing_off_and_on_across_two_releases(repo) + hold_a_conflicting_hotfix(repo) + # Staging adds a flag under the old one, so main's change to it conflicts and `-X ours` would hide it. + repo.checkout("staging") + repo.write("flags.js", "NEW_BILLING=true\nNEW_SEARCH=true\n") + repo.commit("feat: new search flag") + turn_billing_off_on_main(repo) + repo.checkout("staging") + + result = sync_staging.sync(repo.runner, "main", "Sync main into staging") + + assert result.outcome is Outcome.NEEDS_A_PERSON + assert result.conflicts == ("flags.js", "settings.js") + assert [commit.subject for commit in result.lacking] == ["hotfix: turn new billing off"] + assert result.differs == () + + +def test_a_change_staging_reverted_fails_although_its_patch_matches(repo): + repo.checkout("staging") + repo.write("flags.js", "NEW_BILLING=false\n") + off = repo.commit("chore: turn new billing off") + repo.cherry_pick_to_main(off) + repo.checkout("staging") + repo.git("revert", "--no-edit", off) + hold_a_conflicting_hotfix(repo) + repo.checkout("staging") + before = repo.git("rev-parse", "HEAD") + + result = sync_staging.sync(repo.runner, "main", "Sync main into staging") + + assert result.outcome is Outcome.NEEDS_A_PERSON + assert result.lacking == () + assert result.differs == ("flags.js",) + assert repo.git("rev-parse", "HEAD") == before + + +def test_a_conflict_ours_cannot_settle_needs_a_person(repo): + # Main gets a change staging made, then staging deletes the file: a modify/delete conflict. + repo.checkout("staging") + repo.write("other.js", "one\ntwo\n") + change = repo.commit("feat: other file") + repo.cherry_pick_to_main(change) + repo.checkout("staging") + repo.git("rm", "-q", "other.js") + repo.commit("chore: drop other file") + + result = sync_staging.sync(repo.runner, "main", "Sync main into staging") + + assert result.outcome is Outcome.NEEDS_A_PERSON + assert [commit.subject for commit in result.held] == ["feat: other file"] + assert result.differs == ("other.js",) + + +def test_a_deletion_staging_undid_needs_a_person(repo): + # Main gets a deletion staging made, then staging brings the file back edited: delete/modify. + # `-X ours` leaves staging's copy in the tree, so only the unsettled conflict shows the gap. + repo.checkout("staging") + repo.git("rm", "-q", "other.js") + drop = repo.commit("chore: drop other file") + repo.cherry_pick_to_main(drop) + repo.checkout("staging") + repo.write("other.js", "one\ntwo\n") + repo.commit("feat: other file is back") + hold_a_conflicting_hotfix(repo) + repo.checkout("staging") + + result = sync_staging.sync(repo.runner, "main", "Sync main into staging") + + assert result.outcome is Outcome.NEEDS_A_PERSON + assert not result.lacking + assert result.differs == ("other.js",) + + def test_a_conflict_with_a_merge_commit_on_main_fails(repo): fix = add_greeting_on_staging(repo) repo.checkout("main") @@ -136,8 +277,23 @@ def test_a_conflict_with_a_merge_commit_on_main_fails(repo): result = sync_staging.sync(repo.runner, "main", "Sync main into staging") - assert result.outcome == sync_staging.NEEDS_A_PERSON - assert any("merge commit" in line for line in result.lacking) + assert result.outcome is Outcome.NEEDS_A_PERSON + assert [commit.subject for commit in result.merges] == ["Merge hotfix"] + + +def test_a_conflicting_path_with_a_space_stays_one_name(repo): + repo.checkout("staging") + repo.write("app settings.js", "staging\n") + repo.commit("feat: staging's settings") + repo.checkout("main") + repo.write("app settings.js", "main\n") + repo.commit("fix: main's settings") + repo.checkout("staging") + + result = sync_staging.sync(repo.runner, "main", "Sync main into staging") + + assert result.outcome is Outcome.NEEDS_A_PERSON + assert result.conflicts == ("app settings.js",) def test_a_clean_merge_is_an_ordinary_merge(repo): @@ -149,7 +305,7 @@ def test_a_clean_merge_is_an_ordinary_merge(repo): result = sync_staging.sync(repo.runner, "main", "Sync main into staging") - assert result.outcome == sync_staging.MERGED + assert result.outcome is Outcome.MERGED assert repo.read("other.js") == "one\ntwo\n" assert repo.read("settings.js") == "import a\nimport greeting\nimport urls_v2\nimport b\n" @@ -171,6 +327,23 @@ def test_main_exits_1_with_an_error_naming_files_and_commits(repo, capsys): assert "fix: main-only change" in out +def test_main_names_the_files_a_reverted_change_would_alter(repo, capsys): + repo.checkout("staging") + repo.write("flags.js", "NEW_BILLING=false\n") + off = repo.commit("chore: turn new billing off") + repo.cherry_pick_to_main(off) + repo.checkout("staging") + repo.git("revert", "--no-edit", off) + hold_a_conflicting_hotfix(repo) + repo.checkout("staging") + + code = sync_staging.main(["--base", "main", "--message", "Sync main into staging"], runner=repo.runner) + + out = capsys.readouterr().out + assert code == 1 + assert "would still change staging's flags.js" in out + + def test_a_merge_that_fails_without_conflicts_raises(repo): repo.checkout("staging")