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

Allow find commit by short hash prefix #1011

Merged
merged 1 commit into from
Jan 23, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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