Skip to content

Commit

Permalink
Merge pull request #1011 from Brooooooklyn/master
Browse files Browse the repository at this point in the history
Allow find commit by short hash prefix
  • Loading branch information
ehuss authored Jan 23, 2024
2 parents 2d03f7e + ed5ac5f commit 502099a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions libgit2-sys/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Commit<'_>, 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<AnnotatedCommit<'_>, Error> {
unsafe {
Expand Down

0 comments on commit 502099a

Please sign in to comment.