Skip to content

Commit

Permalink
Add binding for git_odb_exists_ext (#818)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
joshtriplett committed Mar 7, 2022
1 parent d703dd9 commit 871788d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
7 changes: 7 additions & 0 deletions libgit2-sys/lib.rs
Expand Up @@ -1511,6 +1511,12 @@ pub struct git_odb_backend {
pub free: Option<extern "C" fn(*mut git_odb_backend)>,
}

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,
Expand Down Expand Up @@ -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;

Expand Down
11 changes: 11 additions & 0 deletions src/lib.rs
Expand Up @@ -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;
Expand Down
11 changes: 9 additions & 2 deletions src/odb.rs
Expand Up @@ -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> {
Expand Down Expand Up @@ -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<Oid, Error> {
unsafe {
Expand Down

0 comments on commit 871788d

Please sign in to comment.