From c4c325ac2fe45660a52143d5629e5881162c3830 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gil=20Gon=C3=A7alves?= Date: Wed, 4 Nov 2026 09:39:19 +0000 Subject: [PATCH 1/3] Version fractional-indexing from git tags instead of committing a bump The org ruleset now rejects unsigned commits on `main`, so the release workflow can no longer push its `chore: bump version` commit. The version now comes from the `v*` tags through hatch-vcs, matched strictly so the legacy `0.1.x` tags and the upstream fork's tags never count. The release works out the next version from the latest tag, builds with it, and tags the released commit, so nothing is committed. `__version__` reads the installed metadata, which also fixes it still saying 1.0.0. --- .github/workflows/release.yml | 81 +++++++++++++++++++++++++++++------ fractional_indexing.py | 8 +++- pyproject.toml | 17 +++++++- uv.lock | 3 +- 4 files changed, 90 insertions(+), 19 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 35721e6..1d0c981 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -6,9 +6,9 @@ on: workflow_dispatch: inputs: bump: - description: "Version bump (none releases the version already in pyproject.toml)" + description: "Which part of the version to bump" type: choice - options: [none, patch, minor, major] + options: [patch, minor, major] default: patch concurrency: deploy @@ -25,6 +25,14 @@ jobs: contents: write id-token: write steps: + - name: Check this is main + run: | + if [ "$GITHUB_REF" != "refs/heads/main" ]; then + echo "::error::Releases only run from main, not $GITHUB_REF." + exit 1 + fi + + # Full history and tags, to find the latest release tag. - name: Get latest code uses: actions/checkout@v4 with: @@ -33,26 +41,70 @@ jobs: - name: Set up uv uses: astral-sh/setup-uv@v5 - - name: Increment version + # The version lives in the v* git tags, not in pyproject.toml. An org + # ruleset requires every change to main to go through a signed pull + # request, so this job can't commit a version bump. The release created + # below tags the released commit. + - name: Work out the next version id: version + env: + BUMP: ${{ inputs.bump }} run: | - if [ "${{ inputs.bump }}" != "none" ]; then - uv version --bump "${{ inputs.bump }}" + # Only tags shaped vX.Y.Z, with no leading zeros, count, which leaves + # out the legacy 0.1.x tags. Bash would read a leading zero as octal + # in the arithmetic below. + RELEASE_TAG='^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$' + + LATEST_TAG=$(git tag --list 'v*' | grep -E "$RELEASE_TAG" | sort -V | tail -n 1 || true) + if [ -z "$LATEST_TAG" ]; then + echo "::error::No vX.Y.Z tag found." + exit 1 fi - echo "version=$(uv version --short)" >> $GITHUB_OUTPUT - - name: Commit updated version - if: ${{ inputs.bump != 'none' }} - uses: stefanzweifel/git-auto-commit-action@04702edda442b2e678b25b537cec683a1493fcb9 # v7.1.0 - with: - commit_message: "chore: bump version to ${{ steps.version.outputs.version }}" - file_pattern: "pyproject.toml uv.lock" + # A re-run of an older run must not release older code under a + # newer version. + if ! git merge-base --is-ancestor "$LATEST_TAG" "$GITHUB_SHA"; then + echo "::error::$LATEST_TAG isn't in this commit's history. Release from the latest main instead." + exit 1 + fi + + # Releases are manual, so a commit that already has a release has + # nothing new to ship, whatever the bump. + ALREADY_RELEASED=$(git tag --points-at "$GITHUB_SHA" | grep -E "$RELEASE_TAG" || true) + if [ -n "$ALREADY_RELEASED" ]; then + echo "::error::This commit is already released as $ALREADY_RELEASED." + exit 1 + fi + + IFS=. read -r MAJOR MINOR PATCH <<< "${LATEST_TAG#v}" + case "$BUMP" in + patch) PATCH=$((PATCH + 1)) ;; + minor) MINOR=$((MINOR + 1)); PATCH=0 ;; + major) MAJOR=$((MAJOR + 1)); MINOR=0; PATCH=0 ;; + *) echo "::error::Unknown bump: $BUMP"; exit 1 ;; + esac + NEXT_VERSION="$MAJOR.$MINOR.$PATCH" + + # Ask the remote, in case a tag was pushed after the checkout. + if git ls-remote --exit-code --tags origin "refs/tags/v$NEXT_VERSION" > /dev/null; then + echo "::error::Tag v$NEXT_VERSION already exists." + exit 1 + fi + + echo "Releasing $NEXT_VERSION ($BUMP bump from $LATEST_TAG)." + echo "version=$NEXT_VERSION" >> "$GITHUB_OUTPUT" - name: Run tests run: uv run --group dev pytest tests.py - name: Build package - run: uv build + env: + # hatch-vcs takes the version from here instead of the git tags. + SETUPTOOLS_SCM_PRETEND_VERSION: ${{ steps.version.outputs.version }} + run: | + uv build + WHEEL="dist/doist_fractional_indexing-$SETUPTOOLS_SCM_PRETEND_VERSION-py3-none-any.whl" + unzip -p "$WHEEL" '*.dist-info/METADATA' | grep -x "Version: $SETUPTOOLS_SCM_PRETEND_VERSION" - name: Configure AWS credentials uses: aws-actions/configure-aws-credentials@v6 @@ -70,9 +122,12 @@ jobs: export UV_PUBLISH_PASSWORD uv publish + # Creating the tag after publishing means a failed publish leaves no tag, + # so a re-run releases the same version rather than skipping one. - name: Create GitHub release uses: softprops/action-gh-release@b4309332981a82ec1c5618f44dd2e27cc8bfbfda # v3.0.0 with: tag_name: v${{ steps.version.outputs.version }} name: v${{ steps.version.outputs.version }} + target_commitish: ${{ github.sha }} generate_release_notes: true diff --git a/fractional_indexing.py b/fractional_indexing.py index 2069041..d0f8176 100644 --- a/fractional_indexing.py +++ b/fractional_indexing.py @@ -13,12 +13,16 @@ import random from functools import lru_cache +from importlib import metadata from math import floor from typing import List, Optional -# Informational; the authoritative release version lives in pyproject.toml. -__version__ = '1.0.0' +try: + __version__ = metadata.version('doist-fractional-indexing') +except metadata.PackageNotFoundError: + # Imported from a checkout without installing it, as the Test workflow does. + __version__ = '0.0.0' __licence__ = 'CC0 1.0 Universal' __all__ = [ diff --git a/pyproject.toml b/pyproject.toml index 58fb696..c383576 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,7 @@ [project] name = "doist-fractional-indexing" -version = "1.1.0" +# The version comes from git tags (see [tool.hatch.version]). +dynamic = ["version"] description = "Fractional indexing: string order keys that sort lexicographically. Doist fork, byte-for-byte compatible with rocicorp/fractional-indexing v4." readme = "README.md" license = "CC0-1.0" @@ -24,9 +25,21 @@ Upstream = "https://github.com/httpie/fractional-indexing-python" dev = ["pytest"] [build-system] -requires = ["hatchling>=1.27"] +requires = ["hatchling>=1.27", "hatch-vcs>=0.4,<1"] build-backend = "hatchling.build" +[tool.hatch.version] +# The version comes from the v* git tags, so it isn't written in this file. +# The release workflow builds with SETUPTOOLS_SCM_PRETEND_VERSION set to the +# released version. Any other build gets a dev version after the latest tag, +# or the fallback when there's no git metadata at all. +source = "vcs" +fallback-version = "0.0.0" +# Only vX.Y.Z tags are releases. The legacy 0.1.x tags, and the tags of the +# upstream repo this one forks, must never set the version. +tag-pattern = '^v(?P\d+\.\d+\.\d+)$' +raw-options = { scm = { git = { describe_command = "git describe --dirty --tags --long --match v[0-9]*.[0-9]*.[0-9]*" } } } + [tool.hatch.build.targets.wheel] only-include = ["fractional_indexing.py"] diff --git a/uv.lock b/uv.lock index 286eac5..e9afc09 100644 --- a/uv.lock +++ b/uv.lock @@ -1,5 +1,5 @@ version = 1 -revision = 3 +revision = 5 requires-python = ">=3.8" resolution-markers = [ "python_full_version >= '3.10'", @@ -18,7 +18,6 @@ wheels = [ [[package]] name = "doist-fractional-indexing" -version = "1.1.0" source = { editable = "." } [package.dev-dependencies] From 28b8c45b548e591b93b3b2a6cb5420887920833d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gil=20Gon=C3=A7alves?= Date: Wed, 4 Nov 2026 09:54:28 +0000 Subject: [PATCH 2/3] Run the test matrix on Ubuntu 24.04 `ubuntu-latest` now resolves to Ubuntu 26.04, and `actions/setup-python` has no Python 3.8 or 3.9 builds for it, so those two jobs fail before any test runs. The package still supports 3.8, so the tests stay on 24.04 until the 3.8 and 3.9 support is dropped on purpose. --- .github/workflows/test.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ea7fb09..708e5d0 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -2,7 +2,8 @@ name: Test on: [push, pull_request] jobs: test: - runs-on: ubuntu-latest + # ubuntu-latest (26.04) has no setup-python builds for 3.8 or 3.9. + runs-on: ubuntu-24.04 strategy: fail-fast: false matrix: From ec136b0a026748bce222bd361c326961c3d7a94e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gil=20Gon=C3=A7alves?= Date: Wed, 4 Nov 2026 15:48:57 +0000 Subject: [PATCH 3/3] Stop releases on a failed tag lookup and check the installed version The release job asks the remote whether the next tag already exists, but `git ls-remote --exit-code` exits 2 for a missing tag and 128 when the lookup itself fails, and both counted as "missing". A network or auth failure skipped the check silently. Only exit 2 lets the release continue now. The tests run without the package installed, so `__version__` always took the 0.0.0 fallback, and a mismatch between the name it looks up and the published name would reach users unnoticed. CI installs the package and fails if the version still falls back. The hatch-vcs `tag-pattern` uses release.yml's pattern, so both agree on which tags are releases. --- .github/workflows/release.yml | 11 +++++++++-- .github/workflows/test.yml | 11 +++++++++++ pyproject.toml | 5 +++-- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 1d0c981..82080c7 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -85,10 +85,17 @@ jobs: esac NEXT_VERSION="$MAJOR.$MINOR.$PATCH" - # Ask the remote, in case a tag was pushed after the checkout. - if git ls-remote --exit-code --tags origin "refs/tags/v$NEXT_VERSION" > /dev/null; then + # Ask the remote, in case a tag was pushed after the checkout. Exit + # code 2 means the tag is missing; any other failure means the lookup + # itself failed, so the check didn't happen. + LOOKUP_STATUS=0 + git ls-remote --exit-code --tags origin "refs/tags/v$NEXT_VERSION" > /dev/null || LOOKUP_STATUS=$? + if [ "$LOOKUP_STATUS" -eq 0 ]; then echo "::error::Tag v$NEXT_VERSION already exists." exit 1 + elif [ "$LOOKUP_STATUS" -ne 2 ]; then + echo "::error::Could not check the remote for tag v$NEXT_VERSION." + exit 1 fi echo "Releasing $NEXT_VERSION ($BUMP bump from $LATEST_TAG)." diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 708e5d0..f98329e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -15,3 +15,14 @@ jobs: python-version: ${{ matrix.python-version }} - run: pip install pytest - run: pytest tests.py --verbose + # The tests above run without the package installed, so they only see + # the 0.0.0 fallback. This checks the installed path reads a version. + - name: Check the installed version + run: | + pip install . + INSTALLED_VERSION=$(python -c 'import fractional_indexing; print(fractional_indexing.__version__)') + echo "Installed version: $INSTALLED_VERSION" + if [ "$INSTALLED_VERSION" = "0.0.0" ]; then + echo "::error::__version__ fell back to 0.0.0 in an installed build." + exit 1 + fi diff --git a/pyproject.toml b/pyproject.toml index c383576..017ee39 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -36,8 +36,9 @@ build-backend = "hatchling.build" source = "vcs" fallback-version = "0.0.0" # Only vX.Y.Z tags are releases. The legacy 0.1.x tags, and the tags of the -# upstream repo this one forks, must never set the version. -tag-pattern = '^v(?P\d+\.\d+\.\d+)$' +# upstream repo this one forks, must never set the version. Same pattern as +# RELEASE_TAG in release.yml. +tag-pattern = '^v(?P(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*))$' raw-options = { scm = { git = { describe_command = "git describe --dirty --tags --long --match v[0-9]*.[0-9]*.[0-9]*" } } } [tool.hatch.build.targets.wheel]