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

Commit aa89003

Browse files
committed
feat(bump): it is now possible to specify a pattern in the files attr to replace the version
1 parent bc54b9a commit aa89003

File tree

3 files changed

+65
-24
lines changed

3 files changed

+65
-24
lines changed

‎commitizen/bump.py‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,27 @@ def update_version_in_files(current_version: str, new_version: str, files: list)
119119
So for example, your tag could look like `v1.0.0` while your version in
120120
the package like `1.0.0`.
121121
"""
122-
for filepath in files:
123-
# Read in the file
124-
withopen(filepath, "r") asfile:
125-
filedata = file.read()
122+
for location in files:
123+
filepath, *regex=location.split(":", maxsplit=1)
124+
iflen(regex) >0:
125+
regex = regex[0]
126126

127-
# Replace the target string
128-
filedata = filedata.replace(current_version, new_version)
127+
# Read in the file
128+
filedata = []
129+
with open(filepath, "r") as f:
130+
for line in f:
131+
if regex:
132+
is_match = re.search(regex, line)
133+
if not is_match:
134+
filedata.append(line)
135+
continue
136+
137+
# Replace the target string
138+
filedata.append(line.replace(current_version, new_version))
129139

130140
# Write the file out again
131141
with open(filepath, "w") as file:
132-
file.write(filedata)
142+
file.write("".join(filedata))
133143

134144

135145
def create_tag(version: Union[Version, str], tag_format: Optional[str] = None):

‎commitizen/commands/bump.py‎

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,24 @@ def __init__(self, config: dict, arguments: dict):
3333
}
3434
self.cz = factory.commiter_factory(self.config)
3535

36+
def is_initial_tag(self, current_tag_version: str, is_yes: bool = False) -> bool:
37+
"""Check if reading the whole git tree up to HEAD is needed."""
38+
is_initial = False
39+
if not git.tag_exist(current_tag_version):
40+
if is_yes:
41+
is_initial = True
42+
else:
43+
out.info(f"Tag {current_tag_version} could not be found. ")
44+
out.info(
45+
(
46+
"Possible causes:\n"
47+
"- version in configuration is not the current version\n"
48+
"- tag_format is missing, check them using 'git tag --list'\n"
49+
)
50+
)
51+
is_initial = questionary.confirm("Is this the first tag created?").ask()
52+
return is_initial
53+
3654
def __call__(self):
3755
"""Steps executed to bump."""
3856
try:
@@ -56,23 +74,8 @@ def __call__(self):
5674
is_yes: bool = self.arguments["yes"]
5775
prerelease: str = self.arguments["prerelease"]
5876
increment: Optional[str] = self.arguments["increment"]
59-
is_initial: bool = False
60-
61-
# Check if reading the whole git tree up to HEAD is needed.
62-
if not git.tag_exist(current_tag_version):
63-
if is_yes:
64-
is_initial = True
65-
else:
66-
out.info(f"Tag {current_tag_version} could not be found. ")
67-
out.info(
68-
(
69-
"Possible causes:\n"
70-
"- version in configuration is not the current version\n"
71-
"- tag_format is missing, check them using 'git tag --list'\n"
72-
)
73-
)
74-
is_initial = questionary.confirm("Is this the first tag created?").ask()
7577

78+
is_initial = self.is_initial_tag(current_tag_version, is_yes)
7679
commits = git.get_commits(current_tag_version, from_beginning=is_initial)
7780

7881
# No commits, there is no need to create an empty tag.

‎tests/test_bump_update_version_in_files.py‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,21 @@
1515
__version__ = '1.2.3'
1616
"""
1717

18-
files_with_content = (("pyproject.toml", PYPROJECT), ("__version__.py", VERSION_PY))
18+
REPEATED_VERSION_NUMBER = """
19+
{
20+
"name": "magictool",
21+
"version": "1.2.3",
22+
"dependencies": {
23+
"lodash": "1.2.3"
24+
}
25+
}
26+
"""
27+
28+
files_with_content = (
29+
("pyproject.toml", PYPROJECT),
30+
("__version__.py", VERSION_PY),
31+
("package.json", REPEATED_VERSION_NUMBER),
32+
)
1933

2034

2135
@pytest.fixture
@@ -40,3 +54,17 @@ def test_update_version_in_files(create_files):
4054
with open(filepath, "r") as f:
4155
data = f.read()
4256
assert new_version in data
57+
58+
59+
def test_partial_update_of_file(create_files):
60+
old_version = "1.2.3"
61+
new_version = "2.0.0"
62+
filepath = "tests/package.json"
63+
regex = "version"
64+
location = f"{filepath}:{regex}"
65+
66+
bump.update_version_in_files(old_version, new_version, [location])
67+
with open(filepath, "r") as f:
68+
data = f.read()
69+
assert new_version in data
70+
assert old_version in data

0 commit comments

Comments
(0)

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