| 100 | |
| 101 | impl Command for Switch { |
| 102 | fn run(&self) -> CliResult<()> { |
| 103 | // Get the view name |
| 104 | let name = self |
| 105 | .name |
| 106 | .as_ref() |
| 107 | .ok_or_else(|| CliError::InvalidArgument { |
| 108 | message: "View name is required".to_string(), |
| 109 | })?; |
| 110 | |
| 111 | // Find the repository |
| 112 | let repo_root = find_repository_root()?; |
| 113 | let mut repo = Repository::open(&repo_root).map_err(|e| match e { |
| 114 | atomic_repository::RepositoryError::NotFound { path } => CliError::RepositoryNotFound { |
| 115 | searched_path: path.into(), |
| 116 | }, |
| 117 | other => CliError::Repository(other), |
| 118 | })?; |
| 119 | |
| 120 | // Check if we're already on this view |
| 121 | if repo.current_view() == name { |
| 122 | print_success(&format!("Already on view: {}", style_view(name))); |
| 123 | return Ok(()); |
| 124 | } |
| 125 | |
| 126 | // Block switch if working copy has unrecorded changes |
| 127 | if !self.force { |
| 128 | let status = repo |
| 129 | .status(atomic_repository::StatusOptions::default()) |
| 130 | .map_err(CliError::Repository)?; |
| 131 | |
| 132 | if !status.is_clean() { |
| 133 | if self.stash { |
| 134 | // Auto-stash: save changes before switching |
| 135 | let stash_cmd = crate::commands::stash::Stash::new() |
| 136 | .with_message(format!("Auto-stash before switching to {}", name)); |
| 137 | stash_cmd.run_push_on(&mut repo, None, false, false)?; |
| 138 | } else { |
| 139 | let dirty: Vec<String> = status |
| 140 | .entries() |
| 141 | .iter() |
| 142 | .filter(|e| e.status().is_dirty()) |
| 143 | .map(|e| format!(" {} {}", e.status().short_code(), e.path().display())) |
| 144 | .collect(); |
| 145 | |
| 146 | crate::output::print_error(&format!( |
| 147 | "Cannot switch views with unrecorded changes ({} file{}):", |
| 148 | dirty.len(), |
| 149 | if dirty.len() == 1 { "" } else { "s" }, |
| 150 | )); |
| 151 | for line in &dirty { |
| 152 | eprintln!("{}", line); |
| 153 | } |
| 154 | eprintln!(); |
| 155 | eprintln!("Use 'atomic record' to save changes, 'atomic view switch --stash' to stash them, or '--force' to discard them."); |
| 156 | return Err(CliError::InvalidArgument { |
| 157 | message: "Working copy has unrecorded changes".to_string(), |
| 158 | }); |
| 159 | } |