Skip to content

feat(bump): add support to bumping local version #303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions commitizen/bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ def semver_generator(current_version: str, increment: str = None) -> str:


def generate_version(
current_version: str, increment: str, prerelease: Optional[str] = None
current_version: str,
increment: str,
prerelease: Optional[str] = None,
is_local_version: bool = False,
) -> Version:
"""Based on the given increment a proper semver will be generated.

Expand All @@ -121,11 +124,19 @@ def generate_version(
MINOR 1.0.0 -> 1.1.0
MAJOR 1.0.0 -> 2.0.0
"""
pre_version = prerelease_generator(current_version, prerelease=prerelease)
semver = semver_generator(current_version, increment=increment)
# TODO: post version
# TODO: dev version
return Version(f"{semver}{pre_version}")
if is_local_version:
version = Version(current_version)
pre_version = prerelease_generator(str(version.local), prerelease=prerelease)
semver = semver_generator(str(version.local), increment=increment)

return Version(f"{version.public}+{semver}{pre_version}")
else:
pre_version = prerelease_generator(current_version, prerelease=prerelease)
semver = semver_generator(current_version, increment=increment)

# TODO: post version
# TODO: dev version
return Version(f"{semver}{pre_version}")


def update_version_in_files(
Expand Down Expand Up @@ -188,7 +199,7 @@ def create_tag(version: Union[Version, str], tag_format: Optional[str] = None):
version = Version(version)

if not tag_format:
return version.public
return str(version)

major, minor, patch = version.release
prerelease = ""
Expand Down
5 changes: 5 additions & 0 deletions commitizen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@
"action": "store_true",
"help": "bump version in the files from the config",
},
{
"name": "--local-version",
"action": "store_true",
"help": "bump only the local version portion",
},
{
"name": ["--changelog", "-ch"],
"action": "store_true",
Expand Down
10 changes: 7 additions & 3 deletions commitizen/commands/bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def __call__(self): # noqa: C901
increment: Optional[str] = self.arguments["increment"]
prerelease: str = self.arguments["prerelease"]
is_files_only: Optional[bool] = self.arguments["files_only"]
is_local_version: Optional[bool] = self.arguments["local_version"]

current_tag_version: str = bump.create_tag(
current_version, tag_format=tag_format
Expand All @@ -117,7 +118,10 @@ def __call__(self): # noqa: C901
increment = None

new_version = bump.generate_version(
current_version, increment, prerelease=prerelease
current_version,
increment,
prerelease=prerelease,
is_local_version=is_local_version,
)
new_tag_version = bump.create_tag(new_version, tag_format=tag_format)
message = bump.create_commit_message(
Expand All @@ -140,7 +144,7 @@ def __call__(self): # noqa: C901

bump.update_version_in_files(
current_version,
new_version.public,
str(new_version),
version_files,
check_consistency=self.check_consistency,
)
Expand All @@ -159,7 +163,7 @@ def __call__(self): # noqa: C901
changelog_cmd()
c = cmd.run(f"git add {changelog_cmd.file_name}")

self.config.set_key("version", new_version.public)
self.config.set_key("version", str(new_version))
c = git.commit(message, args=self._get_commit_args())
if c.return_code != 0:
raise BumpCommitFailedError(f'git.commit error: "{c.err.strip()}"')
Expand Down
21 changes: 20 additions & 1 deletion docs/bump.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Some examples:

```bash
$ cz bump --help
usage: cz bump [-h] [--dry-run] [--files-only] [--changelog] [--no-verify]
usage: cz bump [-h] [--dry-run] [--files-only] [--changelog] [--no-verify] [--local-version]
[--yes] [--tag-format TAG_FORMAT] [--bump-message BUMP_MESSAGE]
[--prerelease {alpha,beta,rc}]
[--increment {MAJOR,MINOR,PATCH}] [--check-consistency]
Expand All @@ -67,6 +67,7 @@ optional arguments:
--no-verify this option bypasses the pre-commit and commit-msg
hooks
--yes accept automatically questions done
--local-version bump the local portion of the version
--tag-format TAG_FORMAT
the format used to tag the commit and read it, use it
in existing projects, wrap around simple quotes
Expand Down Expand Up @@ -131,6 +132,24 @@ However, it will still update `pyproject.toml` and `src/__version__.py`.
To fix it, you'll first `git checkout .` to reset to the status before trying to bump and update
the version in `setup.py` to `1.21.0`


### `--local-version`

Bump the local portion of the version.

```bash
cz bump --local-version
```

For example, if we have `pyproject.toml`

```toml
[tool.commitizen]
version = "5.3.5+0.1.0"
```

If `--local-version` is used, it will bump only the local version `0.1.0` and keep the public version `5.3.5` intact, bumping to the version `5.3.5+0.2.0`.

## Configuration

### `tag_format`
Expand Down
21 changes: 21 additions & 0 deletions tests/commands/test_bump_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,27 @@ def test_bump_files_only(mocker, tmp_commitizen_project):
assert "0.3.0" in f.read()


def test_bump_local_version(mocker, tmp_commitizen_project):
tmp_version_file = tmp_commitizen_project.join("__version__.py")
tmp_version_file.write("4.5.1+0.1.0")
tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml")
tmp_commitizen_cfg_file.write(
f"[tool.commitizen]\n"
'version="4.5.1+0.1.0"\n'
f'version_files = ["{str(tmp_version_file)}"]'
)

create_file_and_commit("feat: new user interface")
testargs = ["cz", "bump", "--yes", "--local-version"]
mocker.patch.object(sys, "argv", testargs)
cli.main()
tag_exists = git.tag_exist("4.5.1+0.2.0")
assert tag_exists is True

with open(tmp_version_file, "r") as f:
assert "4.5.1+0.2.0" in f.read()


def test_bump_dry_run(mocker, capsys, tmp_commitizen_project):
create_file_and_commit("feat: new file")

Expand Down
3 changes: 3 additions & 0 deletions tests/test_bump_create_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
(("1.2.3", "ver$major.$minor.$patch"), "ver1.2.3"),
(("1.2.3a0", "ver$major.$minor.$patch.$prerelease"), "ver1.2.3.a0"),
(("1.2.3rc2", "$major.$minor.$patch.$prerelease-majestic"), "1.2.3.rc2-majestic"),
(("1.2.3+1.0.0", "v$version"), "v1.2.3+1.0.0"),
(("1.2.3+1.0.0", "v$version-local"), "v1.2.3+1.0.0-local"),
(("1.2.3+1.0.0", "ver$major.$minor.$patch"), "ver1.2.3"),
]


Expand Down
22 changes: 22 additions & 0 deletions tests/test_bump_find_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
(("1.2.1", "MAJOR", None), "2.0.0"),
]

local_versions = [
(("4.5.0+0.1.0", "PATCH", None), "4.5.0+0.1.1"),
(("4.5.0+0.1.1", "MINOR", None), "4.5.0+0.2.0"),
(("4.5.0+0.2.0", "MAJOR", None), "4.5.0+1.0.0"),
]

# this cases should be handled gracefully
unexpected_cases = [
(("0.1.1rc0", None, "alpha"), "0.1.1a0"),
Expand Down Expand Up @@ -73,3 +79,19 @@ def test_generate_version(test_input, expected):
assert generate_version(
current_version, increment=increment, prerelease=prerelease
) == Version(expected)


@pytest.mark.parametrize(
"test_input,expected", itertools.chain(local_versions),
)
def test_generate_version_local(test_input, expected):
current_version = test_input[0]
increment = test_input[1]
prerelease = test_input[2]
is_local_version = True
assert generate_version(
current_version,
increment=increment,
prerelease=prerelease,
is_local_version=is_local_version,
) == Version(expected)