| 892 | } |
| 893 | |
| 894 | async fn switch_view( |
| 895 | &self, |
| 896 | request: Request<SwitchViewRequest>, |
| 897 | ) -> Result<Response<SwitchViewResponse>, Status> { |
| 898 | let request = request.into_inner(); |
| 899 | let handle = self.state.resolve(request.repository.as_ref().unwrap())?; |
| 900 | self.state.log_rpc("SwitchView", Some(&handle)); |
| 901 | let gate_handle = handle.clone(); |
| 902 | let _gate = gate_handle.exclusive().await; |
| 903 | let handle_for_task = handle.clone(); |
| 904 | let files_written = tokio::task::spawn_blocking(move || -> Result<u64, Status> { |
| 905 | let name = request.view.trim().to_string(); |
| 906 | let (mut repo, workspace) = handle_for_task.workspace_repository_for_switch( |
| 907 | atomic_repository::WorkspaceTxnMode::Reconcile, |
| 908 | Some(&name), |
| 909 | )?; |
| 910 | let working_copy = workspace.working_copy(); |
| 911 | if !repo.view_exists(&name).map_err(repository_error)? { |
| 912 | return Err(domain_status( |
| 913 | ErrorCode::View, |
| 914 | format!("view '{name}' not found"), |
| 915 | )); |
| 916 | } |
| 917 | // The CLI's safety gate, verbatim: refuse to switch away |
| 918 | // from a dirty working copy — unless the caller explicitly |
| 919 | // bypassed it (--force/--stash, an informed client decision). |
| 920 | if !request.bypass_dirty_check.unwrap_or(false) && workspace.view().name != name { |
| 921 | let status = repo |
| 922 | .status(working_copy, StatusOptions::default()) |
| 923 | .map_err(repository_error)?; |
| 924 | if !status.is_clean() { |
| 925 | return Err(domain_status( |
| 926 | ErrorCode::PreconditionFailed, |
| 927 | "working copy has unrecorded changes — record them or \ |
| 928 | switch with --force/--stash from the client", |
| 929 | )); |
| 930 | } |
| 931 | } |
| 932 | let result = repo |
| 933 | .switch_view(working_copy, &name) |
| 934 | .map_err(repository_error)?; |
| 935 | Ok(result.files_written as u64) |
| 936 | }) |
| 937 | .await |
| 938 | .map_err(|error| Status::internal(error.to_string()))??; |
| 939 | Ok(Response::new(SwitchViewResponse { |
| 940 | files_written, |
| 941 | meta: None, |
| 942 | })) |
| 943 | } |
| 944 | |
| 945 | async fn delete_view( |
| 946 | &self, |