`atomic view split` over SplitView. Change specs and --last N resolve through the daemon's Log RPC; --switch composes SwitchView after. Returns Ok(false) when the call should fall back to the local path.
(args: &super::view::Split)
| 4328 | /// through the daemon's Log RPC; --switch composes SwitchView after. |
| 4329 | /// Returns Ok(false) when the call should fall back to the local path. |
| 4330 | pub fn view_split(args: &super::view::Split) -> CliResult<bool> { |
| 4331 | let Some(session) = Service::open()? else { |
| 4332 | return Ok(false); |
| 4333 | }; |
| 4334 | let from_view = args.from.clone().unwrap_or_else(|| { |
| 4335 | std::fs::read_to_string( |
| 4336 | crate::commands::find_repository_root() |
| 4337 | .expect("session resolved a repository") |
| 4338 | .join(".atomic") |
| 4339 | .join("current_view"), |
| 4340 | ) |
| 4341 | .map(|s| s.trim().to_string()) |
| 4342 | .unwrap_or_else(|_| "dev".to_string()) |
| 4343 | }); |
| 4344 | |
| 4345 | // Resolve the change set through the daemon's own Log: newest-first |
| 4346 | // entries carry the full hashes (--last takes the first N of that |
| 4347 | // list; prefixes match against the base32 form). |
| 4348 | let entries = log_entries(&session, &from_view)?; |
| 4349 | let mut hashes: Vec<[u8; 32]> = Vec::new(); |
| 4350 | if let Some(n) = args.last { |
| 4351 | if n == 0 { |
| 4352 | return Err(CliError::InvalidArgument { |
| 4353 | message: "--last must be greater than zero".to_string(), |
| 4354 | }); |
| 4355 | } |
| 4356 | if entries.len() < n { |
| 4357 | return Err(CliError::InvalidArgument { |
| 4358 | message: format!( |
| 4359 | "view '{from_view}' has only {} change(s); cannot split the last {n}", |
| 4360 | entries.len() |
| 4361 | ), |
| 4362 | }); |
| 4363 | } |
| 4364 | hashes.extend(entries.iter().take(n).filter_map(|e| { |
| 4365 | e.hash.as_ref().and_then(|h| { |
| 4366 | let bytes: [u8; 32] = h.value.clone().try_into().ok()?; |
| 4367 | Some(bytes) |
| 4368 | }) |
| 4369 | })); |
| 4370 | } else { |
| 4371 | if args.changes.is_empty() { |
| 4372 | return Err(CliError::InvalidArgument { |
| 4373 | message: "specify one or more changes to split, or use --last <N>".to_string(), |
| 4374 | }); |
| 4375 | } |
| 4376 | for spec in &args.changes { |
| 4377 | let hash = if let Some(found) = entries.iter().find(|e| { |
| 4378 | e.hash |
| 4379 | .as_ref() |
| 4380 | .and_then(|h| h.value.clone().try_into().ok()) |
| 4381 | .map(|bytes: [u8; 32]| Merkle(bytes).to_base32().starts_with(spec)) |
| 4382 | .unwrap_or(false) |
| 4383 | }) { |
| 4384 | let bytes: [u8; 32] = found |
| 4385 | .hash |
| 4386 | .as_ref() |
| 4387 | .expect("matched entry has a hash") |
no test coverage detected