MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / switch_transaction

Function switch_transaction

atomic-cli/src/commands/git/shim.rs:1445–1580  ·  view source on GitHub ↗
(root: &Path, target: &str)

Source from the content-addressed store, hash-verified

1443
1444#[tracing::instrument(name = "switch", skip_all, fields(view = target))]
1445fn switch_transaction(root: &Path, target: &str) -> CliResult<()> {
1446 let checkpoint = read_workspace_metadata(root)?.ok_or_else(|| {
1447 git_error(
1448 "shim switch requires an existing checkpoint; run 'atomic git shim reconcile' first",
1449 )
1450 })?;
1451 let mut repo = Repository::open(root).map_err(CliError::from)?;
1452
1453 // Shared workspace transaction: switch is a mutating boundary that owns
1454 // Git refs, the Git index, and the Atomic working copy together, so it
1455 // selects an explicit mode and retains WorkspaceTxn authority for the
1456 // entire body. The baseline and view come from the working-copy record.
1457 let workspace = enter_workspace(&mut repo, WorkspaceTxnMode::Reconcile)?;
1458 let working_copy = workspace.working_copy();
1459
1460 let git = open_git(root)?;
1461 let current = current_heads(&repo, working_copy, &git)?;
1462 // §7.5/CB-8A: an attached HEAD must name the current view; a detached
1463 // HEAD (Draft projection) must project exactly to it.
1464 let aligned_view = workspace_head_aligned(&repo, working_copy, &git)?.0;
1465 if aligned_view != current.view || !checkpoint_matches_snapshot(&checkpoint, &current) {
1466 return Err(git_error(
1467 "current Git and Atomic state does not match the shim checkpoint; reconcile before switching",
1468 ));
1469 }
1470 tracing::info!(
1471 from = %current.view,
1472 git_head = %current.git_head,
1473 "switch: checkpoint matches; planning the target"
1474 );
1475
1476 let target_state = repo
1477 .get_view_info(target)
1478 .map_err(CliError::from)?
1479 .state
1480 .to_string();
1481 let current_paths = git_head_paths(&git)?;
1482 let target_paths = git_branch_paths(&git, target)?;
1483 plan_switch_collisions(root, &current_paths, &target_paths).map_err(git_error)?;
1484 tracing::info!(
1485 current_paths = current_paths.len(),
1486 target_paths = target_paths.len(),
1487 "switch: no path collisions"
1488 );
1489 let policy = conversion_policy(&git)?;
1490 let target_project = repo.project_tree(target, &policy).map_err(|error| {
1491 git_error(format!(
1492 "cannot build complete prospective projection for Atomic view '{target}': {error}"
1493 ))
1494 })?;
1495 require_target_branch_interpreted(&repo, &git, target, &target_project)?;
1496 drop(git);
1497
1498 // After collision-specific checks, require the full clean/equality
1499 // invariant. The already-open Atomic handle is reused (redb admits one
1500 // open per process); the Git handle reopens for the verifier.
1501 {
1502 let git = open_git(root)?;

Callers 1

switchFunction · 0.85

Calls 15

read_workspace_metadataFunction · 0.85
current_headsFunction · 0.85
workspace_head_alignedFunction · 0.85
git_head_pathsFunction · 0.85
git_branch_pathsFunction · 0.85
plan_switch_collisionsFunction · 0.85
conversion_policyFunction · 0.85
verify_withFunction · 0.85
require_clean_git_indexFunction · 0.85

Tested by

no test coverage detected