From b61199ff7a06e8614754baadf18ba31cda810d6a Mon Sep 17 00:00:00 2001 From: Christian Meffert Date: Mon, 18 Mar 2024 21:38:51 +0100 Subject: [PATCH] fix: Deduplicate commits for unreleased version with merged branches PR-76: https://github.com/pawamoy/git-changelog/pull/76 --- src/git_changelog/build.py | 2 +- tests/test_build.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/git_changelog/build.py b/src/git_changelog/build.py index d263903..eb11ea3 100644 --- a/src/git_changelog/build.py +++ b/src/git_changelog/build.py @@ -157,7 +157,7 @@ def add_commit(self, commit: Commit) -> None: commit: The git commit. """ self.commits.append(commit) - commit.version = self.tag + commit.version = self.tag or "HEAD" if commit_type := commit.convention.get("type"): if commit_type not in self.sections_dict: section = Section(section_type=commit_type) diff --git a/tests/test_build.py b/tests/test_build.py index 9f5bd4e..c8d4f6c 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -218,3 +218,41 @@ def test_no_remote_url(repo: GitRepo) -> None: expected_prev_tag=None, expected_commits=[commit_a], ) + + +def test_merge_into_unreleased(repo: GitRepo) -> None: + r"""Test parsing and grouping commits to versions. + + Commit graph: + main A---C---E + \ / \ / + feat B D + + Expected: + - Unreleased: E D C B A + + Parameters: + repo: GitRepo to a temporary repository. + """ + commit_a = repo.first_hash + repo.branch("feat/1") + repo.checkout("feat/1") + commit_b = repo.commit("feat: B") + repo.checkout("main") + commit_c = repo.merge("feat/1") + repo.branch("feat/2") + repo.checkout("feat/2") + commit_d = repo.commit("feat: D") + repo.checkout("main") + commit_e = repo.merge("feat/2") + + changelog = Changelog(repo.path, convention=AngularConvention) + + assert len(changelog.versions_list) == 1 + version = changelog.versions_list[0] + _assert_version( + version, + expected_tag="0.1.0", + expected_prev_tag=None, + expected_commits=[commit_e, commit_c, commit_d, commit_a, commit_b], + )