1
1
mirror of https://github.com/Byron/gitoxide synced 2025-10-06 01:52:40 +02:00

Merge pull request #2170 from GitoxideLabs/copilot/fix-7a3e1d32-c145-43e2-8e87-319b255dbc2f

Implement WriteTo trait for gix::Blob
This commit is contained in:
Sebastian Thiel
2025-09-15 05:40:03 +02:00
committed by GitHub
2 changed files with 34 additions and 2 deletions

View File

@@ -150,6 +150,23 @@ impl std::fmt::Debug for Object<'_> {
}
}
/// Note that the `data` written here might not correspond to the `id` of the `Blob` anymore if it was modified.
/// Also, this is merely for convenience when writing empty blobs to the ODB. For writing any blob, use
/// [`Repository::write_blob()`](crate::Repository::write_blob()).
impl gix_object::WriteTo for Blob<'_> {
fn write_to(&self, out: &mut dyn std::io::Write) -> std::io::Result<()> {
out.write_all(&self.data)
}
fn kind(&self) -> gix_object::Kind {
gix_object::Kind::Blob
}
fn size(&self) -> u64 {
self.data.len() as u64
}
}
/// In conjunction with the handles free list, leaving an empty Vec in place of the original causes it to not be
/// returned to the free list.
fn steal_from_freelist(data: &mut Vec<u8>) -> Vec<u8> {

View File

@@ -262,7 +262,7 @@ mod write_object {
let oid = repo.write_object(gix::objs::TreeRef::empty())?;
assert_eq!(
oid,
gix::hash::ObjectId::empty_tree(repo.object_hash()),
repo.object_hash().empty_tree(),
"it produces a well-known empty tree id"
);
Ok(())
@@ -277,7 +277,7 @@ mod write_object {
time: Default::default(),
};
let commit = gix::objs::Commit {
tree: gix::hash::ObjectId::empty_tree(repo.object_hash()),
tree: repo.object_hash().empty_tree(),
author: actor.clone(),
committer: actor,
parents: Default::default(),
@@ -292,6 +292,21 @@ mod write_object {
);
Ok(())
}
#[test]
fn blob_write_to_implementation() -> crate::Result {
let repo = empty_bare_in_memory_repo()?;
let blob = repo.empty_blob();
// Create a blob directly to test our WriteTo implementation
let actual_id = repo.write_object(&blob)?;
let actual_blob = repo.find_object(actual_id)?.into_blob();
assert_eq!(actual_id, repo.object_hash().empty_blob());
assert_eq!(actual_blob.data, blob.data);
Ok(())
}
}
mod write_blob {