Skip to content

Commit 871788d

Browse files
authored
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.
1 parent d703dd9 commit 871788d

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

libgit2-sys/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1511,6 +1511,12 @@ pub struct git_odb_backend {
15111511
pub free: Option<extern "C" fn(*mut git_odb_backend)>,
15121512
}
15131513

1514+
git_enum! {
1515+
pub enum git_odb_lookup_flags_t {
1516+
GIT_ODB_LOOKUP_NO_REFRESH = 1 << 0,
1517+
}
1518+
}
1519+
15141520
#[repr(C)]
15151521
pub struct git_odb_writepack {
15161522
pub backend: *mut git_odb_backend,
@@ -3836,6 +3842,7 @@ extern "C" {
38363842
) -> c_int;
38373843

38383844
pub fn git_odb_exists(odb: *mut git_odb, oid: *const git_oid) -> c_int;
3845+
pub fn git_odb_exists_ext(odb: *mut git_odb, oid: *const git_oid, flags: c_uint) -> c_int;
38393846

38403847
pub fn git_odb_refresh(odb: *mut git_odb) -> c_int;
38413848

src/lib.rs

+11
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,17 @@ impl MergePreference {
637637
is_bit_set!(is_fastforward_only, MergePreference::FASTFORWARD_ONLY);
638638
}
639639

640+
bitflags! {
641+
/// Flags controlling the behavior of ODB lookup operations
642+
pub struct OdbLookupFlags: u32 {
643+
/// Don't call `git_odb_refresh` if the lookup fails. Useful when doing
644+
/// a batch of lookup operations for objects that may legitimately not
645+
/// exist. When using this flag, you may wish to manually call
646+
/// `git_odb_refresh` before processing a batch of objects.
647+
const NO_REFRESH = raw::GIT_ODB_LOOKUP_NO_REFRESH as u32;
648+
}
649+
}
650+
640651
#[cfg(test)]
641652
#[macro_use]
642653
mod test;

src/odb.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ use std::slice;
66

77
use std::ffi::CString;
88

9-
use libc::{c_char, c_int, c_void, size_t};
9+
use libc::{c_char, c_int, c_uint, c_void, size_t};
1010

1111
use crate::panic;
1212
use crate::util::Binding;
13-
use crate::{raw, Error, IndexerProgress, Mempack, Object, ObjectType, Oid, Progress};
13+
use crate::{
14+
raw, Error, IndexerProgress, Mempack, Object, ObjectType, OdbLookupFlags, Oid, Progress,
15+
};
1416

1517
/// A structure to represent a git object database
1618
pub struct Odb<'repo> {
@@ -186,6 +188,11 @@ impl<'repo> Odb<'repo> {
186188
unsafe { raw::git_odb_exists(self.raw, oid.raw()) != 0 }
187189
}
188190

191+
/// Checks if the object database has an object, with extended flags.
192+
pub fn exists_ext(&self, oid: Oid, flags: OdbLookupFlags) -> bool {
193+
unsafe { raw::git_odb_exists_ext(self.raw, oid.raw(), flags.bits() as c_uint) != 0 }
194+
}
195+
189196
/// Potentially finds an object that starts with the given prefix.
190197
pub fn exists_prefix(&self, short_oid: Oid, len: usize) -> Result<Oid, Error> {
191198
unsafe {

0 commit comments

Comments
 (0)