From 1e8a231777ac471df9096a424552ae42abc1d45a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Santiago=20Fraire=20Willemo=C3=ABs?= Date: 2019年5月10日 21:27:59 +0200 Subject: [PATCH 1/2] feat(bump): it is now possible to specify a pattern in the files attr to replace the version --- commitizen/bump.py | 24 ++++++++++----- commitizen/commands/bump.py | 35 ++++++++++++---------- tests/test_bump_update_version_in_files.py | 30 ++++++++++++++++++- 3 files changed, 65 insertions(+), 24 deletions(-) diff --git a/commitizen/bump.py b/commitizen/bump.py index 6f33e33326..a6718c6ef6 100644 --- a/commitizen/bump.py +++ b/commitizen/bump.py @@ -119,17 +119,27 @@ def update_version_in_files(current_version: str, new_version: str, files: list) So for example, your tag could look like `v1.0.0` while your version in the package like `1.0.0`. """ - for filepath in files: - # Read in the file - with open(filepath, "r") as file: - filedata = file.read() + for location in files: + filepath, *regex = location.split(":", maxsplit=1) + if len(regex)> 0: + regex = regex[0] - # Replace the target string - filedata = filedata.replace(current_version, new_version) + # Read in the file + filedata = [] + with open(filepath, "r") as f: + for line in f: + if regex: + is_match = re.search(regex, line) + if not is_match: + filedata.append(line) + continue + + # Replace the target string + filedata.append(line.replace(current_version, new_version)) # Write the file out again with open(filepath, "w") as file: - file.write(filedata) + file.write("".join(filedata)) def create_tag(version: Union[Version, str], tag_format: Optional[str] = None): diff --git a/commitizen/commands/bump.py b/commitizen/commands/bump.py index 16405da7e8..a14776abf0 100644 --- a/commitizen/commands/bump.py +++ b/commitizen/commands/bump.py @@ -33,6 +33,24 @@ def __init__(self, config: dict, arguments: dict): } self.cz = factory.commiter_factory(self.config) + def is_initial_tag(self, current_tag_version: str, is_yes: bool = False) -> bool: + """Check if reading the whole git tree up to HEAD is needed.""" + is_initial = False + if not git.tag_exist(current_tag_version): + if is_yes: + is_initial = True + else: + out.info(f"Tag {current_tag_version} could not be found. ") + out.info( + ( + "Possible causes:\n" + "- version in configuration is not the current version\n" + "- tag_format is missing, check them using 'git tag --list'\n" + ) + ) + is_initial = questionary.confirm("Is this the first tag created?").ask() + return is_initial + def __call__(self): """Steps executed to bump.""" try: @@ -56,23 +74,8 @@ def __call__(self): is_yes: bool = self.arguments["yes"] prerelease: str = self.arguments["prerelease"] increment: Optional[str] = self.arguments["increment"] - is_initial: bool = False - - # Check if reading the whole git tree up to HEAD is needed. - if not git.tag_exist(current_tag_version): - if is_yes: - is_initial = True - else: - out.info(f"Tag {current_tag_version} could not be found. ") - out.info( - ( - "Possible causes:\n" - "- version in configuration is not the current version\n" - "- tag_format is missing, check them using 'git tag --list'\n" - ) - ) - is_initial = questionary.confirm("Is this the first tag created?").ask() + is_initial = self.is_initial_tag(current_tag_version, is_yes) commits = git.get_commits(current_tag_version, from_beginning=is_initial) # No commits, there is no need to create an empty tag. diff --git a/tests/test_bump_update_version_in_files.py b/tests/test_bump_update_version_in_files.py index 5548b0da48..941f3cf198 100644 --- a/tests/test_bump_update_version_in_files.py +++ b/tests/test_bump_update_version_in_files.py @@ -15,7 +15,21 @@ __version__ = '1.2.3' """ -files_with_content = (("pyproject.toml", PYPROJECT), ("__version__.py", VERSION_PY)) +REPEATED_VERSION_NUMBER = """ +{ + "name": "magictool", + "version": "1.2.3", + "dependencies": { + "lodash": "1.2.3" + } +} +""" + +files_with_content = ( + ("pyproject.toml", PYPROJECT), + ("__version__.py", VERSION_PY), + ("package.json", REPEATED_VERSION_NUMBER), +) @pytest.fixture @@ -40,3 +54,17 @@ def test_update_version_in_files(create_files): with open(filepath, "r") as f: data = f.read() assert new_version in data + + +def test_partial_update_of_file(create_files): + old_version = "1.2.3" + new_version = "2.0.0" + filepath = "tests/package.json" + regex = "version" + location = f"{filepath}:{regex}" + + bump.update_version_in_files(old_version, new_version, [location]) + with open(filepath, "r") as f: + data = f.read() + assert new_version in data + assert old_version in data From 18164eb44bfb55c651efa69681d700d135333f0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Santiago=20Fraire=20Willemo=C3=ABs?= Date: 2019年5月11日 10:37:44 +0200 Subject: [PATCH 2/2] docs: add info about extra pattern in the files when bumping --- docs/bump.md | 58 ++++++++++++++++++++++++++------------------------ docs/config.md | 26 +++++++++++----------- 2 files changed, 44 insertions(+), 40 deletions(-) diff --git a/docs/bump.md b/docs/bump.md index 9ae347115a..6a0a4a542a 100644 --- a/docs/bump.md +++ b/docs/bump.md @@ -1,4 +1,3 @@ - ![Bump version](images/bump.gif) ## About @@ -15,11 +14,11 @@ The version format follows [semantic versioning][semver]. This means `MAJOR.MINOR.PATCH` -| Increment | Description | Conventional commit map | -| ------- | ----- | ------ | -| `MAJOR` | Breaking changes introduced | `BREAKING CHANGE` | -| `MINOR` | New features | `feat` | -| `PATCH` | Fixes | `fix` + everything else | +| Increment | Description | Conventional commit map | +| --------- | --------------------------- | ----------------------- | +| `MAJOR` | Breaking changes introduced | `BREAKING CHANGE` | +| `MINOR` | New features | `feat` | +| `PATCH` | Fixes | `fix` + everything else | Prereleases are supported following python's [PEP 0440][pep440] @@ -71,10 +70,9 @@ optional arguments: manually specify the desired increment ``` - ## Configuration -`tag_format` +### `tag_format` Used to read the format from the git tags, and also to generate the tags. @@ -90,7 +88,6 @@ cz bump --tag_format="v$minor.$major.$path$prerelease" In your `pyproject.toml` - [tool.commitizen] tag_format = "v$minor.$major.$path$prerelease" @@ -103,20 +100,23 @@ The variables must be preceded by a `$` sign. Suppported variables: -| Variable | Description | -| --- | ----------- | -| `$version` | full generated version | -| `$major` | MAJOR increment | -| `$minor` | MINOR increment | -| `$patch` | PATCH increment | +| Variable | Description | +| ------------- | ------------------------------------------ | +| `$version` | full generated version | +| `$major` | MAJOR increment | +| `$minor` | MINOR increment | +| `$patch` | PATCH increment | | `$prerelease` | Prerelase (alpha, beta, release candidate) | -`files` +--- + +### `files` Used to identify the files which should be updated with the new version. -In your `pyproject.toml` +It is also possible to provide a pattern for each file, separated by colons (`:`). -Commitizen will update it's configuration (`pyproject.toml`, `.cz`) when bumping. +Commitizen will update it's configuration file automatically (`pyproject.toml`, `.cz`) when bumping, +regarding if the file is present or not in `files`. Some examples @@ -125,30 +125,33 @@ Some examples [tool.commitizen] files = [ "src/__version__.py", - "setup.py" + "setup.py:version" ] - `.cz` [commitizen] files = [ "src/__version__.py", - "setup.py" + "setup.py:version" ] -`bump_message` +In the example above, we can see the reference `"setup.py:version"`. +This means that it will find a file `setup.py` and will only make a change +in a line containing the `version` substring. -Template used to specify the commit message generated when bumping +--- -defaults to: `bump: version $current_version → $new_version` +### `bump_message` +Template used to specify the commit message generated when bumping +defaults to: `bump: version $current_version → $new_version` -| Variable | Description | -| --- | ----------- | +| Variable | Description | +| ------------------ | ----------------------------------- | | `$current_version` | the version existing before bumping | -| `$new_version` | version generated after bumping | +| `$new_version` | version generated after bumping | Some examples @@ -162,7 +165,6 @@ Some examples [commitizen] bump_message = release $current_version → $new_version [skip-ci] - ## Custom bump Read the [customizing section](./customization.md). diff --git a/docs/config.md b/docs/config.md index 0f3b03513b..06def64c25 100644 --- a/docs/config.md +++ b/docs/config.md @@ -1,33 +1,35 @@ # Configuration -**New!** Support for `pyproject.toml` +Commitizen has support for `toml` and `ini` files. -Add an entry to `pyproject.toml`. +## pyproject.toml +Add an entry to `pyproject.toml`. Recommended for **python** projects. [tool.commitizen] name = "cz_conventional_commits" version = "0.1.0" files = [ "src/__version__.py", - "pyproject.toml" + "pyproject.toml:version" ] +## INI files -Also, you can create in your project folder a file called `.cz`, -`.cz.cfg` or in your `setup.cfg` or if you want to configure the global -default in your user's home folder a `.cz` file with the following -information: +Supported files: `.cz`, `.cz.cfg`, `setup.py`, and `$HOME/.cz` + +The format is slightly different to the `toml`, so pay attention. +Recommended for **other languages** projects (js, go, etc). [commitizen] name = cz_conventional_commits version = 0.1.0 files = [ "src/__version__.py", - "pyproject.toml" + "pyproject.toml:version" ] -The extra tab at the end (`]`) is required. +The extra tab before the square brakets (`]`) at the end is required. ## Settings @@ -35,6 +37,6 @@ The extra tab at the end (`]`) is required. | -------- | ---- | ------- | ----------- | | `name` | `str` | `"cz_conventional_commits"` | Name of the commiting rules to use | | `version` | `str` | `None` | Current version. Example: "0.1.2" | -| `files` | `list` | `[ ]` | Files were the version needs to be updated | -| `tag_format` | `str` | `None` | Format for the git tag, useful for old projects, that use a convention like `"v1.2.1"`. [See more](/bump#configuration) | -| `bump_message` | `str` | `None` | Create custom commit message, useful to skip ci. [See more](/bump#configuration) | +| `files` | `list` | `[ ]` | Files were the version will be updated. A pattern to match a line, can also be specified, separated by `:` [See more](https://woile.github.io/commitizen/bump#files) | +| `tag_format` | `str` | `None` | Format for the git tag, useful for old projects, that use a convention like `"v1.2.1"`. [See more](https://woile.github.io/commitizen/bump#tag_format) | +| `bump_message` | `str` | `None` | Create custom commit message, useful to skip ci. [See more](https://woile.github.io/commitizen/bump#bump_message) |

AltStyle によって変換されたページ (->オリジナル) /