From 871788d261f60b3b319f1d0d8a450b1cfc70c667 Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Mon, 7 Mar 2022 08:04:20 -0800 Subject: [PATCH] Add binding for git_odb_exists_ext (#818) This allows checking for the existence of an object without refreshing the ODB if the lookup fails. Useful when doing a batch of lookup operations for objects that may legitimately not exist. --- libgit2-sys/lib.rs | 7 +++++++ src/lib.rs | 11 +++++++++++ src/odb.rs | 11 +++++++++-- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/libgit2-sys/lib.rs b/libgit2-sys/lib.rs index d23c3ec4ff..195f1371e2 100644 --- a/libgit2-sys/lib.rs +++ b/libgit2-sys/lib.rs @@ -1511,6 +1511,12 @@ pub struct git_odb_backend { pub free: Option, } +git_enum! { + pub enum git_odb_lookup_flags_t { + GIT_ODB_LOOKUP_NO_REFRESH = 1 << 0, + } +} + #[repr(C)] pub struct git_odb_writepack { pub backend: *mut git_odb_backend, @@ -3836,6 +3842,7 @@ extern "C" { ) -> c_int; pub fn git_odb_exists(odb: *mut git_odb, oid: *const git_oid) -> c_int; + pub fn git_odb_exists_ext(odb: *mut git_odb, oid: *const git_oid, flags: c_uint) -> c_int; pub fn git_odb_refresh(odb: *mut git_odb) -> c_int; diff --git a/src/lib.rs b/src/lib.rs index 60cfc3e071..cdc3648d83 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -637,6 +637,17 @@ impl MergePreference { is_bit_set!(is_fastforward_only, MergePreference::FASTFORWARD_ONLY); } +bitflags! { + /// Flags controlling the behavior of ODB lookup operations + pub struct OdbLookupFlags: u32 { + /// Don't call `git_odb_refresh` if the lookup fails. Useful when doing + /// a batch of lookup operations for objects that may legitimately not + /// exist. When using this flag, you may wish to manually call + /// `git_odb_refresh` before processing a batch of objects. + const NO_REFRESH = raw::GIT_ODB_LOOKUP_NO_REFRESH as u32; + } +} + #[cfg(test)] #[macro_use] mod test; diff --git a/src/odb.rs b/src/odb.rs index 12f0707fa5..f64a52c7e4 100644 --- a/src/odb.rs +++ b/src/odb.rs @@ -6,11 +6,13 @@ use std::slice; use std::ffi::CString; -use libc::{c_char, c_int, c_void, size_t}; +use libc::{c_char, c_int, c_uint, c_void, size_t}; use crate::panic; use crate::util::Binding; -use crate::{raw, Error, IndexerProgress, Mempack, Object, ObjectType, Oid, Progress}; +use crate::{ + raw, Error, IndexerProgress, Mempack, Object, ObjectType, OdbLookupFlags, Oid, Progress, +}; /// A structure to represent a git object database pub struct Odb<'repo> { @@ -186,6 +188,11 @@ impl<'repo> Odb<'repo> { unsafe { raw::git_odb_exists(self.raw, oid.raw()) != 0 } } + /// Checks if the object database has an object, with extended flags. + pub fn exists_ext(&self, oid: Oid, flags: OdbLookupFlags) -> bool { + unsafe { raw::git_odb_exists_ext(self.raw, oid.raw(), flags.bits() as c_uint) != 0 } + } + /// Potentially finds an object that starts with the given prefix. pub fn exists_prefix(&self, short_oid: Oid, len: usize) -> Result { unsafe {