Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add binding for git_odb_exists_ext #818

Merged
merged 1 commit into from Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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