Skip to content

Commit

Permalink
Merge pull request #77 from jenshnielsen/better_error_message_on_miss…
Browse files Browse the repository at this point in the history
…ing_tag

Improve error message when a tag is not found and match is set.
  • Loading branch information
jwodder authored Mar 16, 2024
2 parents 4d090c1 + d07ffbf commit 9e78935
Show file tree
Hide file tree
Showing 16 changed files with 74 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ repos:
exclude: ^test/data

- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
rev: 7.0.0
hooks:
- id: flake8
additional_dependencies:
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
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))

v3.0.0 (2023-12-13)
-------------------
- Migrated from setuptools to hatch
Expand Down
7 changes: 7 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
Changelog
=========

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>`_)


v3.0.0 (2023-12-13)
-------------------
- Migrated from setuptools to hatch
Expand Down
2 changes: 1 addition & 1 deletion src/versioningit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<https://versioningit.rtfd.io> for more information.
"""

__version__ = "3.0.0"
__version__ = "3.1.0.dev1"
__author__ = "John Thorvald Wodder II"
__author_email__ = "[email protected]"
__license__ = "MIT"
Expand Down
17 changes: 13 additions & 4 deletions src/versioningit/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import datetime
from pathlib import Path
import re
import shlex
import subprocess
from typing import Any, NamedTuple, Optional
from .core import VCSDescription
Expand Down Expand Up @@ -128,6 +129,11 @@ def as_args(self) -> list[str]:
args.append(f"--exclude={pat}")
return args

def as_cmdline_str(self) -> str:
return "git describe --long --dirty --always" + "".join(
" " + shlex.quote(a) for a in self.as_args()
)


@dataclass
class GitRepo:
Expand Down Expand Up @@ -194,7 +200,9 @@ def describe(self, opts: DescribeOpts) -> str:
except subprocess.CalledProcessError as e:
# As far as I'm aware, this only happens in a repo without any
# commits or a corrupted repo.
raise NoTagError(f"`git describe` command failed: {e.stderr.strip()}")
raise NoTagError(
f"`{opts.as_cmdline_str()}` command failed: {e.stderr.strip()}"
)

def get_branch(self) -> Optional[str]:
"""
Expand Down Expand Up @@ -338,15 +346,16 @@ def describe_git_core(
except ValueError:
if default_tag is not None:
log.info(
"`git describe` returned a hash instead of a tag; falling back to"
" default tag %r",
"`%s` returned a hash instead of a tag; falling back to default"
" tag %r",
opts.as_cmdline_str(),
default_tag,
)
tag = default_tag
distance = int(repo.read("rev-list", "--count", "HEAD")) - 1
rev = description
else:
raise NoTagError("`git describe` could not find a tag")
raise NoTagError(f"`{opts.as_cmdline_str()}` could not find a tag")
if distance and dirty:
state = "distance-dirty"
elif distance:
Expand Down
4 changes: 4 additions & 0 deletions test/data/repos/errors/archive-no-tag.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "NoTagError",
"message": "`git describe --long --dirty --always '--exclude=v*'` could not find a tag"
}
1 change: 1 addition & 0 deletions test/data/repos/errors/archive-no-tag.marks
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
describe_exclude
Binary file added test/data/repos/errors/archive-no-tag.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion test/data/repos/errors/hatch-no-tag.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"type": "NoTagError",
"message": "`git describe` could not find a tag"
"message": "`git describe --long --dirty --always --tags '--exclude=*'` could not find a tag"
}
2 changes: 1 addition & 1 deletion test/data/repos/errors/no-tag.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"type": "NoTagError",
"message": "`git describe` could not find a tag"
"message": "`git describe --long --dirty --always --tags '--exclude=*'` could not find a tag"
}
12 changes: 11 additions & 1 deletion test/data/repos/git/added-no-commits-default-tag.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
{
"version": "0.0.0+d20380119",
"next_version": "0.1.0"
"next_version": "0.1.0",
"logmsgs": [
{
"level": "ERROR",
"message": "`git describe --long --dirty --always --tags` command failed: fatal: bad revision 'HEAD'"
},
{
"level": "INFO",
"message": "Falling back to default tag 'v0.0.0'"
}
]
}
8 changes: 7 additions & 1 deletion test/data/repos/git/default-tag.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"version": "0.0.0.post2+gb4461bd",
"next_version": "0.1.0"
"next_version": "0.1.0",
"logmsgs": [
{
"level": "INFO",
"message": "`git describe --long --dirty --always --tags` returned a hash instead of a tag; falling back to default tag 'v0.0.0'"
}
]
}
4 changes: 2 additions & 2 deletions test/data/repos/git/default-version-bad.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"version": "1.1.1m",
"next_version": {
"type": "NoTagError",
"message": "`git describe` could not find a tag"
"message": "`git describe --long --dirty --always --tags` could not find a tag"
},
"logmsgs": [
{
"level": "ERROR",
"message": "NoTagError: `git describe` could not find a tag"
"message": "NoTagError: `git describe --long --dirty --always --tags` could not find a tag"
},
{
"level": "INFO",
Expand Down
4 changes: 2 additions & 2 deletions test/data/repos/git/default-version-onbuild-write.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"version": "0.0.0+error",
"next_version": {
"type": "NoTagError",
"message": "`git describe` could not find a tag"
"message": "`git describe --long --dirty --always --tags '--match=v*'` could not find a tag"
},
"logmsgs": [
{
"level": "ERROR",
"message": "NoTagError: `git describe` could not find a tag"
"message": "NoTagError: `git describe --long --dirty --always --tags '--match=v*'` could not find a tag"
},
{
"level": "INFO",
Expand Down
4 changes: 2 additions & 2 deletions test/data/repos/git/default-version.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
"version": "0.0.0+error",
"next_version": {
"type": "NoTagError",
"message": "`git describe` could not find a tag"
"message": "`git describe --long --dirty --always --tags` could not find a tag"
},
"logmsgs": [
{
"level": "ERROR",
"message": "NoTagError: `git describe` could not find a tag"
"message": "NoTagError: `git describe --long --dirty --always --tags` could not find a tag"
},
{
"level": "INFO",
Expand Down
21 changes: 15 additions & 6 deletions test/test_methods/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ def test_describe_git_no_tag(tmp_path: Path) -> None:
shutil.unpack_archive(DATA_DIR / "repos" / "git" / "default-tag.zip", tmp_path)
with pytest.raises(NoTagError) as excinfo:
describe_git(project_dir=tmp_path, params={})
assert str(excinfo.value) == "`git describe` could not find a tag"
assert (
str(excinfo.value)
== "`git describe --long --dirty --always --tags` could not find a tag"
)


@needs_git
Expand All @@ -105,7 +108,10 @@ def test_describe_git_added_no_commits(tmp_path: Path) -> None:
shutil.unpack_archive(
DATA_DIR / "repos" / "git" / "added-no-commits-default-tag.zip", tmp_path
)
with pytest.raises(NoTagError, match=r"^`git describe` command failed: "):
with pytest.raises(
NoTagError,
match=r"^`git describe --long --dirty --always --tags` command failed: ",
):
describe_git(project_dir=tmp_path, params={})


Expand Down Expand Up @@ -226,7 +232,7 @@ def test_describe_git_archive_added_no_commits_default_tag(
assert any(
logger == "versioningit"
and level == logging.ERROR
and re.match("^`git describe` command failed: ", msg)
and re.match("^`git describe --long --dirty --always` command failed: ", msg)
for logger, level, msg in caplog.record_tuples
)
assert (
Expand All @@ -244,7 +250,10 @@ def test_describe_git_archive_lightweight_only(tmp_path: Path) -> None:
project_dir=tmp_path,
params={"describe-subst": "$Format:%(describe)$"},
)
assert str(excinfo.value) == "`git describe` could not find a tag"
assert (
str(excinfo.value)
== "`git describe --long --dirty --always` could not find a tag"
)


@needs_git
Expand Down Expand Up @@ -274,8 +283,8 @@ def test_describe_git_archive_lightweight_only_default_tag(
assert (
"versioningit",
logging.INFO,
"`git describe` returned a hash instead of a tag; falling back to"
" default tag '0.0.0'",
"`git describe --long --dirty --always` returned a hash instead of a"
" tag; falling back to default tag '0.0.0'",
) in caplog.record_tuples


Expand Down

0 comments on commit 9e78935

Please sign in to comment.