Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(bump): it is now possible to specify a pattern in the files attr... #25

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
woile merged 2 commits into master from feature/files-with-regex
May 11, 2019
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
24 changes: 17 additions & 7 deletions commitizen/bump.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
35 changes: 19 additions & 16 deletions commitizen/commands/bump.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
Expand Down
58 changes: 30 additions & 28 deletions docs/bump.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

![Bump version](images/bump.gif)

## About
Expand All @@ -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]

Expand Down Expand Up @@ -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.

Expand All @@ -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"

Expand All @@ -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

Expand All @@ -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

Expand All @@ -162,7 +165,6 @@ Some examples
[commitizen]
bump_message = release $current_version → $new_version [skip-ci]


## Custom bump

Read the [customizing section](./customization.md).
Expand Down
26 changes: 14 additions & 12 deletions docs/config.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
# 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

| Variable | Type | Default | Description |
| -------- | ---- | ------- | ----------- |
| `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) |
30 changes: 29 additions & 1 deletion tests/test_bump_update_version_in_files.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

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