Skip to content

Commit c69030c

Browse files
committed
feat: Add OdbBackend::refresh and OdbBackend::freshen
1 parent d6ac58a commit c69030c

File tree

1 file changed

+77
-3
lines changed

1 file changed

+77
-3
lines changed

src/odb_backend.rs

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,63 @@ pub trait OdbBackend {
211211
unimplemented!("OdbBackend::exists_prefix")
212212
}
213213

214+
/// Refreshes the backend.
215+
///
216+
/// Corresponds to the `refresh` function of [`git_odb_backend`].
217+
/// Requires that [`SupportedOperations::REFRESH`] is present in the value returned from
218+
/// [`supported_operations`] to expose it to libgit2.
219+
///
220+
/// The default implementation of this method returns `Ok(())`.
221+
///
222+
/// # Implementation notes
223+
///
224+
/// This method is called automatically when a lookup fails (e.g. through
225+
/// [`OdbBackend::exists`], [`OdbBackend::read`], or [`OdbBackend::read_header`]),
226+
/// or when [`Odb::refresh`](crate::Odb::refresh) is invoked.
227+
///
228+
/// # Errors
229+
///
230+
/// See [`OdbBackend`].
231+
///
232+
/// [`git_odb_backend`]: raw::git_odb_backend
233+
/// [`supported_operations`]: Self::supported_operations
234+
fn refresh(&mut self, ctx: &OdbBackendContext) -> Result<(), Error> {
235+
Ok(())
236+
}
237+
238+
/// "Freshens" an already existing object, updating its last-used time.
239+
///
240+
/// Corresponds to the `freshen` function of [`git_odb_backend`].
241+
/// Requires that [`SupportedOperations::REFRESH`] is present in the value returned from
242+
/// [`supported_operations`] to expose it to libgit2.
243+
///
244+
/// The default implementation of this method panics.
245+
///
246+
/// # Implementation notes
247+
///
248+
/// This method is called when [`Odb::write`](crate::Odb::write) is called, but the object
249+
/// already exists and will not be rewritten.
250+
///
251+
/// Implementations may want to update last-used timestamps.
252+
///
253+
/// Implementations SHOULD return `Ok(())` if the object exists and was freshened; otherwise,
254+
/// they SHOULD return an error.
255+
///
256+
/// # Errors
257+
///
258+
/// See [`OdbBackend`].
259+
///
260+
/// [`git_odb_backend`]: raw::git_odb_backend
261+
/// [`supported_operations`]: Self::supported_operations
262+
fn freshen(&mut self, ctx: &OdbBackendContext, oid: Oid) -> Result<(), Error> {
263+
unimplemented!("OdbBackend::freshen")
264+
}
265+
214266
// TODO: fn writestream()
215267
// TODO: fn readstream()
216-
// TODO: fn exists_prefix()
217-
// TODO: fn refresh()
218268
// TODO: fn foreach()
219269
// TODO: fn writepack()
220270
// TODO: fn writemidx()
221-
// TODO: fn freshen()
222271
}
223272

224273
bitflags! {
@@ -409,6 +458,8 @@ impl<'a, B: OdbBackend> CustomOdbBackend<'a, B> {
409458
op_if!(write if WRITE);
410459
op_if!(exists if EXISTS);
411460
op_if!(exists_prefix if EXISTS_PREFIX);
461+
op_if!(refresh if REFRESH);
462+
op_if!(freshen if FRESHEN);
412463

413464
backend.free = Some(Backend::<B>::free);
414465
}
@@ -578,6 +629,29 @@ impl<B: OdbBackend> Backend<B> {
578629
raw::GIT_OK
579630
}
580631

632+
extern "C" fn refresh(backend_ptr: *mut raw::git_odb_backend) -> libc::c_int {
633+
let backend = unsafe { backend_ptr.cast::<Self>().as_mut().unwrap() };
634+
let context = OdbBackendContext { backend_ptr };
635+
if let Err(e) = backend.inner.refresh(&context) {
636+
return unsafe { e.raw_set_git_error() };
637+
}
638+
raw::GIT_OK
639+
}
640+
641+
extern "C" fn freshen(
642+
backend_ptr: *mut raw::git_odb_backend,
643+
oid_ptr: *const raw::git_oid,
644+
) -> libc::c_int {
645+
let backend = unsafe { backend_ptr.cast::<Self>().as_mut().unwrap() };
646+
let oid = unsafe { Oid::from_raw(oid_ptr) };
647+
let context = OdbBackendContext { backend_ptr };
648+
if let Err(e) = backend.inner.freshen(&context, oid) {
649+
return unsafe { e.raw_set_git_error() };
650+
}
651+
652+
raw::GIT_OK
653+
}
654+
581655
extern "C" fn free(backend: *mut raw::git_odb_backend) {
582656
let inner = unsafe { Box::from_raw(backend.cast::<Self>()) };
583657
drop(inner);

0 commit comments

Comments
 (0)