Debian Codegraph @

docs/config/version_provider.md

raw
1 # Version Providers
2
3 Version providers are the mechanism by which Commitizen reads and writes version information in your project.
4
5 They abstract away the details of where and how version numbers are stored, allowing Commitizen to work seamlessly with different project types and package management systems.
6
7 ## Overview
8
9 By default, Commitizen uses the `commitizen` provider, which stores the version in your Commitizen configuration file.
10 However, you can configure Commitizen to use any available provider that matches your project's setup.
11 This is particularly useful when you want Commitizen to manage versions in the same location as your package manager (e.g., `package.json` for Node.js projects, `pyproject.toml` for Python projects).
12
13 ## Built-in Providers
14
15 Commitizen includes several built-in version providers for common package management formats:
16
17 ### `commitizen` (Default)
18
19 The default version provider stores and retrieves the version from your Commitizen configuration file (e.g., `pyproject.toml`, `.cz.toml`, etc.).
20
21 **Use when:**
22
23 - You want to keep version management separate from your package manager
24 - Your project doesn't use a standard package manager
25 - You need maximum flexibility in version management
26
27 **Configuration:**
28
29 ```toml
30 [tool.commitizen]
31 version_provider = "commitizen"
32 version = "0.1.0" # Required when using this provider
33 ```
34
35 ### `scm`
36
37 Fetches the version from Git tags using `git describe`. This provider **only reads** version information and never writes it back to files. It's designed to work with tools like `setuptools-scm` or other package manager `*-scm` plugins that derive version numbers from Git history.
38
39 **Use when:**
40
41 - You're using `setuptools-scm` or similar tools
42 - You want version numbers derived from Git tags
43 - You don't want Commitizen to modify any files for version management
44
45 **Configuration:**
46
47 ```toml
48 [tool.commitizen]
49 version_provider = "scm"
50 # No version field needed - it's read from Git tags
51 ```
52
53 !!! note
54 The `scm` provider is read-only. When you run `cz bump`, it will create a Git tag but won't update any files. This is intentional and works well with tools that derive versions from Git tags.
55
56 ### `pep621`
57
58 Manages version in `pyproject.toml` under the `project.version` field, following [PEP 621](https://peps.python.org/pep-0621/) standards.
59
60 **Use when:**
61
62 - You're using a modern Python project with PEP 621-compliant `pyproject.toml`
63 - You want version management integrated with your Python project metadata
64
65 **Configuration:**
66
67 ```toml
68 [tool.commitizen]
69 version_provider = "pep621"
70 ```
71
72 **Example `pyproject.toml`:**
73
74 ```toml
75 [project]
76 name = "my-package"
77 version = "0.1.0" # Managed by Commitizen
78 ```
79
80 ### `poetry`
81
82 Manages version in `pyproject.toml` under the `tool.poetry.version` field, which is used by the [Poetry](https://python-poetry.org/) package manager. This approach is recommended only for users running Poetry versions earlier than 2.0 or relying on Poetry-specific features. For most users on Poetry 2.0 or later, it is recommended to use `pep621` instead. [Read More](https://python-poetry.org/docs/main/managing-dependencies/)
83
84 **Use when:**
85
86 - You're using Poetry < 2.0 as your Python package manager
87 - You're using Poetry >= 2.0 as your Python package manager, but don't need poetry-specific features
88 - You want Commitizen to manage the version that Poetry uses
89
90 **Configuration:**
91
92 ```toml
93 [tool.commitizen]
94 version_provider = "poetry"
95 ```
96
97 **Example `pyproject.toml`:**
98
99 ```toml
100 [tool.poetry]
101 name = "my-package"
102 version = "0.1.0" # Managed by Commitizen
103 ```
104
105 ### `uv`
106
107 Manages version in both `pyproject.toml` (`project.version`) and `uv.lock` (`package.version` for the matching package name). This ensures consistency between your project metadata and lock file.
108
109
110 !!! note
111 Even though uv follows PEP 621 format, `pep621` does not manage the version in `uv.lock`. `uv` is still suggested for uv users.
112
113 **Use when:**
114
115 - You're using `uv` as your Python package manager
116 - You want version synchronization between `pyproject.toml` and `uv.lock`
117
118 **Configuration:**
119
120 ```toml
121 [tool.commitizen]
122 version_provider = "uv"
123 ```
124
125 ### `cargo`
126
127 Manages version in both `Cargo.toml` (`package.version`) and `Cargo.lock` (`package.version` for the matching package name). This ensures consistency between your Rust project's manifest and lock file.
128
129 **Use when:**
130
131 - You're working with a Rust project using Cargo
132 - You want Commitizen to manage Rust package versions
133
134 **Configuration:**
135
136 ```toml
137 [tool.commitizen]
138 version_provider = "cargo"
139 ```
140
141 **Example `Cargo.toml`:**
142
143 ```toml
144 [package]
145 name = "my-crate"
146 version = "0.1.0" # Managed by Commitizen
147 ```
148
149 ### `npm`
150
151 Manages version in `package.json` and optionally synchronizes with `package-lock.json` and `npm-shrinkwrap.json` if they exist.
152
153 **Use when:**
154
155 - You're working with a Node.js/JavaScript project
156 - You want Commitizen to manage npm package versions
157
158 **Configuration:**
159
160 ```toml
161 [tool.commitizen]
162 version_provider = "npm"
163 ```
164
165 **Example `package.json`:**
166
167 ```json
168 {
169 "name": "my-package",
170 "version": "0.1.0"
171 }
172 ```
173
174 ### `composer`
175
176 Manages version in `composer.json` under the `version` field, used by PHP's Composer package manager.
177
178 **Use when:**
179
180 - You're working with a PHP project using Composer
181 - You want Commitizen to manage Composer package versions
182
183 **Configuration:**
184
185 ```toml
186 [tool.commitizen]
187 version_provider = "composer"
188 ```
189
190 **Example `composer.json`:**
191
192 ```json
193 {
194 "name": "vendor/package",
195 "version": "0.1.0"
196 }
197 ```
198
199 ## Provider Comparison Table
200
201 | Provider | File(s) Modified | Read-Only | Best For |
202 | ------------ | ----------------------------------- | --------- | --------------------------------- |
203 | `commitizen` | Commitizen config file | No | General use, flexible projects |
204 | `scm` | None (reads from Git tags) | Yes | `setuptools-scm` users |
205 | `pep621` | `pyproject.toml` (`project.version`) | No | Modern Python (PEP 621) |
206 | `poetry` | `pyproject.toml` (`tool.poetry.version`) | No | Poetry projects |
207 | `uv` | `pyproject.toml` + `uv.lock` | No | uv package manager |
208 | `cargo` | `Cargo.toml` + `Cargo.lock` | No | Rust/Cargo projects |
209 | `npm` | `package.json` (+ lock files) | No | Node.js/npm projects |
210 | `composer` | `composer.json` | No | PHP/Composer projects |
211
212 ## Creating Custom Version Providers
213
214 If none of the built-in providers meet your needs, you can create a custom version provider by extending the `VersionProvider` base class and registering it as a plugin.
215
216 ### Step 1: Create Your Provider Class
217
218 Create a Python file (e.g., `my_provider.py`) that extends `VersionProvider`:
219
220 ```python title="my_provider.py"
221 from pathlib import Path
222 from commitizen.providers import VersionProvider
223
224
225 class MyProvider(VersionProvider):
226 """
227 Custom version provider that reads/writes from a VERSION file.
228 """
229
230 def get_version(self) -> str:
231 """Read version from VERSION file."""
232 version_file = Path("VERSION")
233 if not version_file.is_file():
234 return "0.0.0"
235 return version_file.read_text().strip()
236
237 def set_version(self, version: str) -> None:
238 """Write version to VERSION file."""
239 version_file = Path("VERSION")
240 version_file.write_text(f"{version}\n")
241 ```
242
243 ### Step 2: Register as an Entry Point
244
245 Register your provider using the `commitizen.provider` entry point. You can do this in your `setup.py`, `setup.cfg`, or `pyproject.toml`:
246
247 **Using `pyproject.toml` (recommended):**
248
249 ```toml title="pyproject.toml"
250 [project]
251 name = "my-commitizen-provider"
252 version = "0.1.0"
253 dependencies = ["commitizen"]
254
255 [project.entry-points."commitizen.provider"]
256 my-provider = "my_provider:MyProvider"
257 ```
258
259 **Using `setup.py` (for legacy setup):**
260
261 ```python title="setup.py"
262 from setuptools import setup
263
264 setup(
265 name="my-commitizen-provider",
266 version="0.1.0",
267 py_modules=["my_provider"],
268 install_requires=["commitizen"],
269 entry_points={
270 "commitizen.provider": [
271 "my-provider = my_provider:MyProvider",
272 ]
273 },
274 )
275 ```
276
277 ### Step 3: Install and Use
278
279 1. Install your provider package:
280
281 - Once your custom Commitizen provider is packaged and published (for example, to PyPI), install it like any standard Python package:
282
283 ```bash
284 pip install my-commitizen-provider
285 ```
286
287 - If you want to use the provider directly from the current project source (during development), install it in editable mode ([See pip documentation](https://pip.pypa.io/en/stable/cli/pip_install/#cmdoption-e)):
288
289 ```bash
290 pip install -e .
291 ```
292
293 2. Configure Commitizen to use your provider:
294
295 ```toml
296 [tool.commitizen]
297 version_provider = "my-provider"
298 ```
299
300 ### Provider Implementation Guidelines
301
302 When creating a custom provider, keep these guidelines in mind:
303
304 - **`get_version()`** should return a string representing the current version. If no version is found, you can return `"0.0.0"` or raise an appropriate exception.
305 - **`set_version(version: str)`** should write the version to your chosen storage location. The version string will be properly formatted according to your `version_scheme` setting.
306 - The provider has access to `self.config`, which is a `BaseConfig` instance containing all Commitizen settings.
307 - For file-based providers, consider using the `FileProvider` or `JsonProvider`/`TomlProvider` base classes from `commitizen.providers.base_provider` to simplify implementation.
308
309 ### Example: JSON-based Provider
310
311 Here's a more complete example using the `JsonProvider` base class:
312
313 ```python title="json_version_provider.py"
314 from commitizen.providers.base_provider import JsonProvider
315
316
317 class JsonVersionProvider(JsonProvider):
318 """
319 Version provider that uses a custom version.json file.
320 """
321
322 filename = "version.json"
323
324 def get(self, document):
325 """Extract version from JSON document."""
326 return document["version"]
327
328 def set(self, document, version):
329 """Set version in JSON document."""
330 document["version"] = version
331 ```
332
333 This example leverages the `JsonProvider` base class, which handles file reading, writing, and JSON parsing automatically.
334
335 ## Choosing the Right Provider
336
337 Select a version provider based on your project's characteristics:
338
339 - **Python projects**
340 - **with `uv`**: Use `uv`
341 - **with `pyproject.toml` that follows PEP 621**: Use `pep621`
342 - **with Poetry**: Use `poetry`
343 - **setuptools-scm**: Use `scm`
344 - **Rust projects**: Use `cargo`
345 - **Node.js projects**: Use `npm`
346 - **PHP projects**: Use `composer`
347 - **Other cases or custom needs**: Use `commitizen` (default) or create a custom provider
348
349 Remember that you can always use `version_files` in combination with any provider to update additional files during version bumps, regardless of which provider you choose for reading/writing the primary version.