Skip to content

Commit d6ac58a

Browse files
committed
feat: Add OdbBackend::write
1 parent d094829 commit d6ac58a

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

src/odb_backend.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,34 @@ pub trait OdbBackend {
129129
unimplemented!("OdbBackend::read_header")
130130
}
131131

132+
/// Write an object.
133+
///
134+
/// Corresponds to the `write` function of [`git_odb_backend`].
135+
/// Requires that [`SupportedOperations::WRITE`] is present in the value returned from
136+
/// [`supported_operations`] to expose it to libgit2.
137+
///
138+
/// The default implementation of this method panics.
139+
///
140+
/// # Implementation notes
141+
///
142+
/// `oid` is calculated by libgit2 prior to this method being called.
143+
///
144+
/// # Errors
145+
///
146+
/// See [`OdbBackend`].
147+
///
148+
/// [`git_odb_backend`]: raw::git_odb_backend
149+
/// [`supported_operations`]: Self::supported_operations
150+
fn write(
151+
&mut self,
152+
ctx: &OdbBackendContext,
153+
oid: Oid,
154+
object_type: ObjectType,
155+
data: &[u8],
156+
) -> Result<(), Error> {
157+
unimplemented!("OdbBackend::write")
158+
}
159+
132160
/// Check if an object exists.
133161
///
134162
/// Corresponds to the `exists` function of [`git_odb_backend`].
@@ -183,7 +211,6 @@ pub trait OdbBackend {
183211
unimplemented!("OdbBackend::exists_prefix")
184212
}
185213

186-
// TODO: fn write()
187214
// TODO: fn writestream()
188215
// TODO: fn readstream()
189216
// TODO: fn exists_prefix()
@@ -379,6 +406,7 @@ impl<'a, B: OdbBackend> CustomOdbBackend<'a, B> {
379406
op_if!(read if READ);
380407
op_if!(read_prefix if READ_PREFIX);
381408
op_if!(read_header if READ_HEADER);
409+
op_if!(write if WRITE);
382410
op_if!(exists if EXISTS);
383411
op_if!(exists_prefix if EXISTS_PREFIX);
384412

@@ -493,6 +521,24 @@ impl<B: OdbBackend> Backend<B> {
493521
raw::GIT_OK
494522
}
495523

524+
extern "C" fn write(
525+
backend_ptr: *mut raw::git_odb_backend,
526+
oid_ptr: *const raw::git_oid,
527+
data_ptr: *const libc::c_void,
528+
len: usize,
529+
otype: raw::git_object_t,
530+
) -> libc::c_int {
531+
let backend = unsafe { backend_ptr.cast::<Backend<B>>().as_mut().unwrap() };
532+
let oid = unsafe { Oid::from_raw(oid_ptr) };
533+
let data = unsafe { slice::from_raw_parts(data_ptr.cast::<u8>(), len) };
534+
let object_type = ObjectType::from_raw(otype).unwrap();
535+
let context = OdbBackendContext { backend_ptr };
536+
if let Err(e) = backend.inner.write(&context, oid, object_type, data) {
537+
return unsafe { e.raw_set_git_error() };
538+
}
539+
raw::GIT_OK
540+
}
541+
496542
extern "C" fn exists(
497543
backend_ptr: *mut raw::git_odb_backend,
498544
oid_ptr: *const raw::git_oid,

0 commit comments

Comments
 (0)