Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 86efdcb

Browse files
authored
Merge pull request #294 from janw/add-changelog-argument-options
Add changelog argument options
2 parents 14b6c38 + 9e9fdb2 commit 86efdcb

File tree

5 files changed

+86
-2
lines changed

5 files changed

+86
-2
lines changed

commitizen/commands/changelog.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@ def __init__(self, config: BaseConfig, args):
2929
self.config: BaseConfig = config
3030
self.cz = factory.commiter_factory(self.config)
3131

32-
self.start_rev = args.get("start_rev")
32+
self.start_rev = args.get("start_rev") or self.config.settings.get(
33+
"changelog_start_rev"
34+
)
3335
self.file_name = args.get("file_name") or self.config.settings.get(
3436
"changelog_file"
3537
)
36-
self.incremental = args["incremental"]
38+
self.incremental = args["incremental"] or self.config.settings.get(
39+
"changelog_incremental"
40+
)
3741
self.dry_run = args["dry_run"]
3842
self.unreleased_version = args["unreleased_version"]
3943
self.change_type_map = (

commitizen/defaults.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
"tag_format": None, # example v$version
1212
"bump_message": None, # bumped v$current_version to $new_version
1313
"changelog_file": "CHANGELOG.md",
14+
"changelog_incremental": False,
15+
"changelog_start_rev": None,
1416
}
1517

1618
MAJOR = "MAJOR"

docs/changelog.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ cz changelog --file-name="CHANGES.md"
114114
115115
### `incremental`
116116
117+
This flag can be set in the `toml` file with the key `changelog_incremental` under `tools.commitizen`
118+
117119
Benefits:
118120
119121
- 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:
124126
cz changelog --incremental
125127
```
126128
129+
```toml
130+
[tools.commitizen]
131+
# ...
132+
changelog_incremental = true
133+
```
134+
135+
### `start-rev`
136+
137+
This value can be set in the `toml` file with the key `changelog_start_rev` under `tools.commitizen`
138+
139+
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.
140+
141+
```bash
142+
cz changelog --start-rev="v0.2.0"
143+
```
144+
145+
```toml
146+
[tools.commitizen]
147+
# ...
148+
changelog_start_rev = "v0.2.0"
149+
```
150+
127151
## Hooks
128152
129153
Supported hook methods:

tests/commands/test_changelog_command.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ def changelog_path() -> str:
2020
return os.path.join(os.getcwd(), "CHANGELOG.md")
2121

2222

23+
@pytest.fixture()
24+
def config_path() -> str:
25+
return os.path.join(os.getcwd(), "pyproject.toml")
26+
27+
2328
@pytest.mark.usefixtures("tmp_commitizen_project")
2429
def test_changelog_on_empty_project(mocker):
2530
testargs = ["cz", "changelog", "--dry-run"]
@@ -389,3 +394,48 @@ def test_breaking_change_content_v1(mocker, capsys):
389394
"## Unreleased\n\n### Feat\n\n- **users**: email pattern corrected\n\n"
390395
"### BREAKING CHANGE\n\n- migrate by renaming user to users\n\n"
391396
)
397+
398+
399+
@pytest.mark.usefixtures("tmp_commitizen_project")
400+
def test_changelog_config_flag_increment(mocker, changelog_path, config_path):
401+
402+
with open(config_path, "a") as f:
403+
f.write("changelog_incremental = true\n")
404+
with open(changelog_path, "a") as f:
405+
f.write("\nnote: this should be persisted using increment\n")
406+
407+
create_file_and_commit("feat: add new output")
408+
409+
testargs = ["cz", "changelog"]
410+
mocker.patch.object(sys, "argv", testargs)
411+
cli.main()
412+
413+
with open(changelog_path, "r") as f:
414+
out = f.read()
415+
416+
assert "this should be persisted using increment" in out
417+
418+
419+
@pytest.mark.usefixtures("tmp_commitizen_project")
420+
def test_changelog_config_start_rev_option(mocker, capsys, config_path):
421+
422+
# create commit and tag
423+
create_file_and_commit("feat: new file")
424+
testargs = ["cz", "bump", "--yes"]
425+
mocker.patch.object(sys, "argv", testargs)
426+
cli.main()
427+
capsys.readouterr()
428+
429+
create_file_and_commit("feat: after 0.2.0")
430+
create_file_and_commit("feat: after 0.2")
431+
432+
with open(config_path, "a") as f:
433+
f.write('changelog_start_rev = "0.2.0"\n')
434+
435+
testargs = ["cz", "changelog", "--dry-run"]
436+
mocker.patch.object(sys, "argv", testargs)
437+
with pytest.raises(DryRunExit):
438+
cli.main()
439+
440+
out, _ = capsys.readouterr()
441+
assert out == "## Unreleased\n\n### Feat\n\n- after 0.2\n- after 0.2.0\n\n"

tests/test_conf.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
"version_files": ["commitizen/__version__.py", "pyproject.toml"],
3333
"style": [["pointer", "reverse"], ["question", "underline"]],
3434
"changelog_file": "CHANGELOG.md",
35+
"changelog_incremental": False,
36+
"changelog_start_rev": None,
3537
}
3638

3739
_new_settings = {
@@ -42,6 +44,8 @@
4244
"version_files": ["commitizen/__version__.py", "pyproject.toml"],
4345
"style": [["pointer", "reverse"], ["question", "underline"]],
4446
"changelog_file": "CHANGELOG.md",
47+
"changelog_incremental": False,
48+
"changelog_start_rev": None,
4549
}
4650

4751
_read_settings = {

0 commit comments

Comments
 (0)