Skip to content
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

Allow filtering tags when using SCM to determine the version #219

Closed
mezuzza opened this issue Mar 21, 2024 · 3 comments · Fixed by #225
Closed

Allow filtering tags when using SCM to determine the version #219

mezuzza opened this issue Mar 21, 2024 · 3 comments · Fixed by #225

Comments

@mezuzza
Copy link
Contributor

mezuzza commented Mar 21, 2024

I've used PDM in a few monorepos. In each one, I need to build different projects which all have dynamic SCM versioning enabled. Each project has it's own versioning determined by git tags. e.g. project-a/0.1.1 and project-b/2.1.1. However, when using scm versioning, only the most recent of those tags will be used and only one project will derive a proper version number.

The workaround for git is to use a call based version and reuse most of the same machinery (see below for the workaround).

I don't know the equivalent for mercurial, but for git, we simply need to be able to provide an argument for the --match command line argument for git describe called here in pdm.

Would it be possible to provide a tag_filter key in the tool.pdm.version table which accepts a glob that is used for the --match parameter?

Workaround:

import subprocess

from pdm.backend.hooks.version.scm import (
    DEFAULT_TAG_REGEX,
    Config,
    VersionInfo,
    _git_parse_describe,
    format_version,
    tag_to_version,
)

_PACKAGE_PREFIX = "project-a/"


def get_version():
    branch = subprocess.run(
        ["git", "rev-parse", "--abbrev-ref", "HEAD"],
        capture_output=True,
        encoding="utf-8",
    )

    describe = subprocess.run(
        ["git", "describe", "--long", "--match", f"{_PACKAGE_PREFIX}*", "--dirty"],
        capture_output=True,
        encoding="utf-8",
    )

    tag, distance, node, dirty = _git_parse_describe(describe.stdout.strip())

    version = tag_to_version(Config(DEFAULT_TAG_REGEX), tag)

    return format_version(
        VersionInfo(
            version=version,
            distance=distance or None,
            dirty=dirty,
            node=node,
            branch=branch.stdout.strip(),
        )
    )
@frostming
Copy link
Contributor

This requirement is reasonable and the solution is also very clear. Would you like to submit a PR?

@mezuzza
Copy link
Contributor Author

mezuzza commented Mar 25, 2024

I'll try to get to it this week, but no promises.

Do you happen to have any ideas on whether this is an issue for hg and if so how you might solve for it? I'll probably leave that as a TODO and let that happen in a separate PR.

@frostming
Copy link
Contributor

frostming commented Mar 25, 2024

There are two situations here:

  1. If the current commit is associated with a tag, you need to check if it matches the pattern. hg does this through here:
    _, output, _ = _subprocess_call("hg id -i -b -t", root)
  2. If not, it looks for its ancestors to find the nearest tag, and it accepts a regex pattern(the part after re: is a regex):
    "ancestors(.) and tag('re:\\.')",

This was referenced Mar 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants