`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)
| 4288 | /// through the daemon's Log RPC; --switch composes SwitchView after. |
| 4289 | /// Returns Ok(false) when the call should fall back to the local path. |
| 4290 | pub fn view_split(args: &super::view::Split) -> CliResult<bool> { |
| 4291 | let Some(session) = Service::open()? else { |
| 4292 | return Ok(false); |
| 4293 | }; |
| 4294 | let from_view = args.from.clone().unwrap_or_else(|| { |
| 4295 | std::fs::read_to_string( |
| 4296 | crate::commands::find_repository_root() |
| 4297 | .expect("session resolved a repository") |
| 4298 | .join(".atomic") |
| 4299 | .join("current_view"), |
| 4300 | ) |
| 4301 | .map(|s| s.trim().to_string()) |
| 4302 | .unwrap_or_else(|_| "dev".to_string()) |
| 4303 | }); |
| 4304 | |
| 4305 | // Resolve the change set through the daemon's own Log: newest-first |
| 4306 | // entries carry the full hashes (--last takes the first N of that |
| 4307 | // list; prefixes match against the base32 form). |
| 4308 | let entries = log_entries(&session, &from_view)?; |
| 4309 | let mut hashes: Vec<[u8; 32]> = Vec::new(); |
| 4310 | if let Some(n) = args.last { |
| 4311 | if n == 0 { |
| 4312 | return Err(CliError::InvalidArgument { |
| 4313 | message: "--last must be greater than zero".to_string(), |
| 4314 | }); |
| 4315 | } |
| 4316 | if entries.len() < n { |
| 4317 | return Err(CliError::InvalidArgument { |
| 4318 | message: format!( |
| 4319 | "view '{from_view}' has only {} change(s); cannot split the last {n}", |
| 4320 | entries.len() |
| 4321 | ), |
| 4322 | }); |
| 4323 | } |
| 4324 | hashes.extend(entries.iter().take(n).filter_map(|e| { |
| 4325 | e.hash.as_ref().and_then(|h| { |
| 4326 | let bytes: [u8; 32] = h.value.clone().try_into().ok()?; |
| 4327 | Some(bytes) |
| 4328 | }) |
| 4329 | })); |
| 4330 | } else { |
| 4331 | if args.changes.is_empty() { |
| 4332 | return Err(CliError::InvalidArgument { |
| 4333 | message: "specify one or more changes to split, or use --last <N>".to_string(), |
| 4334 | }); |
| 4335 | } |
| 4336 | for spec in &args.changes { |
| 4337 | let hash = if let Some(found) = entries.iter().find(|e| { |
| 4338 | e.hash |
| 4339 | .as_ref() |
| 4340 | .and_then(|h| h.value.clone().try_into().ok()) |
| 4341 | .map(|bytes: [u8; 32]| Merkle(bytes).to_base32().starts_with(spec)) |
| 4342 | .unwrap_or(false) |
| 4343 | }) { |
| 4344 | let bytes: [u8; 32] = found |
| 4345 | .hash |
| 4346 | .as_ref() |
| 4347 | .expect("matched entry has a hash") |
no test coverage detected