Skip to content
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

fix(bump)!: only add version files and staged changes #1261

Open
wants to merge 4 commits into
base: v5
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion commitizen/commands/bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ def __call__(self) -> None: # noqa: C901
)

provider.set_version(str(new_version))
files.extend(provider.get_files())

if self.pre_bump_hooks:
hooks.run(
Expand Down Expand Up @@ -420,7 +421,7 @@ def __call__(self) -> None: # noqa: C901
out.success("Done!")

def _get_commit_args(self) -> str:
commit_args = ["-a"]
commit_args = []
if self.no_verify:
commit_args.append("--no-verify")
return " ".join(commit_args)
12 changes: 12 additions & 0 deletions commitizen/providers/base_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ def set_version(self, version: str):
Set the new current version
"""

@abstractmethod
def get_files(self) -> list[str]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's make it a property

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe renamed as version_files would be better?

"""
Get the version files
"""


class FileProvider(VersionProvider):
"""
Expand Down Expand Up @@ -66,6 +72,9 @@ def set_version(self, version: str):
def get(self, document: dict[str, Any]) -> str:
return document["version"] # type: ignore

def get_files(self) -> list[str]:
return [str(self.file)]

def set(self, document: dict[str, Any], version: str):
document["version"] = version

Expand All @@ -87,5 +96,8 @@ def set_version(self, version: str):
def get(self, document: tomlkit.TOMLDocument) -> str:
return document["project"]["version"] # type: ignore

def get_files(self) -> list[str]:
return [str(self.file)]

def set(self, document: tomlkit.TOMLDocument, version: str):
document["project"]["version"] = version # type: ignore
3 changes: 3 additions & 0 deletions commitizen/providers/commitizen_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ def get_version(self) -> str:

def set_version(self, version: str):
self.config.set_key("version", version)

def get_files(self) -> list[str]:
return [str(self.config._path)]
9 changes: 9 additions & 0 deletions commitizen/providers/npm_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ def set_version(self, version: str) -> None:
json.dumps(shrinkwrap_document, indent=self.indent) + "\n"
)

def get_files(self) -> list[str]:
files = []
files.append(str(self.package_file))
if self.lock_file.exists():
files.append(str(self.lock_file))
if self.shrinkwrap_file.exists():
files.append(str(self.shrinkwrap_file))
return files

def get_package_version(self, document: dict[str, Any]) -> str:
return document["version"] # type: ignore

Expand Down
4 changes: 4 additions & 0 deletions commitizen/providers/scm_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@ def get_version(self) -> str:
def set_version(self, version: str):
# Not necessary
pass

def get_files(self) -> list[str]:
# No files for this provider
return []
3 changes: 3 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,9 @@ class MyProvider(VersionProvider):
def set_version(self, version: str):
self.file.write_text(version)

def get_files(self) -> list[str]:
return [str(file)]

```

```python title="setup.py"
Expand Down
12 changes: 12 additions & 0 deletions tests/commands/test_bump_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,18 @@ def test_bump_major_version_zero_when_major_is_not_zero(mocker, tmp_commitizen_p
assert expected_error_message in str(excinfo.value)


def test_bump_not_add_unstaged(mocker: MockFixture, tmp_commitizen_project):
unstaged_file = "ui.py"
create_file_and_commit("feat: new file", unstaged_file)
tmp_file = tmp_commitizen_project.join(unstaged_file)
tmp_file.write("updated content")
testargs = ["cz", "bump", "--yes"]
mocker.patch.object(sys, "argv", testargs)
cli.main()
cmd_res = cmd.run("git diff --name-only")
assert unstaged_file in cmd_res.out


def test_bump_files_only(mocker: MockFixture, tmp_commitizen_project):
tmp_version_file = tmp_commitizen_project.join("__version__.py")
tmp_version_file.write("0.1.0")
Expand Down
3 changes: 3 additions & 0 deletions tests/providers/test_cargo_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ def test_cargo_provider(

provider.set_version("42.1")
assert file.read_text() == dedent(expected)

files = provider.get_files()
assert filename in files
3 changes: 3 additions & 0 deletions tests/providers/test_composer_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,6 @@ def test_composer_provider(

provider.set_version("42.1")
assert file.read_text() == dedent(expected)

files = provider.get_files()
assert filename in files
7 changes: 7 additions & 0 deletions tests/providers/test_npm_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,10 @@ def test_npm_provider(
assert pkg_lock.read_text() == dedent(pkg_lock_expected)
if pkg_shrinkwrap_content:
assert pkg_shrinkwrap.read_text() == dedent(pkg_shrinkwrap_expected)

files = provider.get_files()
assert NpmProvider.package_filename in files
if pkg_lock_content:
assert NpmProvider.lock_filename in files
if pkg_shrinkwrap_content:
assert NpmProvider.shrinkwrap_filename in files
3 changes: 3 additions & 0 deletions tests/providers/test_pep621_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ def test_cargo_provider(

provider.set_version("42.1")
assert file.read_text() == dedent(expected)

files = provider.get_files()
assert filename in files
3 changes: 3 additions & 0 deletions tests/providers/test_poetry_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ def test_cargo_provider(

provider.set_version("42.1")
assert file.read_text() == dedent(expected)

files = provider.get_files()
assert filename in files
4 changes: 4 additions & 0 deletions tests/providers/test_scm_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ def test_scm_provider(
# Should not fail on set_version()
provider.set_version("43.1")

# Should not have any files
files = provider.get_files()
assert files == []


@pytest.mark.usefixtures("tmp_git_project")
def test_scm_provider_default_without_commits_and_tags(config: BaseConfig):
Expand Down
Loading