Skip to content

Add tag_filter config option #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/options/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ Config options
sort_by
branch_formatter
tag_formatter
tag_filter
85 changes: 85 additions & 0 deletions docs/options/tag_filter.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
.. _tag_filter-option:

``tag_filter``
~~~~~~~~~~~~~~~~~~~~~

Callback to be used for filtering tag names before formatting and template
substitution.

.. note::

This option is completely ignored if :ref:`version-file` schema is used.
This is because all tags are set on ``master`` / ``main`` branch,
so commits to other branches like ``develop`` are tagless.

.. note::

This option is completely ignored if :ref:`version-callback` schema is used,
because git commit history is not fetched in such a case.

Type
^^^^^
``str``

Default value
^^^^^^^^^^^^^
``None``

Usage
^^^^^^

Set when multiple products are tagged in a single repo.

If, for example, your repo has:

- ``product_x/1.2.0``
- ``product_x/1.2.1``
- ``product_x/1.3.0``
- ``product_y/2.0.0``
- ``product_y/2.1.0``

and you only want versions from ``product_y``, simply set:

.. code:: toml

tag_filter = "product_y/(?P<tag>.*)"

This will limit the tags considered to those that start with ``product_y``.

You will likely still need to construct a :ref:`tag-formatter-option` that
takes the entire tag into consideration. To make thing easier, you can often
use the same regexp/callback for the filter that you would use for the
formatter.

Possible values
^^^^^^^^^^^^^^^
- ``None``

Disables this feature

- function full name in format ``"some.module:function_name"``

Function should have signature ``(str) -> str | None``. It accepts original
tag name and returns the tag name (or subset thereof) if it should be in
the list and None if it is to be filtered out. When a formatter is
required, it it often easiest to use the same function for both the filter
and the formatter, following the rules for the formatter function.

.. warning::

Exception will be raised if module or function/lambda is missing or has invalid signature

- regexp like ``"tag-prefix/.*"`` or ``"tag-prefix/(?P<tag>.*)"``


The ``<tag>`` group isn't required for the filter, but makes it simpler to
share with the formatter option.

.. warning::

Exception will be raised if regexp is invalid

.. warning::

If regexp doesn't match any tag, the filter will return the empty list, and
the default "0.0.1" version will be selected.
54 changes: 51 additions & 3 deletions setuptools_git_versioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
DEFAULT_DIRTY_TEMPLATE = "{tag}.post{ccount}+git.{sha}.dirty"
DEFAULT_STARTING_VERSION = "0.0.1"
DEFAULT_SORT_BY = "creatordate"
DEFAULT_TAG_FILTER = None
ENV_VARS_REGEXP = re.compile(r"\{env:(?P<name>[^:}]+):?(?P<default>[^}]+\}*)?\}", re.IGNORECASE | re.UNICODE)
TIMESTAMP_REGEXP = re.compile(r"\{timestamp:?(?P<fmt>[^:}]+)?\}", re.IGNORECASE | re.UNICODE)

Expand Down Expand Up @@ -55,6 +56,7 @@
"tag_formatter": None,
"branch_formatter": None,
"sort_by": DEFAULT_SORT_BY,
"tag_filter": DEFAULT_TAG_FILTER,
}

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -96,8 +98,16 @@ def get_branch_tags(*args, **kwargs) -> list[str]:
return get_tags(*args, **kwargs)


def get_tags(sort_by: str = DEFAULT_SORT_BY, root: str | os.PathLike | None = None) -> list[str]:
def get_tags(
sort_by: str = DEFAULT_SORT_BY,
filter_callback: Callable[[str], str | None] | None = None,
root: str | os.PathLike | None = None,
) -> list[str]:
tags = _exec(f"git tag --sort=-{sort_by} --merged", root=root)
if filter_callback:
# pull the tags that don't start with tag_prefix out of the list
mytags = list(filter(filter_callback, tags))
return mytags
if tags:
return tags
return []
Expand Down Expand Up @@ -417,6 +427,39 @@ def formatter(branch):
raise ValueError("Cannot parse branch_formatter") from e


def load_tag_filter(
tag_filter: str | Callable[[str], str | None],
package_name: str | None = None,
root: str | os.PathLike | None = None,
) -> Callable[[str], str | None]:
log.log(INFO, "Parsing tag_filter %r of type %r", tag_filter, type(tag_filter).__name__)

if callable(tag_filter):
log.log(DEBUG, "Value is callable with signature %s", inspect.Signature.from_callable(tag_filter))
return tag_filter

try:
return load_callable(tag_filter, package_name, root=root)
except (ImportError, NameError) as e:
log.warning("tag_filter is not a valid function reference: %s", e)

try:
pattern = re.compile(tag_filter)

def formatter(tag: str) -> str | None:
match = pattern.match(tag)
if match:
log.error("Matched %s", tag)
return tag
else:
return None

