diff --git a/gix/src/object/impls.rs b/gix/src/object/impls.rs index 941b42dacb6..f8c8894123f 100644 --- a/gix/src/object/impls.rs +++ b/gix/src/object/impls.rs @@ -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) -> Vec { diff --git a/gix/tests/gix/repository/object.rs b/gix/tests/gix/repository/object.rs index 3ef570606a6..89b0e6419a5 100644 --- a/gix/tests/gix/repository/object.rs +++ b/gix/tests/gix/repository/object.rs @@ -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 {