diff --git a/commitizen/commands/changelog.py b/commitizen/commands/changelog.py index e177062b5..62d175d27 100644 --- a/commitizen/commands/changelog.py +++ b/commitizen/commands/changelog.py @@ -29,11 +29,15 @@ def __init__(self, config: BaseConfig, args): self.config: BaseConfig = config self.cz = factory.commiter_factory(self.config) - self.start_rev = args.get("start_rev") + self.start_rev = args.get("start_rev") or self.config.settings.get( + "changelog_start_rev" + ) self.file_name = args.get("file_name") or self.config.settings.get( "changelog_file" ) - self.incremental = args["incremental"] + self.incremental = args["incremental"] or self.config.settings.get( + "changelog_incremental" + ) self.dry_run = args["dry_run"] self.unreleased_version = args["unreleased_version"] self.change_type_map = ( diff --git a/commitizen/defaults.py b/commitizen/defaults.py index 8c871840e..97854453e 100644 --- a/commitizen/defaults.py +++ b/commitizen/defaults.py @@ -11,6 +11,8 @@ "tag_format": None, # example v$version "bump_message": None, # bumped v$current_version to $new_version "changelog_file": "CHANGELOG.md", + "changelog_incremental": False, + "changelog_start_rev": None, } MAJOR = "MAJOR" diff --git a/docs/changelog.md b/docs/changelog.md index a50b906df..41a5615d6 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -114,6 +114,8 @@ cz changelog --file-name="CHANGES.md" ### `incremental` +This flag can be set in the `toml` file with the key `changelog_incremental` under `tools.commitizen` + Benefits: - Build from latest version found in changelog, this is useful if you have a different changelog and want to use commitizen @@ -124,6 +126,28 @@ Benefits: cz changelog --incremental ``` +```toml +[tools.commitizen] +# ... +changelog_incremental = true +``` + +### `start-rev` + +This value can be set in the `toml` file with the key `changelog_start_rev` under `tools.commitizen` + +Start from a given git rev to generate the changelog. Commits before that rev will not be considered. This is especially useful for long-running projects adopting conventional commits, where old commit messages might fail to be parsed for changelog generation. + +```bash +cz changelog --start-rev="v0.2.0" +``` + +```toml +[tools.commitizen] +# ... +changelog_start_rev = "v0.2.0" +``` + ## Hooks Supported hook methods: diff --git a/tests/commands/test_changelog_command.py b/tests/commands/test_changelog_command.py index 09a96dfd7..d52d157c7 100644 --- a/tests/commands/test_changelog_command.py +++ b/tests/commands/test_changelog_command.py @@ -20,6 +20,11 @@ def changelog_path() -> str: return os.path.join(os.getcwd(), "CHANGELOG.md") +@pytest.fixture() +def config_path() -> str: + return os.path.join(os.getcwd(), "pyproject.toml") + + @pytest.mark.usefixtures("tmp_commitizen_project") def test_changelog_on_empty_project(mocker): testargs = ["cz", "changelog", "--dry-run"] @@ -389,3 +394,48 @@ def test_breaking_change_content_v1(mocker, capsys): "## Unreleased\n\n### Feat\n\n- **users**: email pattern corrected\n\n" "### BREAKING CHANGE\n\n- migrate by renaming user to users\n\n" ) + + +@pytest.mark.usefixtures("tmp_commitizen_project") +def test_changelog_config_flag_increment(mocker, changelog_path, config_path): + + with open(config_path, "a") as f: + f.write("changelog_incremental = true\n") + with open(changelog_path, "a") as f: + f.write("\nnote: this should be persisted using increment\n") + + create_file_and_commit("feat: add new output") + + testargs = ["cz", "changelog"] + mocker.patch.object(sys, "argv", testargs) + cli.main() + + with open(changelog_path, "r") as f: + out = f.read() + + assert "this should be persisted using increment" in out + + +@pytest.mark.usefixtures("tmp_commitizen_project") +def test_changelog_config_start_rev_option(mocker, capsys, config_path): + + # create commit and tag + create_file_and_commit("feat: new file") + testargs = ["cz", "bump", "--yes"] + mocker.patch.object(sys, "argv", testargs) + cli.main() + capsys.readouterr() + + create_file_and_commit("feat: after 0.2.0") + create_file_and_commit("feat: after 0.2") + + with open(config_path, "a") as f: + f.write('changelog_start_rev = "0.2.0"\n') + + testargs = ["cz", "changelog", "--dry-run"] + mocker.patch.object(sys, "argv", testargs) + with pytest.raises(DryRunExit): + cli.main() + + out, _ = capsys.readouterr() + assert out == "## Unreleased\n\n### Feat\n\n- after 0.2\n- after 0.2.0\n\n" diff --git a/tests/test_conf.py b/tests/test_conf.py index 241f2117d..ba621bd63 100644 --- a/tests/test_conf.py +++ b/tests/test_conf.py @@ -32,6 +32,8 @@ "version_files": ["commitizen/__version__.py", "pyproject.toml"], "style": [["pointer", "reverse"], ["question", "underline"]], "changelog_file": "CHANGELOG.md", + "changelog_incremental": False, + "changelog_start_rev": None, } _new_settings = { @@ -42,6 +44,8 @@ "version_files": ["commitizen/__version__.py", "pyproject.toml"], "style": [["pointer", "reverse"], ["question", "underline"]], "changelog_file": "CHANGELOG.md", + "changelog_incremental": False, + "changelog_start_rev": None, } _read_settings = {