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

Method run

atomic-cli/src/commands/view/switch.rs:105–199  ·  view source on GitHub ↗
(&self)

Source from the content-addressed store, hash-verified

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

Callers 4

test_run_without_nameFunction · 0.45

Calls 15

find_repository_rootFunction · 0.85
RepositoryClass · 0.85
print_successFunction · 0.85
print_errorFunction · 0.85
create_spinnerFunction · 0.85
finish_successFunction · 0.85
sync_git_head_to_viewFunction · 0.85
print_warningFunction · 0.85
as_refMethod · 0.80
current_viewMethod · 0.80
run_push_onMethod · 0.80
entriesMethod · 0.80

Tested by 4

test_run_without_nameFunction · 0.36