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

Function switch_transaction

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

Source from the content-addressed store, hash-verified

1399
1400#[tracing::instrument(name = "switch", skip_all, fields(view = target))]
1401fn 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, &current) {
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, &current_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)?;

Callers 1

switchFunction · 0.85

Calls 15

read_workspace_metadataFunction · 0.85
openFunction · 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