Skip to content

Commit

Permalink
fix: support annotated tags in cog check -l
Browse files Browse the repository at this point in the history
Annotated tags are unique git objects with their own hashes.
Consequently, prior to this commit, cog check would not recognize these
tags in the commit ranges it checked.

This commit fixes this bug by detecting if a tag is an annotated tag,
and identifying its associated commit.

Fixes #151.
  • Loading branch information
lukehsiao authored and oknozor committed Dec 7, 2021
1 parent 7fcb0bd commit 66faca2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
3 changes: 2 additions & 1 deletion cog.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ authors = [
{ signature = "orhun", username = "orhun" },
{ signature = "Danny Tatom", username = "its-danny" },
{ signature = "Clément Poissonnier", username = "cpoissonnier" },
]
{ signature = "Luke Hsiao", username = "lukehsiao" },
]
57 changes: 56 additions & 1 deletion src/git/revspec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,22 @@ impl Repository {

let mut tags = vec![];
self.0
.tag_foreach(|oid, name| {
.tag_foreach(|mut oid, name| {
let name = String::from_utf8_lossy(name);
let name = name.as_ref().strip_prefix("refs/tags/").unwrap();

// If this is an annotated tag, find the first parent commit
if self.0.revparse_single(name).unwrap().as_commit().is_none() {
if let Some(commit) = self
.0
.revparse_single([name, "^{}"].concat().as_str())
.unwrap()
.as_commit()
{
oid = commit.id();
}
};

if range.contains(&oid) {
if let Ok(tag) = Tag::new(name, Some(oid)) {
tags.push(tag);
Expand Down Expand Up @@ -361,6 +374,48 @@ mod test {
Ok(())
}

#[sealed_test]
fn get_annotated_tag_commits() -> Result<()> {
// Arrange
let repo = Repository::init(".")?;
run_cmd!(
git init
echo changes > file
git add .
)?;

let start = repo.commit("chore: init")?;

run_cmd!(
git init
echo changes > file2
git add .
)?;

let _end = repo.commit("chore: 1.0.0")?;

// Create an annotated tag
let head = repo.get_head_commit().unwrap();
let sig = git2::Signature::now("Author", "email@example.com")?;
repo.0
.tag("1.0.0", &head.into_object(), &sig, "the_msg", false)?;

run_cmd!(
git init
echo changes > file3
git add .
)?;

repo.commit("feat: a commit")?;

let commit_range = repo.get_commit_range(&RevspecPattern::from("..1.0.0"))?;

assert_that!(commit_range.from).is_equal_to(OidOf::Other(start));
assert_that!(commit_range.to.to_string()).is_equal_to("1.0.0".to_string());
assert_that!(commit_range.commits).has_length(1);
Ok(())
}

#[sealed_test]
fn get_tag_commits() -> Result<()> {
// Arrange
Expand Down

0 comments on commit 66faca2

Please sign in to comment.