Skip to content

Commit 630ff4e

Browse files
committed
add --no-git-tag
1 parent 8cb0198 commit 630ff4e

4 files changed

Lines changed: 58 additions & 14 deletions

File tree

src/ctl/plugins/git.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,25 @@ def execute(self, **kwargs):
204204

205205
fn(**kwargs)
206206

207+
def find_git_root(self, start_path=None):
208+
"""
209+
Find git root by walking up from start_path or checkout_path.
210+
211+
**Arguments**
212+
213+
- start_path (`str`): path to start searching from (default: checkout_path)
214+
215+
**Returns**
216+
217+
git root path (`str`) or `None` if not found
218+
"""
219+
path = os.path.abspath(start_path or self.checkout_path)
220+
while path != "/":
221+
if os.path.exists(os.path.join(path, ".git")):
222+
return path
223+
path = os.path.dirname(path)
224+
return None
225+
207226
def command(self, *command):
208227
"""
209228
Prepare git command to use with `run_git_command`
@@ -218,12 +237,15 @@ def command(self, *command):
218237
219238
prepared command (`list`)
220239
"""
240+
git_root = self.find_git_root()
241+
if git_root is None:
242+
git_root = self.checkout_path # fallback
221243
return [
222244
"git",
223245
"--git-dir",
224-
os.path.join(self.checkout_path, ".git"),
246+
os.path.join(git_root, ".git"),
225247
"--work-tree",
226-
self.checkout_path,
248+
git_root,
227249
] + list(command)
228250

229251
def run_git_command(self, command):

src/ctl/plugins/semver2.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ def add_arguments(cls, parser, plugin_config, confu_cli_args):
4444
type=str,
4545
help="tag a prerelease with the specified prerlease name",
4646
)
47+
op_tag_parser.add_argument(
48+
"--no-git-tag",
49+
dest="no_git_tag",
50+
action="store_true",
51+
help="Skip creating git tag (still commits version file changes)",
52+
default=False,
53+
)
4754

4855
# operation `bump`
4956
op_bump_parser = parsers.get("op_bump_parser")
@@ -174,9 +181,7 @@ def bump(self, version, repo, **kwargs):
174181

175182
self.log.info(f"Bumping semantic version: {current} to {new_version}")
176183

177-
no_git_tag = kwargs.pop("no_git_tag", False)
178-
if not no_git_tag:
179-
self.tag(version=str(new_version), repo=repo, **kwargs)
184+
self.tag(version=str(new_version), repo=repo, **kwargs)
180185

181186
@expose("ctl.{plugin_name}.release")
182187
def release(self, repo, **kwargs):

src/ctl/plugins/version_base.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,15 +228,31 @@ def update_ctl_version(self, repo_plugin, version):
228228
def update_pyproject_version(self, repo_plugin, version):
229229
"""
230230
Writes a new version to the pyproject.toml file
231-
if it exists
231+
if it exists. Supports both Poetry format ([tool.poetry].version)
232+
and PEP 621 format ([project].version).
232233
"""
233234

234235
try:
235236
pyproject_path = os.path.join(repo_plugin.checkout_path, "pyproject.toml")
236237
pyproject = munge.load_datafile(
237238
"pyproject.toml", search_path=(repo_plugin.checkout_path)
238239
)
239-
pyproject["tool"]["poetry"]["version"] = version
240+
241+
updated = False
242+
243+
# Check for Poetry format: [tool.poetry].version
244+
if "tool" in pyproject and "poetry" in pyproject["tool"]:
245+
if "version" in pyproject["tool"]["poetry"]:
246+
pyproject["tool"]["poetry"]["version"] = version
247+
updated = True
248+
249+
# Check for PEP 621 format: [project].version
250+
if "project" in pyproject and "version" in pyproject["project"]:
251+
pyproject["project"]["version"] = version
252+
updated = True
253+
254+
if not updated:
255+
return None
240256

241257
codec = munge.get_codec("toml")
242258

@@ -246,7 +262,8 @@ def update_pyproject_version(self, repo_plugin, version):
246262

247263
except OSError as exc:
248264
if "not found" in str(exc):
249-
return
265+
return None
266+
raise
250267

251268
def validate_changelog(self, repo, version, data_file="CHANGELOG.yaml"):
252269
"""

tests/test_plugin_semver2.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ def test_bump(tmpdir, ctlr):
8787
plugin.bump(version="invalid", repo="dummy_repo")
8888

8989
plugin.bump(version="patch", repo="dummy_repo", no_git_tag=True)
90-
assert dummy_repo.version == "2.0.0"
91-
assert not dummy_repo.has_tag("2.0.1")
90+
assert dummy_repo.version == "2.0.1" # version IS bumped
91+
assert not dummy_repo.has_tag("2.0.1") # but no tag is created
9292

9393

9494
def test_bump_w_prerelease_flag(tmpdir, ctlr):
@@ -100,8 +100,8 @@ def test_bump_w_prerelease_flag(tmpdir, ctlr):
100100
assert dummy_repo.has_tag("1.0.1-rc.1")
101101

102102
plugin.bump(version="patch", repo="dummy_repo", prerelease="beta", no_git_tag=True)
103-
assert dummy_repo.version == "1.0.1-rc.1"
104-
assert not dummy_repo.has_tag("1.0.2-beta.1")
103+
assert dummy_repo.version == "1.0.2-beta.1" # version IS bumped
104+
assert not dummy_repo.has_tag("1.0.2-beta.1") # but no tag is created
105105

106106

107107
def test_bump_prerelease_version(tmpdir, ctlr):
@@ -116,8 +116,8 @@ def test_bump_prerelease_version(tmpdir, ctlr):
116116
assert dummy_repo.has_tag("1.0.0-rc.3")
117117

118118
plugin.bump(version="prerelease", repo="dummy_repo", no_git_tag=True)
119-
assert dummy_repo.version == "1.0.0-rc.3"
120-
assert not dummy_repo.has_tag("1.0.0-rc.4")
119+
assert dummy_repo.version == "1.0.0-rc.4" # version IS bumped
120+
assert not dummy_repo.has_tag("1.0.0-rc.4") # but no tag is created
121121

122122

123123
def test_release(tmpdir, ctlr):

0 commit comments

Comments
 (0)