Something went wrong. Try again.
Integrated repository, worktree, git stacker, and project manager
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363from __future__ import annotations
from pathlib import Path
import pytest
from project_manager.pool import slot as slot_modfrom project_manager.stacker import git as stacker_gitfrom project_manager.stacker.models import ( OperationState, ParentLocator, SelectorTarget, WorktreeInit,)from project_manager.stacker.service import StackerService
from .conftest import commit_file
def _init_on_main( service: StackerService, repo_name: str, slot: slot_mod.Slot, branch: str) -> None: """Create a tracked branch off main in `slot`.""" service.init_new_branch( WorktreeInit( repo_name=repo_name, worktree_path=slot.path, branch=branch, parent=ParentLocator(repo_name=repo_name, branch="main"), ) )
def _head(path: Path) -> str: return stacker_git.rev_parse(path, "HEAD")
def _count_commits(path: Path, revspec: str) -> int: return stacker_git.rev_count(path, revspec)
# ---------------------------------------------------------------------------# Case 1: ff absorb — parent at child's base, child has commits.# ---------------------------------------------------------------------------
def test_absorb_ff_advances_trunk_and_leaves_child_alone( stacker_repo: tuple[str, Path], three_slots: list[slot_mod.Slot], service: StackerService,) -> None: repo_name, repo_path = stacker_repo feature_slot = three_slots[0] _init_on_main(service, repo_name, feature_slot, "feature") commit_file(feature_slot.path, "a.txt", "a\n", "feat: a") commit_file(feature_slot.path, "b.txt", "b\n", "feat: b") child_head_before = _head(feature_slot.path) main_before = _head(repo_path)
result = service.absorb( SelectorTarget(repo_name=repo_name, branch="feature"), )
assert "Absorb complete." in result # Main advanced by exactly the two cherry-picked commits. main_after = _head(repo_path) assert main_after != main_before assert _count_commits(repo_path, f"{main_before}..{main_after}") == 2 # Child unchanged. assert _head(feature_slot.path) == child_head_before # No lingering operation. assert service.db.get_operation(repo_name) is None # Trunk isn't tracked, so no row for main should be created. assert service.db.get_branch(repo_name, "main") is None
# ---------------------------------------------------------------------------# Case 2: parent has diverged (no conflict) — parent gets its own commits# plus the cherry-picked child commits.# ---------------------------------------------------------------------------
def test_absorb_with_diverged_parent_no_conflict( stacker_repo: tuple[str, Path], three_slots: list[slot_mod.Slot], service: StackerService,) -> None: repo_name, repo_path = stacker_repo feature_slot = three_slots[0] _init_on_main(service, repo_name, feature_slot, "feature") commit_file(feature_slot.path, "child-only.txt", "c\n", "feat: c")
# Parent advances on an unrelated file — no conflict expected. main_bump = commit_file(repo_path, "main-only.txt", "m\n", "main: bump")
result = service.absorb( SelectorTarget(repo_name=repo_name, branch="feature"), )
assert "Absorb complete." in result main_after = _head(repo_path) # main_bump is still reachable from main_after (parent's own commit kept). assert stacker_git.is_ancestor(repo_path, main_bump, main_after) # The cherry-picked child commit is now on main (new SHA, same file). assert (repo_path / "child-only.txt").read_text() == "c\n"
# ---------------------------------------------------------------------------# Case 3: genuine conflict during absorb — pauses, abort rolls back.# ---------------------------------------------------------------------------
def test_absorb_conflict_pauses_and_abort_resets_parent( stacker_repo: tuple[str, Path], three_slots: list[slot_mod.Slot], service: StackerService,) -> None: repo_name, repo_path = stacker_repo feature_slot = three_slots[0] _init_on_main(service, repo_name, feature_slot, "feature") commit_file(feature_slot.path, "shared.txt", "child version\n", "feat: shared")
# Parent writes a different version of the same file. commit_file(repo_path, "shared.txt", "main version\n", "main: shared") main_before = _head(repo_path)
result = service.absorb( SelectorTarget(repo_name=repo_name, branch="feature"), ) assert "paused" in result.lower() op = service.db.get_operation(repo_name) assert op is not None assert op.op_type == "local_absorb" assert op.status == "paused"
abort_result = service.abort_operation(repo_name) assert "Aborted" in abort_result assert service.db.get_operation(repo_name) is None # Parent reset to exactly where it was before absorb started. assert _head(repo_path) == main_before
# ---------------------------------------------------------------------------# Case 4: --continue drives a paused absorb to completion.# ---------------------------------------------------------------------------
def test_absorb_continue_after_manual_resolution( stacker_repo: tuple[str, Path], three_slots: list[slot_mod.Slot], service: StackerService,) -> None: repo_name, repo_path = stacker_repo feature_slot = three_slots[0] _init_on_main(service, repo_name, feature_slot, "feature") commit_file(feature_slot.path, "shared.txt", "child version\n", "feat: shared") commit_file(repo_path, "shared.txt", "main version\n", "main: shared")
paused = service.absorb( SelectorTarget(repo_name=repo_name, branch="feature"), ) assert "paused" in paused.lower()
# User resolves by picking the child version in the parent slot. (repo_path / "shared.txt").write_text("child version\n") stacker_git.git(repo_path, "add", "shared.txt")
finished = service.continue_operation(repo_name) assert "Absorb complete." in finished assert service.db.get_operation(repo_name) is None
# ---------------------------------------------------------------------------# Case 5: nothing to absorb short-circuits without touching state.# ---------------------------------------------------------------------------
def test_absorb_nothing_to_do( stacker_repo: tuple[str, Path], three_slots: list[slot_mod.Slot], service: StackerService,) -> None: repo_name, repo_path = stacker_repo feature_slot = three_slots[0] _init_on_main(service, repo_name, feature_slot, "feature") main_before = _head(repo_path)
result = service.absorb( SelectorTarget(repo_name=repo_name, branch="feature"), ) assert "Nothing to absorb" in result assert _head(repo_path) == main_before assert service.db.get_operation(repo_name) is None
# ---------------------------------------------------------------------------# Case 6: absorb into trunk leaves no DB row for trunk.# ---------------------------------------------------------------------------
def test_absorb_into_trunk_does_not_track_trunk( stacker_repo: tuple[str, Path], three_slots: list[slot_mod.Slot], service: StackerService,) -> None: repo_name, _repo_path = stacker_repo feature_slot = three_slots[0] _init_on_main(service, repo_name, feature_slot, "feature") commit_file(feature_slot.path, "f.txt", "f\n", "feat: f")
service.absorb(SelectorTarget(repo_name=repo_name, branch="feature"))
# Trunk must remain untracked — absorb never creates a TrackedBranch row # for main just because commits landed on it. assert service.db.get_branch(repo_name, "main") is None
# ---------------------------------------------------------------------------# Case 7: current branch is not tracked.# ---------------------------------------------------------------------------
def test_absorb_rejects_untracked_branch( stacker_repo: tuple[str, Path], service: StackerService,) -> None: repo_name, _ = stacker_repo with pytest.raises(stacker_git.GitError): service.absorb(SelectorTarget(repo_name=repo_name, branch="nope"))
# ---------------------------------------------------------------------------# Case 8: another op is already active.# ---------------------------------------------------------------------------
def test_absorb_rejects_when_op_active( stacker_repo: tuple[str, Path], three_slots: list[slot_mod.Slot], service: StackerService,) -> None: repo_name, _ = stacker_repo feature_slot = three_slots[0] _init_on_main(service, repo_name, feature_slot, "feature") commit_file(feature_slot.path, "f.txt", "f\n", "feat: f")
service.db.put_operation( OperationState( repo_name=repo_name, op_type="local_sync", status="paused", branch="feature", ) )
with pytest.raises(stacker_git.GitError, match="Another stacker operation"): service.absorb(SelectorTarget(repo_name=repo_name, branch="feature"))
# ---------------------------------------------------------------------------# Case 9: trunk has sibling tracked children — absorb on one doesn't# invalidate the sibling; sync on both converges cleanly.# ---------------------------------------------------------------------------
def test_absorb_leaves_sibling_tracked_children_intact( stacker_repo: tuple[str, Path], three_slots: list[slot_mod.Slot], service: StackerService,) -> None: repo_name, repo_path = stacker_repo a_slot, b_slot = three_slots[0], three_slots[1] _init_on_main(service, repo_name, a_slot, "a") _init_on_main(service, repo_name, b_slot, "b") commit_file(a_slot.path, "a.txt", "a\n", "a: first") commit_file(b_slot.path, "b.txt", "b\n", "b: first")
b_row_before = service.db.get_branch(repo_name, "b") assert b_row_before is not None main_before = _head(repo_path)
service.absorb(SelectorTarget(repo_name=repo_name, branch="a"))
# Main advanced. assert _head(repo_path) != main_before # Sibling B's row is byte-for-byte unchanged. b_row_after = service.db.get_branch(repo_name, "b") assert b_row_after is not None assert b_row_after == b_row_before # Sibling B's managed_base is still a reachable ancestor of new main. assert stacker_git.is_ancestor( repo_path, b_row_after.managed_base_commit, _head(repo_path), )
# Sync on B rebases it onto new main. Use skip_descendants so the walk # stays on B itself (B has no descendants but this keeps the scope tight). sync_b = service.sync(SelectorTarget(repo_name=repo_name, branch="b")) assert "Sync complete." in sync_b
# Sync on A drops all its (now-duplicated) commits via the patch-id filter. sync_a = service.sync(SelectorTarget(repo_name=repo_name, branch="a")) assert "Sync complete." in sync_a a_after = service.db.get_branch(repo_name, "a") assert a_after is not None # A's managed base has caught up to the new main tip. assert a_after.managed_base_commit == _head(repo_path)
# ---------------------------------------------------------------------------# Case 10: sync drops patch-id duplicates silently (the new filter).# Guards against a future regression where sync_plan is reverted to a plain# rev-list and the absorb → sync flow starts prompting for conflicts.# ---------------------------------------------------------------------------
def test_sync_drops_patchid_duplicates_without_pause( stacker_repo: tuple[str, Path], three_slots: list[slot_mod.Slot], service: StackerService,) -> None: repo_name, repo_path = stacker_repo feature_slot = three_slots[0] _init_on_main(service, repo_name, feature_slot, "feature") commit_file(feature_slot.path, "x.txt", "x\n", "feat: x")
# Mimic the post-absorb shape: the same patch lands on main under a # different SHA. git cherry-pick on main via the pm repo (which has main # checked out) is the simplest way to fake this. feature_head = _head(feature_slot.path) stacker_git.git(repo_path, "cherry-pick", feature_head)
result = service.sync(SelectorTarget(repo_name=repo_name, branch="feature")) # Must complete without pausing — the patch-id filter should drop the # child's commit before git cherry-pick ever attempts it. assert "paused" not in result.lower() assert "Sync complete." in result
# ---------------------------------------------------------------------------# Case 11: the patch-id filter doesn't hide genuine content conflicts.# ---------------------------------------------------------------------------
def test_sync_still_surfaces_real_conflicts( stacker_repo: tuple[str, Path], three_slots: list[slot_mod.Slot], service: StackerService,) -> None: repo_name, repo_path = stacker_repo feature_slot = three_slots[0] _init_on_main(service, repo_name, feature_slot, "feature") commit_file(feature_slot.path, "shared.txt", "child side\n", "feat: shared")
# Different content on the same file — different patch-id, real conflict. commit_file(repo_path, "shared.txt", "main side\n", "main: shared")
result = service.sync(SelectorTarget(repo_name=repo_name, branch="feature")) assert "paused" in result.lower()
service.abort_operation(repo_name)