use std::path::Path;
use dk_core::{Error, Result};
pub struct GitObjects<'a> {
repo: &'a super::GitRepository,
}
impl<'a> GitObjects<'a> {
pub fn new(repo: &'a super::GitRepository) -> Self {
Self { repo }
}
pub fn write_blob(&self, data: &[u8]) -> Result<String> {
let id = self.repo.inner().write_blob(data).map_err(|e| {
Error::Git(format!("failed to write blob: {}", e))
})?;
Ok(id.to_hex().to_string())
}
pub fn read_blob(&self, oid_hex: &str) -> Result<Vec<u8>> {
let oid = gix::ObjectId::from_hex(oid_hex.as_bytes()).map_err(|e| {
Error::Git(format!("invalid OID '{}': {}", oid_hex, e))
})?;
let object = self.repo.inner().find_object(oid).map_err(|e| {
Error::Git(format!("object not found '{}': {}", oid_hex, e))
})?;
if object.kind != gix::object::Kind::Blob {
return Err(Error::Git(format!(
"object '{}' is not a blob (found {:?})",
oid_hex, object.kind
)));
}
Ok(object.data.to_vec())
}
pub fn read_file(&self, file_path: &Path) -> Result<Vec<u8>> {
let full_path = self.repo.path().join(file_path);
std::fs::read(&full_path).map_err(|e| {
Error::Git(format!("failed to read file {}: {}", full_path.display(), e))
})
}
pub fn write_file(&self, file_path: &Path, content: &[u8]) -> Result<()> {
let full_path = self.repo.path().join(file_path);
if let Some(parent) = full_path.parent() {
std::fs::create_dir_all(parent).map_err(|e| {
Error::Git(format!(
"failed to create parent dirs for {}: {}",
full_path.display(),
e
))
})?;
}
std::fs::write(&full_path, content).map_err(|e| {
Error::Git(format!("failed to write file {}: {}", full_path.display(), e))
})
}
}