Skip to content

Add prefer_tag option to use the latest tag over the version file - #162

Open
lxcxjxhx wants to merge 1 commit into
dolfinus:masterfrom
lxcxjxhx:feat/prefer-tag
Open

lxcxjxhx wants to merge 1 commit into
dolfinus:masterfrom
lxcxjxhx:feat/prefer-tag

Conversation

@lxcxjxhx

Copy link
Copy Markdown

Description

Adds a new boolean option prefer_tag (default False) for the version_file versioning schema: when enabled and the repo contains at least one tag, the latest Git tag is used as the version source instead of the version file content.

When the tag and the version file content differ (a leading v is ignored in the comparison), a warning is logged either way, so stale version files are easier to notice.

Motivation

This follows the direction discussed in #155. The footgun: a release flow pushes a tag v2.1.0rc1, but the version_file still contains 2.1.0 — the built package silently gets version 2.1.0, because version_file ignores tags entirely (documented in docs/schemas/file/version_file.rst).

In #155, @dolfinus suggested:

It can be an option prefer_tag: bool, for this use case.

and, instead of a strict tag/file equality check:

IMHO there should be a warning if tag and version file content are not the same.

This PR implements exactly that.

Behavior

  • prefer_tag not set (default): behavior is unchanged — version file wins, tags ignored (no breaking change for existing repos).
  • prefer_tag = true, repo has tags: latest tag (per sort_by, honoring tag_filter) is the version source; tag_formatter, dev_template / dirty_template and ccount work exactly like in the tag-based schema.
  • prefer_tag = true, no tags: version file is used as usual.

Testing

  • New integration tests in tests/test_integration/test_version_file.py (6 test functions, 12 cases): default-off behavior, tagged HEAD with/without count_commits_from_version_file, dev commits after the tag, no-tag fallback, and v-prefixed tag comparison.
  • Locally: 116 passed on the pre-existing suite plus the new tests; the only failures are the pre-existing git_missing / git_not_executable cases which are unrelated to this change (they require an environment without git on PATH).

When 'version_file' is set, tags in the repo are ignored. This is a
footgun for release flows that push a tag like 'v2.1.0rc1' while the
version file still says '2.1.0': the built package silently gets version
2.1.0 (see issue dolfinus#155).

Add a 'prefer_tag: bool' option (default False to keep the current
behavior). When enabled and the repo has at least one tag, the latest
tag is used as the version source instead of the version file content,
honoring 'tag_formatter', dev/dirty templates and 'ccount' exactly like
the tag-based schema does.

When the tag and the version file content differ (ignoring a leading
'v'), a warning is logged either way, so stale version files are easier
to notice.
@lxcxjxhx
lxcxjxhx requested a review from dolfinus as a code owner September 29, 2026 12:54
Comment on lines +9 to +11
ignored (see :issue:`155` in the issue tracker of this project for the
discussion). With this option enabled, the latest Git tag takes precedence
over the version file content.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ignored (see :issue:`155` in the issue tracker of this project for the
discussion). With this option enabled, the latest Git tag takes precedence
over the version file content.
ignored (see :issue:`155`). With this option enabled, the latest Git tag takes precedence over the version file content.


version_file_path = project_root.joinpath(version_file)
if not version_file_path.exists():
if prefer_tag and tag is not None:

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be simplified instead of copy-pasting the branch

This branch has not been deployed

No deployments
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 this pull request may close these issues.

2 participants