Skip to content

Commit

Permalink
Merge pull request #1015 from Brooooooklyn/lookup-tag-prefix
Browse files Browse the repository at this point in the history
Allow find tag by prefix hash
  • Loading branch information
ehuss authored Feb 7, 2024
2 parents 93dc901 + 15e61e4 commit 4f9b2c5
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1963,6 +1963,20 @@ impl Repository {
}
}

/// Lookup a tag object by prefix hash from the repository.
pub fn find_tag_by_prefix(&self, prefix_hash: &str) -> Result<Tag<'_>, Error> {
let mut raw = ptr::null_mut();
unsafe {
try_call!(raw::git_tag_lookup_prefix(
&mut raw,
self.raw,
Oid::from_str(prefix_hash)?.raw(),
prefix_hash.len()
));
Ok(Binding::from_raw(raw))
}
}

/// Delete an existing tag reference.
///
/// The tag name will be checked for validity, see `tag` for some rules
Expand Down Expand Up @@ -4237,4 +4251,26 @@ Committer Name <committer.proper@email> <committer@email>"#,
assert_eq!(mm_resolve_author.email(), mailmapped_author.email());
assert_eq!(mm_resolve_committer.email(), mailmapped_committer.email());
}

#[test]
fn smoke_find_tag_by_prefix() {
let (_td, repo) = crate::test::repo_init();
let head = repo.head().unwrap();
let tag_oid = repo
.tag(
"tag",
&repo
.find_object(head.peel_to_commit().unwrap().id(), None)
.unwrap(),
&repo.signature().unwrap(),
"message",
false,
)
.unwrap();
let tag = repo.find_tag(tag_oid).unwrap();
let found_tag = repo
.find_tag_by_prefix(&tag.id().to_string()[0..7])
.unwrap();
assert_eq!(tag.id(), found_tag.id());
}
}

0 comments on commit 4f9b2c5

Please sign in to comment.