From f3cab8959ebc92fa91de25d2ad6391ea73816cd3 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Fri, 2 Feb 2024 16:41:43 +0800 Subject: [PATCH] Allow find object by prefix --- libgit2-sys/lib.rs | 7 +++++++ src/repo.rs | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/libgit2-sys/lib.rs b/libgit2-sys/lib.rs index 420ca6d7d3..3de6881842 100644 --- a/libgit2-sys/lib.rs +++ b/libgit2-sys/lib.rs @@ -2205,6 +2205,13 @@ extern "C" { id: *const git_oid, kind: git_object_t, ) -> c_int; + pub fn git_object_lookup_prefix( + dest: *mut *mut git_object, + repo: *mut git_repository, + id: *const git_oid, + len: size_t, + kind: git_object_t, + ) -> c_int; pub fn git_object_type(obj: *const git_object) -> git_object_t; pub fn git_object_peel( peeled: *mut *mut git_object, diff --git a/src/repo.rs b/src/repo.rs index 6d3b20f8f8..b11a3f1685 100644 --- a/src/repo.rs +++ b/src/repo.rs @@ -1459,6 +1459,25 @@ impl Repository { } } + /// Lookup a reference to one of the objects by id prefix in a repository. + pub fn find_object_by_prefix( + &self, + prefix_hash: &str, + kind: Option, + ) -> Result, Error> { + let mut raw = ptr::null_mut(); + unsafe { + try_call!(raw::git_object_lookup_prefix( + &mut raw, + self.raw(), + Oid::from_str(prefix_hash)?.raw(), + prefix_hash.len(), + kind + )); + Ok(Binding::from_raw(raw)) + } + } + /// Create a new direct reference. /// /// This function will return an error if a reference already exists with