| 163 | |
| 164 | impl Command for Switch { |
| 165 | fn run(&self) -> CliResult<()> { |
| 166 | // Route the plain form through the daemon; --force/--stash stay |
| 167 | // local (the wire carries no bypass of the dirty-copy gate). |
| 168 | if crate::commands::rpc::view_switch(self)? { |
| 169 | return Ok(()); |
| 170 | } |
| 171 | |
| 172 | // Get the view name |
| 173 | let name = self |
| 174 | .name |
| 175 | .as_ref() |
| 176 | .ok_or_else(|| CliError::InvalidArgument { |
| 177 | message: "View name is required".to_string(), |
| 178 | })?; |
| 179 | |
| 180 | // Find the repository |
| 181 | let repo_root = find_repository_root()?; |
| 182 | |
| 183 | let mut repo = |
| 184 | Repository::open_for_workspace_transaction(&repo_root).map_err(|e| match e { |
| 185 | atomic_repository::RepositoryError::NotFound { path } => { |
| 186 | CliError::RepositoryNotFound { |
| 187 | searched_path: path.into(), |
| 188 | } |
| 189 | } |
| 190 | other => CliError::Repository(other), |
| 191 | })?; |
| 192 | let workspace = match repo |
| 193 | .begin_workspace_txn(WorkspaceTxnMode::Reconcile) |
| 194 | .map_err(CliError::Repository)? |
| 195 | { |
| 196 | WorkspaceTxnStart::Ready(workspace) => workspace, |
| 197 | WorkspaceTxnStart::Remediation(WorkspaceRemediation::Unanchored { |
| 198 | state: UnanchoredWorkspace::AtomicCheckpointDrift { desired_view, .. }, |
| 199 | .. |
| 200 | }) if desired_view == *name => enter_workspace(&mut repo, WorkspaceTxnMode::Force)?, |
| 201 | WorkspaceTxnStart::Remediation(remediation) => { |
| 202 | return Err(remediation_error(remediation)); |
| 203 | } |
| 204 | }; |
| 205 | let working_copy = workspace.working_copy(); |
| 206 | let desired_view = workspace.view().name.clone(); |
| 207 | |
| 208 | // A prior coordinated switch may have materialized this view but failed |
| 209 | // before Git/checkpoint alignment. In an active shadow repo, retry the |
| 210 | // projection even when Atomic already points at the requested view; do |
| 211 | // not print a false success while Git evidence is still stale. |
| 212 | if desired_view == name.as_str() { |
| 213 | drop(workspace); |
| 214 | let shadow_sync = |
| 215 | crate::commands::git::shadow::sync_git_head_to_view(&repo, &repo_root, name)?; |
| 216 | if !shadow_sync.is_synchronized() { |
| 217 | print_success(&format!("Already on view: {}", style_view(name))); |
| 218 | return Ok(()); |
| 219 | } |
| 220 | drop(repo); |
| 221 | checkpoint_shadow_switch(shadow_sync, &repo_root, name)?; |
| 222 | print_success(&format!("Already on view: {}", style_view(name))); |