diff --git a/libgit2-sys/lib.rs b/libgit2-sys/lib.rs index 6c42d70d2d..420ca6d7d3 100644 --- a/libgit2-sys/lib.rs +++ b/libgit2-sys/lib.rs @@ -2827,6 +2827,12 @@ extern "C" { repo: *mut git_repository, id: *const git_oid, ) -> c_int; + pub fn git_commit_lookup_prefix( + commit: *mut *mut git_commit, + repo: *mut git_repository, + id: *const git_oid, + len: size_t, + ) -> c_int; pub fn git_commit_message(commit: *const git_commit) -> *const c_char; pub fn git_commit_message_encoding(commit: *const git_commit) -> *const c_char; pub fn git_commit_message_raw(commit: *const git_commit) -> *const c_char; diff --git a/src/repo.rs b/src/repo.rs index 921e2b30e2..6d3b20f8f8 100644 --- a/src/repo.rs +++ b/src/repo.rs @@ -1418,6 +1418,20 @@ impl Repository { } } + /// Lookup a reference to one of the commits in a repository by short hash. + pub fn find_commit_by_prefix(&self, prefix_hash: &str) -> Result, Error> { + let mut raw = ptr::null_mut(); + unsafe { + try_call!(raw::git_commit_lookup_prefix( + &mut raw, + self.raw(), + Oid::from_str(prefix_hash)?.raw(), + prefix_hash.len() + )); + Ok(Binding::from_raw(raw)) + } + } + /// Creates an `AnnotatedCommit` from the given commit id. pub fn find_annotated_commit(&self, id: Oid) -> Result, Error> { unsafe {