Skip to content

Commit 3499050

Browse files
committed
refactor
1 parent ca99882 commit 3499050

File tree

2 files changed

+12
-68
lines changed

2 files changed

+12
-68
lines changed

gix/src/object/impls.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,10 @@ impl std::fmt::Debug for Object<'_> {
150150
}
151151
}
152152

153-
impl<'repo> gix_object::WriteTo for Blob<'repo> {
153+
/// Note that the `data` written here might not correspond to the `id` of the `Blob` anymore if it was modified.
154+
/// Also, this is merely for convenience when writing empty blobs to the ODB. For writing any blob, use
155+
/// [`Repository::write_blob()`](crate::Repository::write_blob()).
156+
impl gix_object::WriteTo for Blob<'_> {
154157
fn write_to(&self, out: &mut dyn std::io::Write) -> std::io::Result<()> {
155158
out.write_all(&self.data)
156159
}

gix/tests/gix/repository/object.rs

Lines changed: 8 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ mod write_object {
262262
let oid = repo.write_object(gix::objs::TreeRef::empty())?;
263263
assert_eq!(
264264
oid,
265-
gix::hash::ObjectId::empty_tree(repo.object_hash()),
265+
repo.object_hash().empty_tree(),
266266
"it produces a well-known empty tree id"
267267
);
268268
Ok(())
@@ -277,7 +277,7 @@ mod write_object {
277277
time: Default::default(),
278278
};
279279
let commit = gix::objs::Commit {
280-
tree: gix::hash::ObjectId::empty_tree(repo.object_hash()),
280+
tree: repo.object_hash().empty_tree(),
281281
author: actor.clone(),
282282
committer: actor,
283283
parents: Default::default(),
@@ -296,74 +296,15 @@ mod write_object {
296296
#[test]
297297
fn blob_write_to_implementation() -> crate::Result {
298298
let repo = empty_bare_in_memory_repo()?;
299-
let test_data = b"hello world";
300-
299+
let blob = repo.empty_blob();
300+
301301
// Create a blob directly to test our WriteTo implementation
302-
let blob_id = repo.write_blob(test_data)?;
303-
let blob = repo.find_object(blob_id)?.into_blob();
304-
305-
// Test that we can use the blob with write_object (which requires WriteTo)
306-
let written_id = repo.write_object(blob)?;
307-
308-
// The written blob should have the same ID as the original
309-
assert_eq!(blob_id, written_id, "WriteTo implementation should produce identical blob");
310-
311-
// Verify the content is correct
312-
let retrieved_blob = repo.find_object(written_id)?.into_blob();
313-
assert_eq!(retrieved_blob.data, test_data, "Blob data should be preserved");
314-
315-
Ok(())
316-
}
302+
let actual_id = repo.write_object(&blob)?;
303+
let actual_blob = repo.find_object(actual_id)?.into_blob();
304+
assert_eq!(actual_id, repo.object_hash().empty_blob());
317305

318-
#[test]
319-
fn blob_write_to_properties() -> crate::Result {
320-
let repo = empty_bare_in_memory_repo()?;
321-
let test_data = b"test data for WriteTo properties";
322-
323-
// Create a blob to test WriteTo trait methods
324-
let blob_id = repo.write_blob(test_data)?;
325-
let blob = repo.find_object(blob_id)?.into_blob();
326-
327-
// Test WriteTo trait methods directly
328-
use gix_object::WriteTo;
329-
330-
// Test kind() method
331-
assert_eq!(blob.kind(), gix_object::Kind::Blob, "kind() should return Blob");
332-
333-
// Test size() method
334-
assert_eq!(blob.size(), test_data.len() as u64, "size() should return data length");
335-
336-
// Test write_to() method
337-
let mut buffer = Vec::new();
338-
blob.write_to(&mut buffer)?;
339-
assert_eq!(buffer, test_data, "write_to() should write blob data verbatim");
340-
341-
Ok(())
342-
}
306+
assert_eq!(actual_blob.data, blob.data);
343307

344-
#[test]
345-
fn blob_write_to_empty_blob() -> crate::Result {
346-
let repo = empty_bare_in_memory_repo()?;
347-
let empty_data = b"";
348-
349-
// Create an empty blob to test edge case
350-
let blob_id = repo.write_blob(empty_data)?;
351-
let blob = repo.find_object(blob_id)?.into_blob();
352-
353-
// Test WriteTo trait methods with empty blob
354-
use gix_object::WriteTo;
355-
356-
assert_eq!(blob.kind(), gix_object::Kind::Blob, "kind() should return Blob for empty blob");
357-
assert_eq!(blob.size(), 0, "size() should return 0 for empty blob");
358-
359-
let mut buffer = Vec::new();
360-
blob.write_to(&mut buffer)?;
361-
assert_eq!(buffer, empty_data, "write_to() should write empty data for empty blob");
362-
363-
// Test that we can write the empty blob using write_object
364-
let written_id = repo.write_object(blob)?;
365-
assert_eq!(blob_id, written_id, "WriteTo implementation should work for empty blobs");
366-
367308
Ok(())
368309
}
369310
}

0 commit comments

Comments
 (0)