Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions gix/src/object/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
19 changes: 17 additions & 2 deletions gix/tests/gix/repository/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand All @@ -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(),
Expand All @@ -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 {
Expand Down
Loading