Skip to content

Commit 3f891ed

Browse files
feat: add cz dump-config command to output effective configuration
Closes #1331 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 4d99415 commit 3f891ed

12 files changed

Lines changed: 219 additions & 0 deletions

commitizen/cli.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,20 @@ def __call__(
197197
"help": "Show commit schema.",
198198
"func": commands.Schema,
199199
},
200+
{
201+
"name": "dump-config",
202+
"description": "Output the current commitizen configuration",
203+
"help": "Output the current commitizen configuration.",
204+
"func": commands.DumpConfig,
205+
"arguments": [
206+
{
207+
"name": ["--format", "-f"],
208+
"choices": ["toml", "yaml", "json"],
209+
"default": "toml",
210+
"help": "Output format (default: toml).",
211+
}
212+
],
213+
},
200214
{
201215
"name": "bump",
202216
"description": "Bump semantic version based on the git log",
@@ -660,6 +674,7 @@ class Args(argparse.Namespace):
660674
| commands.Example # example
661675
| commands.Info # info
662676
| commands.Schema # schema
677+
| commands.DumpConfig # dump-config
663678
| commands.Bump # bump
664679
| commands.Changelog # changelog (ch)
665680
| commands.Check # check

commitizen/commands/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from .changelog import Changelog
33
from .check import Check
44
from .commit import Commit
5+
from .dump_config import DumpConfig
56
from .example import Example
67
from .info import Info
78
from .init import Init
@@ -14,6 +15,7 @@
1415
"Changelog",
1516
"Check",
1617
"Commit",
18+
"DumpConfig",
1719
"Example",
1820
"Info",
1921
"Init",

commitizen/commands/dump_config.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from __future__ import annotations
2+
3+
import json
4+
from collections.abc import Mapping
5+
from typing import TYPE_CHECKING, Any
6+
7+
import tomlkit
8+
import yaml
9+
10+
from commitizen import out
11+
12+
if TYPE_CHECKING:
13+
from commitizen.config import BaseConfig
14+
15+
16+
class DumpConfig:
17+
"""Output the current commitizen configuration."""
18+
19+
def __init__(self, config: BaseConfig, arguments: dict[str, Any]) -> None:
20+
self.config: BaseConfig = config
21+
self.format: str = arguments.get("format", "toml")
22+
23+
def __call__(self) -> None:
24+
settings = dict(self.config.settings)
25+
26+
if self.format == "toml":
27+
filtered = _filter_none_values(settings)
28+
doc = tomlkit.document()
29+
tool_table = tomlkit.table()
30+
cz_table = tomlkit.table()
31+
for key, value in filtered.items():
32+
cz_table.add(key, value)
33+
tool_table.add("commitizen", cz_table)
34+
doc.add("tool", tool_table)
35+
out.write(tomlkit.dumps(doc))
36+
elif self.format == "yaml":
37+
out.write(
38+
yaml.safe_dump(
39+
{"tool": {"commitizen": settings}},
40+
default_flow_style=False,
41+
allow_unicode=True,
42+
)
43+
)
44+
else:
45+
out.write(
46+
json.dumps({"tool": {"commitizen": settings}}, indent=2, default=str)
47+
)
48+
49+
50+
def _filter_none_values(settings: Mapping[str, Any]) -> dict[str, Any]:
51+
return {
52+
key: _filter_none_values(value) if isinstance(value, Mapping) else value
53+
for key, value in settings.items()
54+
if value is not None
55+
}

docs/commands/dump_config.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# dump-config
2+
3+
Output the current commitizen configuration (defaults merged with any project overrides) so you can paste it directly into a configuration file as a starting point.
4+
5+
## Usage
6+
7+
```
8+
cz dump-config [--format {toml,yaml,json}]
9+
```
10+
11+
## Options
12+
13+
| Option | Description |
14+
|---|---|
15+
| `--format`, `-f` | Output format: `toml` (default), `yaml`, or `json` |
16+
17+
## Examples
18+
19+
Dump the current configuration as TOML (default):
20+
21+
```bash
22+
cz dump-config
23+
```
24+
25+
Dump as YAML:
26+
27+
```bash
28+
cz dump-config --format yaml
29+
```
30+
31+
Dump as JSON:
32+
33+
```bash
34+
cz dump-config --format json
35+
```
36+
37+
Use the TOML output as a starting point for `pyproject.toml`:
38+
39+
```bash
40+
cz dump-config >> pyproject.toml
41+
```

mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ nav:
4747
- info: "commands/info.md"
4848
- ls: "commands/ls.md"
4949
- schema: "commands/schema.md"
50+
- dump-config: "commands/dump_config.md"
5051
- version: "commands/version.md"
5152
- Configuration:
5253
- Configuration File: "config/configuration_file.md"
@@ -137,6 +138,7 @@ plugins:
137138
- commands/info.md: Show information about the active configuration.
138139
- commands/ls.md: List supported commit message choices for the active rules.
139140
- commands/schema.md: Show the commit message schema for the active convention.
141+
- commands/dump_config.md: Output the current commitizen configuration.
140142
- commands/version.md: Show the installed or project version.
141143
Configuration:
142144
- config/configuration_file.md: Where configuration can live and how it is loaded.

tests/commands/test_common_command.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"changelog",
1313
"check",
1414
"commit",
15+
"dump-config",
1516
"example",
1617
"info",
1718
"init",
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
usage: cz dump-config [-h] [--format {toml,yaml,json}]
2+
3+
Output the current commitizen configuration
4+
5+
options:
6+
-h, --help show this help message and exit
7+
--format, -f {toml,yaml,json}
8+
Output format (default: toml).
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
usage: cz dump-config [-h] [--format {toml,yaml,json}]
2+
3+
Output the current commitizen configuration
4+
5+
options:
6+
-h, --help show this help message and exit
7+
--format, -f {toml,yaml,json}
8+
Output format (default: toml).
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
usage: cz dump-config [-h] [--format {toml,yaml,json}]
2+
3+
Output the current commitizen configuration
4+
5+
options:
6+
-h, --help show this help message and exit
7+
--format, -f {toml,yaml,json}
8+
Output format (default: toml).
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
usage: cz dump-config [-h] [--format {toml,yaml,json}]
2+
3+
Output the current commitizen configuration
4+
5+
options:
6+
-h, --help show this help message and exit
7+
--format, -f {toml,yaml,json}
8+
Output format (default: toml).

0 commit comments

Comments
 (0)