use rusqlite::{params, Connection, OptionalExtension};
pub mod file_inspect;
pub mod refs_inspect;
pub mod tree_inspect;
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Mutex;
use crate::hash::hash_bytes;
use crate::{
Blob, Branch, BranchStatus, CanonicalChangeSetV1, ChangeScopeV1, ChangeType, ChunkInfo,
CloseReason, Commit, FileChange, FileMode, Hash, Manifest, ManifestEntry, MetadataKey,
OakError, Result, Tree, TreeEntry, TreeEntryKind, CHANGE_SET_OBJECT_FORMAT_V1,
};
use crate::traits::{BlobSource, Repository, StatCacheEntry};
fn decode_branch_row(
name: String,
description: Option<String>,
parent_branch: Option<String>,
status: BranchStatus,
close_reason: Option<String>,
created_at: &str,
) -> std::result::Result<Branch, crate::UnreadableBranch> {
let unreadable = |name: &str, column| crate::UnreadableBranch {
name: name.to_string(),
status,
column,
};
let Some(created_at) = crate::parse_stored_timestamp(created_at) else {
return Err(unreadable(&name, "created_at"));
};
let close_reason = match close_reason.as_deref().map(CloseReason::parse).transpose() {
Ok(reason) => reason,
Err(_) => return Err(unreadable(&name, "close_reason")),
};
Ok(Branch {
name,
description,
parent_branch,
status,
close_reason,
created_at,
})
}
fn advance_description_generation(conn: &Connection) -> Result<i64> {
conn.query_row(
"UPDATE branch_description_clock SET generation = generation + 1
WHERE singleton = 1 AND generation < 9223372036854775807 RETURNING generation",
[],
|row| row.get(0),
)
.map_err(|error| {
OakError::Database(format!(
"could not advance branch description generation: {error}"
))
})
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ChangeCaptureProvenance {
pub owner: String,
pub name: String,
pub origin: Option<String>,
pub source_branch: String,
pub base_commit: Option<Hash>,
pub captured_at: chrono::DateTime<chrono::Utc>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StoredChangeCapture {
pub capture_id: String,
pub change_set: CanonicalChangeSetV1,
pub provenance: ChangeCaptureProvenance,
}
struct ChangeCaptureRow {
change_id: String,
format_version: i64,
owner: String,
name: String,
origin: Option<String>,
source_branch: String,
base_commit: Option<String>,
captured_at: String,
}
struct ChangeSetRow {
schema_version: i64,
object_format: String,
base_tree: String,
result_tree: String,
scope_kind: String,
scope_paths_json: String,
descriptor_json: String,
created_at: String,
}
const MIGRATION_BASELINE: &str = include_str!("migrations/sqlite/0000_baseline.sql");
const MIGRATION_0001_RELAX_BRANCH_FKS_AND_MESSAGE: &str =
include_str!("migrations/sqlite/0001_relax_branch_fks_and_message.sql");
const MIGRATION_0002_RELAX_COMMITS_PARENT_HASH_FK: &str =
include_str!("migrations/sqlite/0002_relax_commits_parent_hash_fk.sql");
const MIGRATION_0003_STAT_CACHE: &str = include_str!("migrations/sqlite/0003_stat_cache.sql");
const MIGRATION_0004_STAT_CACHE_CTIME: &str =
include_str!("migrations/sqlite/0004_stat_cache_ctime.sql");
const MIGRATION_0005_MERGE_PARENT_INDEX: &str =
include_str!("migrations/sqlite/0005_merge_parent_index.sql");
const MIGRATION_0006_DROP_REDUNDANT_TREE_ENTRIES_INDEX: &str =
include_str!("migrations/sqlite/0006_drop_redundant_tree_entries_index.sql");
const MIGRATION_0007_BLOBS_CODEC: &str = include_str!("migrations/sqlite/0007_blobs_codec.sql");
const MIGRATION_0008_TREES_CONTENT: &str = include_str!("migrations/sqlite/0008_trees_content.sql");
const MIGRATION_0009_RELAX_BLOB_CHUNKS_FK: &str =
include_str!("migrations/sqlite/0009_relax_blob_chunks_fk.sql");
const MIGRATION_0010_BRANCH_CLOSE_REASON: &str =
include_str!("migrations/sqlite/0010_branch_close_reason.sql");
const MIGRATION_0011_SERVE_MAPPING_PROOFS: &str =
include_str!("migrations/sqlite/0011_serve_mapping_proofs.sql");
pub struct SqliteRepository {
conn: Mutex<Connection>,
tree_flattens: AtomicU64,
tree_reads: AtomicU64,
}
struct Migration {
version: &'static str,
sql: &'static str,
required: bool,
}
const MIGRATIONS: &[Migration] = &[
Migration {
version: "0000_baseline",
sql: MIGRATION_BASELINE,
required: true,
},
Migration {
version: "0001_relax_branch_fks_and_message",
sql: MIGRATION_0001_RELAX_BRANCH_FKS_AND_MESSAGE,
required: true,
},
Migration {
version: "0002_relax_commits_parent_hash_fk",
sql: MIGRATION_0002_RELAX_COMMITS_PARENT_HASH_FK,
required: true,
},
Migration {
version: "0003_stat_cache",
sql: MIGRATION_0003_STAT_CACHE,
required: true,
},
Migration {
version: "0004_stat_cache_ctime",
sql: MIGRATION_0004_STAT_CACHE_CTIME,
required: true,
},
Migration {
version: "0005_merge_parent_index",
sql: MIGRATION_0005_MERGE_PARENT_INDEX,
required: true,
},
Migration {
version: "0006_drop_redundant_tree_entries_index",
sql: MIGRATION_0006_DROP_REDUNDANT_TREE_ENTRIES_INDEX,
required: true,
},
Migration {
version: "0007_blobs_codec",
sql: MIGRATION_0007_BLOBS_CODEC,
required: true,
},
Migration {
version: "0008_trees_content",
sql: MIGRATION_0008_TREES_CONTENT,
required: true,
},
Migration {
version: "0009_relax_blob_chunks_fk",
sql: MIGRATION_0009_RELAX_BLOB_CHUNKS_FK,
required: true,
},
Migration {
version: "0010_branch_close_reason",
sql: MIGRATION_0010_BRANCH_CLOSE_REASON,
required: true,
},
Migration {
version: "0011_serve_mapping_proofs",
sql: MIGRATION_0011_SERVE_MAPPING_PROOFS,
required: true,
},
Migration {
version: "0012_pending_branch_description",
sql: include_str!("migrations/sqlite/0012_pending_branch_description.sql"),
required: true,
},
Migration {
version: "0013_branch_description_generation",
sql: include_str!("migrations/sqlite/0013_branch_description_generation.sql"),
required: true,
},
Migration {
version: "0014_change_captures",
sql: include_str!("migrations/sqlite/0014_change_captures.sql"),
required: true,
},
Migration {
version: "0015_serve_published_objects",
sql: include_str!("migrations/sqlite/0015_serve_published_objects.sql"),
required: true,
},
Migration {
version: "0016_serve_stage_objects",
sql: include_str!("migrations/sqlite/0016_serve_stage_objects.sql"),
required: true,
},
Migration {
version: "0017_tag_releases",
sql: include_str!("migrations/sqlite/0017_tag_releases.sql"),
required: true,
},
Migration {
version: "0018_drop_tags_commit_fk",
sql: include_str!("migrations/sqlite/0018_drop_tags_commit_fk.sql"),
required: true,
},
];
const ZSTD_LEVEL: i32 = 3;
const CODEC_RAW: i64 = 0;
const CODEC_ZSTD: i64 = 1;
fn zstd_compress(data: &[u8]) -> Result<Vec<u8>> {
zstd::encode_all(data, ZSTD_LEVEL)
.map_err(|e| OakError::Database(format!("zstd compress: {e}")))
}
fn zstd_decompress(data: &[u8]) -> Result<Vec<u8>> {
zstd::decode_all(data).map_err(|e| OakError::Database(format!("zstd decompress: {e}")))
}
fn zstd_decompress_tree(data: &[u8]) -> Result<Vec<u8>> {
use std::cell::RefCell;
const INITIAL: usize = 64 * 1024;
const KEEP: usize = 1024 * 1024;
const MAX_REUSED: usize = 16 * 1024 * 1024;
thread_local! {
static TREE_DECODER: RefCell<Option<(zstd::bulk::Decompressor<'static>, Vec<u8>)>> =
const { RefCell::new(None) };
}
if data.is_empty() {
return zstd_decompress(data);
}
TREE_DECODER.with(|cell| {
let mut slot = cell.borrow_mut();
if slot.is_none() {
let decoder = zstd::bulk::Decompressor::new()
.map_err(|e| OakError::Database(format!("zstd decompress: {e}")))?;
*slot = Some((decoder, Vec::with_capacity(INITIAL)));
}
let (decoder, buf) = slot.as_mut().expect("decoder initialized above");
loop {
buf.clear();
match decoder.decompress_to_buffer(data, buf) {
Ok(_) => {
let raw = buf.as_slice().to_vec();
if buf.capacity() > KEEP {
*buf = Vec::with_capacity(INITIAL);
}
return Ok(raw);
}
Err(_) if buf.capacity() < MAX_REUSED => buf.reserve(buf.capacity() * 4),
Err(_) => return zstd_decompress(data),
}
}
})
}
fn encode_blob(content: &[u8]) -> Result<(std::borrow::Cow<'_, [u8]>, i64)> {
let compressed = zstd_compress(content)?;
if compressed.len() < content.len() {
Ok((std::borrow::Cow::Owned(compressed), CODEC_ZSTD))
} else {
Ok((std::borrow::Cow::Borrowed(content), CODEC_RAW))
}
}
fn decode_blob(content: Vec<u8>, codec: i64) -> Result<Vec<u8>> {
if codec == CODEC_ZSTD {
zstd_decompress(&content)
} else {
Ok(content)
}
}
fn insert_tree_row(conn: &Connection, tree: &Tree) -> Result<()> {
let canonical = Tree::new(tree.entries.clone())?;
if canonical.hash != tree.hash {
return Err(OakError::InvalidHash(format!(
"tree {} does not match content hash {}",
tree.hash, canonical.hash
)));
}
let content = zstd_compress(&canonical.canonical_bytes())?;
conn.execute(
"INSERT OR IGNORE INTO trees (hash, content) VALUES (?1, ?2)",
params![canonical.hash.as_str(), content],
)
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(())
}
fn read_tree_entries(conn: &Connection, hash: &Hash) -> Result<Tree> {
let mut stmt = conn
.prepare("SELECT name, kind, hash, mode FROM tree_entries WHERE tree_hash = ?1")
.map_err(|e| OakError::Database(e.to_string()))?;
let mut entries: Vec<TreeEntry> = stmt
.query_map(params![hash.as_str()], |row| {
let name: String = row.get(0)?;
let kind_str: String = row.get(1)?;
let entry_hash: String = row.get(2)?;
let mode_str: String = row.get(3)?;
let kind = match kind_str.as_str() {
"tree" => TreeEntryKind::Tree,
_ => TreeEntryKind::Blob,
};
let mode = match mode_str.as_str() {
"executable" => FileMode::Executable,
"symlink" => FileMode::Symlink,
_ => FileMode::Regular,
};
Ok(TreeEntry {
name,
kind,
hash: Hash(entry_hash),
mode,
})
})
.map_err(|e| OakError::Database(e.to_string()))?
.collect::<std::result::Result<Vec<_>, _>>()
.map_err(|e| OakError::Database(e.to_string()))?;
entries.sort_by(|a, b| a.name.cmp(&b.name));
Ok(Tree {
hash: hash.clone(),
entries,
})
}
fn table_exists(conn: &Connection, name: &str) -> rusqlite::Result<bool> {
conn.query_row(
"SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type='table' AND name=?1)",
params![name],
|row| row.get(0),
)
}
fn column_exists(conn: &Connection, table: &str, column: &str) -> Result<bool> {
let mut stmt = conn
.prepare(&format!("PRAGMA table_info({table})"))
.map_err(|e| OakError::Database(e.to_string()))?;
let mut rows = stmt
.query([])
.map_err(|e| OakError::Database(e.to_string()))?;
while let Some(row) = rows.next().map_err(|e| OakError::Database(e.to_string()))? {
let name: String = row.get(1).map_err(|e| OakError::Database(e.to_string()))?;
if name == column {
return Ok(true);
}
}
Ok(false)
}
fn migration_already_applied(version: &str, conn: &Connection) -> Result<bool> {
match version {
"0000_baseline" => {
table_exists(conn, "commits").map_err(|e| OakError::Database(e.to_string()))
}
"0001_relax_branch_fks_and_message" => {
let sql: Option<String> = conn
.query_row(
"SELECT sql FROM sqlite_master WHERE type='table' AND name='commits'",
[],
|row| row.get(0),
)
.optional()
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(sql
.map(|s| !s.contains("message TEXT NOT NULL"))
.unwrap_or(false))
}
"0002_relax_commits_parent_hash_fk" => {
let sql: Option<String> = conn
.query_row(
"SELECT sql FROM sqlite_master WHERE type='table' AND name='commits'",
[],
|row| row.get(0),
)
.optional()
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(sql
.map(|s| !s.contains("FOREIGN KEY (parent_hash)"))
.unwrap_or(false))
}
"0007_blobs_codec" => column_exists(conn, "blobs", "codec"),
"0008_trees_content" => column_exists(conn, "trees", "content"),
"0010_branch_close_reason" => column_exists(conn, "branches", "close_reason"),
"0012_pending_branch_description" => column_exists(conn, "branches", "description_pending"),
_ => Ok(false),
}
}
fn run_migrations(conn: &Connection) -> Result<()> {
conn.execute_batch(
"CREATE TABLE IF NOT EXISTS schema_migrations (
version TEXT PRIMARY KEY,
applied_at TEXT NOT NULL DEFAULT (datetime('now')),
succeeded INTEGER NOT NULL DEFAULT 1
)",
)
.map_err(|e| OakError::Database(e.to_string()))?;
for m in MIGRATIONS {
let already: Option<i64> = conn
.query_row(
"SELECT succeeded FROM schema_migrations WHERE version = ?1",
params![m.version],
|row| row.get(0),
)
.optional()
.map_err(|e| OakError::Database(e.to_string()))?;
if already == Some(1) {
continue;
}
if migration_already_applied(m.version, conn)? {
conn.execute(
"INSERT INTO schema_migrations (version, succeeded) VALUES (?1, 1)
ON CONFLICT(version) DO UPDATE SET succeeded = 1",
params![m.version],
)
.map_err(|e| OakError::Database(e.to_string()))?;
continue;
}
match conn.execute_batch(m.sql) {
Ok(_) => {
conn.execute(
"INSERT INTO schema_migrations (version, succeeded) VALUES (?1, 1)
ON CONFLICT(version) DO UPDATE SET succeeded = 1",
params![m.version],
)
.map_err(|e| OakError::Database(e.to_string()))?;
}
Err(e) if m.required => {
return Err(OakError::Database(format!(
"migration {} failed: {}",
m.version, e
)));
}
Err(_) => {
conn.execute(
"INSERT INTO schema_migrations (version, succeeded) VALUES (?1, 0)
ON CONFLICT(version) DO NOTHING",
params![m.version],
)
.map_err(|e| OakError::Database(e.to_string()))?;
}
}
}
Ok(())
}
fn harden_db_file_permissions(db_path: &Path) {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let _ = std::fs::set_permissions(db_path, std::fs::Permissions::from_mode(0o600));
}
#[cfg(not(unix))]
{
let _ = db_path;
}
}
fn capture_integrity(capture_id: &str, detail: &str) -> OakError {
OakError::ChangeCaptureIntegrity {
capture_id: capture_id.to_string(),
detail: detail.to_string(),
}
}
fn validate_capture_id(capture_id: &str) -> Result<()> {
let suffix = capture_id
.strip_prefix("capture-")
.ok_or_else(|| capture_integrity(capture_id, "invalid capture occurrence identifier"))?;
if suffix.len() != 32
|| !suffix
.bytes()
.all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
{
return Err(capture_integrity(
capture_id,
"invalid capture occurrence identifier",
));
}
Ok(())
}
fn parse_native_hash(capture_id: &str, value: &str, label: &str) -> Result<Hash> {
if value.len() != 64
|| !value
.bytes()
.all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
{
return Err(capture_integrity(
capture_id,
&format!("invalid {label} identity"),
));
}
Hash::from_hex(value)
.map_err(|_| capture_integrity(capture_id, &format!("invalid {label} identity")))
}
fn validate_capture_segment(capture_id: &str, value: &str, label: &str) -> Result<()> {
if value.is_empty()
|| value.len() > 255
|| value.trim() != value
|| value == "."
|| value == ".."
|| value
.chars()
.any(|c| c.is_whitespace() || c.is_control() || c == '/' || c == '\\')
{
return Err(capture_integrity(
capture_id,
&format!("invalid stored {label}"),
));
}
Ok(())
}
fn validate_capture_branch(capture_id: &str, branch: &str) -> Result<()> {
if branch.is_empty()
|| branch.trim() != branch
|| branch
.chars()
.any(|c| c.is_whitespace() || c.is_control() || c == '/' || c == '\\')
{
return Err(capture_integrity(
capture_id,
"invalid stored source branch",
));
}
Ok(())
}
fn validate_capture_origin(capture_id: &str, origin: &str) -> Result<()> {
if origin.len() > 2_048
|| origin.chars().any(|c| c.is_whitespace() || c.is_control())
|| origin.contains(['?', '#'])
{
return Err(capture_integrity(
capture_id,
"invalid stored repository origin",
));
}
let authority_and_path = origin
.strip_prefix("https://")
.or_else(|| origin.strip_prefix("http://"))
.ok_or_else(|| capture_integrity(capture_id, "invalid stored repository origin"))?;
let authority = authority_and_path.split('/').next().unwrap_or_default();
if authority.is_empty() || authority.contains('@') {
return Err(capture_integrity(
capture_id,
"invalid stored repository origin",
));
}
Ok(())
}
fn validate_capture_provenance(
capture_id: &str,
owner: &str,
name: &str,
origin: Option<&str>,
source_branch: &str,
base_commit: Option<&Hash>,
) -> Result<()> {
validate_capture_segment(capture_id, owner, "repository owner")?;
validate_capture_segment(capture_id, name, "repository name")?;
validate_capture_branch(capture_id, source_branch)?;
if let Some(origin) = origin {
validate_capture_origin(capture_id, origin)?;
}
if let Some(base_commit) = base_commit {
parse_native_hash(capture_id, base_commit.as_str(), "base commit")?;
}
Ok(())
}
fn effective_head_in_connection(conn: &Connection, branch: &str) -> Result<Option<Hash>> {
let mut seen = HashSet::new();
let mut current = branch.to_string();
loop {
if !seen.insert(current.clone()) {
return Ok(None);
}
let head: Option<String> = conn
.query_row(
"SELECT head_hash FROM branch_heads WHERE branch_name = ?1",
[current.as_str()],
|row| row.get(0),
)
.optional()
.map_err(|error| OakError::Database(error.to_string()))?;
if let Some(head) = head {
return Ok(Some(Hash(head)));
}
let parent: Option<Option<String>> = conn
.query_row(
"SELECT parent_branch FROM branches WHERE name = ?1",
[current.as_str()],
|row| row.get(0),
)
.optional()
.map_err(|error| OakError::Database(error.to_string()))?;
match parent.flatten() {
Some(parent) => current = parent,
None => return Ok(None),
}
}
}
fn validate_capture_base_in_connection(
conn: &Connection,
capture_id: &str,
base_commit: Option<&Hash>,
base_tree: &Hash,
) -> Result<()> {
let Some(base_commit) = base_commit else {
if base_tree != &Tree::empty_hash() {
return Err(capture_integrity(
capture_id,
"a headless capture must use the canonical empty base tree",
));
}
return Ok(());
};
let manifest_hash: Option<String> = conn
.query_row(
"SELECT manifest_hash FROM commits WHERE hash = ?1",
[base_commit.as_str()],
|row| row.get(0),
)
.optional()
.map_err(|error| OakError::Database(error.to_string()))?;
let Some(manifest_hash) = manifest_hash else {
return Err(capture_integrity(
capture_id,
"the recorded base commit is unavailable",
));
};
let manifest_hash = parse_native_hash(capture_id, &manifest_hash, "base commit tree")?;
if &manifest_hash != base_tree {
return Err(capture_integrity(
capture_id,
"the descriptor base tree does not match the recorded base commit",
));
}
Ok(())
}
fn sqlite_open_error(path: &Path, error: rusqlite::Error) -> OakError {
if matches!(&error, rusqlite::Error::SqliteFailure(code, _) if code.code == rusqlite::ErrorCode::DiskFull)
{
return OakError::DiskFull {
path: path.display().to_string(),
};
}
diagnose_open_storage_error(path, OakError::Database(error.to_string()))
}
fn diagnose_open_storage_error(path: &Path, error: OakError) -> OakError {
let OakError::Database(message) = &error else {
return error;
};
let sqlite_full = message == "database or disk is full";
let may_be_full = message.starts_with("unable to open database file")
|| message.starts_with("disk I/O error");
if sqlite_full || (may_be_full && filesystem_is_full(path)) {
OakError::DiskFull {
path: path.display().to_string(),
}
} else {
error
}
}
#[cfg(unix)]
fn filesystem_is_full(path: &Path) -> bool {
use std::os::unix::ffi::OsStrExt;
let Some(existing) = path.ancestors().find(|p| p.exists()) else {
return false;
};
let Ok(path) = std::ffi::CString::new(existing.as_os_str().as_bytes()) else {
return false;
};
let mut stats = std::mem::MaybeUninit::<libc::statvfs>::uninit();
if unsafe { libc::statvfs(path.as_ptr(), stats.as_mut_ptr()) } != 0 {
return false;
}
let stats = unsafe { stats.assume_init() };
stats.f_bavail == 0
}
#[cfg(not(unix))]
fn filesystem_is_full(_path: &Path) -> bool {
false
}
const STORED_BLOB_READ_BYTES: usize = 1024 * 1024;
struct StoredBlobRows {
blob: rusqlite::blob::Blob<'static>,
_conn: Box<Connection>,
}
unsafe impl Send for StoredBlobRows {}
impl StoredBlobRows {
fn open(db_path: &Path, rowid: i64) -> Result<Self> {
let conn = Box::new(
Connection::open_with_flags(db_path, rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY)
.map_err(|e| OakError::Database(e.to_string()))?,
);
let blob = conn
.blob_open(rusqlite::MAIN_DB, "blobs", "content", rowid, true)
.map_err(|e| OakError::Database(e.to_string()))?;
let blob = unsafe {
std::mem::transmute::<rusqlite::blob::Blob<'_>, rusqlite::blob::Blob<'static>>(blob)
};
Ok(Self { blob, _conn: conn })
}
}
impl std::io::Read for StoredBlobRows {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
std::io::Read::read(&mut self.blob, buf)
}
}
enum StoredBlobReaderInner {
Raw(std::io::BufReader<StoredBlobRows>),
Zstd(Box<zstd::stream::read::Decoder<'static, std::io::BufReader<StoredBlobRows>>>),
}
pub struct StoredBlobReader {
inner: StoredBlobReaderInner,
}
impl std::io::Read for StoredBlobReader {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
match &mut self.inner {
StoredBlobReaderInner::Raw(rows) => rows.read(buf),
StoredBlobReaderInner::Zstd(decoder) => decoder.read(buf),
}
}
}
impl SqliteRepository {
fn with_read_txn<T>(conn: &Connection, f: impl FnOnce(&Connection) -> Result<T>) -> Result<T> {
if !conn.is_autocommit() {
return f(conn);
}
conn.execute_batch("BEGIN DEFERRED")
.map_err(|e| OakError::Database(e.to_string()))?;
let result = f(conn);
let end = conn
.execute_batch("COMMIT")
.map_err(|e| OakError::Database(e.to_string()));
let value = result?;
end?;
Ok(value)
}
pub fn blob_descriptors(&self, hashes: &[&Hash]) -> Result<Vec<Option<(u64, usize)>>> {
let conn = self.conn.lock().unwrap();
Self::with_read_txn(&conn, |conn| {
let db = |e: rusqlite::Error| OakError::Database(e.to_string());
let mut size_stmt = conn
.prepare_cached("SELECT size FROM blobs WHERE hash = ?1")
.map_err(db)?;
let mut chunks_stmt = conn
.prepare_cached("SELECT COUNT(*) FROM blob_chunks WHERE blob_hash = ?1")
.map_err(db)?;
hashes
.iter()
.map(|hash| {
let Some(size) = size_stmt
.query_row(params![hash.as_str()], |row| row.get::<_, i64>(0))
.optional()
.map_err(db)?
else {
return Ok(None);
};
let chunk_refs: i64 = chunks_stmt
.query_row(params![hash.as_str()], |row| row.get(0))
.map_err(db)?;
Ok(Some((size as u64, chunk_refs as usize)))
})
.collect()
})
}
pub fn open_read_only(db_path: &Path) -> Result<Self> {
let conn = Connection::open_with_flags(db_path, rusqlite::OpenFlags::SQLITE_OPEN_READ_ONLY)
.map_err(|e| OakError::Database(e.to_string()))?;
conn.busy_timeout(std::time::Duration::from_millis(100))
.map_err(|e| OakError::Database(e.to_string()))?;
conn.execute_batch("BEGIN DEFERRED")
.map_err(|e| OakError::Database(e.to_string()))?;
{
let mut stmt = conn
.prepare("SELECT version, succeeded FROM schema_migrations LIMIT 1000")
.map_err(|e| OakError::Database(e.to_string()))?;
let rows = stmt
.query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, bool>(1)?)))
.map_err(|e| OakError::Database(e.to_string()))?;
let versions = rows
.collect::<std::result::Result<Vec<_>, _>>()
.map_err(|e| OakError::Database(e.to_string()))?;
if !versions
.iter()
.any(|(version, ok)| *ok && version == "0000_baseline")
|| versions
.iter()
.any(|(version, ok)| !ok || !MIGRATIONS.iter().any(|m| m.version == version))
{
return Err(OakError::Database(
"unsupported schema for read-only inspection".into(),
));
}
}
Ok(Self {
conn: Mutex::new(conn),
tree_flattens: AtomicU64::new(0),
tree_reads: AtomicU64::new(0),
})
}
pub fn open(db_path: &Path) -> Result<Self> {
(|| -> Result<Self> {
let conn = Connection::open(db_path).map_err(|e| sqlite_open_error(db_path, e))?;
conn.busy_timeout(std::time::Duration::from_secs(5))
.map_err(|e| sqlite_open_error(db_path, e))?;
harden_db_file_permissions(db_path);
conn.execute_batch("PRAGMA journal_mode=WAL;")
.map_err(|e| sqlite_open_error(db_path, e))?;
run_migrations(&conn)?;
Ok(SqliteRepository {
conn: Mutex::new(conn),
tree_flattens: AtomicU64::new(0),
tree_reads: AtomicU64::new(0),
})
})()
.map_err(|error| diagnose_open_storage_error(db_path, error))
}
pub fn open_relaxed(db_path: &Path) -> Result<Self> {
(|| -> Result<Self> {
let conn = Connection::open(db_path).map_err(|e| sqlite_open_error(db_path, e))?;
conn.busy_timeout(std::time::Duration::from_secs(5))
.map_err(|e| sqlite_open_error(db_path, e))?;
harden_db_file_permissions(db_path);
conn.execute_batch("PRAGMA journal_mode=WAL; PRAGMA foreign_keys=OFF;")
.map_err(|e| sqlite_open_error(db_path, e))?;
run_migrations(&conn)?;
Ok(SqliteRepository {
conn: Mutex::new(conn),
tree_flattens: AtomicU64::new(0),
tree_reads: AtomicU64::new(0),
})
})()
.map_err(|error| diagnose_open_storage_error(db_path, error))
}
pub fn stored_blob_reader(&self, hash: &Hash) -> Result<Option<(u64, StoredBlobReader)>> {
let row: Option<(i64, i64, i64)> = {
let conn = self.conn.lock().unwrap();
conn.query_row(
"SELECT rowid, size, codec FROM blobs WHERE hash = ?1",
params![hash.as_str()],
|row| Ok((row.get(0)?, row.get(1)?, row.get(2)?)),
)
.optional()
.map_err(|e| OakError::Database(e.to_string()))?
};
let Some((rowid, size, codec)) = row else {
return Ok(None);
};
let rows = std::io::BufReader::with_capacity(
STORED_BLOB_READ_BYTES,
StoredBlobRows::open(&self.database_path()?, rowid)?,
);
let inner = if codec == CODEC_ZSTD {
StoredBlobReaderInner::Zstd(Box::new(
zstd::stream::read::Decoder::with_buffer(rows)
.map_err(|e| OakError::Database(format!("zstd decompress: {e}")))?,
))
} else {
StoredBlobReaderInner::Raw(rows)
};
Ok(Some((size as u64, StoredBlobReader { inner })))
}
pub fn database_path(&self) -> Result<PathBuf> {
let conn = self.conn.lock().unwrap();
conn.path()
.map(PathBuf::from)
.ok_or_else(|| OakError::Database("repository is not file-backed".to_string()))
}
#[allow(clippy::too_many_arguments)]
pub fn store_change_capture(
&self,
capture_id: &str,
change_set: &CanonicalChangeSetV1,
owner: &str,
name: &str,
origin: Option<&str>,
source_branch: &str,
base_commit: Option<&Hash>,
) -> Result<()> {
validate_capture_id(capture_id)?;
validate_capture_provenance(capture_id, owner, name, origin, source_branch, base_commit)?;
let (scope_kind, scope_paths_json, descriptor_json) =
self.validate_change_set_objects(capture_id, change_set)?;
let now = chrono::Utc::now().to_rfc3339();
let mut conn = self.conn.lock().unwrap();
let tx = conn
.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)
.map_err(|error| OakError::Database(error.to_string()))?;
let current_branch: Option<String> = tx
.query_row(
"SELECT value FROM metadata WHERE key = ?1",
[MetadataKey::CurrentBranch.as_str()],
|row| row.get(0),
)
.optional()
.map_err(|error| OakError::Database(error.to_string()))?;
let current_head = effective_head_in_connection(&tx, source_branch)?;
if current_branch.as_deref() != Some(source_branch) || current_head.as_ref() != base_commit
{
return Err(OakError::WorktreeCaptureRaced {
detail: "the current branch or effective HEAD changed before durable capture publication"
.to_string(),
});
}
validate_capture_base_in_connection(&tx, capture_id, base_commit, change_set.base_tree())?;
tx.execute(
"INSERT OR IGNORE INTO change_sets
(change_id, schema_version, object_format, base_tree_hash, result_tree_hash,
scope_kind, scope_paths_json, descriptor_json, created_at)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9)",
params![
change_set.id().as_str(),
i64::from(change_set.schema_version()),
change_set.object_format(),
change_set.base_tree().as_str(),
change_set.result_tree().as_str(),
scope_kind,
scope_paths_json,
descriptor_json,
now,
],
)
.map_err(|error| OakError::Database(error.to_string()))?;
let stored: (i64, String, String, String, String, String, String) = tx
.query_row(
"SELECT schema_version, object_format, base_tree_hash, result_tree_hash,
scope_kind, scope_paths_json, descriptor_json
FROM change_sets WHERE change_id = ?1",
[change_set.id().as_str()],
|row| {
Ok((
row.get(0)?,
row.get(1)?,
row.get(2)?,
row.get(3)?,
row.get(4)?,
row.get(5)?,
row.get(6)?,
))
},
)
.map_err(|_| capture_integrity(capture_id, "stored descriptor row is unreadable"))?;
if stored
!= (
i64::from(change_set.schema_version()),
change_set.object_format().to_string(),
change_set.base_tree().to_string(),
change_set.result_tree().to_string(),
scope_kind.to_string(),
scope_paths_json.clone(),
descriptor_json.clone(),
)
{
return Err(capture_integrity(
capture_id,
"change ID already names a different or corrupt descriptor",
));
}
tx.execute(
"INSERT INTO change_captures
(capture_id, change_id, format_version, repo_owner, repo_name, origin,
source_branch, base_commit_hash, captured_at)
VALUES (?1, ?2, 1, ?3, ?4, ?5, ?6, ?7, ?8)",
params![
capture_id,
change_set.id().as_str(),
owner,
name,
origin,
source_branch,
base_commit.map(Hash::as_str),
now,
],
)
.map_err(|_| {
capture_integrity(
capture_id,
"capture occurrence ID already exists or provenance could not be stored",
)
})?;
tx.commit()
.map_err(|error| OakError::Database(error.to_string()))?;
Ok(())
}
pub fn get_change_capture(&self, capture_id: &str) -> Result<StoredChangeCapture> {
if validate_capture_id(capture_id).is_err() {
return Err(OakError::ChangeCaptureNotFound {
capture_id: capture_id.to_string(),
});
}
let conn = self.conn.lock().unwrap();
let occurrence: Option<ChangeCaptureRow> = conn
.query_row(
"SELECT change_id, format_version, repo_owner, repo_name, origin,
source_branch, base_commit_hash, captured_at
FROM change_captures WHERE capture_id = ?1",
[capture_id],
|row| {
Ok(ChangeCaptureRow {
change_id: row.get(0)?,
format_version: row.get(1)?,
owner: row.get(2)?,
name: row.get(3)?,
origin: row.get(4)?,
source_branch: row.get(5)?,
base_commit: row.get(6)?,
captured_at: row.get(7)?,
})
},
)
.optional()
.map_err(|_| capture_integrity(capture_id, "capture occurrence row is unreadable"))?;
let Some(ChangeCaptureRow {
change_id,
format_version,
owner,
name,
origin,
source_branch,
base_commit,
captured_at,
}) = occurrence
else {
return Err(OakError::ChangeCaptureNotFound {
capture_id: capture_id.to_string(),
});
};
if format_version != 1 {
return Err(capture_integrity(
capture_id,
"unsupported capture occurrence format",
));
}
let base_commit = base_commit
.as_deref()
.map(|value| parse_native_hash(capture_id, value, "base commit"))
.transpose()?;
validate_capture_provenance(
capture_id,
&owner,
&name,
origin.as_deref(),
&source_branch,
base_commit.as_ref(),
)?;
let captured_at = chrono::DateTime::parse_from_rfc3339(&captured_at)
.map_err(|_| capture_integrity(capture_id, "invalid capture timestamp"))?
.with_timezone(&chrono::Utc);
let descriptor: Option<ChangeSetRow> = conn
.query_row(
"SELECT schema_version, object_format, base_tree_hash, result_tree_hash,
scope_kind, scope_paths_json, descriptor_json, created_at
FROM change_sets WHERE change_id = ?1",
[change_id.as_str()],
|row| {
Ok(ChangeSetRow {
schema_version: row.get(0)?,
object_format: row.get(1)?,
base_tree: row.get(2)?,
result_tree: row.get(3)?,
scope_kind: row.get(4)?,
scope_paths_json: row.get(5)?,
descriptor_json: row.get(6)?,
created_at: row.get(7)?,
})
},
)
.optional()
.map_err(|_| capture_integrity(capture_id, "stored descriptor row is unreadable"))?;
drop(conn);
let Some(ChangeSetRow {
schema_version,
object_format,
base_tree,
result_tree,
scope_kind,
scope_paths_json,
descriptor_json,
created_at: descriptor_created_at,
}) = descriptor
else {
return Err(capture_integrity(
capture_id,
"capture occurrence references a missing descriptor",
));
};
if schema_version != 1 || object_format != CHANGE_SET_OBJECT_FORMAT_V1 {
return Err(capture_integrity(
capture_id,
"unsupported or corrupt change descriptor format",
));
}
chrono::DateTime::parse_from_rfc3339(&descriptor_created_at)
.map_err(|_| capture_integrity(capture_id, "invalid descriptor timestamp"))?;
let base_tree = parse_native_hash(capture_id, &base_tree, "base tree")?;
let result_tree = parse_native_hash(capture_id, &result_tree, "result tree")?;
let stored_change_id = parse_native_hash(capture_id, &change_id, "change ID")?;
let paths: Vec<String> = serde_json::from_str(&scope_paths_json)
.map_err(|_| capture_integrity(capture_id, "invalid descriptor scope"))?;
let scope = match scope_kind.as_str() {
"full" if scope_paths_json == "[]" => ChangeScopeV1::Full,
"paths" => ChangeScopeV1::Paths { paths },
_ => return Err(capture_integrity(capture_id, "invalid descriptor scope")),
};
let base_manifest = self
.get_manifest(&base_tree)
.map_err(|_| capture_integrity(capture_id, "base tree could not be validated"))?
.ok_or_else(|| capture_integrity(capture_id, "base tree is missing"))?;
let result_manifest = self
.get_manifest(&result_tree)
.map_err(|_| capture_integrity(capture_id, "result tree could not be validated"))?
.ok_or_else(|| capture_integrity(capture_id, "result tree is missing"))?;
let change_set =
CanonicalChangeSetV1::from_manifests(&base_manifest, &result_manifest, scope)
.map_err(|_| capture_integrity(capture_id, "descriptor could not be rederived"))?;
let canonical_json = serde_json::to_string(&change_set)
.map_err(|_| capture_integrity(capture_id, "descriptor could not be encoded"))?;
let canonical_scope_json = match change_set.scope() {
ChangeScopeV1::Full => "[]".to_string(),
ChangeScopeV1::Paths { paths } => serde_json::to_string(paths).map_err(|_| {
capture_integrity(capture_id, "descriptor scope could not be encoded")
})?,
};
let expected_scope_kind = match change_set.scope() {
ChangeScopeV1::Full => "full",
ChangeScopeV1::Paths { .. } => "paths",
};
if change_set.id() != &stored_change_id
|| change_set.base_tree() != &base_tree
|| change_set.result_tree() != &result_tree
|| scope_kind != expected_scope_kind
|| scope_paths_json != canonical_scope_json
|| descriptor_json != canonical_json
{
return Err(capture_integrity(
capture_id,
"stored descriptor does not match its canonical tree-derived identity",
));
}
match base_commit.as_ref() {
Some(base_commit) => {
let commit = self
.get_commit(base_commit)
.map_err(|_| {
capture_integrity(capture_id, "the recorded base commit is unavailable")
})?
.ok_or_else(|| {
capture_integrity(capture_id, "the recorded base commit is unavailable")
})?;
if commit.manifest_hash != base_tree {
return Err(capture_integrity(
capture_id,
"the descriptor base tree does not match the recorded base commit",
));
}
}
None if base_tree != Tree::empty_hash() => {
return Err(capture_integrity(
capture_id,
"a headless capture must use the canonical empty base tree",
));
}
None => {}
}
Ok(StoredChangeCapture {
capture_id: capture_id.to_string(),
change_set,
provenance: ChangeCaptureProvenance {
owner,
name,
origin,
source_branch,
base_commit,
captured_at,
},
})
}
fn validate_change_set_objects(
&self,
capture_id: &str,
change_set: &CanonicalChangeSetV1,
) -> Result<(&'static str, String, String)> {
let base = self
.get_manifest(change_set.base_tree())
.map_err(|_| capture_integrity(capture_id, "base tree could not be validated"))?
.ok_or_else(|| capture_integrity(capture_id, "base tree is missing"))?;
let result = self
.get_manifest(change_set.result_tree())
.map_err(|_| capture_integrity(capture_id, "result tree could not be validated"))?
.ok_or_else(|| capture_integrity(capture_id, "result tree is missing"))?;
let rederived =
CanonicalChangeSetV1::from_manifests(&base, &result, change_set.scope().clone())
.map_err(|_| capture_integrity(capture_id, "descriptor could not be rederived"))?;
if &rederived != change_set {
return Err(capture_integrity(
capture_id,
"descriptor does not match its canonical tree-derived identity",
));
}
let (scope_kind, scope_paths_json) = match change_set.scope() {
ChangeScopeV1::Full => ("full", "[]".to_string()),
ChangeScopeV1::Paths { paths } => (
"paths",
serde_json::to_string(paths).map_err(|_| {
capture_integrity(capture_id, "descriptor scope could not be encoded")
})?,
),
};
let descriptor_json = serde_json::to_string(change_set)
.map_err(|_| capture_integrity(capture_id, "descriptor could not be encoded"))?;
Ok((scope_kind, scope_paths_json, descriptor_json))
}
pub fn set_foreign_keys(&self, enabled: bool) -> Result<()> {
let conn = self.conn.lock().unwrap();
let pragma = if enabled {
"PRAGMA foreign_keys=ON;"
} else {
"PRAGMA foreign_keys=OFF;"
};
conn.execute_batch(pragma)
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(())
}
pub fn upsert_branch_metadata_at_generation(
&self,
branch: &Branch,
observed_generation: i64,
) -> Result<bool> {
self.reconcile_branch_metadata(branch, true, observed_generation)
}
pub fn reconcile_pulled_branch_metadata(
&self,
branch: &Branch,
observed_generation: i64,
) -> Result<bool> {
self.reconcile_branch_metadata(branch, false, observed_generation)
}
pub fn description_generation(&self) -> Result<i64> {
self.conn
.lock()
.unwrap()
.query_row(
"SELECT generation FROM branch_description_clock WHERE singleton = 1",
[],
|row| row.get(0),
)
.map_err(|error| OakError::Database(error.to_string()))
}
fn reconcile_branch_metadata(
&self,
branch: &Branch,
refresh_lifecycle: bool,
observed_generation: i64,
) -> Result<bool> {
let mut conn = self.conn.lock().unwrap();
let tx = conn
.savepoint()
.map_err(|error| OakError::Database(error.to_string()))?;
let generation = advance_description_generation(&tx)?;
let fresh = tx
.query_row(
"SELECT description_generation <= ?2 FROM branches WHERE name = ?1",
params![branch.name, observed_generation],
|row| row.get::<_, bool>(0),
)
.optional()
.map_err(|error| OakError::Database(error.to_string()))?
.unwrap_or(true);
tx.execute(
"INSERT INTO branches (name, description, parent_branch, status, close_reason, created_at, description_generation) \
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?9) \
ON CONFLICT(name) DO UPDATE SET \
description = CASE WHEN branches.description_generation <= ?8 AND branches.description_pending = 0 THEN excluded.description ELSE branches.description END, \
description_pending = CASE WHEN branches.description_generation <= ?8 AND branches.description IS excluded.description THEN 0 ELSE branches.description_pending END, \
description_generation = CASE WHEN branches.description_generation <= ?8 AND (branches.description_pending = 0 OR branches.description IS excluded.description) THEN ?9 ELSE branches.description_generation END, \
parent_branch = CASE WHEN ?7 OR excluded.parent_branch IS NOT NULL THEN excluded.parent_branch ELSE branches.parent_branch END, \
status = CASE WHEN ?7 THEN excluded.status ELSE branches.status END, \
close_reason = CASE WHEN ?7 THEN excluded.close_reason ELSE branches.close_reason END, \
created_at = CASE WHEN ?7 THEN excluded.created_at ELSE branches.created_at END",
params![
branch.name,
branch.description,
branch.parent_branch,
branch.status.as_str(),
branch.close_reason.as_ref().map(|r| r.as_str()),
branch.created_at.to_rfc3339(),
refresh_lifecycle,
observed_generation,
generation,
],
)
.map_err(|e| OakError::Database(e.to_string()))?;
tx.commit()
.map_err(|error| OakError::Database(error.to_string()))?;
Ok(fresh)
}
pub fn branch_description_pending(&self, name: &str) -> Result<bool> {
let conn = self.conn.lock().unwrap();
conn.query_row(
"SELECT description_pending != 0 FROM branches WHERE name = ?1",
[name],
|row| row.get(0),
)
.optional()
.map(|value| value.unwrap_or(false))
.map_err(|error| OakError::Database(error.to_string()))
}
pub fn bulk_begin(&self) -> Result<()> {
let conn = self.conn.lock().unwrap();
conn.execute_batch("PRAGMA synchronous=NORMAL; BEGIN;")
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(())
}
pub fn bulk_flush(&self) -> Result<()> {
let conn = self.conn.lock().unwrap();
conn.execute_batch("COMMIT; BEGIN;")
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(())
}
pub fn bulk_commit(&self) -> Result<()> {
let conn = self.conn.lock().unwrap();
conn.execute_batch("COMMIT; PRAGMA synchronous=FULL;")
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(())
}
pub fn bulk_rollback(&self) {
if let Ok(conn) = self.conn.lock() {
let _ = conn.execute_batch("ROLLBACK; PRAGMA synchronous=FULL;");
}
}
pub fn write_txn_begin(&self) -> Result<()> {
let conn = self.conn.lock().unwrap();
conn.execute_batch("BEGIN IMMEDIATE;")
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(())
}
pub fn write_txn_commit(&self) -> Result<()> {
let conn = self.conn.lock().unwrap();
conn.execute_batch("COMMIT;")
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(())
}
pub fn write_txn_rollback(&self) {
if let Ok(conn) = self.conn.lock() {
let _ = conn.execute_batch("ROLLBACK;");
}
}
pub fn delete_metadata(&self, key: MetadataKey) -> Result<()> {
let conn = self.conn.lock().unwrap();
conn.execute("DELETE FROM metadata WHERE key = ?1", params![key.as_str()])
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(())
}
#[cfg(test)]
pub fn in_memory() -> Result<Self> {
let conn = Connection::open_in_memory().map_err(|e| OakError::Database(e.to_string()))?;
run_migrations(&conn)?;
Ok(SqliteRepository {
conn: Mutex::new(conn),
tree_flattens: AtomicU64::new(0),
tree_reads: AtomicU64::new(0),
})
}
pub fn bulk_importer(&self, batch_size: usize) -> Result<BulkImporter> {
let conn = self.conn.lock().unwrap();
let path = conn
.path()
.ok_or_else(|| {
OakError::Database("bulk importer requires a file-backed connection".to_string())
})?
.to_string();
drop(conn);
BulkImporter::open(Path::new(&path), batch_size)
}
pub fn compact_storage(&self) -> Result<CompactStats> {
let mut conn = self.conn.lock().unwrap();
let legacy_trees: Vec<String> = {
let table = table_exists(&conn, "tree_entries")
.map_err(|e| OakError::Database(e.to_string()))?;
if !table {
Vec::new() } else {
let mut stmt = conn
.prepare("SELECT hash FROM trees WHERE content IS NULL")
.map_err(|e| OakError::Database(e.to_string()))?;
let rows = stmt
.query_map([], |r| r.get::<_, String>(0))
.map_err(|e| OakError::Database(e.to_string()))?;
rows.collect::<std::result::Result<Vec<_>, _>>()
.map_err(|e| OakError::Database(e.to_string()))?
}
};
let mut trees_backfilled = 0u64;
if !legacy_trees.is_empty() {
let tx = conn
.transaction()
.map_err(|e| OakError::Database(e.to_string()))?;
for h in &legacy_trees {
let hash = Hash(h.clone());
let tree = read_tree_entries(&tx, &hash)?;
if hash_bytes(&tree.canonical_bytes()) != hash {
return Err(OakError::Database(format!(
"tree {h} failed integrity check during compaction"
)));
}
let content = zstd_compress(&tree.canonical_bytes())?;
tx.execute(
"UPDATE trees SET content = ?2 WHERE hash = ?1",
params![h, content],
)
.map_err(|e| OakError::Database(e.to_string()))?;
trees_backfilled += 1;
}
tx.commit().map_err(|e| OakError::Database(e.to_string()))?;
}
let remaining_null: i64 = conn
.query_row(
"SELECT COUNT(*) FROM trees WHERE content IS NULL",
[],
|r| r.get(0),
)
.map_err(|e| OakError::Database(e.to_string()))?;
let tree_entries_dropped = if remaining_null == 0
&& table_exists(&conn, "tree_entries").map_err(|e| OakError::Database(e.to_string()))?
{
conn.execute_batch("DROP TABLE IF EXISTS tree_entries;")
.map_err(|e| OakError::Database(e.to_string()))?;
true
} else {
false
};
let raw_blobs: Vec<String> = {
let mut stmt = conn
.prepare("SELECT hash FROM blobs WHERE codec = 0")
.map_err(|e| OakError::Database(e.to_string()))?;
let rows = stmt
.query_map([], |r| r.get::<_, String>(0))
.map_err(|e| OakError::Database(e.to_string()))?;
rows.collect::<std::result::Result<Vec<_>, _>>()
.map_err(|e| OakError::Database(e.to_string()))?
};
let mut blobs_recompressed = 0u64;
if !raw_blobs.is_empty() {
let tx = conn
.transaction()
.map_err(|e| OakError::Database(e.to_string()))?;
for h in &raw_blobs {
let content: Vec<u8> = tx
.query_row(
"SELECT content FROM blobs WHERE hash = ?1",
params![h],
|r| r.get(0),
)
.map_err(|e| OakError::Database(e.to_string()))?;
let compressed = zstd_compress(&content)?;
if compressed.len() < content.len() {
tx.execute(
"UPDATE blobs SET content = ?2, codec = 1 WHERE hash = ?1",
params![h, compressed],
)
.map_err(|e| OakError::Database(e.to_string()))?;
blobs_recompressed += 1;
}
}
tx.commit().map_err(|e| OakError::Database(e.to_string()))?;
}
conn.execute_batch("VACUUM;")
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(CompactStats {
trees_backfilled,
tree_entries_dropped,
blobs_recompressed,
})
}
pub fn replace_blob_chunks(&self, blob_hash: &Hash, chunks: &[ChunkInfo]) -> Result<()> {
let mut conn = self.conn.lock().unwrap();
let tx = conn
.savepoint()
.map_err(|e| OakError::Database(e.to_string()))?;
tx.execute(
"DELETE FROM blob_chunks WHERE blob_hash = ?1",
params![blob_hash.as_str()],
)
.map_err(|e| OakError::Database(e.to_string()))?;
for (index, chunk) in chunks.iter().enumerate() {
tx.execute(
"INSERT INTO blob_chunks (blob_hash, chunk_hash, chunk_index, offset, size) VALUES (?1, ?2, ?3, ?4, ?5)",
params![
blob_hash.as_str(),
chunk.hash.as_str(),
index as i64,
chunk.offset as i64,
chunk.length as i64,
],
)
.map_err(|e| OakError::Database(e.to_string()))?;
}
tx.commit().map_err(|e| OakError::Database(e.to_string()))?;
Ok(())
}
pub fn delete_chunk(&self, hash: &Hash) -> Result<()> {
let conn = self.conn.lock().unwrap();
conn.execute("DELETE FROM chunks WHERE hash = ?1", params![hash.as_str()])
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(())
}
fn publish_commit(&self, commit: &Commit, trees: &[Tree], require_root: bool) -> Result<()> {
let mut conn = self.conn.lock().unwrap();
let tx = conn
.transaction()
.map_err(|e| OakError::Database(e.to_string()))?;
for tree in trees {
let already_present: bool = tx
.query_row(
"SELECT EXISTS(SELECT 1 FROM trees WHERE hash = ?1)",
params![tree.hash.as_str()],
|row| row.get(0),
)
.map_err(|e| OakError::Database(e.to_string()))?;
if already_present {
continue;
}
insert_tree_row(&tx, tree)?;
}
if require_root {
let root = &commit.manifest_hash;
let root_present: bool = *root == Tree::empty_hash()
|| tx
.query_row(
"SELECT EXISTS(SELECT 1 FROM trees WHERE hash = ?1)",
params![root.as_str()],
|row| row.get(0),
)
.map_err(|e| OakError::Database(e.to_string()))?;
if !root_present {
return Err(OakError::ManifestNotFound(root.to_string()));
}
}
let inserted = tx
.execute(
"INSERT OR IGNORE INTO commits (hash, branch_name, parent_hash, merge_parent_hash, manifest_hash, author, message, timestamp) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
params![
commit.hash.as_str(),
commit.branch_name.as_str(),
commit.parent_hash.as_ref().map(|h| h.as_str()),
commit.merge_parent_hash.as_ref().map(|h| h.as_str()),
commit.manifest_hash.as_str(),
commit.author.as_str(),
commit.message.as_deref(),
commit.timestamp.to_rfc3339(),
],
)
.map_err(|e| OakError::Database(e.to_string()))?;
if inserted != 0 {
for file in &commit.files {
let change_type_str = match file.change_type {
ChangeType::Added => "added",
ChangeType::Modified => "modified",
ChangeType::Deleted => "deleted",
ChangeType::Renamed => "renamed",
};
tx.execute(
"INSERT INTO commit_files (commit_hash, path, change_type, old_blob_hash, new_blob_hash, old_path) VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
params![
commit.hash.as_str(),
file.path.as_str(),
change_type_str,
file.old_blob_hash.as_ref().map(|h| h.as_str()),
file.new_blob_hash.as_ref().map(|h| h.as_str()),
file.old_path.as_deref(),
],
)
.map_err(|e| OakError::Database(e.to_string()))?;
}
}
tx.execute(
"INSERT OR REPLACE INTO branch_heads (branch_name, head_hash) VALUES (?1, ?2)",
params![commit.branch_name.as_str(), commit.hash.as_str()],
)
.map_err(|e| OakError::Database(e.to_string()))?;
tx.execute(
"INSERT OR REPLACE INTO metadata (key, value) VALUES (?1, ?2)",
params![MetadataKey::Head.as_str(), commit.hash.as_str()],
)
.map_err(|e| OakError::Database(e.to_string()))?;
tx.commit().map_err(|e| OakError::Database(e.to_string()))?;
Ok(())
}
}
#[derive(Debug, Clone, Default)]
pub struct CompactStats {
pub trees_backfilled: u64,
pub tree_entries_dropped: bool,
pub blobs_recompressed: u64,
}
pub struct BulkImporter {
conn: Connection,
tree_cache: HashSet<Hash>,
uncommitted: usize,
batch_size: usize,
in_tx: bool,
}
impl BulkImporter {
fn open(db_path: &Path, batch_size: usize) -> Result<Self> {
let conn = Connection::open(db_path).map_err(|e| OakError::Database(e.to_string()))?;
conn.execute_batch(
"PRAGMA journal_mode=WAL;
PRAGMA synchronous=NORMAL;
PRAGMA temp_store=MEMORY;
PRAGMA cache_size=-262144;
BEGIN;",
)
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(BulkImporter {
conn,
tree_cache: HashSet::new(),
uncommitted: 0,
batch_size: batch_size.max(1),
in_tx: true,
})
}
pub fn put_blob(&mut self, content: Vec<u8>) -> Result<Hash> {
let blob = Blob::new(content);
let (stored, codec) = encode_blob(&blob.content)?;
self.conn
.execute(
"INSERT OR IGNORE INTO blobs (hash, content, size, codec) VALUES (?1, ?2, ?3, ?4)",
params![blob.hash.as_str(), stored.as_ref(), blob.size as i64, codec],
)
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(blob.hash)
}
pub fn put_tree(&mut self, entries: Vec<ManifestEntry>) -> Result<Hash> {
let built = crate::build_tree(&entries)?;
for tree in &built.trees {
if !self.tree_cache.insert(tree.hash.clone()) {
continue;
}
insert_tree_row(&self.conn, tree)?;
}
Ok(built.root_hash)
}
pub fn put_tree_node(&mut self, tree: &Tree) -> Result<()> {
if self.tree_cache.insert(tree.hash.clone()) {
insert_tree_row(&self.conn, tree)?;
}
Ok(())
}
pub fn store_commit(&mut self, commit: &Commit) -> Result<()> {
let inserted = self
.conn
.execute(
"INSERT OR IGNORE INTO commits (hash, branch_name, parent_hash, merge_parent_hash, manifest_hash, author, message, timestamp) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
params![
commit.hash.as_str(),
commit.branch_name,
commit.parent_hash.as_ref().map(|h| h.as_str()),
commit.merge_parent_hash.as_ref().map(|h| h.as_str()),
commit.manifest_hash.as_str(),
commit.author,
commit.message,
commit.timestamp.to_rfc3339(),
],
)
.map_err(|e| OakError::Database(e.to_string()))?;
if inserted == 0 {
return Ok(());
}
for file in &commit.files {
let change_type_str = match file.change_type {
ChangeType::Added => "added",
ChangeType::Modified => "modified",
ChangeType::Deleted => "deleted",
ChangeType::Renamed => "renamed",
};
self.conn
.execute(
"INSERT INTO commit_files (commit_hash, path, change_type, old_blob_hash, new_blob_hash, old_path) VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
params![
commit.hash.as_str(),
file.path,
change_type_str,
file.old_blob_hash.as_ref().map(|h| h.as_str()),
file.new_blob_hash.as_ref().map(|h| h.as_str()),
file.old_path,
],
)
.map_err(|e| OakError::Database(e.to_string()))?;
}
self.uncommitted += 1;
if self.uncommitted >= self.batch_size {
self.flush_batch()?;
}
Ok(())
}
fn flush_batch(&mut self) -> Result<()> {
if !self.in_tx {
return Ok(());
}
self.conn
.execute_batch("COMMIT; BEGIN;")
.map_err(|e| OakError::Database(e.to_string()))?;
self.uncommitted = 0;
Ok(())
}
pub fn finish(mut self) -> Result<()> {
if self.in_tx {
self.conn
.execute_batch("COMMIT;")
.map_err(|e| OakError::Database(e.to_string()))?;
self.in_tx = false;
}
Ok(())
}
}
impl Drop for BulkImporter {
fn drop(&mut self) {
if self.in_tx {
let _ = self.conn.execute_batch("ROLLBACK;");
}
}
}
const BLOB_BATCH_BUDGET_BYTES: i64 = 64 * 1024 * 1024;
const BLOB_STREAMING_THRESHOLD_BYTES: i64 = 8 * 1024 * 1024;
const BLOB_INSERT_SQL: &str =
"INSERT OR IGNORE INTO blobs (hash, content, size, codec) VALUES (?1, ?2, ?3, ?4)";
fn source_charge(source: &BlobSource) -> i64 {
source.size_hint.map_or(BLOB_BATCH_BUDGET_BYTES, |s| {
(s.min(i64::MAX as u64) as i64).min(BLOB_BATCH_BUDGET_BYTES)
})
}
struct ByteBudget {
remaining: std::sync::Mutex<i64>,
available: std::sync::Condvar,
}
impl ByteBudget {
fn new(cap: i64) -> Self {
Self {
remaining: std::sync::Mutex::new(cap),
available: std::sync::Condvar::new(),
}
}
fn acquire(&self, n: i64) {
let mut remaining = self.remaining.lock().unwrap();
while *remaining < n {
remaining = self.available.wait(remaining).unwrap();
}
*remaining -= n;
}
fn release(&self, n: i64) {
*self.remaining.lock().unwrap() += n;
self.available.notify_all();
}
fn close(&self) {
*self.remaining.lock().unwrap() = i64::MAX;
self.available.notify_all();
}
}
impl Repository for SqliteRepository {
fn store_blob(&self, blob: &Blob) -> Result<()> {
let conn = self.conn.lock().unwrap();
let (stored, codec) = encode_blob(&blob.content)?;
conn.execute(
"INSERT OR IGNORE INTO blobs (hash, content, size, codec) VALUES (?1, ?2, ?3, ?4)",
params![blob.hash.as_str(), stored.as_ref(), blob.size as i64, codec],
)
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(())
}
fn put_blob_sources(&self, sources: Vec<BlobSource>) -> Result<Vec<Hash>> {
if sources.is_empty() {
return Ok(Vec::new());
}
if sources.len() == 1 {
let content = (sources.into_iter().next().unwrap().read)()?;
return Ok(vec![self.put_blob(content)?]);
}
use rayon::prelude::*;
let total_hinted = sources
.iter()
.map(source_charge)
.fold(0i64, |acc, charge| acc.saturating_add(charge));
if total_hinted <= BLOB_STREAMING_THRESHOLD_BYTES {
let rows: Vec<(Hash, Vec<u8>, i64, i64)> = sources
.into_par_iter()
.map(|source| -> Result<(Hash, Vec<u8>, i64, i64)> {
let blob = Blob::new((source.read)()?);
let size = blob.size as i64;
let (encoded, codec) = encode_blob(&blob.content)?;
let stored = match encoded {
std::borrow::Cow::Owned(v) => v,
std::borrow::Cow::Borrowed(_) => blob.content,
};
Ok((blob.hash, stored, size, codec))
})
.collect::<Result<Vec<_>>>()?;
let mut conn = self.conn.lock().unwrap();
let tx = conn
.transaction()
.map_err(|e| OakError::Database(e.to_string()))?;
{
let mut stmt = tx
.prepare(BLOB_INSERT_SQL)
.map_err(|e| OakError::Database(e.to_string()))?;
for (hash, stored, size, codec) in &rows {
stmt.execute(params![hash.as_str(), stored, size, codec])
.map_err(|e| OakError::Database(e.to_string()))?;
}
}
tx.commit().map_err(|e| OakError::Database(e.to_string()))?;
return Ok(rows.into_iter().map(|(hash, _, _, _)| hash).collect());
}
let n = sources.len();
let budget = ByteBudget::new(BLOB_BATCH_BUDGET_BYTES);
type Row = (usize, Hash, Vec<u8>, i64, i64, i64);
let (sender, receiver) = std::sync::mpsc::channel::<Row>();
let mut hashes: Vec<Option<Hash>> = Vec::with_capacity(n);
hashes.resize_with(n, || None);
let (producer_result, consumer_result) = std::thread::scope(|scope| {
let budget_ref = &budget;
let producer = scope.spawn(move || -> Result<()> {
sources
.into_par_iter()
.enumerate()
.try_for_each(|(idx, source)| -> Result<()> {
let charge = source_charge(&source);
budget_ref.acquire(charge);
let blob = Blob::new((source.read)()?);
let size = blob.size as i64;
let (encoded, codec) = encode_blob(&blob.content)?;
let stored = match encoded {
std::borrow::Cow::Owned(v) => v,
std::borrow::Cow::Borrowed(_) => blob.content,
};
sender
.send((idx, blob.hash, stored, size, codec, charge))
.map_err(|_| {
OakError::Database("blob batch insert aborted".to_string())
})
})
});
let consumer_result = (|| -> Result<()> {
let mut conn = self.conn.lock().unwrap();
let tx = conn
.transaction()
.map_err(|e| OakError::Database(e.to_string()))?;
{
let mut stmt = tx
.prepare(BLOB_INSERT_SQL)
.map_err(|e| OakError::Database(e.to_string()))?;
for (idx, hash, stored, size, codec, charge) in &receiver {
let inserted = stmt
.execute(params![hash.as_str(), stored, size, codec])
.map_err(|e| OakError::Database(e.to_string()));
drop(stored);
budget_ref.release(charge);
inserted?;
hashes[idx] = Some(hash);
}
}
tx.commit().map_err(|e| OakError::Database(e.to_string()))
})();
drop(receiver);
budget.close();
let producer_result = match producer.join() {
Ok(result) => result,
Err(_) => Err(OakError::Database(
"blob batch producer panicked".to_string(),
)),
};
(producer_result, consumer_result)
});
consumer_result?;
producer_result?;
Ok(hashes
.into_iter()
.map(|h| h.expect("every source yields exactly one inserted row"))
.collect())
}
fn get_blob(&self, hash: &Hash) -> Result<Option<Blob>> {
let conn = self.conn.lock().unwrap();
let result: Option<(Vec<u8>, i64, i64)> = conn
.prepare_cached("SELECT content, size, codec FROM blobs WHERE hash = ?1")
.and_then(|mut stmt| {
stmt.query_row(params![hash.as_str()], |row| {
Ok((row.get(0)?, row.get(1)?, row.get(2)?))
})
.optional()
})
.map_err(|e| OakError::Database(e.to_string()))?;
match result {
None => Ok(None),
Some((content, size, codec)) => Ok(Some(Blob {
hash: hash.clone(),
content: decode_blob(content, codec)?,
size: size as u64,
})),
}
}
fn has_blob(&self, hash: &Hash) -> Result<bool> {
let conn = self.conn.lock().unwrap();
let exists: bool = conn
.prepare_cached("SELECT EXISTS(SELECT 1 FROM blobs WHERE hash = ?1)")
.and_then(|mut stmt| stmt.query_row(params![hash.as_str()], |row| row.get(0)))
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(exists)
}
fn has_blobs(&self, hashes: &[&Hash]) -> Result<Vec<bool>> {
let conn = self.conn.lock().unwrap();
Self::with_read_txn(&conn, |conn| {
let mut stmt = conn
.prepare_cached("SELECT EXISTS(SELECT 1 FROM blobs WHERE hash = ?1)")
.map_err(|e| OakError::Database(e.to_string()))?;
hashes
.iter()
.map(|hash| {
stmt.query_row(params![hash.as_str()], |row| row.get(0))
.map_err(|e| OakError::Database(e.to_string()))
})
.collect()
})
}
fn get_blob_size(&self, hash: &Hash) -> Result<Option<u64>> {
let conn = self.conn.lock().unwrap();
conn.query_row(
"SELECT size FROM blobs WHERE hash = ?1",
params![hash.as_str()],
|row| row.get::<_, i64>(0),
)
.optional()
.map(|size| size.map(|size| size as u64))
.map_err(|error| OakError::Database(error.to_string()))
}
fn store_manifest(&self, manifest: &Manifest) -> Result<()> {
self.put_tree(manifest.entries.clone()).map(|_| ())
}
fn get_manifest(&self, hash: &Hash) -> Result<Option<Manifest>> {
if self.has_tree(hash)? {
let entries = self.walk_tree(hash)?;
return Ok(Some(Manifest {
hash: hash.clone(),
entries,
}));
}
Ok(None)
}
fn store_tree(&self, tree: &Tree) -> Result<()> {
let mut conn = self.conn.lock().unwrap();
let tx = conn
.savepoint()
.map_err(|e| OakError::Database(e.to_string()))?;
insert_tree_row(&tx, tree)?;
tx.commit().map_err(|e| OakError::Database(e.to_string()))?;
Ok(())
}
fn walk_tree(&self, root: &Hash) -> Result<Vec<ManifestEntry>> {
self.tree_flattens.fetch_add(1, Ordering::Relaxed);
let mut fetch = |h: &Hash| -> Result<Tree> {
self.get_tree(h)?
.ok_or_else(|| OakError::ManifestNotFound(h.to_string()))
};
crate::flatten_tree(root, &mut fetch)
}
fn get_tree(&self, hash: &Hash) -> Result<Option<Tree>> {
self.tree_reads.fetch_add(1, Ordering::Relaxed);
if hash == &Tree::empty_hash() {
return Ok(Some(Tree::empty()));
}
let conn = self.conn.lock().unwrap();
let row: Option<Option<Vec<u8>>> = conn
.prepare_cached("SELECT content FROM trees WHERE hash = ?1")
.and_then(|mut stmt| {
stmt.query_row(params![hash.as_str()], |r| r.get::<_, Option<Vec<u8>>>(0))
.optional()
})
.map_err(|e| OakError::Database(e.to_string()))?;
match row {
None => Ok(None), Some(Some(content)) => {
let raw = zstd_decompress_tree(&content)?;
let actual = hash_bytes(&raw);
if &actual != hash {
return Err(OakError::InvalidHash(format!(
"tree {} does not match stored content hash {}",
hash, actual
)));
}
Ok(Some(Tree::from_canonical_bytes(hash.clone(), &raw)?))
}
Some(None) => Ok(Some(read_tree_entries(&conn, hash)?)), }
}
fn has_tree(&self, hash: &Hash) -> Result<bool> {
if hash == &Tree::empty_hash() {
return Ok(true);
}
let conn = self.conn.lock().unwrap();
let exists: bool = conn
.query_row(
"SELECT EXISTS(SELECT 1 FROM trees WHERE hash = ?1)",
params![hash.as_str()],
|row| row.get(0),
)
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(exists)
}
fn put_tree(&self, entries: Vec<ManifestEntry>) -> Result<Hash> {
let built = crate::build_tree(&entries)?;
let mut conn = self.conn.lock().unwrap();
let tx = conn
.savepoint()
.map_err(|e| OakError::Database(e.to_string()))?;
for tree in &built.trees {
let already_present: bool = tx
.query_row(
"SELECT EXISTS(SELECT 1 FROM trees WHERE hash = ?1)",
params![tree.hash.as_str()],
|row| row.get(0),
)
.map_err(|e| OakError::Database(e.to_string()))?;
if already_present {
continue;
}
insert_tree_row(&tx, tree)?;
}
tx.commit().map_err(|e| OakError::Database(e.to_string()))?;
Ok(built.root_hash)
}
fn close_branch(&self, name: &str, close_reason: Option<crate::CloseReason>) -> Result<()> {
let conn = self.conn.lock().unwrap();
conn.execute(
"UPDATE branches SET status = ?1, close_reason = ?2 WHERE name = ?3",
params![
BranchStatus::Closed.as_str(),
close_reason.as_ref().map(|r| r.as_str()),
name,
],
)
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(())
}
fn update_branch_close_reason(
&self,
name: &str,
close_reason: crate::CloseReason,
) -> Result<()> {
let conn = self.conn.lock().unwrap();
conn.execute(
"UPDATE branches SET close_reason = ?1 WHERE name = ?2",
params![close_reason.as_str(), name],
)
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(())
}
fn store_branch(&self, branch: &Branch) -> Result<()> {
let mut conn = self.conn.lock().unwrap();
let tx = conn
.savepoint()
.map_err(|error| OakError::Database(error.to_string()))?;
let generation = advance_description_generation(&tx)?;
tx.execute(
"INSERT OR IGNORE INTO branches (name, description, parent_branch, status, close_reason, created_at, description_generation) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
params![
branch.name,
branch.description,
branch.parent_branch,
branch.status.as_str(),
branch.close_reason.as_ref().map(|r| r.as_str()),
branch.created_at.to_rfc3339(),
generation,
],
)
.map_err(|e| OakError::Database(e.to_string()))?;
tx.commit()
.map_err(|error| OakError::Database(error.to_string()))
}
fn get_branch(&self, name: &str) -> Result<Option<Branch>> {
type BranchRow = (
Option<String>,
Option<String>,
String,
Option<String>,
String,
);
let conn = self.conn.lock().unwrap();
let result: Option<BranchRow> = conn
.query_row(
"SELECT description, parent_branch, status, close_reason, created_at FROM branches WHERE name = ?1",
params![name],
|row| Ok((row.get(0)?, row.get(1)?, row.get(2)?, row.get(3)?, row.get(4)?)),
)
.optional()
.map_err(|e| OakError::Database(e.to_string()))?;
match result {
Some((description, parent_branch, status_str, close_reason_str, created_at_str)) => {
let status = BranchStatus::from_db_str(&status_str);
decode_branch_row(
name.to_string(),
description,
parent_branch,
status,
close_reason_str,
&created_at_str,
)
.map(Some)
.map_err(|unreadable| unreadable.to_error())
}
None => Ok(None),
}
}
fn list_branches(&self) -> Result<Vec<Branch>> {
self.list_branches_isolated()?
.into_iter()
.map(|row| row.map_err(|unreadable| unreadable.to_error()))
.collect()
}
fn list_branches_isolated(
&self,
) -> Result<Vec<std::result::Result<Branch, crate::UnreadableBranch>>> {
let conn = self.conn.lock().unwrap();
let mut stmt = conn
.prepare("SELECT name, description, parent_branch, status, close_reason, created_at FROM branches ORDER BY created_at ASC")
.map_err(|e| OakError::Database(e.to_string()))?;
let branches = stmt
.query_map([], |row| {
let name: String = row.get(0)?;
let description: Option<String> = row.get(1)?;
let parent_branch: Option<String> = row.get(2)?;
let status_str: String = row.get(3)?;
let close_reason_str: Option<String> = row.get(4)?;
let created_at_str: String = row.get(5)?;
Ok((
name,
description,
parent_branch,
status_str,
close_reason_str,
created_at_str,
))
})
.map_err(|e| OakError::Database(e.to_string()))?
.collect::<std::result::Result<Vec<_>, _>>()
.map_err(|e| OakError::Database(e.to_string()))?;
branches
.into_iter()
.map(
|(
name,
description,
parent_branch,
status_str,
close_reason_str,
created_at_str,
)| {
decode_branch_row(
name,
description,
parent_branch,
BranchStatus::from_db_str(&status_str),
close_reason_str,
&created_at_str,
)
},
)
.map(Ok)
.collect()
}
fn update_branch_status(&self, name: &str, status: BranchStatus) -> Result<()> {
let conn = self.conn.lock().unwrap();
conn.execute(
"UPDATE branches SET status = ?1 WHERE name = ?2",
params![status.as_str(), name],
)
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(())
}
fn set_branch_parent(&self, name: &str, parent: Option<&str>) -> Result<()> {
let conn = self.conn.lock().unwrap();
conn.execute(
"UPDATE branches SET parent_branch = ?1 WHERE name = ?2",
params![parent, name],
)
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(())
}
fn update_branch_description(&self, name: &str, description: &str) -> Result<()> {
let mut conn = self.conn.lock().unwrap();
let tx = conn
.savepoint()
.map_err(|error| OakError::Database(error.to_string()))?;
let generation = advance_description_generation(&tx)?;
tx.execute(
"UPDATE branches SET description = ?1, description_generation = ?3 WHERE name = ?2",
params![description, name, generation],
)
.map_err(|e| OakError::Database(e.to_string()))?;
tx.commit()
.map_err(|error| OakError::Database(error.to_string()))
}
fn edit_branch_description(&self, name: &str, description: &str) -> Result<()> {
let mut conn = self.conn.lock().unwrap();
let tx = conn
.savepoint()
.map_err(|error| OakError::Database(error.to_string()))?;
let generation = advance_description_generation(&tx)?;
tx.execute(
"UPDATE branches SET description = ?1, description_pending = 1, description_generation = ?3 WHERE name = ?2",
params![description, name, generation],
)
.map_err(|error| OakError::Database(error.to_string()))?;
tx.commit()
.map_err(|error| OakError::Database(error.to_string()))
}
fn description_publication_generation(&self) -> Result<Option<i64>> {
self.description_generation().map(Some)
}
fn acknowledge_branch_description(
&self,
name: &str,
description: Option<&str>,
generation: Option<i64>,
) -> Result<()> {
let mut conn = self.conn.lock().unwrap();
let tx = conn
.savepoint()
.map_err(|error| OakError::Database(error.to_string()))?;
let acknowledged_generation = advance_description_generation(&tx)?;
tx.execute(
"UPDATE branches SET description_pending = 0, description_generation = ?4 WHERE name = ?1 AND description IS ?2 AND description_generation <= ?3",
params![name, description, generation, acknowledged_generation],
)
.map_err(|error| OakError::Database(error.to_string()))?;
tx.commit()
.map_err(|error| OakError::Database(error.to_string()))
}
fn rename_branch(&self, old_name: &str, new_name: &str) -> Result<()> {
if old_name == new_name {
return Ok(());
}
if new_name.is_empty() {
return Err(OakError::Database("new branch name cannot be empty".into()));
}
let mut conn = self.conn.lock().unwrap();
let tx = conn
.savepoint()
.map_err(|e| OakError::Database(e.to_string()))?;
let exists: bool = tx
.query_row(
"SELECT 1 FROM branches WHERE name = ?1",
params![old_name],
|_| Ok(true),
)
.optional()
.map_err(|e| OakError::Database(e.to_string()))?
.unwrap_or(false);
if !exists {
return Err(OakError::BranchNotFound(old_name.to_string()));
}
let conflict: bool = tx
.query_row(
"SELECT 1 FROM branches WHERE name = ?1
UNION
SELECT 1 FROM branch_heads WHERE branch_name = ?1",
params![new_name],
|_| Ok(true),
)
.optional()
.map_err(|e| OakError::Database(e.to_string()))?
.unwrap_or(false);
if conflict {
return Err(OakError::BranchAlreadyExists(new_name.to_string()));
}
let generation = advance_description_generation(&tx)?;
tx.execute(
"INSERT INTO branches (name, description, parent_branch, status, close_reason, created_at, description_pending, description_generation) \
SELECT ?1, description, parent_branch, status, close_reason, created_at, description_pending, ?3 FROM branches WHERE name = ?2",
params![new_name, old_name, generation],
)
.map_err(|e| OakError::Database(e.to_string()))?;
tx.execute(
"UPDATE branches SET parent_branch = ?1 WHERE parent_branch = ?2",
params![new_name, old_name],
)
.map_err(|e| OakError::Database(e.to_string()))?;
tx.execute(
"UPDATE commits SET branch_name = ?1 WHERE branch_name = ?2",
params![new_name, old_name],
)
.map_err(|e| OakError::Database(e.to_string()))?;
tx.execute(
"UPDATE branch_heads SET branch_name = ?1 WHERE branch_name = ?2",
params![new_name, old_name],
)
.map_err(|e| OakError::Database(e.to_string()))?;
tx.execute(
"UPDATE metadata SET value = ?1 WHERE key = ?2 AND value = ?3",
params![new_name, MetadataKey::CurrentBranch.as_str(), old_name],
)
.map_err(|e| OakError::Database(e.to_string()))?;
tx.execute("DELETE FROM branches WHERE name = ?1", params![old_name])
.map_err(|e| OakError::Database(e.to_string()))?;
tx.commit().map_err(|e| OakError::Database(e.to_string()))?;
Ok(())
}
fn delete_branch(&self, name: &str) -> Result<()> {
let mut conn = self.conn.lock().unwrap();
let tx = conn
.transaction()
.map_err(|e| OakError::Database(e.to_string()))?;
tx.execute(
"DELETE FROM branch_heads WHERE branch_name = ?1",
params![name],
)
.map_err(|e| OakError::Database(e.to_string()))?;
tx.execute("DELETE FROM branches WHERE name = ?1", params![name])
.map_err(|e| OakError::Database(e.to_string()))?;
tx.commit().map_err(|e| OakError::Database(e.to_string()))?;
Ok(())
}
fn get_branch_head(&self, name: &str) -> Result<Option<Hash>> {
let conn = self.conn.lock().unwrap();
let result: Option<String> = conn
.query_row(
"SELECT head_hash FROM branch_heads WHERE branch_name = ?1",
params![name],
|row| row.get(0),
)
.optional()
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(result.map(Hash))
}
fn set_branch_head(&self, name: &str, hash: &Hash) -> Result<()> {
let conn = self.conn.lock().unwrap();
conn.execute(
"INSERT OR REPLACE INTO branch_heads (branch_name, head_hash) VALUES (?1, ?2)",
params![name, hash.as_str()],
)
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(())
}
fn reconcile_remote_merge_branch_state(
&self,
closed_branch: &str,
new_branch: &Branch,
seed_head: &Hash,
) -> Result<()> {
let mut conn = self.conn.lock().unwrap();
let tx = conn
.transaction()
.map_err(|e| OakError::Database(e.to_string()))?;
let generation = advance_description_generation(&tx)?;
tx.execute(
"UPDATE branches SET status = ?1 WHERE name = ?2",
params![BranchStatus::Closed.as_str(), closed_branch],
)
.map_err(|e| OakError::Database(e.to_string()))?;
tx.execute(
"INSERT OR IGNORE INTO branches (name, description, parent_branch, status, close_reason, created_at, description_generation) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
params![
new_branch.name,
new_branch.description,
new_branch.parent_branch,
new_branch.status.as_str(),
new_branch.close_reason.as_ref().map(|r| r.as_str()),
new_branch.created_at.to_rfc3339(),
generation,
],
)
.map_err(|e| OakError::Database(e.to_string()))?;
tx.execute(
"INSERT OR REPLACE INTO branch_heads (branch_name, head_hash) VALUES (?1, ?2)",
params![new_branch.name, seed_head.as_str()],
)
.map_err(|e| OakError::Database(e.to_string()))?;
tx.execute(
"INSERT OR REPLACE INTO metadata (key, value) VALUES (?1, ?2)",
params![MetadataKey::Head.as_str(), seed_head.as_str()],
)
.map_err(|e| OakError::Database(e.to_string()))?;
tx.execute(
"INSERT OR REPLACE INTO metadata (key, value) VALUES (?1, ?2)",
params![MetadataKey::CurrentBranch.as_str(), new_branch.name],
)
.map_err(|e| OakError::Database(e.to_string()))?;
tx.commit().map_err(|e| OakError::Database(e.to_string()))?;
Ok(())
}
fn store_commit(&self, commit: &Commit) -> Result<()> {
let mut conn = self.conn.lock().unwrap();
let tx = conn
.savepoint()
.map_err(|e| OakError::Database(e.to_string()))?;
let inserted = tx
.execute(
"INSERT OR IGNORE INTO commits (hash, branch_name, parent_hash, merge_parent_hash, manifest_hash, author, message, timestamp) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
params![
commit.hash.as_str(),
commit.branch_name,
commit.parent_hash.as_ref().map(|h| h.as_str()),
commit.merge_parent_hash.as_ref().map(|h| h.as_str()),
commit.manifest_hash.as_str(),
commit.author,
commit.message,
commit.timestamp.to_rfc3339(),
],
)
.map_err(|e| OakError::Database(e.to_string()))?;
if inserted != 0 {
for file in &commit.files {
let change_type_str = match file.change_type {
ChangeType::Added => "added",
ChangeType::Modified => "modified",
ChangeType::Deleted => "deleted",
ChangeType::Renamed => "renamed",
};
tx.execute(
"INSERT INTO commit_files (commit_hash, path, change_type, old_blob_hash, new_blob_hash, old_path) VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
params![
commit.hash.as_str(),
file.path,
change_type_str,
file.old_blob_hash.as_ref().map(|h| h.as_str()),
file.new_blob_hash.as_ref().map(|h| h.as_str()),
file.old_path,
],
)
.map_err(|e| OakError::Database(e.to_string()))?;
}
}
tx.commit().map_err(|e| OakError::Database(e.to_string()))?;
Ok(())
}
fn get_commit(&self, hash: &Hash) -> Result<Option<Commit>> {
let conn = self.conn.lock().unwrap();
#[allow(clippy::type_complexity)]
let result: Option<(String, Option<String>, Option<String>, String, String, Option<String>, String)> = conn
.query_row(
"SELECT branch_name, parent_hash, merge_parent_hash, manifest_hash, author, message, timestamp FROM commits WHERE hash = ?1",
params![hash.as_str()],
|row| Ok((row.get(0)?, row.get(1)?, row.get(2)?, row.get(3)?, row.get(4)?, row.get(5)?, row.get(6)?)),
)
.optional()
.map_err(|e| OakError::Database(e.to_string()))?;
let (
branch_name,
parent_hash,
merge_parent_hash,
manifest_hash,
author,
message,
timestamp_str,
) = match result {
Some(r) => r,
None => return Ok(None),
};
let mut stmt = conn
.prepare("SELECT path, change_type, old_blob_hash, new_blob_hash, old_path FROM commit_files WHERE commit_hash = ?1")
.map_err(|e| OakError::Database(e.to_string()))?;
let files: Vec<FileChange> = stmt
.query_map(params![hash.as_str()], |row| {
let path: String = row.get(0)?;
let change_type_str: String = row.get(1)?;
let old_blob_hash: Option<String> = row.get(2)?;
let new_blob_hash: Option<String> = row.get(3)?;
let old_path: Option<String> = row.get(4)?;
let change_type = match change_type_str.as_str() {
"added" => ChangeType::Added,
"deleted" => ChangeType::Deleted,
"renamed" => ChangeType::Renamed,
_ => ChangeType::Modified,
};
Ok(FileChange {
path,
change_type,
old_blob_hash: old_blob_hash.map(Hash),
new_blob_hash: new_blob_hash.map(Hash),
old_path,
old_mode: None,
new_mode: None,
})
})
.map_err(|e| OakError::Database(e.to_string()))?
.collect::<std::result::Result<Vec<_>, _>>()
.map_err(|e| OakError::Database(e.to_string()))?;
let timestamp = chrono::DateTime::parse_from_rfc3339(×tamp_str)
.map_err(|e| OakError::Database(e.to_string()))?
.with_timezone(&chrono::Utc);
Ok(Some(Commit {
hash: hash.clone(),
branch_name,
parent_hash: parent_hash.map(Hash),
merge_parent_hash: merge_parent_hash.map(Hash),
manifest_hash: Hash(manifest_hash),
author,
message,
timestamp,
files,
}))
}
fn repair_commit_fields(&self, verified: &Commit) -> Result<bool> {
verified.verify_fields()?;
let Some(stored) = self.get_commit(&verified.hash)? else {
return Ok(false);
};
if stored.verify_fields().is_ok() {
return Ok(false);
}
if stored.parent_hash != verified.parent_hash
|| stored.merge_parent_hash != verified.merge_parent_hash
|| stored.manifest_hash != verified.manifest_hash
{
return Err(OakError::InvalidHash(format!(
"refusing to repair commit {}: the verified fields name different parents or a different manifest than the stored row",
verified.hash.short()
)));
}
let conn = self.conn.lock().unwrap();
let changed = conn
.execute(
"UPDATE commits SET branch_name = ?2, author = ?3, message = ?4, timestamp = ?5 WHERE hash = ?1",
params![
verified.hash.as_str(),
verified.branch_name,
verified.author,
verified.message,
verified.timestamp.to_rfc3339(),
],
)
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(changed != 0)
}
fn has_commit(&self, hash: &Hash) -> Result<bool> {
let conn = self.conn.lock().unwrap();
let count: i64 = conn
.query_row(
"SELECT COUNT(*) FROM commits WHERE hash = ?1",
params![hash.as_str()],
|row| row.get(0),
)
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(count > 0)
}
fn get_commits_for_branch(&self, branch_name: &str) -> Result<Vec<Commit>> {
let conn = self.conn.lock().unwrap();
let mut stmt = conn
.prepare("SELECT hash FROM commits WHERE branch_name = ?1 ORDER BY timestamp ASC")
.map_err(|e| OakError::Database(e.to_string()))?;
let hashes: Vec<String> = stmt
.query_map(params![branch_name], |row| row.get(0))
.map_err(|e| OakError::Database(e.to_string()))?
.collect::<std::result::Result<Vec<_>, _>>()
.map_err(|e| OakError::Database(e.to_string()))?;
drop(stmt);
drop(conn);
let mut commits = Vec::new();
for hash in hashes {
if let Some(edit) = self.get_commit(&Hash(hash))? {
commits.push(edit);
}
}
Ok(commits)
}
fn count_commits_for_branch(&self, branch_name: &str) -> Result<usize> {
let conn = self.conn.lock().unwrap();
let count: i64 = conn
.query_row(
"SELECT COUNT(*) FROM commits WHERE branch_name = ?1",
params![branch_name],
|row| row.get(0),
)
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(count as usize)
}
fn merge_child_exists(&self, branch_head: &Hash) -> Result<bool> {
let conn = self.conn.lock().unwrap();
let exists: bool = conn
.query_row(
"SELECT EXISTS(SELECT 1 FROM commits WHERE merge_parent_hash = ?1)",
params![branch_head.as_str()],
|row| row.get(0),
)
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(exists)
}
fn get_commits_since(&self, branch_name: &str, since: Option<&Hash>) -> Result<Vec<Commit>> {
let all = self.get_commits_for_branch(branch_name)?;
match since {
None => Ok(all),
Some(since_hash) => {
let mut found = false;
let mut result = Vec::new();
for commit in &all {
if &commit.hash == since_hash {
found = true;
} else if found {
result.push(commit.clone());
}
}
if !found {
return Ok(all);
}
Ok(result)
}
}
}
fn get_all_commits(&self) -> Result<Vec<Commit>> {
let conn = self.conn.lock().unwrap();
let mut stmt = conn
.prepare("SELECT hash FROM commits ORDER BY timestamp ASC")
.map_err(|e| OakError::Database(e.to_string()))?;
let hashes: Vec<String> = stmt
.query_map([], |row| row.get(0))
.map_err(|e| OakError::Database(e.to_string()))?
.collect::<std::result::Result<Vec<_>, _>>()
.map_err(|e| OakError::Database(e.to_string()))?;
drop(stmt);
drop(conn);
let mut commits = Vec::new();
for hash in hashes {
if let Some(edit) = self.get_commit(&Hash(hash))? {
commits.push(edit);
}
}
Ok(commits)
}
fn get_metadata(&self, key: MetadataKey) -> Result<Option<String>> {
let conn = self.conn.lock().unwrap();
let result: Option<String> = conn
.query_row(
"SELECT value FROM metadata WHERE key = ?1",
params![key.as_str()],
|row| row.get(0),
)
.optional()
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(result)
}
fn set_metadata(&self, key: MetadataKey, value: &str) -> Result<()> {
let conn = self.conn.lock().unwrap();
conn.execute(
"INSERT OR REPLACE INTO metadata (key, value) VALUES (?1, ?2)",
params![key.as_str(), value],
)
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(())
}
fn insert_metadata_if_absent(&self, key: MetadataKey, value: &str) -> Result<bool> {
let conn = self.conn.lock().unwrap();
let inserted = conn
.execute(
"INSERT OR IGNORE INTO metadata (key, value) VALUES (?1, ?2)",
params![key.as_str(), value],
)
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(inserted > 0)
}
fn load_stat_cache(&self) -> Result<HashMap<String, StatCacheEntry>> {
let conn = self.conn.lock().unwrap();
let mut stmt = conn
.prepare("SELECT path, mtime_ns, ctime_ns, size, blob_hash FROM stat_cache")
.map_err(|e| OakError::Database(e.to_string()))?;
let rows = stmt
.query_map([], |row| {
Ok((
row.get::<_, String>(0)?,
StatCacheEntry {
mtime_ns: row.get(1)?,
ctime_ns: row.get(2)?,
size: row.get(3)?,
blob_hash: Hash(row.get::<_, String>(4)?),
},
))
})
.map_err(|e| OakError::Database(e.to_string()))?;
let mut map = HashMap::new();
for row in rows {
let (path, entry) = row.map_err(|e| OakError::Database(e.to_string()))?;
map.insert(path, entry);
}
Ok(map)
}
fn update_stat_cache(
&self,
upserts: &[(String, StatCacheEntry)],
removed: &[String],
) -> Result<()> {
if upserts.is_empty() && removed.is_empty() {
return Ok(());
}
let mut conn = self.conn.lock().unwrap();
let tx = conn
.transaction()
.map_err(|e| OakError::Database(e.to_string()))?;
{
let mut upsert = tx
.prepare(
"INSERT INTO stat_cache (path, mtime_ns, ctime_ns, size, blob_hash) VALUES (?1, ?2, ?3, ?4, ?5)
ON CONFLICT(path) DO UPDATE SET
mtime_ns = excluded.mtime_ns,
ctime_ns = excluded.ctime_ns,
size = excluded.size,
blob_hash = excluded.blob_hash",
)
.map_err(|e| OakError::Database(e.to_string()))?;
for (path, entry) in upserts {
upsert
.execute(params![
path,
entry.mtime_ns,
entry.ctime_ns,
entry.size,
entry.blob_hash.as_str()
])
.map_err(|e| OakError::Database(e.to_string()))?;
}
let mut delete = tx
.prepare("DELETE FROM stat_cache WHERE path = ?1")
.map_err(|e| OakError::Database(e.to_string()))?;
for path in removed {
delete
.execute(params![path])
.map_err(|e| OakError::Database(e.to_string()))?;
}
}
tx.commit().map_err(|e| OakError::Database(e.to_string()))?;
Ok(())
}
fn store_chunk(&self, hash: &Hash, content: &[u8]) -> Result<()> {
let actual = hash_bytes(content);
if &actual != hash {
return Err(OakError::InvalidHash(format!(
"chunk {} does not match content hash {}",
hash, actual
)));
}
let conn = self.conn.lock().unwrap();
conn.execute(
"INSERT OR IGNORE INTO chunks (hash, content, size) VALUES (?1, ?2, ?3)",
params![hash.as_str(), content, content.len() as i64],
)
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(())
}
fn get_chunk(&self, hash: &Hash) -> Result<Option<Vec<u8>>> {
let conn = self.conn.lock().unwrap();
let result: Option<Vec<u8>> = conn
.query_row(
"SELECT content FROM chunks WHERE hash = ?1",
params![hash.as_str()],
|row| row.get(0),
)
.optional()
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(result)
}
fn has_chunk(&self, hash: &Hash) -> Result<bool> {
let conn = self.conn.lock().unwrap();
let exists: bool = conn
.query_row(
"SELECT EXISTS(SELECT 1 FROM chunks WHERE hash = ?1)",
params![hash.as_str()],
|row| row.get(0),
)
.map_err(|e| OakError::Database(e.to_string()))?;
Ok(exists)
}
fn store_blob_chunks(&self, blob_hash: &Hash, chunks: &[ChunkInfo]) -> Result<()> {
let conn = self.conn.lock().unwrap();
for (index, chunk) in chunks.iter().enumerate() {
conn.execute(
"INSERT OR IGNORE INTO blob_chunks (blob_hash, chunk_hash, chunk_index, offset, size) VALUES (?1, ?2, ?3, ?4, ?5)",
params![
blob_hash.as_str(),
chunk.hash.as_str(),
index as i64,
chunk.offset as i64,
chunk.length as i64,
],
)
.map_err(|e| OakError::Database(e.to_string()))?;
}
Ok(())
}
fn get_blob_chunks(&self, blob_hash: &Hash) -> Result<Option<Vec<ChunkInfo>>> {
let conn = self.conn.lock().unwrap();
let mut stmt = conn
.prepare(
"SELECT chunk_hash, offset, size FROM blob_chunks WHERE blob_hash = ?1 ORDER BY chunk_index",
)
.map_err(|e| OakError::Database(e.to_string()))?;
let rows: Vec<ChunkInfo> = stmt
.query_map(params![blob_hash.as_str()], |row| {
let hash_str: String = row.get(0)?;
let offset: i64 = row.get(1)?;
let size: i64 = row.get(2)?;
Ok(ChunkInfo {
hash: Hash(hash_str),
offset: offset as u64,
length: size as u32,
})
})
.map_err(|e| OakError::Database(e.to_string()))?
.collect::<std::result::Result<Vec<_>, _>>()
.map_err(|e| OakError::Database(e.to_string()))?;
if rows.is_empty() {
Ok(None)
} else {
Ok(Some(rows))
}
}
fn put_commit_and_advance_refs(
&self,
branch_name: String,
parent_hash: Option<Hash>,
merge_parent_hash: Option<Hash>,
entries: Vec<ManifestEntry>,
author: String,
message: Option<String>,
timestamp: chrono::DateTime<chrono::Utc>,
files: Vec<FileChange>,
) -> Result<Hash> {
let built = crate::build_tree(&entries)?;
let manifest_hash = built.root_hash.clone();
let commit = Commit::with_timestamp(
branch_name,
parent_hash,
merge_parent_hash,
manifest_hash,
author,
message,
files,
timestamp,
)?;
let commit_hash = commit.hash.clone();
self.publish_commit(&commit, &built.trees, false)?;
Ok(commit_hash)
}
fn put_tree_commit_and_advance_refs(
&self,
branch_name: String,
parent_hash: Option<Hash>,
merge_parent_hash: Option<Hash>,
root: Hash,
trees: &[Tree],
author: String,
message: Option<String>,
timestamp: chrono::DateTime<chrono::Utc>,
files: Vec<FileChange>,
) -> Result<Hash> {
let commit = Commit::with_timestamp(
branch_name,
parent_hash,
merge_parent_hash,
root,
author,
message,
files,
timestamp,
)?;
let commit_hash = commit.hash.clone();
self.publish_commit(&commit, trees, true)?;
Ok(commit_hash)
}
}
impl SqliteRepository {
#[doc(hidden)]
pub fn tree_flatten_count(&self) -> u64 {
self.tree_flattens.load(Ordering::Relaxed)
}
#[doc(hidden)]
pub fn tree_read_count(&self) -> u64 {
self.tree_reads.load(Ordering::Relaxed)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tree_decoder_reuses_context_and_grows_for_large_rows() {
let small = b"blob\ta.txt\tabc\tregular".to_vec();
let large: Vec<u8> = (0..300_000u32)
.map(|i| (i.wrapping_mul(2_654_435_761) >> 13) as u8)
.collect();
for raw in [&small, &large, &small] {
let stored = zstd_compress(raw).unwrap();
assert_eq!(&zstd_decompress_tree(&stored).unwrap(), raw);
}
let frame = zstd_compress(&small).unwrap();
for bad in [
b"not a zstd frame".to_vec(),
Vec::new(),
[frame.as_slice(), b"trailing"].concat(),
frame[..frame.len() - 1].to_vec(),
] {
assert_eq!(
zstd_decompress_tree(&bad).is_err(),
zstd_decompress(&bad).is_err(),
"{bad:?}"
);
}
let two = [frame.as_slice(), frame.as_slice()].concat();
assert_eq!(
zstd_decompress_tree(&two).unwrap(),
zstd_decompress(&two).unwrap()
);
assert!(zstd_decompress_tree(b"not a zstd frame").is_err());
assert_eq!(
zstd_decompress_tree(&zstd_compress(&small).unwrap()).unwrap(),
small
);
}
fn delete_migration_row(db_path: &Path, version: &str) {
let conn = Connection::open(db_path).unwrap();
conn.execute(
"DELETE FROM schema_migrations WHERE version = ?1",
params![version],
)
.unwrap();
}
fn migration_succeeded(db_path: &Path, version: &str) -> bool {
let conn = Connection::open(db_path).unwrap();
conn.query_row(
"SELECT succeeded FROM schema_migrations WHERE version = ?1",
params![version],
|row| row.get::<_, i64>(0),
)
.optional()
.unwrap()
== Some(1)
}
#[test]
fn missing_0007_row_with_existing_blob_codec_is_marked_applied() {
let temp = tempfile::TempDir::new().unwrap();
let db_path = temp.path().join("oak.db");
drop(SqliteRepository::open(&db_path).unwrap());
delete_migration_row(&db_path, "0007_blobs_codec");
SqliteRepository::open(&db_path).unwrap();
assert!(migration_succeeded(&db_path, "0007_blobs_codec"));
}
#[test]
fn missing_0008_row_with_existing_tree_content_is_marked_applied() {
let temp = tempfile::TempDir::new().unwrap();
let db_path = temp.path().join("oak.db");
drop(SqliteRepository::open(&db_path).unwrap());
delete_migration_row(&db_path, "0008_trees_content");
SqliteRepository::open(&db_path).unwrap();
assert!(migration_succeeded(&db_path, "0008_trees_content"));
}
#[test]
fn test_blob_roundtrip() {
let repo = SqliteRepository::in_memory().unwrap();
let blob = Blob::from_str("hello world");
repo.store_blob(&blob).unwrap();
let retrieved = repo.get_blob(&blob.hash).unwrap().unwrap();
assert_eq!(retrieved.hash, blob.hash);
assert_eq!(retrieved.content, blob.content);
}
#[test]
fn test_tree_put_walk_roundtrip() {
let repo = SqliteRepository::in_memory().unwrap();
for content in ["a", "b", "c"] {
repo.store_blob(&Blob::from_str(content)).unwrap();
}
let entries = vec![
ManifestEntry {
path: "src/lib.rs".to_string(),
blob_hash: Blob::from_str("a").hash,
mode: FileMode::Regular,
},
ManifestEntry {
path: "src/bin/main.rs".to_string(),
blob_hash: Blob::from_str("b").hash,
mode: FileMode::Regular,
},
ManifestEntry {
path: "README.md".to_string(),
blob_hash: Blob::from_str("c").hash,
mode: FileMode::Regular,
},
];
let root = repo.put_tree(entries.clone()).unwrap();
assert!(repo.has_tree(&root).unwrap());
let walked = repo.walk_tree(&root).unwrap();
assert_eq!(walked.len(), 3);
let manifest = Manifest::new(entries);
let walked_manifest = Manifest::new(walked);
assert_eq!(manifest.hash, walked_manifest.hash);
}
#[test]
fn put_tree_commit_stores_new_trees_and_advances_refs() {
let repo = SqliteRepository::in_memory().unwrap();
let entry = |path: &str, content: &str| ManifestEntry {
path: path.to_string(),
blob_hash: crate::hash_string(content),
mode: FileMode::Regular,
};
let base = crate::build_tree(&[entry("src/a.rs", "a"), entry("docs/x.md", "x")]).unwrap();
for tree in &base.trees {
repo.store_tree(tree).unwrap();
}
let mut fetch = |h: &Hash| {
repo.get_tree(h)?
.ok_or_else(|| OakError::ManifestNotFound(h.to_string()))
};
let edited =
crate::edit_tree(&base.root_hash, &[entry("src/a.rs", "a2")], &[], &mut fetch).unwrap();
assert_eq!(edited.trees.len(), 2, "new root and src/ only");
let commit = repo
.put_tree_commit_and_advance_refs(
"feature".to_string(),
None,
None,
edited.root_hash.clone(),
&edited.trees,
"tester".to_string(),
None,
chrono::Utc::now(),
vec![],
)
.unwrap();
assert_eq!(
repo.get_branch_head("feature").unwrap(),
Some(commit.clone())
);
assert_eq!(repo.get_head().unwrap(), Some(commit.clone()));
assert_eq!(
repo.get_commit(&commit).unwrap().unwrap().manifest_hash,
edited.root_hash
);
let manifest = repo.get_manifest(&edited.root_hash).unwrap().unwrap();
assert_eq!(
manifest.hash,
Manifest::new(vec![entry("src/a.rs", "a2"), entry("docs/x.md", "x")]).hash
);
let dangling = crate::build_tree(&[entry("lost.txt", "lost")]).unwrap();
let err = repo
.put_tree_commit_and_advance_refs(
"feature".to_string(),
Some(commit.clone()),
None,
dangling.root_hash,
&[],
"tester".to_string(),
None,
chrono::Utc::now(),
vec![],
)
.unwrap_err();
assert!(matches!(err, OakError::ManifestNotFound(_)), "got {err:?}");
assert_eq!(repo.get_branch_head("feature").unwrap(), Some(commit));
}
#[test]
fn store_tree_rejects_mismatched_storage_hash() {
let repo = SqliteRepository::in_memory().unwrap();
let tree = Tree::new(vec![TreeEntry {
name: "a.txt".to_string(),
kind: TreeEntryKind::Blob,
hash: Hash::from_hex(&"a".repeat(64)).unwrap(),
mode: FileMode::Regular,
}])
.unwrap();
let mut tampered = tree.clone();
tampered.hash = Hash::from_hex(&"b".repeat(64)).unwrap();
let err = repo.store_tree(&tampered).unwrap_err();
assert!(
matches!(err, OakError::InvalidHash(_)),
"expected invalid hash error, got {err:?}"
);
assert!(!repo.has_tree(&tampered.hash).unwrap());
assert!(repo.get_tree(&tampered.hash).unwrap().is_none());
}
#[test]
fn get_tree_rejects_compressed_row_under_mismatched_hash() {
let repo = SqliteRepository::in_memory().unwrap();
let tree = Tree::new(vec![TreeEntry {
name: "a.txt".to_string(),
kind: TreeEntryKind::Blob,
hash: Hash::from_hex(&"a".repeat(64)).unwrap(),
mode: FileMode::Regular,
}])
.unwrap();
let claimed = Hash::from_hex(&"c".repeat(64)).unwrap();
let content = zstd_compress(&tree.canonical_bytes()).unwrap();
{
let conn = repo.conn.lock().unwrap();
conn.execute(
"INSERT INTO trees (hash, content) VALUES (?1, ?2)",
rusqlite::params![claimed.as_str(), content],
)
.unwrap();
}
let err = repo.get_tree(&claimed).unwrap_err();
assert!(
matches!(err, OakError::InvalidHash(_)),
"expected invalid hash error, got {err:?}"
);
}
#[test]
fn legacy_tree_entries_fallback() {
let repo = SqliteRepository::in_memory().unwrap();
let entries = vec![
TreeEntry {
name: "a.txt".to_string(),
kind: TreeEntryKind::Blob,
hash: Hash::from_hex(&"a".repeat(64)).unwrap(),
mode: FileMode::Executable,
},
TreeEntry {
name: "sub".to_string(),
kind: TreeEntryKind::Tree,
hash: Hash::from_hex(&"b".repeat(64)).unwrap(),
mode: FileMode::Regular,
},
];
let tree = Tree::new(entries).unwrap();
{
let conn = repo.conn.lock().unwrap();
conn.execute(
"INSERT INTO trees (hash) VALUES (?1)",
rusqlite::params![tree.hash.as_str()],
)
.unwrap();
for e in &tree.entries {
let mode_str = match (e.kind, e.mode) {
(TreeEntryKind::Tree, _) => "tree",
(TreeEntryKind::Blob, FileMode::Regular) => "regular",
(TreeEntryKind::Blob, FileMode::Executable) => "executable",
(TreeEntryKind::Blob, FileMode::Symlink) => "symlink",
};
conn.execute(
"INSERT INTO tree_entries (tree_hash, name, kind, hash, mode) VALUES (?1, ?2, ?3, ?4, ?5)",
rusqlite::params![tree.hash.as_str(), e.name, e.kind.as_str(), e.hash.as_str(), mode_str],
)
.unwrap();
}
}
let got = repo.get_tree(&tree.hash).unwrap().unwrap();
assert_eq!(got.hash, tree.hash);
assert_eq!(got.canonical_bytes(), tree.canonical_bytes());
}
#[test]
fn compact_storage_backfills_drops_and_recompresses() {
let repo = SqliteRepository::in_memory().unwrap();
repo.store_blob(&Blob::from_str("x")).unwrap();
let new_root = repo
.put_tree(vec![ManifestEntry {
path: "dir/x.txt".to_string(),
blob_hash: Blob::from_str("x").hash,
mode: FileMode::Regular,
}])
.unwrap();
let legacy = Tree::new(vec![TreeEntry {
name: "old.txt".to_string(),
kind: TreeEntryKind::Blob,
hash: Hash::from_hex(&"c".repeat(64)).unwrap(),
mode: FileMode::Regular,
}])
.unwrap();
let legacy_blob = Blob::from_str(&"zzzz ".repeat(80));
{
let conn = repo.conn.lock().unwrap();
conn.execute(
"INSERT INTO trees (hash) VALUES (?1)",
rusqlite::params![legacy.hash.as_str()],
)
.unwrap();
for e in &legacy.entries {
conn.execute(
"INSERT INTO tree_entries (tree_hash, name, kind, hash, mode) VALUES (?1, ?2, ?3, ?4, 'regular')",
rusqlite::params![legacy.hash.as_str(), e.name, e.kind.as_str(), e.hash.as_str()],
)
.unwrap();
}
conn.execute(
"INSERT INTO blobs (hash, content, size, codec) VALUES (?1, ?2, ?3, 0)",
rusqlite::params![
legacy_blob.hash.as_str(),
legacy_blob.content,
legacy_blob.size as i64
],
)
.unwrap();
}
let stats = repo.compact_storage().unwrap();
assert_eq!(stats.trees_backfilled, 1);
assert!(stats.tree_entries_dropped);
assert!(stats.blobs_recompressed >= 1);
{
let conn = repo.conn.lock().unwrap();
assert!(!table_exists(&conn, "tree_entries").unwrap());
}
assert!(repo.get_tree(&new_root).unwrap().is_some());
let got_legacy = repo.get_tree(&legacy.hash).unwrap().unwrap();
assert_eq!(got_legacy.canonical_bytes(), legacy.canonical_bytes());
assert_eq!(
repo.get_blob(&legacy_blob.hash).unwrap().unwrap().content,
legacy_blob.content
);
let stats2 = repo.compact_storage().unwrap();
assert_eq!(stats2.trees_backfilled, 0);
assert_eq!(stats2.blobs_recompressed, 0);
}
#[test]
fn legacy_raw_blob_fallback() {
let repo = SqliteRepository::in_memory().unwrap();
let blob = Blob::from_str(&"hello world ".repeat(50)); {
let conn = repo.conn.lock().unwrap();
conn.execute(
"INSERT INTO blobs (hash, content, size, codec) VALUES (?1, ?2, ?3, 0)",
rusqlite::params![blob.hash.as_str(), blob.content, blob.size as i64],
)
.unwrap();
}
let got = repo.get_blob(&blob.hash).unwrap().unwrap();
assert_eq!(got.content, blob.content);
}
#[test]
fn new_blob_compresses_and_roundtrips() {
let repo = SqliteRepository::in_memory().unwrap();
let blob = Blob::from_str(&"abcabcabc ".repeat(100));
repo.store_blob(&blob).unwrap();
let codec: i64 = {
let conn = repo.conn.lock().unwrap();
conn.query_row(
"SELECT codec FROM blobs WHERE hash = ?1",
rusqlite::params![blob.hash.as_str()],
|r| r.get(0),
)
.unwrap()
};
assert_eq!(codec, 1, "compressible blob should store as zstd");
let got = repo.get_blob(&blob.hash).unwrap().unwrap();
assert_eq!(got.content, blob.content);
}
#[test]
fn test_tree_subtree_dedup() {
let repo = SqliteRepository::in_memory().unwrap();
for content in ["x", "y"] {
repo.store_blob(&Blob::from_str(content)).unwrap();
}
let entries_a = vec![
ManifestEntry {
path: "shared/x.txt".to_string(),
blob_hash: Blob::from_str("x").hash,
mode: FileMode::Regular,
},
ManifestEntry {
path: "shared/y.txt".to_string(),
blob_hash: Blob::from_str("y").hash,
mode: FileMode::Regular,
},
];
let entries_b = vec![
ManifestEntry {
path: "shared/x.txt".to_string(),
blob_hash: Blob::from_str("x").hash,
mode: FileMode::Regular,
},
ManifestEntry {
path: "shared/y.txt".to_string(),
blob_hash: Blob::from_str("y").hash,
mode: FileMode::Regular,
},
ManifestEntry {
path: "extra.txt".to_string(),
blob_hash: Blob::from_str("x").hash,
mode: FileMode::Regular,
},
];
let root_a = repo.put_tree(entries_a).unwrap();
let root_b = repo.put_tree(entries_b).unwrap();
assert_ne!(root_a, root_b);
let shared_a = repo.subtree_at_path(&root_a, "shared").unwrap().unwrap();
let shared_b = repo.subtree_at_path(&root_b, "shared").unwrap().unwrap();
assert_eq!(shared_a, shared_b);
}
#[test]
fn test_tree_resolve_path() {
let repo = SqliteRepository::in_memory().unwrap();
repo.store_blob(&Blob::from_str("file")).unwrap();
let entries = vec![ManifestEntry {
path: "a/b/c.txt".to_string(),
blob_hash: Blob::from_str("file").hash,
mode: FileMode::Regular,
}];
let root = repo.put_tree(entries).unwrap();
let blob = repo.resolve_path_in_tree(&root, "a/b/c.txt").unwrap();
assert!(blob.is_some());
assert_eq!(blob.unwrap().kind, TreeEntryKind::Blob);
let dir = repo.resolve_path_in_tree(&root, "a/b").unwrap();
assert_eq!(dir.unwrap().kind, TreeEntryKind::Tree);
let missing = repo.resolve_path_in_tree(&root, "a/zzz.txt").unwrap();
assert!(missing.is_none());
}
#[test]
fn test_tree_put_idempotent() {
let repo = SqliteRepository::in_memory().unwrap();
repo.store_blob(&Blob::from_str("c")).unwrap();
let entries = vec![ManifestEntry {
path: "x.txt".to_string(),
blob_hash: Blob::from_str("c").hash,
mode: FileMode::Regular,
}];
let r1 = repo.put_tree(entries.clone()).unwrap();
let r2 = repo.put_tree(entries).unwrap();
assert_eq!(r1, r2);
}
#[test]
fn test_put_commit_and_advance_refs_stores_commit_and_moves_heads() {
let repo = SqliteRepository::in_memory().unwrap();
let blob = Blob::from_str("content");
repo.store_blob(&blob).unwrap();
let entries = vec![ManifestEntry {
path: "file.txt".to_string(),
blob_hash: blob.hash.clone(),
mode: FileMode::Regular,
}];
let files = vec![FileChange {
path: "file.txt".to_string(),
change_type: ChangeType::Added,
old_blob_hash: None,
new_blob_hash: Some(blob.hash),
old_path: None,
old_mode: None,
new_mode: None,
}];
let timestamp = chrono::DateTime::from_timestamp(1_700_000_000, 0).unwrap();
let commit_hash = repo
.put_commit_and_advance_refs(
"main".to_string(),
None,
None,
entries,
"tester <t@example.com>".to_string(),
None,
timestamp,
files,
)
.unwrap();
assert_eq!(
repo.get_branch_head("main").unwrap(),
Some(commit_hash.clone())
);
assert_eq!(repo.get_head().unwrap(), Some(commit_hash.clone()));
let commit = repo.get_commit(&commit_hash).unwrap().unwrap();
assert_eq!(commit.timestamp, timestamp);
assert_eq!(commit.files.len(), 1);
let manifest = repo.get_manifest(&commit.manifest_hash).unwrap().unwrap();
assert_eq!(manifest.entries.len(), 1);
assert_eq!(manifest.entries[0].path, "file.txt");
}
#[test]
fn test_manifest_roundtrip() {
let repo = SqliteRepository::in_memory().unwrap();
let blob = Blob::from_str("content");
repo.store_blob(&blob).unwrap();
let manifest = Manifest::new(vec![ManifestEntry {
path: "test.txt".to_string(),
blob_hash: blob.hash.clone(),
mode: FileMode::Regular,
}]);
repo.store_manifest(&manifest).unwrap();
let retrieved = repo.get_manifest(&manifest.hash).unwrap().unwrap();
assert_eq!(retrieved.hash, manifest.hash);
assert_eq!(retrieved.entries.len(), 1);
assert_eq!(retrieved.entries[0].path, "test.txt");
}
#[test]
fn test_branch_roundtrip() {
let repo = SqliteRepository::in_memory().unwrap();
let br = Branch::new("feature-x".to_string(), Some("A feature".to_string()), None);
repo.store_branch(&br).unwrap();
let retrieved = repo.get_branch("feature-x").unwrap().unwrap();
assert_eq!(retrieved.name, "feature-x");
assert_eq!(retrieved.description, Some("A feature".to_string()));
assert_eq!(retrieved.status, BranchStatus::Open);
}
#[test]
fn test_commit_roundtrip() {
let repo = SqliteRepository::in_memory().unwrap();
let blob = Blob::from_str("content");
repo.store_blob(&blob).unwrap();
let manifest = Manifest::new(vec![ManifestEntry {
path: "test.txt".to_string(),
blob_hash: blob.hash.clone(),
mode: FileMode::Regular,
}]);
repo.store_manifest(&manifest).unwrap();
let br = Branch::new("main".to_string(), None, None);
repo.store_branch(&br).unwrap();
let commit = Commit::new(
"main".to_string(),
None,
None,
manifest.hash.clone(),
"Test Author".to_string(),
Some("Initial commit".to_string()),
vec![FileChange {
path: "test.txt".to_string(),
change_type: ChangeType::Added,
old_blob_hash: None,
new_blob_hash: Some(blob.hash.clone()),
old_path: None,
old_mode: None,
new_mode: None,
}],
)
.unwrap();
repo.store_commit(&commit).unwrap();
let retrieved = repo.get_commit(&commit.hash).unwrap().unwrap();
assert_eq!(retrieved.hash, commit.hash);
assert_eq!(retrieved.branch_name, "main");
assert_eq!(retrieved.message, Some("Initial commit".to_string()));
assert_eq!(retrieved.files.len(), 1);
}
#[test]
fn store_commit_is_idempotent_for_file_changes() {
let repo = SqliteRepository::in_memory().unwrap();
let blob = Blob::from_str("content");
repo.store_blob(&blob).unwrap();
let manifest = Manifest::new(vec![ManifestEntry {
path: "test.txt".to_string(),
blob_hash: blob.hash.clone(),
mode: FileMode::Regular,
}]);
repo.store_manifest(&manifest).unwrap();
let commit = Commit::new(
"main".to_string(),
None,
None,
manifest.hash.clone(),
"Test Author".to_string(),
Some("Initial commit".to_string()),
vec![FileChange {
path: "test.txt".to_string(),
change_type: ChangeType::Added,
old_blob_hash: None,
new_blob_hash: Some(blob.hash.clone()),
old_path: None,
old_mode: None,
new_mode: None,
}],
)
.unwrap();
repo.store_commit(&commit).unwrap();
repo.store_commit(&commit).unwrap();
let conn = repo.conn.lock().unwrap();
let count: i64 = conn
.query_row(
"SELECT COUNT(*) FROM commit_files WHERE commit_hash = ?1",
rusqlite::params![commit.hash.as_str()],
|row| row.get(0),
)
.unwrap();
assert_eq!(count, 1);
}
fn squash_commit_and_placeholder_row() -> (Commit, Commit) {
let server = Commit::with_timestamp(
"main".to_string(),
None,
Some(Hash("e5".repeat(32))),
Tree::empty_hash(),
"zach".to_string(),
Some("Land the feature".to_string()),
Vec::new(),
chrono::DateTime::parse_from_rfc3339("2026-10-22T02:14:31.185316+00:00")
.unwrap()
.with_timezone(&chrono::Utc),
)
.unwrap();
let placeholder = Commit {
author: "<remote>".to_string(),
timestamp: server.timestamp + chrono::Duration::microseconds(153_983),
..server.clone()
};
(server, placeholder)
}
#[test]
fn repair_commit_fields_rewrites_a_placeholder_row_to_the_verified_preimage() {
let repo = SqliteRepository::in_memory().unwrap();
let (server, placeholder) = squash_commit_and_placeholder_row();
repo.store_commit(&placeholder).unwrap();
repo.store_commit(&server).unwrap();
let err = repo
.get_commit(&server.hash)
.unwrap()
.unwrap()
.verify_fields()
.unwrap_err();
assert!(err.to_string().contains("author=\"<remote>\""), "{err}");
assert!(repo.repair_commit_fields(&server).unwrap());
let repaired = repo.get_commit(&server.hash).unwrap().unwrap();
repaired.verify_fields().unwrap();
assert_eq!(repaired.author, "zach");
assert_eq!(repaired.timestamp, server.timestamp);
assert!(!repo.repair_commit_fields(&server).unwrap());
}
#[test]
fn repair_commit_fields_refuses_unverified_input_and_edge_changes() {
let repo = SqliteRepository::in_memory().unwrap();
let (server, placeholder) = squash_commit_and_placeholder_row();
repo.store_commit(&placeholder).unwrap();
assert!(repo.repair_commit_fields(&placeholder).is_err());
repo.conn
.lock()
.unwrap()
.execute(
"UPDATE commits SET merge_parent_hash = ?2 WHERE hash = ?1",
rusqlite::params![server.hash.as_str(), "ab".repeat(32)],
)
.unwrap();
let err = repo.repair_commit_fields(&server).unwrap_err();
assert!(err.to_string().contains("refusing to repair"), "{err}");
assert_eq!(
repo.get_commit(&server.hash).unwrap().unwrap().author,
"<remote>"
);
let empty = SqliteRepository::in_memory().unwrap();
assert!(!empty.repair_commit_fields(&server).unwrap());
}
#[test]
fn test_branch_head() {
let repo = SqliteRepository::in_memory().unwrap();
let br = Branch::new("main".to_string(), None, None);
repo.store_branch(&br).unwrap();
assert!(repo.get_branch_head("main").unwrap().is_none());
let blob = Blob::new(b"test".to_vec());
repo.store_blob(&blob).unwrap();
let manifest = Manifest::new(vec![ManifestEntry {
path: "test.txt".to_string(),
blob_hash: blob.hash.clone(),
mode: FileMode::Regular,
}]);
repo.store_manifest(&manifest).unwrap();
let commit = Commit::new(
"main".to_string(),
None,
None,
manifest.hash.clone(),
"test".to_string(),
Some("test commit".to_string()),
vec![],
)
.unwrap();
repo.store_commit(&commit).unwrap();
repo.set_branch_head("main", &commit.hash).unwrap();
let retrieved = repo.get_branch_head("main").unwrap().unwrap();
assert_eq!(retrieved, commit.hash);
}
#[test]
fn test_list_branches() {
let repo = SqliteRepository::in_memory().unwrap();
let br1 = Branch::new("main".to_string(), None, None);
let br2 = Branch::new(
"feature".to_string(),
Some("feature branch".to_string()),
Some("main".to_string()),
);
repo.store_branch(&br1).unwrap();
repo.store_branch(&br2).unwrap();
let all = repo.list_branches().unwrap();
assert_eq!(all.len(), 2);
}
#[test]
fn legacy_sqlite_default_created_at_decodes_losslessly() {
let repo = SqliteRepository::in_memory().unwrap();
repo.store_branch(&Branch::new("main".into(), None, None))
.unwrap();
repo.store_branch(&Branch::new("legacy".into(), None, Some("main".into())))
.unwrap();
repo.conn
.lock()
.unwrap()
.execute(
"UPDATE branches SET created_at = '2026-03-04 05:06:07' WHERE name = 'legacy'",
[],
)
.unwrap();
let legacy = repo.get_branch("legacy").unwrap().unwrap();
assert_eq!(legacy.created_at.to_rfc3339(), "2026-03-04T05:06:07+00:00");
assert_eq!(repo.list_branches().unwrap().len(), 2);
}
#[test]
fn corrupt_created_at_is_isolated_to_its_row() {
let repo = SqliteRepository::in_memory().unwrap();
repo.store_branch(&Branch::new("main".into(), None, None))
.unwrap();
repo.store_branch(&Branch::new("broken".into(), None, Some("main".into())))
.unwrap();
repo.store_branch(&Branch::new("healthy".into(), None, Some("main".into())))
.unwrap();
repo.conn
.lock()
.unwrap()
.execute(
"UPDATE branches SET created_at = 'not-a-time' WHERE name = 'broken'",
[],
)
.unwrap();
let rows = repo.list_branches_isolated().unwrap();
assert_eq!(rows.len(), 3);
let unreadable: Vec<_> = rows.iter().filter_map(|row| row.as_ref().err()).collect();
assert_eq!(unreadable.len(), 1);
assert_eq!(unreadable[0].name, "broken");
assert_eq!(unreadable[0].column, "created_at");
assert_eq!(unreadable[0].status, BranchStatus::Open);
assert_eq!(unreadable[0].code(), crate::BRANCH_METADATA_UNREADABLE);
assert!(repo.get_branch("healthy").unwrap().is_some());
let error = repo.get_branch("broken").unwrap_err().to_string();
assert!(error.contains("branch_metadata_unreadable"), "{error}");
assert!(error.contains("'broken'") && error.contains("created_at"));
assert!(!error.contains("not-a-time") && !error.contains("premature"));
}
#[test]
fn parse_stored_timestamp_accepts_rfc3339_and_sqlite_forms_only() {
use crate::parse_stored_timestamp as parse;
assert!(parse("2026-03-04T05:06:07.123456+00:00").is_some());
assert!(parse("2026-03-04T05:06:07Z").is_some());
assert_eq!(
parse("2026-03-04 05:06:07.250").unwrap().to_rfc3339(),
"2026-03-04T05:06:07.250+00:00"
);
assert!(parse("2026-03-04").is_none());
assert!(parse("").is_none());
assert!(parse("yesterday").is_none());
}
#[test]
fn test_commits_for_branch() {
let repo = SqliteRepository::in_memory().unwrap();
let br = Branch::new("main".to_string(), None, None);
repo.store_branch(&br).unwrap();
let manifest = Manifest::empty();
repo.store_manifest(&manifest).unwrap();
let commit1 = Commit::new(
"main".to_string(),
None,
None,
manifest.hash.clone(),
"author".to_string(),
Some("commit 1".to_string()),
vec![],
)
.unwrap();
repo.store_commit(&commit1).unwrap();
let commit2 = Commit::new(
"main".to_string(),
Some(commit1.hash.clone()),
None,
manifest.hash.clone(),
"author".to_string(),
Some("commit 2".to_string()),
vec![],
)
.unwrap();
repo.store_commit(&commit2).unwrap();
let commits = repo.get_commits_for_branch("main").unwrap();
assert_eq!(commits.len(), 2);
}
#[test]
fn test_count_commits_for_branch() {
let repo = SqliteRepository::in_memory().unwrap();
let manifest = Manifest::empty();
repo.store_manifest(&manifest).unwrap();
assert_eq!(repo.count_commits_for_branch("feature").unwrap(), 0);
let base = Commit::new(
"main".to_string(),
None,
None,
manifest.hash.clone(),
"author".to_string(),
None,
vec![],
)
.unwrap();
repo.store_commit(&base).unwrap();
let feat1 = Commit::new(
"feature".to_string(),
Some(base.hash.clone()),
None,
manifest.hash.clone(),
"author".to_string(),
None,
vec![],
)
.unwrap();
repo.store_commit(&feat1).unwrap();
let feat2 = Commit::new(
"feature".to_string(),
Some(feat1.hash.clone()),
None,
manifest.hash.clone(),
"author".to_string(),
None,
vec![],
)
.unwrap();
repo.store_commit(&feat2).unwrap();
assert_eq!(repo.count_commits_for_branch("feature").unwrap(), 2);
assert_eq!(repo.count_commits_for_branch("main").unwrap(), 1);
assert_eq!(
repo.count_commits_for_branch("feature").unwrap(),
repo.get_commits_for_branch("feature").unwrap().len()
);
}
#[test]
fn test_merge_child_exists() {
let repo = SqliteRepository::in_memory().unwrap();
let manifest = Manifest::empty();
repo.store_manifest(&manifest).unwrap();
let feature = Commit::new(
"feature".to_string(),
None,
None,
manifest.hash.clone(),
"author".to_string(),
None,
vec![],
)
.unwrap();
repo.store_commit(&feature).unwrap();
assert!(!repo.merge_child_exists(&feature.hash).unwrap());
let main_merge = Commit::new(
"main".to_string(),
None,
Some(feature.hash.clone()),
manifest.hash.clone(),
"author".to_string(),
Some("merged feature".to_string()),
vec![],
)
.unwrap();
repo.store_commit(&main_merge).unwrap();
assert!(repo.merge_child_exists(&feature.hash).unwrap());
}
#[test]
fn test_metadata() {
let repo = SqliteRepository::in_memory().unwrap();
assert!(repo.get_head().unwrap().is_none());
let hash = Hash("a".repeat(64));
repo.set_head(&hash).unwrap();
let retrieved = repo.get_head().unwrap().unwrap();
assert_eq!(retrieved, hash);
}
#[test]
fn test_current_branch() {
let repo = SqliteRepository::in_memory().unwrap();
assert!(repo.get_current_branch_name().unwrap().is_none());
repo.set_current_branch("main").unwrap();
let name = repo.get_current_branch_name().unwrap().unwrap();
assert_eq!(name, "main");
}
#[test]
fn tag_foreign_key_migration_preserves_existing_tags() {
let temp = tempfile::TempDir::new().unwrap();
let path = temp.path().join("oak.db");
drop(SqliteRepository::open(&path).unwrap());
let conn = Connection::open(&path).unwrap();
let commit = "cd".repeat(32);
conn.execute_batch(&format!(
"PRAGMA foreign_keys=OFF;
DROP TABLE tags;
CREATE TABLE tags (
name TEXT PRIMARY KEY,
commit_hash TEXT NOT NULL,
created_at TEXT NOT NULL DEFAULT (datetime('now')),
title TEXT,
notes TEXT,
is_prerelease INTEGER NOT NULL DEFAULT 0,
finalized INTEGER NOT NULL DEFAULT 0,
published_at TEXT,
created_by TEXT NOT NULL DEFAULT '',
revision INTEGER NOT NULL DEFAULT 0,
synced INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (commit_hash) REFERENCES commits(hash)
);
INSERT INTO tags VALUES ('v1.0', '{commit}', '2026-11-01T00:00:00+00:00',
'Title', 'Notes', 1, 1, '2026-11-02T00:00:00+00:00', 'bot', 7, 1);
DELETE FROM schema_migrations WHERE version = '0018_drop_tags_commit_fk';"
))
.unwrap();
drop(conn);
let repo = SqliteRepository::open(&path).unwrap();
let conn = repo.conn.lock().unwrap();
let row: (String, String, String, i64, i64, String, i64, i64) = conn
.query_row(
"SELECT commit_hash, title, notes, is_prerelease, finalized, created_by, \
revision, synced FROM tags WHERE name = 'v1.0'",
[],
|r| {
Ok((
r.get(0)?,
r.get(1)?,
r.get(2)?,
r.get(3)?,
r.get(4)?,
r.get(5)?,
r.get(6)?,
r.get(7)?,
))
},
)
.unwrap();
assert_eq!(
row,
(
commit,
"Title".into(),
"Notes".into(),
1,
1,
"bot".into(),
7,
1
)
);
let fks: i64 = conn
.query_row(
"SELECT COUNT(*) FROM pragma_foreign_key_list('tags')",
[],
|r| r.get(0),
)
.unwrap();
assert_eq!(fks, 0);
let enforced: i64 = conn
.query_row("PRAGMA foreign_keys", [], |r| r.get(0))
.unwrap();
assert_eq!(enforced, 1, "the migration must not change enforcement");
}
#[test]
fn test_branch_description_update() {
let repo = SqliteRepository::in_memory().unwrap();
let br = Branch::new("main".to_string(), Some("old desc".to_string()), None);
repo.store_branch(&br).unwrap();
repo.update_branch_description("main", "new desc").unwrap();
let updated = repo.get_branch("main").unwrap().unwrap();
assert_eq!(updated.description, Some("new desc".to_string()));
}
#[test]
fn legacy_description_requires_matching_baseline_before_remote_refresh() {
let temp = tempfile::tempdir().unwrap();
let path = temp.path().join("oak.db");
{
let repo = SqliteRepository::open(&path).unwrap();
repo.store_branch(&Branch::new(
"feature".into(),
Some("legacy local".into()),
None,
))
.unwrap();
repo.conn
.lock()
.unwrap()
.execute_batch(
"ALTER TABLE branches DROP COLUMN description_pending;
DELETE FROM schema_migrations WHERE version = '0012_pending_branch_description';",
)
.unwrap();
}
let repo = SqliteRepository::open(&path).unwrap();
assert!(repo.branch_description_pending("feature").unwrap());
let mut remote = Branch::new("feature".into(), Some("remote changed".into()), None);
repo.upsert_branch_metadata_at_generation(&remote, repo.description_generation().unwrap())
.unwrap();
assert_eq!(
repo.get_branch("feature")
.unwrap()
.unwrap()
.description
.as_deref(),
Some("legacy local")
);
remote.description = Some("legacy local".into());
repo.upsert_branch_metadata_at_generation(&remote, repo.description_generation().unwrap())
.unwrap();
assert!(!repo.branch_description_pending("feature").unwrap());
remote.description = None;
repo.upsert_branch_metadata_at_generation(&remote, repo.description_generation().unwrap())
.unwrap();
assert_eq!(
repo.get_branch("feature").unwrap().unwrap().description,
None
);
}
#[test]
fn pending_description_survives_rename_and_stale_publish_ack() {
let repo = SqliteRepository::in_memory().unwrap();
repo.store_branch(&Branch::new("feature".into(), None, None))
.unwrap();
repo.edit_branch_description("feature", "pending").unwrap();
repo.rename_branch("feature", "renamed").unwrap();
assert!(repo.branch_description_pending("renamed").unwrap());
repo.edit_branch_description("renamed", "newer").unwrap();
repo.acknowledge_branch_description(
"renamed",
Some("pending"),
repo.description_publication_generation().unwrap(),
)
.unwrap();
assert!(repo.branch_description_pending("renamed").unwrap());
repo.acknowledge_branch_description(
"renamed",
Some("newer"),
repo.description_publication_generation().unwrap(),
)
.unwrap();
assert!(!repo.branch_description_pending("renamed").unwrap());
}
#[test]
fn description_fetch_fence_covers_aba_new_rows_and_overlapping_refreshes() {
let repo = SqliteRepository::in_memory().unwrap();
let mut remote = Branch::new("feature".into(), Some("A".into()), None);
repo.store_branch(&remote).unwrap();
let before = repo.description_generation().unwrap();
repo.edit_branch_description("feature", "B").unwrap();
repo.edit_branch_description("feature", "A").unwrap();
assert!(!repo
.reconcile_pulled_branch_metadata(&remote, before)
.unwrap());
assert!(repo.branch_description_pending("feature").unwrap());
repo.acknowledge_branch_description("feature", Some("A"), Some(before))
.unwrap();
assert!(
repo.branch_description_pending("feature").unwrap(),
"old successful POST cannot acknowledge an ABA edit"
);
repo.acknowledge_branch_description(
"feature",
Some("A"),
repo.description_publication_generation().unwrap(),
)
.unwrap();
remote.description = Some("stale remote".into());
assert!(!repo
.reconcile_pulled_branch_metadata(&remote, before)
.unwrap());
assert_eq!(
repo.get_branch("feature")
.unwrap()
.unwrap()
.description
.as_deref(),
Some("A")
);
let fresh = repo.description_generation().unwrap();
assert!(repo
.reconcile_pulled_branch_metadata(&remote, fresh)
.unwrap());
remote.description = Some("overlapping old response".into());
assert!(!repo
.upsert_branch_metadata_at_generation(&remote, fresh)
.unwrap());
assert_eq!(
repo.get_branch("feature")
.unwrap()
.unwrap()
.description
.as_deref(),
Some("stale remote")
);
let before_creation = repo.description_generation().unwrap();
let mut new_branch = Branch::new("new".into(), Some("locally created".into()), None);
repo.store_branch(&new_branch).unwrap();
new_branch.description = Some("older remote response".into());
assert!(!repo
.upsert_branch_metadata_at_generation(&new_branch, before_creation)
.unwrap());
assert_eq!(
repo.get_branch("new")
.unwrap()
.unwrap()
.description
.as_deref(),
Some("locally created")
);
let before_sibling = repo.description_generation().unwrap();
repo.edit_branch_description("new", "sibling update")
.unwrap();
assert!(repo
.reconcile_pulled_branch_metadata(&remote, before_sibling)
.unwrap());
}
#[test]
fn test_get_commits_since_edge_cases() {
let repo = SqliteRepository::in_memory().unwrap();
let br = Branch::new("main".to_string(), None, None);
repo.store_branch(&br).unwrap();
let manifest = Manifest::empty();
repo.store_manifest(&manifest).unwrap();
let commits = repo.get_commits_since("main", None).unwrap();
assert!(commits.is_empty());
let commits = repo
.get_commits_since("main", Some(&Hash("nonexistent".to_string())))
.unwrap();
assert!(commits.is_empty());
}
#[test]
fn test_get_commits_since_cross_branch_hash_returns_all() {
let repo = SqliteRepository::in_memory().unwrap();
let manifest = Manifest::empty();
repo.store_manifest(&manifest).unwrap();
repo.store_branch(&Branch::new("default".to_string(), None, None))
.unwrap();
repo.store_branch(&Branch::new(
"update".to_string(),
None,
Some("default".to_string()),
))
.unwrap();
let c_default = Commit::new(
"default".to_string(),
None,
None,
manifest.hash.clone(),
"a".to_string(),
Some("default commit".to_string()),
vec![],
)
.unwrap();
repo.store_commit(&c_default).unwrap();
let c_update1 = Commit::new(
"update".to_string(),
None,
None,
manifest.hash.clone(),
"a".to_string(),
Some("update commit 1".to_string()),
vec![],
)
.unwrap();
let c_update2 = Commit::new(
"update".to_string(),
Some(c_update1.hash.clone()),
None,
manifest.hash.clone(),
"a".to_string(),
Some("update commit 2".to_string()),
vec![],
)
.unwrap();
repo.store_commit(&c_update1).unwrap();
repo.store_commit(&c_update2).unwrap();
let since = repo
.get_commits_since("update", Some(&c_default.hash))
.unwrap();
assert_eq!(
since.len(),
2,
"should return all update commits when since_hash is from another branch"
);
assert_eq!(since[0].message, Some("update commit 1".to_string()));
assert_eq!(since[1].message, Some("update commit 2".to_string()));
}
#[test]
fn test_has_blob() {
let repo = SqliteRepository::in_memory().unwrap();
let blob = Blob::from_str("test content");
assert!(!repo.has_blob(&blob.hash).unwrap());
repo.store_blob(&blob).unwrap();
assert!(repo.has_blob(&blob.hash).unwrap());
}
#[test]
fn test_get_nonexistent_blob() {
let repo = SqliteRepository::in_memory().unwrap();
let result = repo.get_blob(&Hash("nonexistent".to_string())).unwrap();
assert!(result.is_none());
}
#[test]
fn test_store_blob_idempotent() {
let repo = SqliteRepository::in_memory().unwrap();
let blob = Blob::from_str("same content");
repo.store_blob(&blob).unwrap();
repo.store_blob(&blob).unwrap();
let retrieved = repo.get_blob(&blob.hash).unwrap().unwrap();
assert_eq!(retrieved.content, blob.content);
}
#[test]
fn test_blob_empty_content() {
let repo = SqliteRepository::in_memory().unwrap();
let blob = Blob::new(Vec::new());
repo.store_blob(&blob).unwrap();
let retrieved = repo.get_blob(&blob.hash).unwrap().unwrap();
assert!(retrieved.content.is_empty());
assert_eq!(retrieved.size, 0);
}
#[test]
fn test_put_blob_sources_matches_put_blob() {
let repo = SqliteRepository::in_memory().unwrap();
let contents: Vec<Vec<u8>> = vec![
b"alpha".to_vec(),
Vec::new(),
b"alpha".to_vec(), vec![0u8; 64 * 1024],
];
let hashes = repo
.put_blob_sources(
contents
.iter()
.cloned()
.map(BlobSource::from_content)
.collect(),
)
.unwrap();
assert_eq!(hashes.len(), contents.len());
for (content, hash) in contents.iter().zip(&hashes) {
assert_eq!(hash, &repo.put_blob(content.clone()).unwrap());
let retrieved = repo.get_blob(hash).unwrap().unwrap();
assert_eq!(&retrieved.content, content);
}
assert_eq!(hashes[0], hashes[2]);
assert!(repo.put_blob_sources(Vec::new()).unwrap().is_empty());
let sources: Vec<BlobSource> = contents
.iter()
.cloned()
.enumerate()
.map(|(i, c)| {
let hint = if i % 2 == 0 {
Some(c.len() as u64)
} else {
None
};
BlobSource::new(hint, move || Ok(c))
})
.collect();
assert_eq!(repo.put_blob_sources(sources).unwrap(), hashes);
assert!(repo.put_blob_sources(Vec::new()).unwrap().is_empty());
let single = vec![BlobSource::new(None, || Ok(b"alpha".to_vec()))];
assert_eq!(
repo.put_blob_sources(single).unwrap(),
vec![hashes[0].clone()]
);
}
#[test]
fn test_blob_binary_content() {
let repo = SqliteRepository::in_memory().unwrap();
let binary_data: Vec<u8> = (0..=255).collect();
let blob = Blob::new(binary_data.clone());
repo.store_blob(&blob).unwrap();
let retrieved = repo.get_blob(&blob.hash).unwrap().unwrap();
assert_eq!(retrieved.content, binary_data);
}
#[test]
fn test_get_nonexistent_manifest() {
let repo = SqliteRepository::in_memory().unwrap();
let result = repo.get_manifest(&Hash("nonexistent".to_string())).unwrap();
assert!(result.is_none());
}
#[test]
fn test_manifest_with_multiple_entries() {
let repo = SqliteRepository::in_memory().unwrap();
let blob1 = Blob::from_str("content1");
let blob2 = Blob::from_str("content2");
repo.store_blob(&blob1).unwrap();
repo.store_blob(&blob2).unwrap();
let manifest = Manifest::new(vec![
ManifestEntry {
path: "src/main.rs".to_string(),
blob_hash: blob1.hash.clone(),
mode: FileMode::Regular,
},
ManifestEntry {
path: "scripts/build.sh".to_string(),
blob_hash: blob2.hash.clone(),
mode: FileMode::Executable,
},
]);
repo.store_manifest(&manifest).unwrap();
let retrieved = repo.get_manifest(&manifest.hash).unwrap().unwrap();
assert_eq!(retrieved.entries.len(), 2);
let paths: Vec<&str> = retrieved.entries.iter().map(|e| e.path.as_str()).collect();
assert!(paths.contains(&"src/main.rs"));
assert!(paths.contains(&"scripts/build.sh"));
let exec_entry = retrieved
.entries
.iter()
.find(|e| e.path == "scripts/build.sh")
.unwrap();
assert_eq!(exec_entry.mode, FileMode::Executable);
}
#[test]
fn test_store_manifest_idempotent() {
let repo = SqliteRepository::in_memory().unwrap();
let manifest = Manifest::empty();
repo.store_manifest(&manifest).unwrap();
repo.store_manifest(&manifest).unwrap(); }
#[test]
fn test_get_nonexistent_branch() {
let repo = SqliteRepository::in_memory().unwrap();
let result = repo.get_branch("nonexistent").unwrap();
assert!(result.is_none());
}
#[test]
fn test_branch_status_update() {
let repo = SqliteRepository::in_memory().unwrap();
let br = Branch::new("feature".to_string(), None, None);
repo.store_branch(&br).unwrap();
assert_eq!(
repo.get_branch("feature").unwrap().unwrap().status,
BranchStatus::Open
);
repo.update_branch_status("feature", BranchStatus::Closed)
.unwrap();
assert_eq!(
repo.get_branch("feature").unwrap().unwrap().status,
BranchStatus::Closed
);
repo.update_branch_status("feature", BranchStatus::Open)
.unwrap();
assert_eq!(
repo.get_branch("feature").unwrap().unwrap().status,
BranchStatus::Open
);
}
#[test]
fn test_branch_with_parent() {
let repo = SqliteRepository::in_memory().unwrap();
let main = Branch::new("main".to_string(), None, None);
let feature = Branch::new(
"feature".to_string(),
Some("my feature".to_string()),
Some("main".to_string()),
);
repo.store_branch(&main).unwrap();
repo.store_branch(&feature).unwrap();
let retrieved = repo.get_branch("feature").unwrap().unwrap();
assert_eq!(retrieved.parent_branch, Some("main".to_string()));
assert_eq!(retrieved.description, Some("my feature".to_string()));
}
#[test]
fn test_get_branch_head_nonexistent() {
let repo = SqliteRepository::in_memory().unwrap();
let result = repo.get_branch_head("nonexistent").unwrap();
assert!(result.is_none());
}
#[test]
fn test_rename_branch_rejects_orphan_target_head() {
let repo = SqliteRepository::in_memory().unwrap();
let manifest = Manifest::empty();
repo.store_manifest(&manifest).unwrap();
repo.store_branch(&Branch::new("old".to_string(), None, None))
.unwrap();
let old_commit = Commit::new(
"old".to_string(),
None,
None,
manifest.hash.clone(),
"author".to_string(),
Some("old".to_string()),
vec![],
)
.unwrap();
let target_commit = Commit::new(
"target".to_string(),
None,
None,
manifest.hash.clone(),
"author".to_string(),
Some("target".to_string()),
vec![],
)
.unwrap();
repo.store_commit(&old_commit).unwrap();
repo.store_commit(&target_commit).unwrap();
repo.set_branch_head("old", &old_commit.hash).unwrap();
repo.set_branch_head("target", &target_commit.hash).unwrap();
let err = repo.rename_branch("old", "target").unwrap_err();
assert!(
matches!(err, OakError::BranchAlreadyExists(ref name) if name == "target"),
"expected BranchAlreadyExists, got {err:?}"
);
assert_eq!(
repo.get_branch_head("old").unwrap(),
Some(old_commit.hash.clone())
);
assert_eq!(
repo.get_branch_head("target").unwrap(),
Some(target_commit.hash.clone())
);
}
#[test]
fn test_has_commit() {
let repo = SqliteRepository::in_memory().unwrap();
let manifest = Manifest::empty();
repo.store_manifest(&manifest).unwrap();
let br = Branch::new("main".to_string(), None, None);
repo.store_branch(&br).unwrap();
let commit = Commit::new(
"main".to_string(),
None,
None,
manifest.hash.clone(),
"author".to_string(),
Some("test".to_string()),
vec![],
)
.unwrap();
assert!(!repo.has_commit(&commit.hash).unwrap());
repo.store_commit(&commit).unwrap();
assert!(repo.has_commit(&commit.hash).unwrap());
}
#[test]
fn test_get_nonexistent_commit() {
let repo = SqliteRepository::in_memory().unwrap();
let result = repo.get_commit(&Hash("nonexistent".to_string())).unwrap();
assert!(result.is_none());
}
#[test]
fn test_get_commits_since_with_valid_hash() {
let repo = SqliteRepository::in_memory().unwrap();
let manifest = Manifest::empty();
repo.store_manifest(&manifest).unwrap();
let br = Branch::new("main".to_string(), None, None);
repo.store_branch(&br).unwrap();
let c1 = Commit::new(
"main".to_string(),
None,
None,
manifest.hash.clone(),
"a".to_string(),
Some("c1".to_string()),
vec![],
)
.unwrap();
let c2 = Commit::new(
"main".to_string(),
Some(c1.hash.clone()),
None,
manifest.hash.clone(),
"a".to_string(),
Some("c2".to_string()),
vec![],
)
.unwrap();
let c3 = Commit::new(
"main".to_string(),
Some(c2.hash.clone()),
None,
manifest.hash.clone(),
"a".to_string(),
Some("c3".to_string()),
vec![],
)
.unwrap();
repo.store_commit(&c1).unwrap();
repo.store_commit(&c2).unwrap();
repo.store_commit(&c3).unwrap();
let since = repo.get_commits_since("main", Some(&c1.hash)).unwrap();
assert_eq!(since.len(), 2);
assert_eq!(since[0].message, Some("c2".to_string()));
assert_eq!(since[1].message, Some("c3".to_string()));
let since = repo.get_commits_since("main", Some(&c2.hash)).unwrap();
assert_eq!(since.len(), 1);
assert_eq!(since[0].message, Some("c3".to_string()));
let since = repo.get_commits_since("main", Some(&c3.hash)).unwrap();
assert!(since.is_empty());
}
#[test]
fn test_get_all_commits() {
let repo = SqliteRepository::in_memory().unwrap();
let manifest = Manifest::empty();
repo.store_manifest(&manifest).unwrap();
repo.store_branch(&Branch::new("main".to_string(), None, None))
.unwrap();
repo.store_branch(&Branch::new(
"feature".to_string(),
None,
Some("main".to_string()),
))
.unwrap();
let c1 = Commit::new(
"main".to_string(),
None,
None,
manifest.hash.clone(),
"a".to_string(),
Some("main commit".to_string()),
vec![],
)
.unwrap();
let c2 = Commit::new(
"feature".to_string(),
None,
None,
manifest.hash.clone(),
"a".to_string(),
Some("feature commit".to_string()),
vec![],
)
.unwrap();
repo.store_commit(&c1).unwrap();
repo.store_commit(&c2).unwrap();
let all = repo.get_all_commits().unwrap();
assert_eq!(all.len(), 2);
}
#[test]
fn test_commit_with_file_changes() {
let repo = SqliteRepository::in_memory().unwrap();
let manifest = Manifest::empty();
repo.store_manifest(&manifest).unwrap();
let br = Branch::new("main".to_string(), None, None);
repo.store_branch(&br).unwrap();
let blob_hash = Hash("abc123".repeat(11)[..64].to_string());
let commit = Commit::new(
"main".to_string(),
None,
None,
manifest.hash.clone(),
"author".to_string(),
Some("with changes".to_string()),
vec![
FileChange {
path: "added.txt".to_string(),
change_type: ChangeType::Added,
old_blob_hash: None,
new_blob_hash: Some(blob_hash.clone()),
old_path: None,
old_mode: None,
new_mode: None,
},
FileChange {
path: "modified.txt".to_string(),
change_type: ChangeType::Modified,
old_blob_hash: Some(blob_hash.clone()),
new_blob_hash: Some(blob_hash.clone()),
old_path: None,
old_mode: None,
new_mode: None,
},
FileChange {
path: "deleted.txt".to_string(),
change_type: ChangeType::Deleted,
old_blob_hash: Some(blob_hash.clone()),
new_blob_hash: None,
old_path: None,
old_mode: None,
new_mode: None,
},
],
)
.unwrap();
repo.store_commit(&commit).unwrap();
let retrieved = repo.get_commit(&commit.hash).unwrap().unwrap();
assert_eq!(retrieved.files.len(), 3);
assert!(retrieved
.files
.iter()
.any(|f| f.path == "added.txt" && f.change_type == ChangeType::Added));
assert!(retrieved
.files
.iter()
.any(|f| f.path == "modified.txt" && f.change_type == ChangeType::Modified));
assert!(retrieved
.files
.iter()
.any(|f| f.path == "deleted.txt" && f.change_type == ChangeType::Deleted));
}
#[test]
fn test_chunk_roundtrip() {
let repo = SqliteRepository::in_memory().unwrap();
let data = b"chunk content here";
let hash = hash_bytes(data);
assert!(!repo.has_chunk(&hash).unwrap());
repo.store_chunk(&hash, data).unwrap();
assert!(repo.has_chunk(&hash).unwrap());
let retrieved = repo.get_chunk(&hash).unwrap().unwrap();
assert_eq!(retrieved, data);
}
#[test]
fn stored_blob_reader_streams_raw_and_compressed_rows() {
use std::io::Read;
let dir = tempfile::tempdir().unwrap();
let repo = SqliteRepository::open(&dir.path().join("oak.db")).unwrap();
let mut state = 0x9E37_79B9u32;
let incompressible: Vec<u8> = (0..3 * 1024 * 1024 + 17)
.map(|_| {
state ^= state << 13;
state ^= state >> 17;
state ^= state << 5;
(state & 0xFF) as u8
})
.collect();
let compressible = b"oak stored blob reader\n".repeat(200_000);
for content in [incompressible, compressible, Vec::new()] {
let hash = repo.put_blob(content.clone()).unwrap();
let (size, mut reader) = repo.stored_blob_reader(&hash).unwrap().unwrap();
assert_eq!(size, content.len() as u64);
let mut read = Vec::new();
reader.read_to_end(&mut read).unwrap();
assert!(read == content, "streamed bytes differ for {hash}");
let (_, mut interleaved) = repo.stored_blob_reader(&hash).unwrap().unwrap();
let mut first = [0u8; 7];
let got = interleaved.read(&mut first).unwrap();
assert_eq!(repo.get_blob_size(&hash).unwrap(), Some(size));
assert_eq!(&first[..got], &content[..got]);
}
assert!(repo
.stored_blob_reader(&crate::hash_bytes(b"absent"))
.unwrap()
.is_none());
}
#[test]
fn test_store_chunk_rejects_hash_mismatch() {
let repo = SqliteRepository::in_memory().unwrap();
let claimed = hash_bytes(b"expected");
let err = repo.store_chunk(&claimed, b"different").unwrap_err();
assert!(
matches!(err, OakError::InvalidHash(_)),
"expected invalid hash error, got {err:?}"
);
assert!(!repo.has_chunk(&claimed).unwrap());
}
#[test]
fn test_get_nonexistent_chunk() {
let repo = SqliteRepository::in_memory().unwrap();
let result = repo.get_chunk(&Hash("nonexistent".to_string())).unwrap();
assert!(result.is_none());
}
#[test]
fn test_blob_chunks_mapping() {
let repo = SqliteRepository::in_memory().unwrap();
let blob_hash = Hash("blobhash".repeat(8)[..64].to_string());
let chunk1_data = b"chunk1data";
let chunk2_data = b"chunk2data";
let chunk1_hash = hash_bytes(chunk1_data);
let chunk2_hash = hash_bytes(chunk2_data);
repo.store_chunk(&chunk1_hash, chunk1_data).unwrap();
repo.store_chunk(&chunk2_hash, chunk2_data).unwrap();
let chunks = vec![
ChunkInfo {
hash: chunk1_hash,
offset: 0,
length: 1024,
},
ChunkInfo {
hash: chunk2_hash,
offset: 1024,
length: 512,
},
];
assert!(repo.get_blob_chunks(&blob_hash).unwrap().is_none());
repo.store_blob_chunks(&blob_hash, &chunks).unwrap();
let retrieved = repo.get_blob_chunks(&blob_hash).unwrap().unwrap();
assert_eq!(retrieved.len(), 2);
assert_eq!(retrieved[0].offset, 0);
assert_eq!(retrieved[0].length, 1024);
assert_eq!(retrieved[1].offset, 1024);
assert_eq!(retrieved[1].length, 512);
}
#[test]
fn test_metadata_overwrite() {
let repo = SqliteRepository::in_memory().unwrap();
repo.set_metadata(MetadataKey::RemoteUrl, "http://old.example.com")
.unwrap();
repo.set_metadata(MetadataKey::RemoteUrl, "http://new.example.com")
.unwrap();
let val = repo.get_metadata(MetadataKey::RemoteUrl).unwrap().unwrap();
assert_eq!(val, "http://new.example.com");
}
#[test]
fn test_metadata_nonexistent_key() {
let repo = SqliteRepository::in_memory().unwrap();
let result = repo.get_metadata(MetadataKey::ApiKey).unwrap();
assert!(result.is_none());
}
#[test]
fn test_bulk_importer_roundtrip() {
let tmp = tempfile::tempdir().unwrap();
let db_path = tmp.path().join("oak.db");
let repo = SqliteRepository::open(&db_path).unwrap();
let branch = Branch::new("main".to_string(), None, None);
repo.store_branch(&branch).unwrap();
let mut importer = repo.bulk_importer(2).unwrap();
let blob_hash = importer.put_blob(b"hello".to_vec()).unwrap();
let entries = vec![ManifestEntry {
path: "a.txt".to_string(),
blob_hash: blob_hash.clone(),
mode: FileMode::Regular,
}];
let manifest_hash = importer.put_tree(entries.clone()).unwrap();
let ts = chrono::DateTime::from_timestamp(1_000_000, 0).unwrap();
let c1 = Commit::with_timestamp(
"main".to_string(),
None,
None,
manifest_hash.clone(),
"tester <t@example.com>".to_string(),
None,
vec![FileChange {
path: "a.txt".to_string(),
change_type: ChangeType::Added,
old_blob_hash: None,
new_blob_hash: Some(blob_hash.clone()),
old_path: None,
old_mode: None,
new_mode: None,
}],
ts,
)
.unwrap();
importer.store_commit(&c1).unwrap();
importer.store_commit(&c1).unwrap();
let manifest_hash2 = importer.put_tree(entries).unwrap();
assert_eq!(manifest_hash, manifest_hash2);
let c2 = Commit::with_timestamp(
"main".to_string(),
Some(c1.hash.clone()),
None,
manifest_hash2,
"tester <t@example.com>".to_string(),
None,
Vec::new(),
ts,
)
.unwrap();
importer.store_commit(&c2).unwrap();
importer.finish().unwrap();
assert!(repo.has_commit(&c1.hash).unwrap());
assert!(repo.has_commit(&c2.hash).unwrap());
assert!(repo.has_blob(&blob_hash).unwrap());
let manifest = repo.get_manifest(&manifest_hash).unwrap().unwrap();
assert_eq!(manifest.entries.len(), 1);
assert_eq!(manifest.entries[0].path, "a.txt");
let commit_file_count: i64 = repo
.conn
.lock()
.unwrap()
.query_row(
"SELECT COUNT(*) FROM commit_files WHERE commit_hash = ?1",
rusqlite::params![c1.hash.as_str()],
|row| row.get(0),
)
.unwrap();
assert_eq!(commit_file_count, 1);
}
#[test]
fn test_bulk_importer_drop_rolls_back() {
let tmp = tempfile::tempdir().unwrap();
let db_path = tmp.path().join("oak.db");
let repo = SqliteRepository::open(&db_path).unwrap();
let commit_hash = {
let mut importer = repo.bulk_importer(100).unwrap();
let blob_hash = importer.put_blob(b"ephemeral".to_vec()).unwrap();
let entries = vec![ManifestEntry {
path: "x.txt".to_string(),
blob_hash,
mode: FileMode::Regular,
}];
let manifest_hash = importer.put_tree(entries).unwrap();
let c = Commit::with_timestamp(
"main".to_string(),
None,
None,
manifest_hash,
"tester <t@example.com>".to_string(),
None,
Vec::new(),
chrono::DateTime::from_timestamp(0, 0).unwrap(),
)
.unwrap();
importer.store_commit(&c).unwrap();
c.hash
};
assert!(!repo.has_commit(&commit_hash).unwrap());
}
#[test]
fn test_bulk_txn_commit_persists() {
let tmp = tempfile::tempdir().unwrap();
let db_path = tmp.path().join("oak.db");
let repo = SqliteRepository::open(&db_path).unwrap();
let a = hash_bytes(b"aaaa");
let b = hash_bytes(b"bbbb");
repo.bulk_begin().unwrap();
repo.store_chunk(&a, b"aaaa").unwrap();
repo.store_chunk(&b, b"bbbb").unwrap();
repo.bulk_commit().unwrap();
assert!(repo.has_chunk(&a).unwrap());
assert!(repo.has_chunk(&b).unwrap());
}
#[test]
fn test_bulk_txn_rollback_discards() {
let tmp = tempfile::tempdir().unwrap();
let db_path = tmp.path().join("oak.db");
let repo = SqliteRepository::open(&db_path).unwrap();
let a = hash_bytes(b"aaaa");
repo.bulk_begin().unwrap();
repo.store_chunk(&a, b"aaaa").unwrap();
repo.bulk_rollback();
assert!(!repo.has_chunk(&a).unwrap());
repo.store_chunk(&a, b"aaaa").unwrap();
assert!(repo.has_chunk(&a).unwrap());
}
#[test]
fn test_bulk_txn_flush_checkpoints_then_rollback() {
let tmp = tempfile::tempdir().unwrap();
let db_path = tmp.path().join("oak.db");
let repo = SqliteRepository::open(&db_path).unwrap();
let before = hash_bytes(b"keep");
let after = hash_bytes(b"drop");
repo.bulk_begin().unwrap();
repo.store_chunk(&before, b"keep").unwrap();
repo.bulk_flush().unwrap();
repo.store_chunk(&after, b"drop").unwrap();
repo.bulk_rollback();
assert!(repo.has_chunk(&before).unwrap());
assert!(!repo.has_chunk(&after).unwrap());
}
#[test]
fn test_write_txn_commit_persists() {
let repo = SqliteRepository::in_memory().unwrap();
repo.write_txn_begin().unwrap();
repo.set_metadata(MetadataKey::RemoteUrl, "https://example.com")
.unwrap();
repo.write_txn_commit().unwrap();
assert_eq!(
repo.get_metadata(MetadataKey::RemoteUrl)
.unwrap()
.as_deref(),
Some("https://example.com")
);
}
#[test]
fn test_write_txn_rollback_discards() {
let repo = SqliteRepository::in_memory().unwrap();
repo.write_txn_begin().unwrap();
repo.set_metadata(MetadataKey::RemoteUrl, "https://example.com")
.unwrap();
repo.write_txn_rollback();
assert_eq!(repo.get_metadata(MetadataKey::RemoteUrl).unwrap(), None);
repo.set_metadata(MetadataKey::RemoteUrl, "https://example.com")
.unwrap();
assert_eq!(
repo.get_metadata(MetadataKey::RemoteUrl)
.unwrap()
.as_deref(),
Some("https://example.com")
);
}
}
#[cfg(test)]
mod storage_diagnostic_tests {
use super::*;
#[test]
fn sqlite_full_becomes_typed_path_diagnostic() {
let sqlite = rusqlite::Error::SqliteFailure(
rusqlite::ffi::Error::new(rusqlite::ffi::SQLITE_FULL),
None,
);
let error = sqlite_open_error(Path::new("repo/.oak/oak.db"), sqlite);
assert!(matches!(error, OakError::DiskFull { path } if path == "repo/.oak/oak.db"));
}
#[test]
fn missing_database_parent_is_not_misreported_as_full() {
let temp = tempfile::TempDir::new().unwrap();
let path = temp.path().join("missing/oak.db");
let error = SqliteRepository::open(&path).err().unwrap();
assert!(matches!(error, OakError::Database(_)), "{error}");
assert!(!path.exists());
}
}