Skip to content

Commit

Permalink
Merge pull request #79 from jwodder/hg-no-tag
Browse files Browse the repository at this point in the history
hg method: Include pattern (if any) in messages about tag-retrieval failure
  • Loading branch information
jwodder authored Mar 16, 2024
2 parents 9e78935 + 25f0b90 commit ccd10b2
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ v3.1.0 (in development)
- When `git describe` fails to retrieve a tag, the resulting log/error message
now includes all options passed to the command (based on contribution by
[@jenshnielsen](https://github.com/jenshnielsen))
- When `hg log` fails to retrieve a tag, the resulting log/error message now
includes the tag pattern passed to `latesttag()`, if any

v3.0.0 (2023-12-13)
-------------------
Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ v3.1.0 (in development)
- When :command:`git describe` fails to retrieve a tag, the resulting log/error
message now includes all options passed to the command (based on contribution
by `@jenshnielsen <https://github.com/jenshnielsen>`_)
- When :command:`hg log` fails to retrieve a tag, the resulting log/error
message now includes the tag pattern passed to ``latesttag()``, if any


v3.0.0 (2023-12-13)
Expand Down
12 changes: 10 additions & 2 deletions src/versioningit/hg.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,23 @@ def describe_hg(*, project_dir: str | Path, params: dict[str, Any]) -> VCSDescri
else:
dirty = False
if tag == "null":
if pattern is None:
suffix = ""
else:
suffix = f" (pattern = {pattern!r})"
# Unlike the Git methods, don't show the full `hg log` command run, as
# shlex.quote() on `--template` arguments returns something *ugly*.
if default_tag is not None:
log.info("No latest tag; falling back to default tag %r", default_tag)
log.info(
"No latest tag%s; falling back to default tag %r", suffix, default_tag
)
tag = default_tag
# Act as though the first commit is the one with the default tag,
# i.e., don't count it (unless there is no first commit, of course)
if distance > 0:
distance -= 1
else:
raise NoTagError("No latest tag in Mercurial repository")
raise NoTagError(f"No latest tag in Mercurial repository{suffix}")
if distance and dirty:
state = "distance-dirty"
elif distance:
Expand Down
4 changes: 4 additions & 0 deletions test/data/repos/errors/hg-no-tag.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "NoTagError",
"message": "No latest tag in Mercurial repository (pattern = 're:^v')"
}
Binary file added test/data/repos/errors/hg-no-tag.zip
Binary file not shown.
8 changes: 8 additions & 0 deletions test/data/repos/hg/default-tag-fallback.fields.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"build_date": "2038-01-19 03:14:07+00:00",
"distance": 3,
"rev": "5296453555c9",
"revision": "5296453555c92c22bab469752758a549e297c922",
"vcs": "h",
"vcs_name": "hg"
}
10 changes: 10 additions & 0 deletions test/data/repos/hg/default-tag-fallback.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"version": "0.0.0.post3+h5296453555c9",
"next_version": "0.1.0",
"logmsgs": [
{
"level": "INFO",
"message": "No latest tag (pattern = 're:^v'); falling back to default tag 'v0.0.0'"
}
]
}
Binary file added test/data/repos/hg/default-tag-fallback.zip
Binary file not shown.
6 changes: 6 additions & 0 deletions test/test_methods/test_hg.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ class HGFields(BaseModel):
("distance", {}, "v0.1.0", "distance"),
("distance-dirty", {}, "v0.1.0", "distance-dirty"),
("default-tag", {"default-tag": "v0.0.0"}, "v0.0.0", "distance"),
(
"default-tag-fallback",
{"default-tag": "v0.0.0", "pattern": "re:^v"},
"v0.0.0",
"distance",
),
("pattern", {"pattern": r"re:^v"}, "v0.1.0", "distance"),
],
)
Expand Down

0 comments on commit ccd10b2

Please sign in to comment.