pub mod change_set;
pub mod chunking;
pub mod diff;
pub mod error;
pub mod hash;
pub mod hash_format;
pub mod ignore;
pub mod merge;
pub mod protocol;
pub mod sparse;
pub mod tree;
pub mod view;
#[cfg(feature = "local-repo")]
pub mod git;
#[cfg(feature = "local-repo")]
pub mod sqlite;
#[cfg(feature = "local-repo")]
pub mod traits;
pub use change_set::{
CanonicalChangeSetV1, CanonicalPathChangeV1, ChangeScopeV1, ChangeStateV1,
CHANGE_SET_OBJECT_FORMAT_V1,
};
pub use chunking::{
blob_chunk_mapping, chunk_content, reassemble_chunks, stream_chunk_content, BlobChunkMapping,
ChunkInfo,
};
#[cfg(feature = "local-repo")]
pub use chunking::{chunk_decode, chunk_decode_limited, chunk_encode};
pub use diff::{
binary_or_large_notice, binary_or_oversize_notice, inline_diff_ranges, is_binary, DiffLine,
FileDiff, Hunk, DEFAULT_CONTEXT_LINES, MAX_TEXT_DIFF_BYTES,
};
pub use error::{
CommitPhaseError, DescriptionStaleState, ErrorCode, ExitCode, FinishBlocker, FinishPhaseError,
FinishPreflightError, MissingApproval, OakError, OakErrorKind, Result, RootRefusal,
WorktreeTransition,
};
pub use hash::{hash_bytes, hash_string, Hash, HashKind};
pub use hash_format::HashFormat;
pub use ignore::{
is_system_metadata_path, root_dirs, IgnorePatterns, ROOT_DIRS_FILE, ROOT_STATE_FILE,
TRACKED_OAK_FILES,
};
pub use merge::{
three_way_merge_manifests, three_way_merge_text, three_way_merge_trees, MergeConflict,
MergeOutcome, TextMerge, TreeMergeOutcome,
};
pub use sparse::SparseCone;
pub use tree::{
build_tree, collect_tree_objects, diff_tree_entries, diff_trees, edit_tree, flatten_tree,
is_metadata_dir_component, normalize_path, resolve_path, subtree_at_path, validate_entry_name,
validate_tree_path, validate_worktree_path, worktree_path_problem, BuiltTree, Tree, TreeEntry,
TreeEntryKind,
};
pub use view::{
derive_view_commit, graft_tree, prune_tree, DerivedViewCommit, ViewSpec, VIEW_PROTOCOL,
};
#[cfg(feature = "local-repo")]
pub use git::GitRepository;
#[cfg(feature = "local-repo")]
pub use sqlite::{
BulkImporter, ChangeCaptureProvenance, CompactStats, SqliteRepository, StoredBlobReader,
StoredChangeCapture,
};
#[cfg(feature = "local-repo")]
pub use traits::{BlobSource, Repository, StatCacheEntry};
use chrono::{DateTime, Utc};
use std::collections::{HashMap, HashSet, VecDeque};
pub const LARGE_FILE_THRESHOLD: u64 = 10 * 1024 * 1024; use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Blob {
pub hash: Hash,
pub content: Vec<u8>,
pub size: u64,
}
impl Blob {
pub fn new(content: Vec<u8>) -> Self {
let hash = hash_bytes(&content);
let size = content.len() as u64;
Blob {
hash,
content,
size,
}
}
pub fn from_string(s: &str) -> Self {
Self::new(s.as_bytes().to_vec())
}
#[allow(clippy::should_implement_trait)]
pub fn from_str(s: &str) -> Self {
Self::from_string(s)
}
pub fn empty() -> Self {
Self::new(Vec::new())
}
pub fn empty_hash() -> Hash {
hash_format::hash_blob(HashFormat::V1, b"")
}
pub fn is_empty_hash(hash: &Hash) -> bool {
hash == &Self::empty_hash()
}
}
#[cfg(feature = "local-repo")]
pub fn ensure_empty_blob(repo: &dyn Repository, hash: &Hash) -> Result<bool> {
if !Blob::is_empty_hash(hash) {
return Ok(false);
}
repo.store_blob(&Blob::empty())?;
Ok(true)
}
#[cfg(feature = "local-repo")]
pub fn ensure_empty_blobs_in_manifest(repo: &dyn Repository, manifest: &Manifest) -> Result<()> {
if manifest
.entries
.iter()
.any(|e| Blob::is_empty_hash(&e.blob_hash))
&& !repo.has_blob(&Blob::empty_hash())?
{
repo.store_blob(&Blob::empty())?;
}
Ok(())
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub enum FileMode {
#[default]
Regular,
Executable,
Symlink,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ManifestEntry {
pub path: String,
pub blob_hash: Hash,
pub mode: FileMode,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Manifest {
pub hash: Hash,
pub entries: Vec<ManifestEntry>,
}
impl Manifest {
pub fn new(mut entries: Vec<ManifestEntry>) -> Self {
entries.sort_by(|a, b| a.path.cmp(&b.path));
let hash = tree::build_tree_unchecked(&entries).root_hash;
Manifest { hash, entries }
}
pub fn empty() -> Self {
Manifest {
hash: tree::Tree::empty_hash(),
entries: Vec::new(),
}
}
pub fn get(&self, path: &str) -> Option<&ManifestEntry> {
self.entries.iter().find(|e| e.path == path)
}
pub fn diff(&self, other: &Manifest) -> Vec<FileChange> {
self.diff_with_renames(other, &RenameConfig::default())
}
pub fn diff_with_renames(&self, other: &Manifest, _config: &RenameConfig) -> Vec<FileChange> {
self.diff_entries_with_renames(&other.entries)
}
pub fn diff_with_content_renames<F>(
&self,
other: &Manifest,
get_content: F,
config: &RenameConfig,
) -> Result<Vec<FileChange>>
where
F: FnMut(&Hash) -> Result<Option<Vec<u8>>>,
{
self.diff_entries_with_content_renames(&other.entries, get_content, config)
}
pub fn diff_entries_with_content_renames<F>(
&self,
other_entries: &[ManifestEntry],
get_content: F,
config: &RenameConfig,
) -> Result<Vec<FileChange>>
where
F: FnMut(&Hash) -> Result<Option<Vec<u8>>>,
{
let mut changes = self.diff_entries_with_renames(other_entries);
detect_renames_by_content(&mut changes, get_content, config)?;
Ok(changes)
}
pub fn diff_entries_with_renames(&self, other_entries: &[ManifestEntry]) -> Vec<FileChange> {
let mut changes = Vec::new();
let mut deleted: Vec<(&str, &ManifestEntry)> = Vec::new();
let mut added: Vec<(&str, &ManifestEntry)> = Vec::new();
let self_map: HashMap<&str, &ManifestEntry> =
self.entries.iter().map(|e| (e.path.as_str(), e)).collect();
let mut seen_other: HashSet<&str> = HashSet::with_capacity(other_entries.len());
for other_entry in other_entries {
let path = other_entry.path.as_str();
seen_other.insert(path);
match self_map.get(path) {
None => {
added.push((path, other_entry));
}
Some(entry) => {
if entry.blob_hash != other_entry.blob_hash || entry.mode != other_entry.mode {
changes.push(FileChange {
path: path.to_string(),
change_type: ChangeType::Modified,
old_blob_hash: Some(entry.blob_hash.clone()),
new_blob_hash: Some(other_entry.blob_hash.clone()),
old_path: None,
old_mode: Some(entry.mode),
new_mode: Some(other_entry.mode),
});
}
}
}
}
for entry in &self.entries {
let path = entry.path.as_str();
if !seen_other.contains(path) {
deleted.push((path, entry));
}
}
added.sort_by(|(a, _), (b, _)| a.cmp(b));
let mut added_by_hash: HashMap<&Hash, VecDeque<usize>> = HashMap::new();
for (idx, (_, add_entry)) in added.iter().enumerate() {
added_by_hash
.entry(&add_entry.blob_hash)
.or_default()
.push_back(idx);
}
let mut matched_added = vec![false; added.len()];
for (del_path, del_entry) in &deleted {
match added_by_hash
.get_mut(&del_entry.blob_hash)
.and_then(VecDeque::pop_front)
{
Some(add_idx) => {
let (add_path, add_entry) = added[add_idx];
changes.push(FileChange {
path: add_path.to_string(),
change_type: ChangeType::Renamed,
old_blob_hash: Some(del_entry.blob_hash.clone()),
new_blob_hash: Some(add_entry.blob_hash.clone()),
old_path: Some(del_path.to_string()),
old_mode: Some(del_entry.mode),
new_mode: Some(add_entry.mode),
});
matched_added[add_idx] = true;
}
None => {
changes.push(FileChange {
path: del_path.to_string(),
change_type: ChangeType::Deleted,
old_blob_hash: Some(del_entry.blob_hash.clone()),
new_blob_hash: None,
old_path: None,
old_mode: Some(del_entry.mode),
new_mode: None,
});
}
}
}
for (idx, (add_path, add_entry)) in added.iter().enumerate() {
if !matched_added[idx] {
changes.push(FileChange {
path: add_path.to_string(),
change_type: ChangeType::Added,
old_blob_hash: None,
new_blob_hash: Some(add_entry.blob_hash.clone()),
old_path: None,
old_mode: None,
new_mode: Some(add_entry.mode),
});
}
}
changes.sort_by(|a, b| a.path.cmp(&b.path));
changes
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ChangeType {
Added,
Modified,
Deleted,
Renamed,
}
impl std::fmt::Display for ChangeType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ChangeType::Added => write!(f, "A"),
ChangeType::Modified => write!(f, "M"),
ChangeType::Deleted => write!(f, "D"),
ChangeType::Renamed => write!(f, "R"),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileChange {
pub path: String,
pub change_type: ChangeType,
pub old_blob_hash: Option<Hash>,
pub new_blob_hash: Option<Hash>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub old_path: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub old_mode: Option<FileMode>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub new_mode: Option<FileMode>,
}
pub fn validate_manifest_transition(
parent: &Manifest,
current: &Manifest,
changes: &[FileChange],
) -> std::result::Result<(), String> {
let parent_entries: std::collections::BTreeMap<String, (Hash, FileMode)> = parent
.entries
.iter()
.map(|entry| (entry.path.clone(), (entry.blob_hash.clone(), entry.mode)))
.collect();
let current_entries: std::collections::BTreeMap<String, (Hash, FileMode)> = current
.entries
.iter()
.map(|entry| (entry.path.clone(), (entry.blob_hash.clone(), entry.mode)))
.collect();
if parent_entries.len() != parent.entries.len()
|| current_entries.len() != current.entries.len()
{
return Err("manifest contains duplicate paths".to_string());
}
let expected_old: std::collections::BTreeSet<String> = parent_entries
.iter()
.filter(|(path, entry)| current_entries.get(*path) != Some(*entry))
.map(|(path, _)| path.clone())
.collect();
let expected_new: std::collections::BTreeSet<String> = current_entries
.iter()
.filter(|(path, entry)| parent_entries.get(*path) != Some(*entry))
.map(|(path, _)| path.clone())
.collect();
let mut claimed_old = std::collections::BTreeSet::new();
let mut claimed_new = std::collections::BTreeSet::new();
let claim_old = |path: &str,
hash: Option<&Hash>,
mode: Option<FileMode>,
claimed: &mut std::collections::BTreeSet<String>|
-> std::result::Result<(), String> {
let hash = hash.ok_or_else(|| format!("old-side path {path:?} has no blob"))?;
let entry = parent_entries
.get(path)
.ok_or_else(|| format!("old-side path {path:?} is absent from parent"))?;
if entry.0 != *hash || mode.is_some_and(|claimed_mode| claimed_mode != entry.1) {
return Err(format!(
"old-side path {path:?} claims blob {hash} but does not match parent blob {}",
entry.0
));
}
if !claimed.insert(path.to_string()) {
return Err(format!("old-side path {path:?} is claimed more than once"));
}
Ok(())
};
let claim_new = |path: &str,
hash: Option<&Hash>,
mode: Option<FileMode>,
claimed: &mut std::collections::BTreeSet<String>|
-> std::result::Result<(), String> {
let hash = hash.ok_or_else(|| format!("new-side path {path:?} has no blob"))?;
let entry = current_entries
.get(path)
.ok_or_else(|| format!("new-side path {path:?} is absent from current"))?;
if entry.0 != *hash || mode.is_some_and(|claimed_mode| claimed_mode != entry.1) {
return Err(format!(
"new-side path {path:?} claims blob {hash} but does not match current blob {}",
entry.0
));
}
if !claimed.insert(path.to_string()) {
return Err(format!("new-side path {path:?} is claimed more than once"));
}
Ok(())
};
for change in changes {
match change.change_type {
ChangeType::Added => {
if change.old_blob_hash.is_some()
|| change.old_path.is_some()
|| change.old_mode.is_some()
{
return Err(format!(
"addition {:?} has invalid old-side state",
change.path
));
}
claim_new(
&change.path,
change.new_blob_hash.as_ref(),
change.new_mode,
&mut claimed_new,
)?;
}
ChangeType::Deleted => {
if change.new_blob_hash.is_some()
|| change.old_path.is_some()
|| change.new_mode.is_some()
{
return Err(format!(
"deletion {:?} has invalid new-side state",
change.path
));
}
claim_old(
&change.path,
change.old_blob_hash.as_ref(),
change.old_mode,
&mut claimed_old,
)?;
}
ChangeType::Renamed => {
let old_path = change
.old_path
.as_ref()
.ok_or_else(|| format!("rename {:?} has no old_path", change.path))?;
if old_path == &change.path {
return Err(format!(
"rename source and destination are both {old_path:?}"
));
}
claim_old(
old_path,
change.old_blob_hash.as_ref(),
change.old_mode,
&mut claimed_old,
)?;
claim_new(
&change.path,
change.new_blob_hash.as_ref(),
change.new_mode,
&mut claimed_new,
)?;
}
ChangeType::Modified => {
if change.old_path.is_some() {
return Err(format!(
"modification {:?} must not carry old_path",
change.path
));
}
claim_old(
&change.path,
change.old_blob_hash.as_ref(),
change.old_mode,
&mut claimed_old,
)?;
claim_new(
&change.path,
change.new_blob_hash.as_ref(),
change.new_mode,
&mut claimed_new,
)?;
}
}
}
if claimed_old != expected_old || claimed_new != expected_new {
return Err("file changes do not describe the complete manifest transition".to_string());
}
Ok(())
}
#[derive(Debug, Clone)]
pub struct RenameConfig {
pub similarity_threshold: f64,
pub max_file_bytes: usize,
pub max_candidates: usize,
}
impl Default for RenameConfig {
fn default() -> Self {
RenameConfig {
similarity_threshold: 0.5,
max_file_bytes: 1 << 20,
max_candidates: 1000,
}
}
}
pub fn content_similarity(old: &[u8], new: &[u8]) -> f64 {
if old.is_empty() || new.is_empty() {
return 0.0;
}
const BINARY_SNIFF: usize = 8192;
if old.iter().take(BINARY_SNIFF).any(|b| *b == 0)
|| new.iter().take(BINARY_SNIFF).any(|b| *b == 0)
{
return 0.0;
}
fn line_hashes(content: &[u8]) -> HashSet<u64> {
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash as _, Hasher};
content
.split(|b| *b == b'\n')
.map(|line| {
let mut h = DefaultHasher::new();
line.hash(&mut h);
h.finish()
})
.collect()
}
let a = line_hashes(old);
let b = line_hashes(new);
let inter = a.intersection(&b).count();
let union = a.len() + b.len() - inter;
if union == 0 {
0.0
} else {
inter as f64 / union as f64
}
}
pub fn detect_renames_by_content<F>(
changes: &mut Vec<FileChange>,
mut get_content: F,
config: &RenameConfig,
) -> Result<()>
where
F: FnMut(&Hash) -> Result<Option<Vec<u8>>>,
{
let mut deleted_idxs: Vec<usize> = Vec::new();
let mut added_idxs: Vec<usize> = Vec::new();
for (i, c) in changes.iter().enumerate() {
match c.change_type {
ChangeType::Deleted => deleted_idxs.push(i),
ChangeType::Added => added_idxs.push(i),
_ => {}
}
}
if deleted_idxs.is_empty() || added_idxs.is_empty() {
return Ok(());
}
if deleted_idxs.len() + added_idxs.len() > config.max_candidates {
return Ok(());
}
let max_bytes = config.max_file_bytes;
fn fetch_for<F>(
idx: usize,
hash: Option<&Hash>,
max_bytes: usize,
get_content: &mut F,
) -> Result<Option<(usize, Hash, Vec<u8>)>>
where
F: FnMut(&Hash) -> Result<Option<Vec<u8>>>,
{
let Some(h) = hash else { return Ok(None) };
let Some(bytes) = get_content(h)? else {
return Ok(None);
};
if bytes.len() > max_bytes {
return Ok(None);
}
Ok(Some((idx, h.clone(), bytes)))
}
let mut del_contents: Vec<(usize, Hash, Vec<u8>)> = Vec::new();
for &di in &deleted_idxs {
if let Some(entry) = fetch_for(
di,
changes[di].old_blob_hash.as_ref(),
max_bytes,
&mut get_content,
)? {
del_contents.push(entry);
}
}
let mut add_contents: Vec<(usize, Hash, Vec<u8>)> = Vec::new();
for &ai in &added_idxs {
if let Some(entry) = fetch_for(
ai,
changes[ai].new_blob_hash.as_ref(),
max_bytes,
&mut get_content,
)? {
add_contents.push(entry);
}
}
let mut scores: Vec<(usize, usize, f64)> = Vec::new();
for (di, _, del_bytes) in &del_contents {
for (ai, _, add_bytes) in &add_contents {
let s = content_similarity(del_bytes, add_bytes);
if s >= config.similarity_threshold {
scores.push((*di, *ai, s));
}
}
}
scores.sort_by(|a, b| b.2.partial_cmp(&a.2).unwrap_or(std::cmp::Ordering::Equal));
let mut used_del: HashSet<usize> = HashSet::new();
let mut used_add: HashSet<usize> = HashSet::new();
let mut renames: Vec<FileChange> = Vec::new();
for (di, ai, _) in &scores {
if used_del.contains(di) || used_add.contains(ai) {
continue;
}
let del = &changes[*di];
let add = &changes[*ai];
renames.push(FileChange {
path: add.path.clone(),
change_type: ChangeType::Renamed,
old_blob_hash: del.old_blob_hash.clone(),
new_blob_hash: add.new_blob_hash.clone(),
old_path: Some(del.path.clone()),
old_mode: del.old_mode,
new_mode: add.new_mode,
});
used_del.insert(*di);
used_add.insert(*ai);
}
if renames.is_empty() {
return Ok(());
}
let to_drop: HashSet<usize> = used_del.iter().chain(used_add.iter()).cloned().collect();
let kept: Vec<FileChange> = std::mem::take(changes)
.into_iter()
.enumerate()
.filter_map(|(i, c)| if to_drop.contains(&i) { None } else { Some(c) })
.collect();
*changes = kept;
changes.extend(renames);
changes.sort_by(|a, b| a.path.cmp(&b.path));
Ok(())
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Commit {
pub hash: Hash,
pub branch_name: String,
pub parent_hash: Option<Hash>,
pub merge_parent_hash: Option<Hash>,
pub manifest_hash: Hash,
pub author: String,
#[serde(default)]
pub message: Option<String>,
pub timestamp: DateTime<Utc>,
pub files: Vec<FileChange>,
}
impl Commit {
pub fn new(
branch_name: String,
parent_hash: Option<Hash>,
merge_parent_hash: Option<Hash>,
manifest_hash: Hash,
author: String,
message: Option<String>,
files: Vec<FileChange>,
) -> Result<Self> {
Self::with_timestamp(
branch_name,
parent_hash,
merge_parent_hash,
manifest_hash,
author,
message,
files,
truncate_to_micros(Utc::now()),
)
}
#[allow(clippy::too_many_arguments)]
pub fn with_timestamp(
branch_name: String,
parent_hash: Option<Hash>,
merge_parent_hash: Option<Hash>,
manifest_hash: Hash,
author: String,
message: Option<String>,
files: Vec<FileChange>,
timestamp: DateTime<Utc>,
) -> Result<Self> {
validate_commit_field("branch name", &branch_name)?;
validate_commit_field("author", &author)?;
let hash = hash_format::hash_commit(
hash_format::HashFormat::V1,
&hash_format::CommitFields {
branch_name: &branch_name,
parent_hash: parent_hash.as_ref(),
merge_parent_hash: merge_parent_hash.as_ref(),
manifest_hash: &manifest_hash,
author: &author,
message: message.as_deref(),
timestamp: ×tamp,
files: &files,
},
);
Ok(Commit {
hash,
branch_name,
parent_hash,
merge_parent_hash,
manifest_hash,
author,
message,
timestamp,
files,
})
}
#[allow(clippy::too_many_arguments)]
pub fn rehydrate_verified(
claimed_hash: &Hash,
branch_name: String,
parent_hash: Option<Hash>,
merge_parent_hash: Option<Hash>,
manifest_hash: Hash,
author: String,
message: Option<String>,
files: Vec<FileChange>,
timestamp: DateTime<Utc>,
) -> Result<Self> {
validate_commit_field("branch name", &branch_name)?;
validate_commit_field("author", &author)?;
let fields = hash_format::CommitFields {
branch_name: &branch_name,
parent_hash: parent_hash.as_ref(),
merge_parent_hash: merge_parent_hash.as_ref(),
manifest_hash: &manifest_hash,
author: &author,
message: message.as_deref(),
timestamp: ×tamp,
files: &files,
};
let exact = hash_format::hash_commit(hash_format::HashFormat::V1, &fields);
let verified_timestamp = if &exact == claimed_hash {
Some(timestamp)
} else if timestamp.timestamp_subsec_nanos().is_multiple_of(1_000) {
(1..1_000).find_map(|tail| {
let candidate = timestamp + chrono::Duration::nanoseconds(tail);
let hash = hash_format::hash_commit(
hash_format::HashFormat::V1,
&hash_format::CommitFields {
timestamp: &candidate,
..fields
},
);
(&hash == claimed_hash).then_some(candidate)
})
} else {
None
};
let verified_timestamp = verified_timestamp.or_else(|| {
(merge_parent_hash.is_none()
&& &hash_format::hash_commit_v1_pre_merge_parent(&fields) == claimed_hash)
.then_some(timestamp)
});
let Some(timestamp) = verified_timestamp else {
return Err(OakError::InvalidHash(format!(
"commit hash mismatch: peer claimed {claimed_hash}, canonical fields hash to {exact}; {}",
describe_commit_preimage(&fields)
)));
};
Ok(Commit {
hash: claimed_hash.clone(),
branch_name,
parent_hash,
merge_parent_hash,
manifest_hash,
author,
message,
timestamp,
files,
})
}
pub fn verify_fields(&self) -> Result<()> {
Commit::rehydrate_verified(
&self.hash,
self.branch_name.clone(),
self.parent_hash.clone(),
self.merge_parent_hash.clone(),
self.manifest_hash.clone(),
self.author.clone(),
self.message.clone(),
Vec::new(),
self.timestamp,
)
.map(|_| ())
}
}
fn describe_commit_preimage(fields: &hash_format::CommitFields<'_>) -> String {
let optional = |hash: Option<&Hash>| hash.map_or_else(|| "none".to_string(), Hash::to_string);
let message = match fields.message {
Some(message) => format!(
"{} bytes, blake3 {}",
message.len(),
hash_bytes(message.as_bytes()).short()
),
None => "none".to_string(),
};
let legacy_rule = if fields.merge_parent_hash.is_none() {
"v1 6-field pre-merge-parent preimage"
} else {
"v1 6-field pre-merge-parent preimage (skipped: commit has a merge parent)"
};
let tail_rule = if fields
.timestamp
.timestamp_subsec_nanos()
.is_multiple_of(1_000)
{
"v1 with restored nanosecond timestamp tails 1..999"
} else {
"v1 nanosecond-tail restoration (skipped: timestamp is not microsecond-truncated)"
};
format!(
"hashed fields: branch_name={:?}, parent={}, merge_parent={}, manifest={}, author={:?}, message=[{message}], timestamp={}; rules tried: v1 7-field preimage, {tail_rule}, {legacy_rule}",
fields.branch_name,
optional(fields.parent_hash),
optional(fields.merge_parent_hash),
fields.manifest_hash,
fields.author,
fields.timestamp.to_rfc3339(),
)
}
pub fn truncate_to_micros(ts: DateTime<Utc>) -> DateTime<Utc> {
ts - chrono::Duration::nanoseconds((ts.timestamp_subsec_nanos() % 1_000) as i64)
}
fn validate_commit_field(field: &str, value: &str) -> Result<()> {
if value.chars().any(char::is_control) {
return Err(OakError::InvalidArgument(format!(
"commit {field} {value:?} contains a control character"
)));
}
Ok(())
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CloseReason {
Superseded,
Stale,
Duplicate,
Other(String),
}
impl CloseReason {
pub fn as_str(&self) -> &str {
match self {
CloseReason::Superseded => "superseded",
CloseReason::Stale => "stale",
CloseReason::Duplicate => "duplicate",
CloseReason::Other(reason) => reason.as_str(),
}
}
pub fn parse(s: &str) -> Result<Self> {
if s.trim() != s || s.is_empty() || s.chars().any(char::is_control) {
return Err(OakError::InvalidArgument(
"close reason cannot be empty, have leading/trailing whitespace, or contain control characters"
.to_string(),
));
}
match s {
"superseded" => Ok(CloseReason::Superseded),
"stale" => Ok(CloseReason::Stale),
"duplicate" => Ok(CloseReason::Duplicate),
_ => Ok(CloseReason::Other(s.to_string())),
}
}
}
impl Serialize for CloseReason {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(self.as_str())
}
}
impl<'de> Deserialize<'de> for CloseReason {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let value = String::deserialize(deserializer)?;
CloseReason::parse(&value).map_err(serde::de::Error::custom)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum BranchStatus {
Open,
Closed,
}
impl BranchStatus {
pub fn as_str(&self) -> &'static str {
match self {
BranchStatus::Open => "open",
BranchStatus::Closed => "closed",
}
}
pub fn from_db_str(s: &str) -> Self {
match s {
"closed" => BranchStatus::Closed,
_ => BranchStatus::Open,
}
}
}
impl std::fmt::Display for BranchStatus {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Branch {
pub name: String,
pub description: Option<String>,
pub parent_branch: Option<String>,
pub status: BranchStatus,
#[serde(default)]
pub close_reason: Option<CloseReason>,
pub created_at: DateTime<Utc>,
}
impl Branch {
pub fn new(name: String, description: Option<String>, parent_branch: Option<String>) -> Self {
Branch {
name,
description,
parent_branch,
status: BranchStatus::Open,
close_reason: None,
created_at: Utc::now(),
}
}
}
pub const BRANCH_METADATA_UNREADABLE: &str = "branch_metadata_unreadable";
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UnreadableBranch {
pub name: String,
pub status: BranchStatus,
pub column: &'static str,
}
impl UnreadableBranch {
pub fn code(&self) -> &'static str {
BRANCH_METADATA_UNREADABLE
}
pub fn to_error(&self) -> OakError {
OakError::Database(format!(
"{}: branch '{}' has an unreadable {} value in local storage",
BRANCH_METADATA_UNREADABLE, self.name, self.column
))
}
}
pub fn parse_stored_timestamp(value: &str) -> Option<DateTime<Utc>> {
let value = value.trim();
if let Ok(parsed) = DateTime::parse_from_rfc3339(value) {
return Some(parsed.with_timezone(&Utc));
}
for format in ["%Y-%m-%d %H:%M:%S%.f", "%Y-%m-%dT%H:%M:%S%.f"] {
if let Ok(naive) = chrono::NaiveDateTime::parse_from_str(value, format) {
return Some(naive.and_utc());
}
}
None
}
pub const DEFAULT_BRANCH: &str = "main";
pub const MAX_STACK_DEPTH: usize = 32;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StackParentError {
Empty,
SelfParent,
Missing(String),
Closed(String),
Cycle(Vec<String>),
TooDeep,
}
impl std::fmt::Display for StackParentError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Empty => write!(f, "the parent branch name is empty"),
Self::SelfParent => write!(f, "a branch can't be stacked on itself"),
Self::Missing(p) => write!(f, "parent branch '{p}' doesn't exist"),
Self::Closed(p) => write!(
f,
"parent branch '{p}' is closed; stack on an open branch (or on '{DEFAULT_BRANCH}')"
),
Self::Cycle(chain) => write!(
f,
"stacking would create a cycle ({}); every stack must end at '{DEFAULT_BRANCH}'",
chain.join(" -> ")
),
Self::TooDeep => write!(
f,
"the stack would be deeper than {MAX_STACK_DEPTH} branches"
),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StackNode {
pub parent_branch: Option<String>,
pub open: bool,
}
pub fn validate_stack_parent(
branch: &str,
parent: Option<&str>,
mut lookup: impl FnMut(&str) -> Option<StackNode>,
) -> std::result::Result<(), StackParentError> {
let Some(parent) = parent else {
return Ok(());
};
if parent == DEFAULT_BRANCH {
return Ok(());
}
if parent.is_empty() {
return Err(StackParentError::Empty);
}
if parent == branch {
return Err(StackParentError::SelfParent);
}
let node = lookup(parent).ok_or_else(|| StackParentError::Missing(parent.to_string()))?;
if !node.open {
return Err(StackParentError::Closed(parent.to_string()));
}
let mut chain = vec![branch.to_string(), parent.to_string()];
let mut next = node.parent_branch;
while let Some(name) = next {
if name == DEFAULT_BRANCH {
return Ok(());
}
if chain.contains(&name) {
chain.push(name);
return Err(StackParentError::Cycle(chain));
}
if chain.len() > MAX_STACK_DEPTH {
return Err(StackParentError::TooDeep);
}
chain.push(name.clone());
next = match lookup(&name) {
Some(node) => node.parent_branch,
None => return Ok(()),
};
}
Ok(())
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BranchRename {
pub id: i64,
pub old_name: String,
pub new_name: String,
pub renamed_at: DateTime<Utc>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MetadataKey {
Head,
RemoteUrl,
RepoName,
RepoOwner,
CurrentBranch,
ApiKey,
ApiKeyOrigin,
LastRenameId,
MainLastCheckedAt,
MainLastObservedHead,
MainLastFetchIntegrityError,
MainHydratedHead,
HydratedTree,
HashFormat,
SparsePaths,
ViewSpec,
RestrictedBlobs,
KnownLostBlobs,
ParentSyncDeferred,
}
impl MetadataKey {
pub fn as_str(&self) -> &'static str {
match self {
MetadataKey::Head => "head",
MetadataKey::RemoteUrl => "remote_url",
MetadataKey::RepoName => "repo_name",
MetadataKey::RepoOwner => "repo_owner",
MetadataKey::CurrentBranch => "current_branch",
MetadataKey::ApiKey => "api_key",
MetadataKey::ApiKeyOrigin => "api_key_origin",
MetadataKey::LastRenameId => "last_rename_id",
MetadataKey::MainLastCheckedAt => "main_last_checked_at",
MetadataKey::MainLastObservedHead => "main_last_observed_head",
MetadataKey::MainLastFetchIntegrityError => "main_last_fetch_integrity_error",
MetadataKey::MainHydratedHead => "main_hydrated_head",
MetadataKey::HydratedTree => "hydrated_tree",
MetadataKey::HashFormat => "hash_format",
MetadataKey::SparsePaths => "sparse_paths",
MetadataKey::ViewSpec => "view_spec",
MetadataKey::RestrictedBlobs => "restricted_blobs",
MetadataKey::KnownLostBlobs => "known_lost_blobs",
MetadataKey::ParentSyncDeferred => "parent_sync_deferred",
}
}
}
impl std::fmt::Display for MetadataKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_str())
}
}
#[cfg(test)]
mod tests {
use super::*;
fn stack_lookup<'a>(
rows: &'a [(&'a str, Option<&'a str>, bool)],
) -> impl FnMut(&str) -> Option<StackNode> + 'a {
move |name| {
rows.iter()
.find(|(n, _, _)| *n == name)
.map(|(_, parent, open)| StackNode {
parent_branch: parent.map(str::to_string),
open: *open,
})
}
}
#[test]
fn stack_parent_trunk_and_none_always_allowed() {
assert_eq!(validate_stack_parent("b", None, |_| None), Ok(()));
assert_eq!(
validate_stack_parent("b", Some(DEFAULT_BRANCH), |_| None),
Ok(())
);
}
#[test]
fn stack_parent_accepts_open_chain_to_trunk() {
let rows = [("a", Some("main"), true), ("b", Some("a"), true)];
assert_eq!(
validate_stack_parent("c", Some("b"), stack_lookup(&rows)),
Ok(())
);
}
#[test]
fn stack_parent_rejects_bad_parents() {
let rows = [
("a", Some("main"), true),
("gone", Some("main"), false),
("b", Some("a"), true),
];
assert_eq!(
validate_stack_parent("x", Some(""), stack_lookup(&rows)),
Err(StackParentError::Empty)
);
assert_eq!(
validate_stack_parent("a", Some("a"), stack_lookup(&rows)),
Err(StackParentError::SelfParent)
);
assert_eq!(
validate_stack_parent("x", Some("nope"), stack_lookup(&rows)),
Err(StackParentError::Missing("nope".into()))
);
assert_eq!(
validate_stack_parent("x", Some("gone"), stack_lookup(&rows)),
Err(StackParentError::Closed("gone".into()))
);
assert!(matches!(
validate_stack_parent("a", Some("b"), stack_lookup(&rows)),
Err(StackParentError::Cycle(_))
));
}
#[test]
fn stack_parent_rejects_runaway_chains() {
let names: Vec<String> = (0..=MAX_STACK_DEPTH + 1).map(|i| format!("b{i}")).collect();
let rows: Vec<(&str, Option<&str>, bool)> = names
.iter()
.enumerate()
.map(|(i, n)| {
let parent = if i == 0 {
"main"
} else {
names[i - 1].as_str()
};
(n.as_str(), Some(parent), true)
})
.collect();
let tip = names.last().unwrap().as_str();
assert_eq!(
validate_stack_parent("new", Some(tip), stack_lookup(&rows)),
Err(StackParentError::TooDeep)
);
}
#[test]
fn test_blob_creation() {
let content = b"hello world".to_vec();
let blob = Blob::new(content.clone());
assert_eq!(blob.content, content);
assert_eq!(blob.size, 11);
}
#[test]
fn test_blob_hash_determinism() {
let blob1 = Blob::from_str("test content");
let blob2 = Blob::from_str("test content");
assert_eq!(blob1.hash, blob2.hash);
}
#[cfg(feature = "local-repo")]
#[test]
fn ensure_empty_blob_only_synthesizes_the_empty_blob() {
let dir = tempfile::tempdir().unwrap();
let repo = SqliteRepository::open(&dir.path().join("t.db")).unwrap();
let absent = Blob::from_str("bytes only the pusher ever had").hash;
assert!(!ensure_empty_blob(&repo, &absent).unwrap());
assert!(!repo.has_blob(&absent).unwrap());
assert!(ensure_empty_blob(&repo, &Blob::empty_hash()).unwrap());
let stored = repo.get_blob(&Blob::empty_hash()).unwrap().unwrap();
assert_eq!(stored.size, 0);
assert!(stored.content.is_empty());
assert!(ensure_empty_blob(&repo, &Blob::empty_hash()).unwrap());
}
#[cfg(feature = "local-repo")]
#[test]
fn ensure_empty_blobs_in_manifest_covers_deduped_entries() {
let dir = tempfile::tempdir().unwrap();
let repo = SqliteRepository::open(&dir.path().join("t.db")).unwrap();
let other = Blob::from_str("real content").hash;
let manifest = Manifest::new(vec![
ManifestEntry {
path: "docs/EMPTY".to_string(),
blob_hash: Blob::empty_hash(),
mode: FileMode::Regular,
},
ManifestEntry {
path: "src/EMPTY".to_string(),
blob_hash: Blob::empty_hash(),
mode: FileMode::Regular,
},
ManifestEntry {
path: "README.md".to_string(),
blob_hash: other.clone(),
mode: FileMode::Regular,
},
]);
ensure_empty_blobs_in_manifest(&repo, &manifest).unwrap();
assert!(repo.has_blob(&Blob::empty_hash()).unwrap());
assert!(
!repo.has_blob(&other).unwrap(),
"only the empty blob is derivable; everything else must stay missing"
);
}
#[test]
fn test_manifest_hash_determinism() {
let entries = vec![
ManifestEntry {
path: "file1.txt".to_string(),
blob_hash: hash_string("content1"),
mode: FileMode::Regular,
},
ManifestEntry {
path: "file2.txt".to_string(),
blob_hash: hash_string("content2"),
mode: FileMode::Regular,
},
];
let manifest1 = Manifest::new(entries.clone());
let manifest2 = Manifest::new(entries);
assert_eq!(manifest1.hash, manifest2.hash);
}
#[test]
fn test_manifest_diff_added() {
let old = Manifest::empty();
let new = Manifest::new(vec![ManifestEntry {
path: "new.txt".to_string(),
blob_hash: hash_string("content"),
mode: FileMode::Regular,
}]);
let changes = old.diff(&new);
assert_eq!(changes.len(), 1);
assert_eq!(changes[0].change_type, ChangeType::Added);
}
#[test]
fn test_manifest_diff_deleted() {
let old = Manifest::new(vec![ManifestEntry {
path: "old.txt".to_string(),
blob_hash: hash_string("content"),
mode: FileMode::Regular,
}]);
let new = Manifest::empty();
let changes = old.diff(&new);
assert_eq!(changes.len(), 1);
assert_eq!(changes[0].change_type, ChangeType::Deleted);
}
#[test]
fn test_manifest_diff_modified() {
let old = Manifest::new(vec![ManifestEntry {
path: "file.txt".to_string(),
blob_hash: hash_string("old content"),
mode: FileMode::Regular,
}]);
let new = Manifest::new(vec![ManifestEntry {
path: "file.txt".to_string(),
blob_hash: hash_string("new content"),
mode: FileMode::Regular,
}]);
let changes = old.diff(&new);
assert_eq!(changes.len(), 1);
assert_eq!(changes[0].change_type, ChangeType::Modified);
}
#[test]
fn test_commit_creation() {
let commit = Commit::new(
"main".to_string(),
None,
None,
hash_string("manifest"),
"author".to_string(),
Some("test message".to_string()),
vec![],
)
.unwrap();
assert_eq!(commit.branch_name, "main");
assert!(commit.parent_hash.is_none());
assert!(commit.merge_parent_hash.is_none());
}
#[test]
fn test_commit_rejects_control_chars_in_branch_and_author() {
for (branch, author) in [
("main\nevil", "author"),
("main\tevil", "author"),
("main\0evil", "author"),
("main\u{1b}evil", "author"),
("main\u{7f}evil", "author"),
("main", "author\nevil"),
("main", "author\tevil"),
("main", "author\0evil"),
("main", "author\u{1b}evil"),
("main", "author\u{7f}evil"),
] {
let err = Commit::new(
branch.to_string(),
None,
None,
hash_string("manifest"),
author.to_string(),
None,
vec![],
)
.unwrap_err();
assert!(
matches!(err, OakError::InvalidArgument(_)),
"({branch:?}, {author:?}) should be rejected, got {err:?}"
);
}
assert!(Commit::new(
"main".to_string(),
None,
None,
hash_string("manifest"),
"author".to_string(),
Some("line one\nline two".to_string()),
vec![],
)
.is_ok());
}
#[test]
fn test_rehydrate_accepts_legacy_pre_merge_parent_preimage() {
let timestamp = chrono::DateTime::parse_from_rfc3339("2026-04-12T09:30:00.123456+00:00")
.unwrap()
.with_timezone(&Utc);
let parent = hash_string("parent commit");
let manifest = hash_string("manifest");
let legacy_preimage = format!(
"main\n{}\n{}\nauthor@example.com\na legacy commit\n{}",
parent,
manifest,
timestamp.to_rfc3339()
);
let claimed = hash_bytes(legacy_preimage.as_bytes());
assert_eq!(
claimed,
hash_format::hash_commit_v1_pre_merge_parent(&hash_format::CommitFields {
branch_name: "main",
parent_hash: Some(&parent),
merge_parent_hash: None,
manifest_hash: &manifest,
author: "author@example.com",
message: Some("a legacy commit"),
timestamp: ×tamp,
files: &[],
}),
"legacy hasher must reproduce the hand-rendered 6-field preimage"
);
let commit = Commit::rehydrate_verified(
&claimed,
"main".to_string(),
Some(parent.clone()),
None,
manifest.clone(),
"author@example.com".to_string(),
Some("a legacy commit".to_string()),
vec![],
timestamp,
)
.expect("legacy 6-field commit without merge parent must verify");
assert_eq!(commit.hash, claimed);
assert_eq!(commit.timestamp, timestamp);
let err = Commit::rehydrate_verified(
&claimed,
"main".to_string(),
Some(parent.clone()),
Some(hash_string("merge parent")),
manifest.clone(),
"author@example.com".to_string(),
Some("a legacy commit".to_string()),
vec![],
timestamp,
)
.expect_err("legacy preimage must not verify a commit with a merge parent");
assert!(
matches!(&err, OakError::InvalidHash(m) if m.contains("commit hash mismatch")),
"expected a commit hash mismatch, got {err:?}"
);
let err = Commit::rehydrate_verified(
&hash_string("some unrelated hash"),
"main".to_string(),
Some(parent.clone()),
None,
manifest.clone(),
"author@example.com".to_string(),
Some("a legacy commit".to_string()),
vec![],
timestamp,
)
.expect_err("a hash matching neither preimage must be rejected");
assert!(
matches!(&err, OakError::InvalidHash(m) if m.contains("commit hash mismatch")),
"expected a commit hash mismatch, got {err:?}"
);
let OakError::InvalidHash(message) = &err else {
unreachable!()
};
for expected in [
"branch_name=\"main\"".to_string(),
"author=\"author@example.com\"".to_string(),
format!("parent={parent}"),
"merge_parent=none".to_string(),
format!("manifest={manifest}"),
format!("timestamp={}", timestamp.to_rfc3339()),
"message=[15 bytes, blake3 ".to_string(),
"rules tried: v1 7-field preimage".to_string(),
"v1 6-field pre-merge-parent preimage".to_string(),
] {
assert!(
message.contains(&expected),
"missing {expected:?} in {message}"
);
}
assert!(!message.contains("a legacy commit"), "{message}");
}
#[test]
fn test_branch_creation() {
let br = Branch::new("feature-x".to_string(), Some("A feature".to_string()), None);
assert_eq!(br.name, "feature-x");
assert_eq!(br.status, BranchStatus::Open);
assert!(br.parent_branch.is_none());
}
#[test]
fn test_manifest_diff_no_changes() {
let entries = vec![ManifestEntry {
path: "file.txt".to_string(),
blob_hash: hash_string("content"),
mode: FileMode::Regular,
}];
let m1 = Manifest::new(entries.clone());
let m2 = Manifest::new(entries);
let changes = m1.diff(&m2);
assert!(changes.is_empty());
}
#[test]
fn test_manifest_diff_multiple_changes() {
let old = Manifest::new(vec![
ManifestEntry {
path: "a.txt".to_string(),
blob_hash: hash_string("a"),
mode: FileMode::Regular,
},
ManifestEntry {
path: "b.txt".to_string(),
blob_hash: hash_string("b"),
mode: FileMode::Regular,
},
]);
let new = Manifest::new(vec![
ManifestEntry {
path: "b.txt".to_string(),
blob_hash: hash_string("b_modified"),
mode: FileMode::Regular,
},
ManifestEntry {
path: "c.txt".to_string(),
blob_hash: hash_string("c"),
mode: FileMode::Regular,
},
]);
let changes = old.diff(&new);
assert_eq!(changes.len(), 3);
assert!(changes
.iter()
.any(|c| c.path == "a.txt" && c.change_type == ChangeType::Deleted));
assert!(changes
.iter()
.any(|c| c.path == "b.txt" && c.change_type == ChangeType::Modified));
assert!(changes
.iter()
.any(|c| c.path == "c.txt" && c.change_type == ChangeType::Added));
}
#[test]
fn test_manifest_diff_mode_only_change() {
let hash = hash_string("content");
let old = Manifest::new(vec![ManifestEntry {
path: "script.sh".to_string(),
blob_hash: hash.clone(),
mode: FileMode::Regular,
}]);
let new = Manifest::new(vec![ManifestEntry {
path: "script.sh".to_string(),
blob_hash: hash.clone(),
mode: FileMode::Executable,
}]);
let changes = old.diff(&new);
assert_eq!(changes.len(), 1);
assert_eq!(changes[0].path, "script.sh");
assert_eq!(changes[0].change_type, ChangeType::Modified);
assert_eq!(changes[0].old_blob_hash, Some(hash.clone()));
assert_eq!(changes[0].new_blob_hash, Some(hash));
}
#[test]
fn test_filemode_default() {
let mode: FileMode = Default::default();
assert_eq!(mode, FileMode::Regular);
}
#[test]
fn test_metadata_key_as_str() {
assert_eq!(MetadataKey::Head.as_str(), "head");
assert_eq!(MetadataKey::RemoteUrl.as_str(), "remote_url");
assert_eq!(MetadataKey::RepoName.as_str(), "repo_name");
assert_eq!(MetadataKey::CurrentBranch.as_str(), "current_branch");
assert_eq!(MetadataKey::ApiKey.as_str(), "api_key");
assert_eq!(
MetadataKey::MainLastCheckedAt.as_str(),
"main_last_checked_at"
);
assert_eq!(
MetadataKey::MainLastObservedHead.as_str(),
"main_last_observed_head"
);
assert_eq!(
MetadataKey::MainLastFetchIntegrityError.as_str(),
"main_last_fetch_integrity_error"
);
assert_eq!(MetadataKey::RestrictedBlobs.as_str(), "restricted_blobs");
assert_eq!(MetadataKey::KnownLostBlobs.as_str(), "known_lost_blobs");
}
#[test]
fn test_metadata_key_display() {
assert_eq!(MetadataKey::Head.to_string(), "head");
}
#[test]
fn test_change_type_display() {
assert_eq!(ChangeType::Added.to_string(), "A");
assert_eq!(ChangeType::Modified.to_string(), "M");
assert_eq!(ChangeType::Deleted.to_string(), "D");
assert_eq!(ChangeType::Renamed.to_string(), "R");
}
#[test]
fn test_branch_status_display() {
assert_eq!(BranchStatus::Open.to_string(), "open");
assert_eq!(BranchStatus::Closed.to_string(), "closed");
}
#[test]
fn test_manifest_get() {
let hash = hash_string("content");
let manifest = Manifest::new(vec![ManifestEntry {
path: "file.txt".to_string(),
blob_hash: hash.clone(),
mode: FileMode::Regular,
}]);
assert!(manifest.get("file.txt").is_some());
assert!(manifest.get("nonexistent.txt").is_none());
}
#[test]
fn test_manifest_empty() {
let empty = Manifest::empty();
assert!(empty.entries.is_empty());
}
#[test]
fn test_blob_from_string() {
let blob = Blob::from_string("hello");
assert_eq!(blob.content, b"hello");
assert_eq!(blob.size, 5);
}
#[test]
fn test_manifest_diff_exact_rename() {
let content_hash = hash_string("same content");
let old = Manifest::new(vec![ManifestEntry {
path: "old_name.txt".to_string(),
blob_hash: content_hash.clone(),
mode: FileMode::Regular,
}]);
let new = Manifest::new(vec![ManifestEntry {
path: "new_name.txt".to_string(),
blob_hash: content_hash,
mode: FileMode::Regular,
}]);
let changes = old.diff(&new);
assert_eq!(changes.len(), 1);
assert_eq!(changes[0].change_type, ChangeType::Renamed);
assert_eq!(changes[0].path, "new_name.txt");
assert_eq!(changes[0].old_path.as_deref(), Some("old_name.txt"));
}
#[test]
fn test_manifest_diff_only_exact_renames() {
let old = Manifest::new(vec![ManifestEntry {
path: "src/player.rs".to_string(),
blob_hash: hash_string("old code"),
mode: FileMode::Regular,
}]);
let new = Manifest::new(vec![ManifestEntry {
path: "lib/player.rs".to_string(),
blob_hash: hash_string("new code"),
mode: FileMode::Regular,
}]);
let changes = old.diff(&new);
assert_eq!(changes.len(), 2);
assert!(changes.iter().any(|c| c.change_type == ChangeType::Deleted));
assert!(changes.iter().any(|c| c.change_type == ChangeType::Added));
}
#[test]
fn test_manifest_diff_exact_renames_with_duplicate_hashes() {
let shared = hash_string("same content");
let old = Manifest::new(vec![
ManifestEntry {
path: "old/a.txt".to_string(),
blob_hash: shared.clone(),
mode: FileMode::Regular,
},
ManifestEntry {
path: "old/b.txt".to_string(),
blob_hash: shared.clone(),
mode: FileMode::Executable,
},
ManifestEntry {
path: "old/deleted.txt".to_string(),
blob_hash: hash_string("deleted"),
mode: FileMode::Regular,
},
]);
let new = Manifest::new(vec![
ManifestEntry {
path: "new/a.txt".to_string(),
blob_hash: shared.clone(),
mode: FileMode::Regular,
},
ManifestEntry {
path: "new/b.txt".to_string(),
blob_hash: shared,
mode: FileMode::Regular,
},
ManifestEntry {
path: "new/added.txt".to_string(),
blob_hash: hash_string("added"),
mode: FileMode::Regular,
},
]);
let changes = old.diff(&new);
assert_eq!(changes.len(), 4);
assert_eq!(
changes
.iter()
.filter(|c| c.change_type == ChangeType::Renamed)
.count(),
2
);
assert!(changes
.iter()
.any(|c| c.path == "old/deleted.txt" && c.change_type == ChangeType::Deleted));
assert!(changes
.iter()
.any(|c| c.path == "new/added.txt" && c.change_type == ChangeType::Added));
}
#[test]
fn test_manifest_diff_entries_matches_manifest_diff_without_new_manifest() {
let unchanged = hash_string("unchanged");
let edited_old = hash_string("old");
let edited_new = hash_string("new");
let renamed = hash_string("renamed");
let old = Manifest::new(vec![
ManifestEntry {
path: "a.txt".to_string(),
blob_hash: unchanged.clone(),
mode: FileMode::Regular,
},
ManifestEntry {
path: "b.txt".to_string(),
blob_hash: edited_old,
mode: FileMode::Regular,
},
ManifestEntry {
path: "gone.txt".to_string(),
blob_hash: hash_string("gone"),
mode: FileMode::Regular,
},
ManifestEntry {
path: "old-name.txt".to_string(),
blob_hash: renamed.clone(),
mode: FileMode::Regular,
},
]);
let scanned_entries = vec![
ManifestEntry {
path: "new-name.txt".to_string(),
blob_hash: renamed,
mode: FileMode::Regular,
},
ManifestEntry {
path: "added.txt".to_string(),
blob_hash: hash_string("added"),
mode: FileMode::Regular,
},
ManifestEntry {
path: "b.txt".to_string(),
blob_hash: edited_new,
mode: FileMode::Regular,
},
ManifestEntry {
path: "a.txt".to_string(),
blob_hash: unchanged,
mode: FileMode::Regular,
},
];
let new = Manifest::new(scanned_entries.clone());
let expected = old.diff(&new);
let actual = old.diff_entries_with_renames(&scanned_entries);
assert_eq!(
expected
.iter()
.map(|c| (&c.path, c.change_type, c.old_path.as_deref()))
.collect::<Vec<_>>(),
actual
.iter()
.map(|c| (&c.path, c.change_type, c.old_path.as_deref()))
.collect::<Vec<_>>()
);
}
#[test]
fn test_content_similarity_identical() {
let bytes = b"line one\nline two\nline three";
assert!((content_similarity(bytes, bytes) - 1.0).abs() < 1e-9);
}
#[test]
fn test_content_similarity_partial_overlap() {
let old = b"line one\nline two\nline three\nline four";
let new = b"line one\nline two\nline three\nline five";
let s = content_similarity(old, new);
assert!((s - 0.6).abs() < 1e-9, "got {s}");
}
#[test]
fn test_content_similarity_disjoint() {
let old = b"alpha\nbeta\ngamma";
let new = b"one\ntwo\nthree";
assert_eq!(content_similarity(old, new), 0.0);
}
#[test]
fn test_content_similarity_binary_returns_zero() {
let old = b"some\0binary";
let new = b"some\0binary";
assert_eq!(content_similarity(old, new), 0.0);
}
#[test]
fn test_content_similarity_empty_returns_zero() {
assert_eq!(content_similarity(b"", b"anything"), 0.0);
assert_eq!(content_similarity(b"anything", b""), 0.0);
}
#[test]
fn test_detect_renames_by_content_promotes_pair() {
let del_hash = hash_string("old code");
let add_hash = hash_string("edited code");
let mut changes = vec![
FileChange {
path: "src/foo.rs".to_string(),
change_type: ChangeType::Deleted,
old_blob_hash: Some(del_hash.clone()),
new_blob_hash: None,
old_path: None,
old_mode: None,
new_mode: None,
},
FileChange {
path: "lib/foo.rs".to_string(),
change_type: ChangeType::Added,
old_blob_hash: None,
new_blob_hash: Some(add_hash.clone()),
old_path: None,
old_mode: None,
new_mode: None,
},
];
let del_bytes = b"alpha\nbeta\ngamma\ndelta".to_vec();
let add_bytes = b"alpha\nbeta\ngamma\nepsilon".to_vec();
let get_content = |h: &Hash| -> Result<Option<Vec<u8>>> {
if *h == del_hash {
Ok(Some(del_bytes.clone()))
} else if *h == add_hash {
Ok(Some(add_bytes.clone()))
} else {
Ok(None)
}
};
detect_renames_by_content(&mut changes, get_content, &RenameConfig::default()).unwrap();
assert_eq!(changes.len(), 1);
assert_eq!(changes[0].change_type, ChangeType::Renamed);
assert_eq!(changes[0].path, "lib/foo.rs");
assert_eq!(changes[0].old_path.as_deref(), Some("src/foo.rs"));
}
#[test]
fn test_detect_renames_by_content_skips_dissimilar() {
let del_hash = hash_string("a");
let add_hash = hash_string("b");
let mut changes = vec![
FileChange {
path: "old.rs".to_string(),
change_type: ChangeType::Deleted,
old_blob_hash: Some(del_hash.clone()),
new_blob_hash: None,
old_path: None,
old_mode: None,
new_mode: None,
},
FileChange {
path: "new.rs".to_string(),
change_type: ChangeType::Added,
old_blob_hash: None,
new_blob_hash: Some(add_hash.clone()),
old_path: None,
old_mode: None,
new_mode: None,
},
];
let get_content = |h: &Hash| -> Result<Option<Vec<u8>>> {
if *h == del_hash {
Ok(Some(b"one\ntwo\nthree".to_vec()))
} else {
Ok(Some(b"forty\nfifty\nsixty".to_vec()))
}
};
detect_renames_by_content(&mut changes, get_content, &RenameConfig::default()).unwrap();
assert_eq!(changes.len(), 2);
assert!(changes.iter().any(|c| c.change_type == ChangeType::Added));
assert!(changes.iter().any(|c| c.change_type == ChangeType::Deleted));
}
#[test]
fn test_detect_renames_by_content_greedy_best_match() {
let del_hash = hash_string("d");
let add_hash_high = hash_string("ah");
let add_hash_low = hash_string("al");
let mut changes = vec![
FileChange {
path: "src/lib.rs".to_string(),
change_type: ChangeType::Deleted,
old_blob_hash: Some(del_hash.clone()),
new_blob_hash: None,
old_path: None,
old_mode: None,
new_mode: None,
},
FileChange {
path: "src/match_high.rs".to_string(),
change_type: ChangeType::Added,
old_blob_hash: None,
new_blob_hash: Some(add_hash_high.clone()),
old_path: None,
old_mode: None,
new_mode: None,
},
FileChange {
path: "src/match_low.rs".to_string(),
change_type: ChangeType::Added,
old_blob_hash: None,
new_blob_hash: Some(add_hash_low.clone()),
old_path: None,
old_mode: None,
new_mode: None,
},
];
let get_content = |h: &Hash| -> Result<Option<Vec<u8>>> {
if *h == del_hash {
Ok(Some(b"a\nb\nc\nd".to_vec()))
} else if *h == add_hash_high {
Ok(Some(b"a\nb\nc\nz".to_vec())) } else {
Ok(Some(b"a\nx\ny\nz".to_vec())) }
};
detect_renames_by_content(&mut changes, get_content, &RenameConfig::default()).unwrap();
let renamed: Vec<_> = changes
.iter()
.filter(|c| c.change_type == ChangeType::Renamed)
.collect();
assert_eq!(renamed.len(), 1);
assert_eq!(renamed[0].path, "src/match_high.rs");
assert!(changes
.iter()
.any(|c| c.path == "src/match_low.rs" && c.change_type == ChangeType::Added));
}
#[test]
fn test_detect_renames_by_content_respects_max_candidates() {
let del_hash = hash_string("d");
let add_hash = hash_string("a");
let mut changes = vec![
FileChange {
path: "old.rs".to_string(),
change_type: ChangeType::Deleted,
old_blob_hash: Some(del_hash.clone()),
new_blob_hash: None,
old_path: None,
old_mode: None,
new_mode: None,
},
FileChange {
path: "new.rs".to_string(),
change_type: ChangeType::Added,
old_blob_hash: None,
new_blob_hash: Some(add_hash.clone()),
old_path: None,
old_mode: None,
new_mode: None,
},
];
let get_content =
|_: &Hash| -> Result<Option<Vec<u8>>> { Ok(Some(b"a\nb\nc\nd\ne".to_vec())) };
let config = RenameConfig {
max_candidates: 1,
..RenameConfig::default()
};
detect_renames_by_content(&mut changes, get_content, &config).unwrap();
assert_eq!(changes.len(), 2);
assert!(!changes.iter().any(|c| c.change_type == ChangeType::Renamed));
}
fn manifest_with_blobs(files: &[(&str, &str)]) -> (Manifest, HashMap<Hash, Vec<u8>>) {
let mut blobs = HashMap::new();
let entries = files
.iter()
.map(|(path, content)| {
let blob_hash = hash_string(content);
blobs.insert(blob_hash.clone(), content.as_bytes().to_vec());
ManifestEntry {
path: (*path).to_string(),
blob_hash,
mode: FileMode::Regular,
}
})
.collect();
(Manifest::new(entries), blobs)
}
fn blob_loader(
blobs: &HashMap<Hash, Vec<u8>>,
) -> impl FnMut(&Hash) -> Result<Option<Vec<u8>>> + '_ {
move |h: &Hash| Ok(blobs.get(h).cloned())
}
#[test]
fn test_diff_with_content_renames_detects_rename_with_edit() {
let (old, mut blobs) = manifest_with_blobs(&[(
"src/old_name.rs",
"line one\nline two\nline three\nline four\nline five\nline six\n",
)]);
let (new, new_blobs) = manifest_with_blobs(&[(
"src/new_name.rs",
"line one\nline two\nline three\nline four\nline five\nline changed\n",
)]);
blobs.extend(new_blobs);
let changes = old
.diff_with_content_renames(&new, blob_loader(&blobs), &RenameConfig::default())
.unwrap();
assert_eq!(changes.len(), 1, "expected one rename, got {changes:?}");
assert_eq!(changes[0].change_type, ChangeType::Renamed);
assert_eq!(changes[0].path, "src/new_name.rs");
assert_eq!(changes[0].old_path.as_deref(), Some("src/old_name.rs"));
assert!(changes[0].old_blob_hash.is_some());
assert!(changes[0].new_blob_hash.is_some());
}
#[test]
fn semantic_manifest_transition_accepts_rename_with_edit() {
let old_hash = hash_bytes(b"before\n");
let new_hash = hash_bytes(b"after\n");
let parent = Manifest::new(vec![ManifestEntry {
path: "old.txt".to_string(),
blob_hash: old_hash.clone(),
mode: FileMode::Regular,
}]);
let current = Manifest::new(vec![ManifestEntry {
path: "new.txt".to_string(),
blob_hash: new_hash.clone(),
mode: FileMode::Executable,
}]);
let changes = vec![FileChange {
path: "new.txt".to_string(),
change_type: ChangeType::Renamed,
old_blob_hash: Some(old_hash),
new_blob_hash: Some(new_hash),
old_path: Some("old.txt".to_string()),
old_mode: Some(FileMode::Regular),
new_mode: Some(FileMode::Executable),
}];
validate_manifest_transition(&parent, ¤t, &changes)
.expect("rename+edit is one valid representation of the exact transition");
}
#[test]
fn semantic_manifest_transition_accepts_equivalent_delete_and_add() {
let old_hash = hash_bytes(b"before\n");
let new_hash = hash_bytes(b"after\n");
let parent = Manifest::new(vec![ManifestEntry {
path: "old.txt".to_string(),
blob_hash: old_hash.clone(),
mode: FileMode::Regular,
}]);
let current = Manifest::new(vec![ManifestEntry {
path: "new.txt".to_string(),
blob_hash: new_hash.clone(),
mode: FileMode::Regular,
}]);
let changes = vec![
FileChange {
path: "old.txt".to_string(),
change_type: ChangeType::Deleted,
old_blob_hash: Some(old_hash),
new_blob_hash: None,
old_path: None,
old_mode: Some(FileMode::Regular),
new_mode: None,
},
FileChange {
path: "new.txt".to_string(),
change_type: ChangeType::Added,
old_blob_hash: None,
new_blob_hash: Some(new_hash),
old_path: None,
old_mode: None,
new_mode: Some(FileMode::Regular),
},
];
validate_manifest_transition(&parent, ¤t, &changes)
.expect("delete+add is an equivalent valid representation");
}
#[test]
fn semantic_manifest_transition_accepts_content_and_mode_modification() {
let old_hash = hash_bytes(b"before\n");
let new_hash = hash_bytes(b"after\n");
let parent = Manifest::new(vec![ManifestEntry {
path: "script".to_string(),
blob_hash: old_hash.clone(),
mode: FileMode::Regular,
}]);
let current = Manifest::new(vec![ManifestEntry {
path: "script".to_string(),
blob_hash: new_hash.clone(),
mode: FileMode::Executable,
}]);
let changes = vec![FileChange {
path: "script".to_string(),
change_type: ChangeType::Modified,
old_blob_hash: Some(old_hash),
new_blob_hash: Some(new_hash),
old_path: None,
old_mode: Some(FileMode::Regular),
new_mode: Some(FileMode::Executable),
}];
validate_manifest_transition(&parent, ¤t, &changes)
.expect("content+mode modification describes the exact transition");
}
#[test]
fn semantic_manifest_transition_rejects_forged_duplicate_and_incomplete_records() {
let old_hash = hash_bytes(b"before\n");
let new_hash = hash_bytes(b"after\n");
let parent = Manifest::new(vec![ManifestEntry {
path: "file".to_string(),
blob_hash: old_hash.clone(),
mode: FileMode::Regular,
}]);
let current = Manifest::new(vec![ManifestEntry {
path: "file".to_string(),
blob_hash: new_hash.clone(),
mode: FileMode::Executable,
}]);
let valid = FileChange {
path: "file".to_string(),
change_type: ChangeType::Modified,
old_blob_hash: Some(old_hash.clone()),
new_blob_hash: Some(new_hash),
old_path: None,
old_mode: Some(FileMode::Regular),
new_mode: Some(FileMode::Executable),
};
let mut forged = valid.clone();
forged.old_blob_hash = Some(hash_bytes(b"forged"));
assert!(validate_manifest_transition(&parent, ¤t, &[forged]).is_err());
let mut wrong_mode = valid.clone();
wrong_mode.new_mode = Some(FileMode::Symlink);
assert!(validate_manifest_transition(&parent, ¤t, &[wrong_mode]).is_err());
assert!(validate_manifest_transition(&parent, ¤t, &[valid.clone(), valid]).is_err());
assert!(validate_manifest_transition(&parent, ¤t, &[]).is_err());
}
#[test]
fn semantic_manifest_transition_is_independent_of_overwrite_record_order() {
let a_hash = hash_bytes(b"a\n");
let b_hash = hash_bytes(b"b\n");
let parent = Manifest::new(vec![
ManifestEntry {
path: "a".to_string(),
blob_hash: a_hash.clone(),
mode: FileMode::Regular,
},
ManifestEntry {
path: "b".to_string(),
blob_hash: b_hash.clone(),
mode: FileMode::Regular,
},
]);
let current = Manifest::new(vec![ManifestEntry {
path: "b".to_string(),
blob_hash: a_hash.clone(),
mode: FileMode::Regular,
}]);
let rename = FileChange {
path: "b".to_string(),
change_type: ChangeType::Renamed,
old_blob_hash: Some(a_hash),
new_blob_hash: Some(current.entries[0].blob_hash.clone()),
old_path: Some("a".to_string()),
old_mode: Some(FileMode::Regular),
new_mode: Some(FileMode::Regular),
};
let delete = FileChange {
path: "b".to_string(),
change_type: ChangeType::Deleted,
old_blob_hash: Some(b_hash),
new_blob_hash: None,
old_path: None,
old_mode: Some(FileMode::Regular),
new_mode: None,
};
validate_manifest_transition(&parent, ¤t, &[rename.clone(), delete.clone()])
.expect("rename before destination deletion is semantically valid");
validate_manifest_transition(&parent, ¤t, &[delete, rename])
.expect("reversing the same records must remain valid");
}
#[test]
fn semantic_manifest_transition_accepts_rename_cycle() {
let a_hash = hash_bytes(b"a\n");
let b_hash = hash_bytes(b"b\n");
let parent = Manifest::new(vec![
ManifestEntry {
path: "a".to_string(),
blob_hash: a_hash.clone(),
mode: FileMode::Regular,
},
ManifestEntry {
path: "b".to_string(),
blob_hash: b_hash.clone(),
mode: FileMode::Executable,
},
]);
let current = Manifest::new(vec![
ManifestEntry {
path: "a".to_string(),
blob_hash: b_hash.clone(),
mode: FileMode::Executable,
},
ManifestEntry {
path: "b".to_string(),
blob_hash: a_hash.clone(),
mode: FileMode::Regular,
},
]);
let changes = vec![
FileChange {
path: "b".to_string(),
change_type: ChangeType::Renamed,
old_blob_hash: Some(a_hash.clone()),
new_blob_hash: Some(a_hash),
old_path: Some("a".to_string()),
old_mode: Some(FileMode::Regular),
new_mode: Some(FileMode::Regular),
},
FileChange {
path: "a".to_string(),
change_type: ChangeType::Renamed,
old_blob_hash: Some(b_hash.clone()),
new_blob_hash: Some(b_hash),
old_path: Some("b".to_string()),
old_mode: Some(FileMode::Executable),
new_mode: Some(FileMode::Executable),
},
];
validate_manifest_transition(&parent, ¤t, &changes)
.expect("a two-path rename cycle is an order-independent transition");
}
#[test]
fn test_diff_with_content_renames_below_threshold_stays_delete_add() {
let (old, mut blobs) =
manifest_with_blobs(&[("a.txt", "alpha\nbeta\ngamma\ndelta\nepsilon\n")]);
let (new, new_blobs) = manifest_with_blobs(&[("b.txt", "one\ntwo\nthree\nfour\nfive\n")]);
blobs.extend(new_blobs);
let changes = old
.diff_with_content_renames(&new, blob_loader(&blobs), &RenameConfig::default())
.unwrap();
assert_eq!(changes.len(), 2, "expected D+A, got {changes:?}");
assert!(changes
.iter()
.any(|c| c.path == "a.txt" && c.change_type == ChangeType::Deleted));
assert!(changes
.iter()
.any(|c| c.path == "b.txt" && c.change_type == ChangeType::Added));
}
#[test]
fn test_diff_with_content_renames_respects_size_cap() {
let big_old = "shared line\n".repeat(100);
let big_new = format!("{}{}", "shared line\n".repeat(99), "edited line\n");
let (old, mut blobs) = manifest_with_blobs(&[("old_big.txt", big_old.as_str())]);
let (new, new_blobs) = manifest_with_blobs(&[("new_big.txt", big_new.as_str())]);
blobs.extend(new_blobs);
let config = RenameConfig {
max_file_bytes: 64,
..RenameConfig::default()
};
let changes = old
.diff_with_content_renames(&new, blob_loader(&blobs), &config)
.unwrap();
assert_eq!(changes.len(), 2, "expected D+A, got {changes:?}");
assert!(!changes.iter().any(|c| c.change_type == ChangeType::Renamed));
}
#[test]
fn test_diff_with_content_renames_respects_max_candidates() {
let (old, mut blobs) = manifest_with_blobs(&[
("old_a.txt", "a1\na2\na3\na4\n"),
("old_b.txt", "b1\nb2\nb3\nb4\n"),
]);
let (new, new_blobs) = manifest_with_blobs(&[
("new_a.txt", "a1\na2\na3\nedited\n"),
("new_b.txt", "b1\nb2\nb3\nedited\n"),
]);
blobs.extend(new_blobs);
let config = RenameConfig {
max_candidates: 3,
..RenameConfig::default()
};
let changes = old
.diff_with_content_renames(&new, blob_loader(&blobs), &config)
.unwrap();
assert_eq!(changes.len(), 4, "expected 2xD + 2xA, got {changes:?}");
assert!(!changes.iter().any(|c| c.change_type == ChangeType::Renamed));
let changes = old
.diff_with_content_renames(&new, blob_loader(&blobs), &RenameConfig::default())
.unwrap();
assert_eq!(
changes
.iter()
.filter(|c| c.change_type == ChangeType::Renamed)
.count(),
2,
"expected both pairs renamed, got {changes:?}"
);
}
}