Skip to content

Commit 9ee27b5

Browse files
committed
feat: gix::Repository implements all traits for object reading and writing.
That way it becomes usable when merging trees, which benefits from automatic checking of hashes before writing loose objects.
1 parent 2c5021c commit 9ee27b5

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

gix/src/repository/impls.rs

+55
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
use gix_object::Exists;
2+
use std::ops::DerefMut;
3+
14
impl Clone for crate::Repository {
25
fn clone(&self) -> Self {
36
let mut new = crate::Repository::from_refs_and_objects(
@@ -93,3 +96,55 @@ impl From<crate::Repository> for crate::ThreadSafeRepository {
9396
}
9497
}
9598
}
99+
100+
impl gix_object::Write for crate::Repository {
101+
fn write(&self, object: &dyn gix_object::WriteTo) -> Result<gix_hash::ObjectId, gix_object::write::Error> {
102+
let mut buf = self.empty_reusable_buffer();
103+
object.write_to(buf.deref_mut())?;
104+
self.write_buf(object.kind(), &buf)
105+
}
106+
107+
fn write_buf(&self, object: gix_object::Kind, from: &[u8]) -> Result<gix_hash::ObjectId, gix_object::write::Error> {
108+
let oid = gix_object::compute_hash(self.object_hash(), object, from);
109+
if self.objects.exists(&oid) {
110+
return Ok(oid);
111+
}
112+
self.objects.write_buf(object, from)
113+
}
114+
115+
fn write_stream(
116+
&self,
117+
kind: gix_object::Kind,
118+
size: u64,
119+
from: &mut dyn std::io::Read,
120+
) -> Result<gix_hash::ObjectId, gix_object::write::Error> {
121+
let mut buf = self.empty_reusable_buffer();
122+
let bytes = std::io::copy(from, buf.deref_mut())?;
123+
if size != bytes {
124+
return Err(format!("Found {bytes} bytes in stream, but had {size} bytes declared").into());
125+
}
126+
self.write_buf(kind, &buf)
127+
}
128+
}
129+
130+
impl gix_object::FindHeader for crate::Repository {
131+
fn try_header(&self, id: &gix_hash::oid) -> Result<Option<gix_object::Header>, gix_object::find::Error> {
132+
self.objects.try_header(id)
133+
}
134+
}
135+
136+
impl gix_object::Find for crate::Repository {
137+
fn try_find<'a>(
138+
&self,
139+
id: &gix_hash::oid,
140+
buffer: &'a mut Vec<u8>,
141+
) -> Result<Option<gix_object::Data<'a>>, gix_object::find::Error> {
142+
self.objects.try_find(id, buffer)
143+
}
144+
}
145+
146+
impl gix_object::Exists for crate::Repository {
147+
fn exists(&self, id: &gix_hash::oid) -> bool {
148+
self.objects.exists(id)
149+
}
150+
}

0 commit comments

Comments
 (0)