Skip to content

Commit

Permalink
feat: fallback_version config for scm version
Browse files Browse the repository at this point in the history
Signed-off-by: Frost Ming <me@frostming.com>
  • Loading branch information
frostming committed Mar 26, 2024
1 parent d63fec7 commit 8b8ab28
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
9 changes: 9 additions & 0 deletions docs/metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ When building from a source tree where SCM is not available, you can use the env
PDM_BUILD_SCM_VERSION=0.1.0 python -m build
```

+++ 2.2.0

Alternatively, you can specify a default version in the configuration:

```toml
[tool.pdm.version]
fallback_version = "0.0.0"
```

You can specify another regex pattern to match the SCM tag, in which a `version` group is required:

```toml
Expand Down
10 changes: 10 additions & 0 deletions src/pdm/backend/hooks/version/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def resolve_version_from_scm(
write_template: str = "{}\n",
tag_regex: str | None = None,
version_format: str | None = None,
fallback_version: str | None = None,
) -> str:
if "PDM_BUILD_SCM_VERSION" in os.environ:
version = os.environ["PDM_BUILD_SCM_VERSION"]
Expand All @@ -89,6 +90,15 @@ def resolve_version_from_scm(
version = get_version_from_scm(
context.root, tag_regex=tag_regex, version_formatter=version_formatter
)
if version is None:
if fallback_version is not None:
version = fallback_version
else:
raise ConfigError(
"Cannot find the version from SCM or SCM isn't detected. \n"
"You can still specify the version via environment variable "
"`PDM_BUILD_SCM_VERSION`, or specify `fallback_version` config."
)

self._write_version(context, version, write_to, write_template)
return version
Expand Down
8 changes: 2 additions & 6 deletions src/pdm/backend/hooks/version/scm.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,16 +333,12 @@ def get_version_from_scm(
*,
tag_regex: str | None = None,
version_formatter: Callable[[SCMVersion], str] | None = None,
) -> str:
) -> str | None:
config = Config(tag_regex=re.compile(tag_regex) if tag_regex else DEFAULT_TAG_REGEX)
for func in (git_parse_version, hg_parse_version):
version = func(root, config) # type: ignore
if version:
if version_formatter is None:
version_formatter = format_version
return version_formatter(version)
raise ValueError(
"Cannot find the version from SCM or SCM isn't detected. \n"
"You can still specify the version via environment variable "
"`PDM_BUILD_SCM_VERSION`."
)
return None

0 comments on commit 8b8ab28

Please sign in to comment.