[feature] Check the consistency of commitizen version and versions in version_file
Currently, this checking behavior does not work when --dry-run is passed. The reason is that reading version in version and writing version to file is implemented in the same function (i.e. commitizen/bump.py::update_version_in_files)
TODO
- [ ] Separate the read step and write step in
commitizen/bump.py::update_version_in_files - [ ] fix the error that checking consistency is not performed when passing
--dry-run
Shall we close this one?
No, #186 only implements a portion of it.
Can u expand on what needs to be done for this ticket?
I just update the description. See if it's clear 🙂
I recently experienced this issue.
We use the --dry-run and --check-consistency flags on feature branches. On master, we use just --check-consistency.
Our expectation would be that any consistency issues would be raised on the feature branch. However, as noted above, the --check-consistency flag isn't valid when using --dry-run.
I have two proposals:
- Update the code to check-consistency when using --dry-run
- Raise a warning or error when both flags are passed
One option is to pass dry_run as a flag into https://github.com/commitizen-tools/commitizen/blob/cb798dc6400fba04df8c6c1d1b3ec96bfe9c13b0/commitizen/bump.py#L148
def update_version_in_files(
current_version: str, new_version: str, files: List[str], *, check_consistency=False, dry_run=False
) -> None:
"""Change old version to the new one in every file given.
Note that this version is not the tag formatted one.
So for example, your tag could look like `v1.0.0` while your version in
the package like `1.0.0`.
"""
# TODO: separate check step and write step
for location in files:
drive, tail = os.path.splitdrive(location)
path, _, regex = tail.partition(":")
filepath = drive + path
if not regex:
regex = _version_to_regex(current_version)
current_version_found, version_file = _bump_with_regex(
filepath, current_version, new_version, regex
)
if check_consistency and not current_version_found:
raise CurrentVersionNotFoundError(
f"Current version {current_version} is not found in {location}.\n"
"The version defined in commitizen configuration and the ones in "
"version_files are possibly inconsistent."
)
# Write the file out again
if not dry_run:
with smart_open(filepath, "w") as file:
file.write(version_file)
The DryRunExit() would also need to move https://github.com/commitizen-tools/commitizen/blob/cb798dc6400fba04df8c6c1d1b3ec96bfe9c13b0/commitizen/commands/bump.py#L247