Skip to content

Commit

Permalink
fix(changelog): correctly pick tagged HEAD commit
Browse files Browse the repository at this point in the history
Closes #144
  • Loading branch information
oknozor committed Nov 30, 2021
1 parent fbe02bb commit 9b5a591
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/git/revspec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,16 @@ impl Repository {
let to = pattern.to.as_deref();

// Is the given `to` arg a tag or an oid ?
let maybe_to_tag = to.map(|to| self.resolve_tag(to).ok()).flatten();
let maybe_to_tag = match to {
// No target tag provided, check if HEAD is tagged
None => {
let head = self.get_head_commit_oid()?;

self.get_latest_tag().ok().filter(|tag| *tag.oid() == head)
}
// Try to resolve a tag from the provided range, ex: ..1.0.0
Some(to) => self.resolve_tag(to).ok(),
};

// get/validate the target oid
let to = match to {
Expand Down
54 changes: 54 additions & 0 deletions tests/cog_tests/changelog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,57 @@ fn get_changelog_at_tag_prefix() -> Result<()> {
Ok(())
})
}

#[test]
fn get_changelog_from_tag_to_tagged_head() -> Result<()> {
run_test_with_context(|_| {
// Arrange
git_init()?;
git_commit("chore: init")?;
let commit_one = git_commit("feat: start")?;
let commit_two = git_commit("feat: feature 1")?;
git_tag("1.0.0")?;
let commit_three = git_commit("feat: feature 2")?;
let commit_four = git_commit("fix: bug fix 1")?;
let commit_five = git_commit("chore(version): 2.0.0")?;
git_tag("2.0.0")?;
git_log()?;

// Act
let changelog = Command::cargo_bin("cog")?
.arg("changelog")
// Assert
.assert()
.success();

let changelog = changelog.get_output();
let changelog = String::from_utf8_lossy(&changelog.stdout);
let today = Utc::today().naive_utc();

assert_eq!(
changelog.as_ref(),
formatdoc!(
"## 2.0.0 - {today}
#### Bug Fixes
- bug fix 1 - ({commit_four}) - Tom
#### Features
- feature 2 - ({commit_three}) - Tom
#### Miscellaneous Chores
- **(version)** 2.0.0 - ({commit_five}) - Tom
- - -
## 1.0.0 - {today}
#### Features
- feature 1 - ({commit_two}) - Tom
- start - ({commit_one}) - Tom
",
today = today,
commit_one = &commit_one[0..7],
commit_two = &commit_two[0..7],
commit_three = &commit_three[0..7],
commit_four = &commit_four[0..7],
commit_five = &commit_five[0..7],
)
);
Ok(())
})
}

0 comments on commit 9b5a591

Please sign in to comment.