Skip to content

Commit

Permalink
Further improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jwodder committed Mar 16, 2024
1 parent 7e902bd commit 225e243
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 21 deletions.
17 changes: 12 additions & 5 deletions src/versioningit/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,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 @@ -195,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(

Check warning on line 203 in src/versioningit/git.py

View check run for this annotation

Codecov / codecov/patch

src/versioningit/git.py#L203

Added line #L203 was not covered by tests
f"`{opts.as_cmdline_str()}` command failed: {e.stderr.strip()}"
)

def get_branch(self) -> Optional[str]:
"""
Expand Down Expand Up @@ -339,16 +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:
argstr = "".join(" " + shlex.quote(a) for a in opts.as_args())
raise NoTagError(f"`git describe{argstr}` could not find a tag")
raise NoTagError(f"`{opts.as_cmdline_str()}` could not find a tag")

Check warning on line 358 in src/versioningit/git.py

View check run for this annotation

Codecov / codecov/patch

src/versioningit/git.py#L358

Added line #L358 was not covered by tests
if distance and dirty:
state = "distance-dirty"
elif distance:
Expand Down
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 --tags '--exclude=*'` 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 --tags '--exclude=*'` 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 --tags '--match=v*'` 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 --tags '--match=v*'` 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 --tags` could not find a tag"
"message": "`git describe --long --dirty --always --tags` could not find a tag"
},
"logmsgs": [
{
"level": "ERROR",
"message": "NoTagError: `git describe --tags` 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 --tags` 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 225e243

Please sign in to comment.