Skip to content

Commit 54e9325

Browse files
authored
Merge pull request #178 from minrk/monotonic-n-commits
don't reset commit count on each tag
2 parents 6ab246b + 805493d commit 54e9325

File tree

3 files changed

+43
-27
lines changed

3 files changed

+43
-27
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ order that could come from using chartpress.
5757
0.8.0-0.dev.git.4.hasdf123
5858
0.8.0-0.dev.git.10.hsdfg234
5959
0.9.0-beta.1
60-
0.9.0-beta.1.git.1.hdfgh345
61-
0.9.0-beta.1.git.5.hfghj456
60+
0.9.0-beta.1.git.12.hdfgh345
61+
0.9.0-beta.1.git.15.hfghj456
6262
0.9.0-beta.2
63-
0.9.0-beta.2.git.1.hghjk567
63+
0.9.0-beta.2.git.20.hghjk567
6464
0.9.0-beta.3
6565
0.9.0
6666
```

chartpress.py

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,17 @@ def _image_needs_building(image, platforms):
474474
def _get_identifier_from_paths(*paths, long=False, base_version=None):
475475
latest_commit = _get_latest_commit_tagged_or_modifying_paths(*paths, echo=False)
476476

477+
# always use monotonic counter since the beginning of the branch
478+
# avoids counter resets when using explicit base_version
479+
n_commits = int(
480+
_check_output(
481+
["git", "rev-list", "--count", latest_commit],
482+
echo=False,
483+
)
484+
.decode("utf-8")
485+
.strip()
486+
)
487+
477488
try:
478489
git_describe = (
479490
_check_output(
@@ -484,30 +495,29 @@ def _get_identifier_from_paths(*paths, long=False, base_version=None):
484495
.decode("utf8")
485496
.strip()
486497
)
487-
latest_tag_in_branch, n_commits, sha = git_describe.rsplit("-", maxsplit=2)
488-
n_commits = int(n_commits)
498+
latest_tag_in_branch, n_commits_since_tag, _g_sha = git_describe.rsplit(
499+
"-", maxsplit=2
500+
)
501+
n_commits_since_tag = int(n_commits_since_tag)
502+
if n_commits_since_tag == 0:
503+
# don't use baseVersion config for development versions
504+
# when we are exactly on a tag
505+
base_version = latest_tag_in_branch
506+
if not long:
507+
# set n_commits=0 to ensure exact tag is used without suffix
508+
# in _get_identifier_from_parts
509+
n_commits = 0
510+
489511
if base_version is None:
490512
base_version = latest_tag_in_branch
491-
# remove "g" prefix output by the git describe command
492-
# ref: https://git-scm.com/docs/git-describe#_examples
493-
sha = sha[1:]
494513
except subprocess.CalledProcessError:
495-
# no tags on branch, so assume 0.0.1 and
496-
# calculate n_commits from latest_commit
497-
n_commits = int(
498-
_check_output(
499-
["git", "rev-list", "--count", latest_commit],
500-
echo=False,
501-
)
502-
.decode("utf-8")
503-
.strip()
504-
)
505-
sha = latest_commit
514+
# no tags on branch
515+
pass
506516

507517
if base_version is None:
508-
base_version = "0.0.1"
518+
base_version = "0.0.1-0.dev"
509519

510-
return _get_identifier_from_parts(base_version, n_commits, sha, long)
520+
return _get_identifier_from_parts(base_version, n_commits, latest_commit, long)
511521

512522

513523
def _get_identifier_from_parts(tag, n_commits, commit, long):

tests/test_repo_interactions.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,17 @@ def test_chartpress_run(git_repo, capfd, use_chart_version):
109109
# already.
110110
assert "Updating" not in out
111111

112+
# Run again, but from a clean repo (versions in git don't match tag)
113+
# Should produce the same result
114+
git_repo.git.checkout(tag, "--", "testchart/values.yaml")
115+
out = _capture_output(["--skip-build"], capfd)
116+
assert f"Updating testchart/values.yaml: image: testchart/testimage:{tag}\n" in out
117+
112118
# verify usage of --long
113119
out = _capture_output(["--skip-build", "--long"], capfd)
114-
assert f"Updating testchart/Chart.yaml: version: {tag}.git.0.h{sha}" in out
120+
assert f"Updating testchart/Chart.yaml: version: {tag}.git.1.h{sha}" in out
115121
assert (
116-
f"Updating testchart/values.yaml: image: testchart/testimage:{tag}.git.0.h{sha}"
122+
f"Updating testchart/values.yaml: image: testchart/testimage:{tag}.git.1.h{sha}"
117123
in out
118124
)
119125

@@ -198,7 +204,7 @@ def test_chartpress_run(git_repo, capfd, use_chart_version):
198204
# verify output of --publish-chart
199205
assert "'gh-pages' set up to track" in out
200206
assert "Successfully packaged chart and saved it to:" in out
201-
assert f"/testchart-{tag}.git.1.h{sha}.tgz" in out
207+
assert f"/testchart-{tag}.git.2.h{sha}.tgz" in out
202208
assert "Skipping chart publishing" not in out
203209

204210
# checkout gh-pages
@@ -214,7 +220,7 @@ def test_chartpress_run(git_repo, capfd, use_chart_version):
214220
assert "version: 1.2.1" in index_yaml
215221
assert "version: 1.2.2" in index_yaml
216222
assert f"version: {tag}" in index_yaml
217-
assert f"version: {tag}.git.1.h{sha}" in index_yaml
223+
assert f"version: {tag}.git.2.h{sha}" in index_yaml
218224

219225
# return to main
220226
git_repo.git.checkout("main")
@@ -341,7 +347,7 @@ def test_dev_tag(git_repo_dev_tag, capfd):
341347
with open("testchart/Chart.yaml") as f:
342348
chart = yaml.load(f)
343349

344-
tag = f"2.0.0-dev.git.1.h{sha}"
350+
tag = f"2.0.0-dev.git.3.h{sha}"
345351
assert chart["version"] == tag
346352
check_version(tag)
347353

@@ -353,7 +359,7 @@ def test_backport_branch(git_repo_backport_branch, capfd):
353359
with open("testchart/Chart.yaml") as f:
354360
chart = yaml.load(f)
355361

356-
tag = f"1.0.1-{PRERELEASE_PREFIX}.1.h{sha}"
362+
tag = f"1.0.1-{PRERELEASE_PREFIX}.3.h{sha}"
357363
assert chart["version"] == tag
358364
check_version(tag)
359365

0 commit comments

Comments
 (0)