diff --git a/CHANGELOG.md b/CHANGELOG.md index fe2d3f2..170757a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) ------------------- diff --git a/docs/changelog.rst b/docs/changelog.rst index 77b0714..3ec9b7f 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -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 `_) +- 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) diff --git a/src/versioningit/hg.py b/src/versioningit/hg.py index 538e8df..1360670 100644 --- a/src/versioningit/hg.py +++ b/src/versioningit/hg.py @@ -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: diff --git a/test/data/repos/errors/hg-no-tag.json b/test/data/repos/errors/hg-no-tag.json new file mode 100644 index 0000000..f70dcfb --- /dev/null +++ b/test/data/repos/errors/hg-no-tag.json @@ -0,0 +1,4 @@ +{ + "type": "NoTagError", + "message": "No latest tag in Mercurial repository (pattern = 're:^v')" +} diff --git a/test/data/repos/errors/hg-no-tag.zip b/test/data/repos/errors/hg-no-tag.zip new file mode 100644 index 0000000..dbe99e1 Binary files /dev/null and b/test/data/repos/errors/hg-no-tag.zip differ diff --git a/test/data/repos/hg/default-tag-fallback.fields.json b/test/data/repos/hg/default-tag-fallback.fields.json new file mode 100644 index 0000000..0ed570c --- /dev/null +++ b/test/data/repos/hg/default-tag-fallback.fields.json @@ -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" +} diff --git a/test/data/repos/hg/default-tag-fallback.json b/test/data/repos/hg/default-tag-fallback.json new file mode 100644 index 0000000..be4ec96 --- /dev/null +++ b/test/data/repos/hg/default-tag-fallback.json @@ -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'" + } + ] +} diff --git a/test/data/repos/hg/default-tag-fallback.zip b/test/data/repos/hg/default-tag-fallback.zip new file mode 100644 index 0000000..39bf79b Binary files /dev/null and b/test/data/repos/hg/default-tag-fallback.zip differ diff --git a/test/test_methods/test_hg.py b/test/test_methods/test_hg.py index 79c7029..4ed77a1 100644 --- a/test/test_methods/test_hg.py +++ b/test/test_methods/test_hg.py @@ -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"), ], )