use crate::{FileWrite, Operation, OperationState, Store, StoreError, execute::read_current};
use serde::{Deserialize, Serialize};
use std::{
collections::BTreeSet,
path::{Path, PathBuf},
};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Absorbed {
pub operation: Operation,
pub absorbed: Vec<PathBuf>,
pub skipped: Vec<PathBuf>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum FileResult {
Unchanged,
Absorbed,
Skipped,
}
impl Store {
pub fn absorb(
&self,
id: &str,
paths: &[PathBuf],
purpose: &str,
) -> Result<Absorbed, StoreError> {
let mut operation = self.operation(id)?;
if operation.state != OperationState::Applied {
return Err(StoreError::Invalid(
"only an applied operation can absorb later changes".into(),
));
}
let written: Vec<PathBuf> = operation
.files
.iter()
.map(|file| file.path.clone())
.collect();
validate_paths(paths, &written)?;
let _locks = self.lock_paths(&written)?;
operation = self.operation(id)?;
let chosen: BTreeSet<&Path> = paths.iter().map(PathBuf::as_path).collect();
let results: Vec<(PathBuf, FileResult)> = operation
.files
.iter_mut()
.filter(|file| chosen.is_empty() || chosen.contains(file.path.as_path()))
.map(|file| Ok((file.path.clone(), self.absorb_file(file)?)))
.collect::<Result<_, StoreError>>()?;
let pick = |wanted: FileResult| -> Vec<PathBuf> {
results
.iter()
.filter(|(_, result)| *result == wanted)
.map(|(path, _)| path.clone())
.collect()
};
let (absorbed, skipped) = (pick(FileResult::Absorbed), pick(FileResult::Skipped));
record(&mut operation, &absorbed, purpose);
self.save_operation(&operation)?;
Ok(Absorbed {
operation,
absorbed,
skipped,
})
}
fn absorb_file(&self, file: &mut FileWrite) -> Result<FileResult, StoreError> {
let (current, current_hash) = read_current(&file.path)?;
let (Some(text), Some(_)) = (current, &file.after_hash) else {
return Ok(FileResult::Skipped);
};
if current_hash == file.after_hash {
return Ok(FileResult::Unchanged);
}
file.after_hash = Some(self.put_blob(&text)?);
Ok(FileResult::Absorbed)
}
}
fn validate_paths(paths: &[PathBuf], written: &[PathBuf]) -> Result<(), StoreError> {
if paths.is_empty() {
return Ok(());
}
let known: BTreeSet<&Path> = written.iter().map(PathBuf::as_path).collect();
if let Some(unknown) = paths.iter().find(|path| !known.contains(path.as_path())) {
return Err(StoreError::Invalid(format!(
"absorb: `{}` is not a file this operation wrote",
unknown.display()
)));
}
Ok(())
}
fn record(operation: &mut Operation, absorbed: &[PathBuf], purpose: &str) {
if absorbed.is_empty() {
return;
}
let mut known: BTreeSet<&Path> = operation.absorbed.iter().map(PathBuf::as_path).collect();
let added: Vec<_> = absorbed
.iter()
.filter(|path| known.insert(path.as_path()))
.cloned()
.collect();
operation.absorbed.extend(added);
operation.absorb_purpose = Some(purpose.to_string());
}