return formatter
except re.error as e:
log.error("tag_filter is not valid regexp: %s", e)
raise ValueError("Cannot parse tag_filter") from e


# TODO: return Version object instead of str
def get_version_from_callback(
version_callback: str | Callable[[], str],
Expand Down Expand Up @@ -477,8 +520,9 @@ def version_from_git(
version_callback: str | Callable[[], str] | None = None,
version_file: str | os.PathLike | None = None,
count_commits_from_version_file: bool = False,
tag_formatter: Callable[[str], str] | None = None,
tag_formatter: Callable[[str], str] | str | None = None,
branch_formatter: Callable[[str], str] | None = None,
tag_filter: Callable[[str], str | None] | str | None = None,
sort_by: str = DEFAULT_SORT_BY,
root: str | os.PathLike | None = None,
) -> str:
Expand All @@ -500,10 +544,14 @@ def version_from_git(
)
return get_version_from_callback(version_callback, package_name, root=root)

filter_callback = None
if tag_filter:
filter_callback = load_tag_filter(tag_filter=tag_filter, package_name=package_name, root=root)

from_file = False
log.log(INFO, "Getting latest tag")
log.log(DEBUG, "Sorting tags by %r", sort_by)
tag = get_tag(sort_by=sort_by, root=root)
tag = get_tag(sort_by=sort_by, root=root, filter_callback=filter_callback)

if tag is None:
log.log(INFO, "No tag, checking for 'version_file'")
Expand Down
22 changes: 20 additions & 2 deletions tests/lib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def rand_sha() -> str:


def execute(cwd: str | os.PathLike, cmd: str, **kwargs) -> str:
log.info(f"Executing '{cmd}' at '{cwd}'")
log.critical(f"Executing '{cmd}' at '{cwd}'")
return subprocess.check_output(cmd, cwd=cwd, shell=True, universal_newlines=True, **kwargs) # nosec


Expand All @@ -48,9 +48,27 @@ def create_commit(
**kwargs,
) -> str:
options = ""

if dt is not None:
# Store committer date in case it was set somewhere else
original_committer_date = os.environ.get("GIT_COMMITTER_DATE", None)

options += f"--date {dt.isoformat()}"
return execute(cwd, f'git commit -m "{message}" {options}', **kwargs)
# The committer date is what is used to determine sort order for tags, etc
os.environ["GIT_COMMITTER_DATE"] = f"--date {dt.isoformat()}"

return_value = execute(cwd, f'git commit -m "{message}" {options}', **kwargs)

# Return committer date env var to prior value if set
if dt is not None:
if original_committer_date is None:
# unset the var
del os.environ["GIT_COMMITTER_DATE"]
else:
# restore previous value
os.environ["GIT_COMMITTER_DATE"] = original_committer_date

return return_value


def create_tag(
Expand Down
14 changes: 1 addition & 13 deletions tests/test_integration/test_tag.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import subprocess
from datetime import datetime, timedelta
import pytest
import time

from tests.lib.util import (
create_file,
Expand Down Expand Up @@ -254,7 +253,6 @@ def test_tag_sort_by_commit_date(repo, create_config, message):
dt = datetime.now() - timedelta(days=len(tags_to_commit) - i)
create_commit(repo, "Some commit", dt=dt)
commits[tag] = get_sha(repo)
time.sleep(1)

tags_to_create = [
"1.1.0",
Expand Down Expand Up @@ -299,16 +297,8 @@ def test_tag_sort_by_tag_date(repo, create_config, message):

for tag in tags_to_create:
create_tag(repo, tag, message=message, commit=commits[tag])
time.sleep(1)

if message:
# the result is not stable because latest tag (by creation time)
# has nothing in common with commit creation time
assert "1.1.10" in get_version(repo)
else:
assert get_version(repo).startswith("1.1")
# the result is totally random because annotaged tags have no such field at all
# https://github.com/dolfinus/setuptools-git-versioning/issues/23
assert get_version(repo).startswith("1.1")


@pytest.mark.parametrize("sort_by", [None, "creatordate"])
Expand All @@ -331,7 +321,6 @@ def test_tag_sort_by_create_date(repo, create_config, message, sort_by):
dt = datetime.now() - timedelta(days=len(tags_to_commit) - i)
create_commit(repo, "Some commit", dt=dt)
commits[tag] = get_sha(repo)
time.sleep(1)

tags_to_create = [
"1.1.10",
Expand All @@ -341,7 +330,6 @@ def test_tag_sort_by_create_date(repo, create_config, message, sort_by):

for tag in tags_to_create:
create_tag(repo, tag, message=message, commit=commits[tag])
time.sleep(1)

if message:
# the result is not stable because latest tag (by creation time)
Expand Down
Loading