History

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

Implement WriteTo trait for gix::Blob

Sebastian Thiel committed 1 year ago

Commit d302fa22 parents e60e253 34990502 files · +34 −2Browse files

Unified · Split

2 files
  1. gix/src/object/impls.rs +17 −0
  2. gix/tests/gix/repository/object.rs +17 −2
gix/src/object/impls.rs+17−0
@@ -150,6 +150,23 @@ impl std::fmt::Debug for Object<'_> { · 149 unmodified lines
150150 }
151151 }
152152
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<'_> {
157+ fn write_to(&self, out: &mut dyn std::io::Write) -> std::io::Result<()> {
158+ out.write_all(&self.data)
159+ }
160+
161+ fn kind(&self) -> gix_object::Kind {
162+ gix_object::Kind::Blob
163+ }
164+
165+ fn size(&self) -> u64 {
166+ self.data.len() as u64
167+ }
168+}
169+
153170 /// In conjunction with the handles free list, leaving an empty Vec in place of the original causes it to not be
154171 /// returned to the free list.
155172 fn steal_from_freelist(data: &mut Vec<u8>) -> Vec<u8> {
gix/tests/gix/repository/object.rs+17−2
@@ -262,7 +262,7 @@ mod write_object { · 261 unmodified lines
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 { · 8 unmodified lines
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(),
@@ -292,6 +292,21 @@ mod write_object { · 8 unmodified lines
292292 );
293293 Ok(())
294294 }
295+
296+ #[test]
297+ fn blob_write_to_implementation() -> crate::Result {
298+ let repo = empty_bare_in_memory_repo()?;
299+ let blob = repo.empty_blob();
300+
301+ // Create a blob directly to test our WriteTo implementation
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());
305+
306+ assert_eq!(actual_blob.data, blob.data);
307+
308+ Ok(())
309+ }
295310 }
296311
297312 mod write_blob {