(root: &Path, target: &str)
| 1399 | |
| 1400 | #[tracing::instrument(name = "switch", skip_all, fields(view = target))] |
| 1401 | fn switch_transaction(root: &Path, target: &str) -> CliResult<()> { |
| 1402 | let checkpoint = read_workspace_metadata(root)?.ok_or_else(|| { |
| 1403 | git_error( |
| 1404 | "shim switch requires an existing checkpoint; run 'atomic git shim reconcile' first", |
| 1405 | ) |
| 1406 | })?; |
| 1407 | let mut repo = Repository::open(root).map_err(CliError::from)?; |
| 1408 | |
| 1409 | // Shared workspace transaction: switch is a mutating boundary that owns |
| 1410 | // Git refs, the Git index, and the Atomic working copy together, so it |
| 1411 | // selects an explicit mode and retains WorkspaceTxn authority for the |
| 1412 | // entire body. The baseline and view come from the working-copy record. |
| 1413 | let workspace = enter_workspace(&mut repo, WorkspaceTxnMode::Reconcile)?; |
| 1414 | let working_copy = workspace.working_copy(); |
| 1415 | |
| 1416 | let git = open_git(root)?; |
| 1417 | let current = current_heads(&repo, working_copy, &git)?; |
| 1418 | // §7.5/CB-8A: an attached HEAD must name the current view; a detached |
| 1419 | // HEAD (Draft projection) must project exactly to it. |
| 1420 | let aligned_view = workspace_head_aligned(&repo, working_copy, &git)?.0; |
| 1421 | if aligned_view != current.view || !checkpoint_matches_snapshot(&checkpoint, ¤t) { |
| 1422 | return Err(git_error( |
| 1423 | "current Git and Atomic state does not match the shim checkpoint; reconcile before switching", |
| 1424 | )); |
| 1425 | } |
| 1426 | tracing::info!( |
| 1427 | from = %current.view, |
| 1428 | git_head = %current.git_head, |
| 1429 | "switch: checkpoint matches; planning the target" |
| 1430 | ); |
| 1431 | |
| 1432 | let target_state = repo |
| 1433 | .get_view_info(target) |
| 1434 | .map_err(CliError::from)? |
| 1435 | .state |
| 1436 | .to_string(); |
| 1437 | let current_paths = git_head_paths(&git)?; |
| 1438 | let target_paths = git_branch_paths(&git, target)?; |
| 1439 | plan_switch_collisions(root, ¤t_paths, &target_paths).map_err(git_error)?; |
| 1440 | tracing::info!( |
| 1441 | current_paths = current_paths.len(), |
| 1442 | target_paths = target_paths.len(), |
| 1443 | "switch: no path collisions" |
| 1444 | ); |
| 1445 | let policy = conversion_policy(&git)?; |
| 1446 | let target_project = repo.project_tree(target, &policy).map_err(|error| { |
| 1447 | git_error(format!( |
| 1448 | "cannot build complete prospective projection for Atomic view '{target}': {error}" |
| 1449 | )) |
| 1450 | })?; |
| 1451 | require_target_branch_interpreted(&repo, &git, target, &target_project)?; |
| 1452 | drop(git); |
| 1453 | |
| 1454 | // After collision-specific checks, require the full clean/equality |
| 1455 | // invariant. The already-open Atomic handle is reused (redb admits one |
| 1456 | // open per process); the Git handle reopens for the verifier. |
| 1457 | { |
| 1458 | let git = open_git(root)?; |
no test coverage detected