Skip to content

Commit 4f9b2c5

Browse files
authored
Merge pull request rust-lang#1015 from Brooooooklyn/lookup-tag-prefix
Allow find tag by prefix hash
2 parents 93dc901 + 15e61e4 commit 4f9b2c5

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/repo.rs

+36
Original file line numberDiff line numberDiff line change
@@ -1963,6 +1963,20 @@ impl Repository {
19631963
}
19641964
}
19651965

1966+
/// Lookup a tag object by prefix hash from the repository.
1967+
pub fn find_tag_by_prefix(&self, prefix_hash: &str) -> Result<Tag<'_>, Error> {
1968+
let mut raw = ptr::null_mut();
1969+
unsafe {
1970+
try_call!(raw::git_tag_lookup_prefix(
1971+
&mut raw,
1972+
self.raw,
1973+
Oid::from_str(prefix_hash)?.raw(),
1974+
prefix_hash.len()
1975+
));
1976+
Ok(Binding::from_raw(raw))
1977+
}
1978+
}
1979+
19661980
/// Delete an existing tag reference.
19671981
///
19681982
/// The tag name will be checked for validity, see `tag` for some rules
@@ -4237,4 +4251,26 @@ Committer Name <committer.proper@email> <committer@email>"#,
42374251
assert_eq!(mm_resolve_author.email(), mailmapped_author.email());
42384252
assert_eq!(mm_resolve_committer.email(), mailmapped_committer.email());
42394253
}
4254+
4255+
#[test]
4256+
fn smoke_find_tag_by_prefix() {
4257+
let (_td, repo) = crate::test::repo_init();
4258+
let head = repo.head().unwrap();
4259+
let tag_oid = repo
4260+
.tag(
4261+
"tag",
4262+
&repo
4263+
.find_object(head.peel_to_commit().unwrap().id(), None)
4264+
.unwrap(),
4265+
&repo.signature().unwrap(),
4266+
"message",
4267+
false,
4268+
)
4269+
.unwrap();
4270+
let tag = repo.find_tag(tag_oid).unwrap();
4271+
let found_tag = repo
4272+
.find_tag_by_prefix(&tag.id().to_string()[0..7])
4273+
.unwrap();
4274+
assert_eq!(tag.id(), found_tag.id());
4275+
}
42404276
}

0 commit comments

Comments
 (0)