Skip to content

Commit 223945f

Browse files
committed
Allow find object by prefix
1 parent 0731960 commit 223945f

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

Diff for: libgit2-sys/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -2205,6 +2205,13 @@ extern "C" {
22052205
id: *const git_oid,
22062206
kind: git_object_t,
22072207
) -> c_int;
2208+
pub fn git_object_lookup_prefix(
2209+
dest: *mut *mut git_object,
2210+
repo: *mut git_repository,
2211+
id: *const git_oid,
2212+
len: size_t,
2213+
kind: git_object_t,
2214+
) -> c_int;
22082215
pub fn git_object_type(obj: *const git_object) -> git_object_t;
22092216
pub fn git_object_peel(
22102217
peeled: *mut *mut git_object,

Diff for: src/repo.rs

+30
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,25 @@ impl Repository {
14591459
}
14601460
}
14611461

1462+
/// Lookup a reference to one of the objects by id prefix in a repository.
1463+
pub fn find_object_by_prefix(
1464+
&self,
1465+
prefix_hash: &str,
1466+
kind: Option<ObjectType>,
1467+
) -> Result<Object<'_>, Error> {
1468+
let mut raw = ptr::null_mut();
1469+
unsafe {
1470+
try_call!(raw::git_object_lookup_prefix(
1471+
&mut raw,
1472+
self.raw(),
1473+
Oid::from_str(prefix_hash)?.raw(),
1474+
prefix_hash.len(),
1475+
kind
1476+
));
1477+
Ok(Binding::from_raw(raw))
1478+
}
1479+
}
1480+
14621481
/// Create a new direct reference.
14631482
///
14641483
/// This function will return an error if a reference already exists with
@@ -3678,6 +3697,17 @@ mod tests {
36783697
assert_eq!(repo.head().unwrap().target().unwrap(), main_oid);
36793698
}
36803699

3700+
#[test]
3701+
fn smoke_find_object_by_prefix() {
3702+
let (_td, repo) = crate::test::repo_init();
3703+
let head = repo.head().unwrap().target().unwrap();
3704+
let head = repo.find_commit(head).unwrap();
3705+
let head_id = head.id();
3706+
let head_prefix = &head_id.to_string()[..7];
3707+
let obj = repo.find_object_by_prefix(head_prefix, None).unwrap();
3708+
assert_eq!(obj.id(), head_id);
3709+
}
3710+
36813711
/// create the following:
36823712
/// /---o4
36833713
/// /---o3

0 commit comments

Comments
 (0)