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

Function view_split

atomic-cli/src/commands/rpc.rs:4290–4425  ·  view source on GitHub ↗

`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)

Source from the content-addressed store, hash-verified

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.
4290pub 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")

Callers 1

runMethod · 0.85

Calls 15

openFunction · 0.85
find_repository_rootFunction · 0.85
log_entriesFunction · 0.85
MerkleClass · 0.85
print_successFunction · 0.85
print_infoFunction · 0.85
view_switch_plainFunction · 0.85
extendMethod · 0.80
as_refMethod · 0.80
cloneMethod · 0.45
lenMethod · 0.45
iterMethod · 0.45

Tested by

no test coverage detected