Skip to content

Commit

Permalink
Prepare git archival style testing
Browse files Browse the repository at this point in the history
  • Loading branch information
RonnyPfannschmidt committed May 29, 2022
1 parent 5ae39d4 commit 0fa8bee
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .git_archival.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node: $Format:%H$
node-date: $Format:%cI$
describe-name: $Format:%(describe)$
ref-names: $Format:%D$
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.git_archival.txt export-subst
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
exclude *.nix
exclude .pre-commit-config.yaml
exclude .git_archival.txt

This comment has been minimized.

Copy link
@polyzen

polyzen Aug 13, 2022

Was this supposed to be include? This file is needed for test_git_archhival_from_unfiltered.

This comment has been minimized.

Copy link
@polyzen

This comment has been minimized.

Copy link
@RonnyPfannschmidt

RonnyPfannschmidt Dec 18, 2022

Author Contributor

@polyzen this was fixed in #757 which was recently released in 7.1.0

This comment has been minimized.

Copy link
@polyzen

polyzen Dec 19, 2022

Missed that, thank you.

include *.py
include testing/*.py
include tox.ini
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ setuptools_scm.parse_scm =
.git = setuptools_scm.git:parse
setuptools_scm.parse_scm_fallback =
.hg_archival.txt = setuptools_scm.hg:parse_archival
.git_archival.txt = setuptools_scm.git:parse_archival
PKG-INFO = setuptools_scm.hacks:parse_pkginfo
pip-egg-info = setuptools_scm.hacks:parse_pip_egg_info
setup.py = setuptools_scm.hacks:fallback_version
Expand Down
4 changes: 4 additions & 0 deletions src/setuptools_scm/.git_archival.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node: $Format:%H$
node-date: $Format:%cI$
describe-name: $Format:%(describe)$
ref-names: $Format:%D$
32 changes: 32 additions & 0 deletions src/setuptools_scm/git.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
import warnings
from datetime import date
from datetime import datetime
Expand All @@ -8,10 +9,15 @@

from .config import Configuration
from .scm_workdir import Workdir
from .utils import data_from_mime
from .utils import do_ex
from .utils import require_command
from .utils import trace
from .version import meta
from .version import tags_to_versions

REF_TAG_RE = re.compile(r"(?<=\btag: )([^,]+)\b")
DESCRIBE_UNSUPPORTED = "%(describe"

# If testing command in shell make sure to quote the match argument like
# '*[0-9]*' as it will expand before being sent to git if there are any matching
Expand Down Expand Up @@ -231,3 +237,29 @@ def search_parent(dirname):

if not tail:
return None


def archival_to_version(data, config=None):
trace("data", data)
archival_describe = data.get("describe-name", DESCRIBE_UNSUPPORTED)
if DESCRIBE_UNSUPPORTED in archival_describe:
warnings.warn("git archive did not support describe output")
else:
tag, number, node, _ = _git_parse_describe(archival_describe)
return meta(
tag,
config=config,
distance=None if number == 0 else number,
node=node,
)
versions = tags_to_versions(REF_TAG_RE.findall(data.get("ref-names", "")))
if versions:
return meta(versions[0], config=config)
else:
return meta("0.0", node=data.get("node"), config=config)


def parse_archival(root, config=None):
archival = os.path.join(root, ".git_archival.txt")
data = data_from_mime(archival)
return archival_to_version(data, config=config)
7 changes: 7 additions & 0 deletions testing/Dockerfile.rawhide-git
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM fedora:rawhide
RUN dnf install git -y
RUN git --version
USER 1000:1000
VOLUME /repo
WORKDIR /repo
ENTRYPOINT mkdir git-archived && git archive HEAD -o git-archived/archival.tar.gz
38 changes: 35 additions & 3 deletions testing/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@

import pytest

from setuptools_scm import Configuration
from setuptools_scm import format_version
from setuptools_scm import git
from setuptools_scm import integration
from setuptools_scm import NonNormalizedVersion
from setuptools_scm.file_finder_git import git_find_files
from setuptools_scm.git import archival_to_version
from setuptools_scm.utils import do
from setuptools_scm.utils import has_command

Expand Down Expand Up @@ -395,9 +398,7 @@ def parse_date():
assert meta.node_date == today


def test_git_getdate_badgit(
wd,
):
def test_git_getdate_badgit(wd):
wd.commit_testfile()
git_wd = git.GitWorkdir(os.fspath(wd.cwd))
with patch.object(git_wd, "do_ex", Mock(return_value=("%cI", "", 0))):
Expand Down Expand Up @@ -435,3 +436,34 @@ def test_git_getdate_signed_commit(signed_commit_wd):
signed_commit_wd.commit_testfile(signed=True)
git_wd = git.GitWorkdir(os.fspath(signed_commit_wd.cwd))
assert git_wd.get_head_date() == today


@pytest.mark.parametrize(
"expected, from_data",
[
(
"1.0",
{"describe-name": "1.0-0-g0000"},
),
(
"1.1.dev3+g0000",
{
"describe-name": "1.0-3-g0000",
"node": "0" * 20,
},
),
("0.0", {"node": "0" * 20}),
("1.2.2", {"describe-name": "release-1.2.2-0-g00000"}),
("1.2.2.dev0", {"ref-names": "tag: release-1.2.2.dev"}),
],
)
@pytest.mark.filterwarnings("ignore:git archive did not support describe output")
def test_git_archival_to_version(expected, from_data):
config = Configuration()
version = archival_to_version(from_data, config=config)
assert (
format_version(
version, version_scheme="guess-next-dev", local_scheme="node-and-date"
)
== expected
)
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ testpaths=testing
filterwarnings=
error
ignore:.*tool\.setuptools_scm.*
ignore:.*git archive did not support describe output.*:UserWarning
markers=
issue(id): reference to github issue
skip_commit: allows to skip committing in the helpers
Expand Down

0 comments on commit 0fa8bee

Please sign in to comment.