use std::collections::BTreeSet;
use std::io::Read;
use nibkeep_base::{EntityId, Hash, SpaceId};
use super::history::{READ_LIMIT, TreePoint};
use super::index::Version;
use super::walk::open_found;
use super::{Change, TreeState, version_of};
use crate::NodeError;
use crate::library::Library;
const TEXT_HEAD: usize = 8_000;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FileDiff {
pub path: String,
pub change: Change,
pub patch: Option<String>,
pub binary: bool,
pub secret: bool,
}
enum Side<'a> {
Nothing,
Version(&'a Version),
Copy(&'a super::walk::Found),
}
impl Library {
pub fn tree_diff(
&self,
space: SpaceId,
tree: EntityId,
between: Option<(TreePoint, TreePoint)>,
) -> Result<Vec<FileDiff>, NodeError> {
self.tree_diff_with(space, tree, between, false)
}
pub fn tree_diff_with(
&self,
space: SpaceId,
tree: EntityId,
between: Option<(TreePoint, TreePoint)>,
secrets: bool,
) -> Result<Vec<FileDiff>, NodeError> {
let mut diffs = match between {
Some((from, to)) => {
let before = self.tree_state_at(space, tree, from)?;
let after = self.tree_state_at(space, tree, to)?;
self.diff_states(space, (&before, &after), secrets)?
}
None => self.diff_copy(space, tree, secrets)?,
};
diffs.sort_by(|a, b| a.path.cmp(&b.path));
Ok(diffs)
}
pub fn revision_diff(
&self,
space: SpaceId,
revision: EntityId,
secrets: bool,
) -> Result<Option<Vec<FileDiff>>, NodeError> {
let Some(tree) = self.tree_of_revision(space, revision)? else {
return Ok(None);
};
let changes = self.revision_changes(space, tree, revision)?;
let Some(&last) = changes.last() else {
return Ok(None);
};
let parts: BTreeSet<_> = changes.iter().copied().collect();
let (after, before) = self.tree_states_of_changes(space, tree, last, &parts)?;
let mut diffs = self.diff_states(space, (&before, &after), secrets)?;
diffs.sort_by(|a, b| a.path.cmp(&b.path));
Ok(Some(diffs))
}
fn diff_states(
&self,
space: SpaceId,
(before, after): (&TreeState, &TreeState),
secrets: bool,
) -> Result<Vec<FileDiff>, NodeError> {
let entities: BTreeSet<EntityId> = before
.by_entity
.keys()
.chain(after.by_entity.keys())
.copied()
.collect();
let mut diffs = Vec::new();
for entity in entities {
let (path, change, old, new) =
match (version_of(before, entity), version_of(after, entity)) {
(None, Some((path, new))) => (path, Change::New, None, new),
(Some((path, old)), None) => (path, Change::Deleted, old, None),
(Some((from, old)), Some((path, new))) => {
let change = if from != path {
Change::Moved { from }
} else if old == new {
continue;
} else {
Change::Edited
};
(path, change, old, new)
}
(None, None) => continue,
};
let secret = before.is_secret(entity) || after.is_secret(entity);
if secret && !self.secrets_unlocked(space) {
continue;
}
let old = old.as_ref().map_or(Side::Nothing, Side::Version);
let new = new.as_ref().map_or(Side::Nothing, Side::Version);
let shown = (secret, secrets);
diffs.push(self.file_diff(space, (path, change), (&old, &new), shown)?);
}
Ok(diffs)
}
fn diff_copy(
&self,
space: SpaceId,
tree: EntityId,
secrets: bool,
) -> Result<Vec<FileDiff>, NodeError> {
let _locked = self.lock_trees()?;
let root = self.root_here(space, tree)?;
let index = self.index()?;
let purged = self.learn_purges(space, &index)?;
let state = self.tree_state(space, tree)?;
let compared = self.compare(space, (tree, &root), &state, &index, (&purged, false))?;
let mut diffs = Vec::new();
for pending in &compared.pending {
let base = pending.base.clone().or_else(|| {
pending
.entity
.and_then(|entity| state.by_entity.get(&entity))
.and_then(|path| state.by_path.get(path))
.and_then(|current| current.version.clone())
});
let old = match (&pending.change, &base) {
(Change::New, _) | (_, None) => Side::Nothing,
(_, Some(version)) => Side::Version(version),
};
let new = match &pending.found {
Some(found) => Side::Copy(found),
None => Side::Nothing,
};
let path = pending.path.clone();
let at = (path, pending.change.clone());
let shown = (pending.secret, secrets);
diffs.push(self.file_diff(space, at, (&old, &new), shown)?);
}
Ok(diffs)
}
fn file_diff(
&self,
space: SpaceId,
(path, change): (String, Change),
(old, new): (&Side<'_>, &Side<'_>),
(secret, shown): (bool, bool),
) -> Result<FileDiff, NodeError> {
if secret && !shown {
return Ok(FileDiff {
path,
change,
patch: None,
binary: false,
secret,
});
}
let old_bytes = self.side_bytes(space, old, secret)?;
let new_bytes = self.side_bytes(space, new, secret)?;
let binary = [&old_bytes, &new_bytes]
.iter()
.any(|bytes| bytes.as_ref().is_some_and(|bytes| !is_text(bytes)));
let unified = match (&old_bytes, &new_bytes) {
(Some(old_bytes), Some(new_bytes)) if !binary && old_bytes != new_bytes => {
let from = match &change {
Change::Moved { from } => from.as_str(),
_ => path.as_str(),
};
let old_text = String::from_utf8_lossy(old_bytes);
let new_text = String::from_utf8_lossy(new_bytes);
let made = diffy::DiffOptions::new()
.set_original_filename(format!("a/{from}"))
.set_modified_filename(format!("b/{path}"))
.create_patch(&old_text, &new_text)
.to_string();
Some(made)
}
_ => None,
};
Ok(FileDiff {
path,
change,
patch: unified,
binary,
secret,
})
}
fn side_bytes(
&self,
space: SpaceId,
side: &Side<'_>,
secret: bool,
) -> Result<Option<Vec<u8>>, NodeError> {
Ok(match side {
Side::Nothing => Some(Vec::new()),
Side::Version(Version::Link { target }) => Some(target.clone().into_bytes()),
Side::Version(Version::File { content, .. }) => {
self.version_bytes(space, content, secret)?
}
Side::Copy(found) => match &found.kind {
super::walk::FoundKind::Link(target) => Some(target.clone().into_bytes()),
super::walk::FoundKind::File => {
let mut bytes = Vec::new();
open_found(found)
.and_then(|file| file.take(READ_LIMIT + 1).read_to_end(&mut bytes))
.ok()
.filter(|_| bytes.len() as u64 <= READ_LIMIT)
.map(|_| bytes)
}
},
})
}
pub(super) fn content_bytes(
&self,
space: SpaceId,
content: &Hash,
) -> Result<Option<Vec<u8>>, NodeError> {
self.file_content(space, content, READ_LIMIT)
}
}
pub(super) fn is_text(bytes: &[u8]) -> bool {
!bytes[..bytes.len().min(TEXT_HEAD)].contains(&0)
}
#[cfg(test)]
mod tests {
use super::is_text;
#[test]
fn text_is_what_holds_no_zero_byte_in_its_first_eight_thousand() {
assert!(is_text(b"notes\n"));
assert!(is_text(&[0xff, 0xfe, b'a']));
assert!(!is_text(&[b'a', 0, b'b']));
let mut late = vec![b'a'; 8_000];
late.push(0);
assert!(is_text(&late));
}
}