From 451f1e5fd25408834ddc8a313517ec8194a8f8e8 Mon Sep 17 00:00:00 2001 From: Ashwin Bharambe Date: Fri, 31 Oct 2025 08:14:20 -0700 Subject: [PATCH] refactor: smart UV index wrapper for all pre-commit hooks Replace isolated UV configuration with unified wrapper script that: - Detects release branches (release-X.Y.x pattern) - Checks upstream target branch for feature branches - Supports LLAMA_STACK_RELEASE_MODE env var for local testing - Wraps all uv commands (lock, run) in local hooks When on/targeting release branches, automatically sets: - UV_EXTRA_INDEX_URL=https://test.pypi.org/simple/ - UV_INDEX_STRATEGY=unsafe-best-match For local testing on feature branches targeting release branches: LLAMA_STACK_RELEASE_MODE=true pre-commit run --all-files This ensures RC versions can be resolved from test.pypi in both CI (on actual release branches) and local development. --- .pre-commit-config.yaml | 10 ++++---- scripts/pre-commit-uv-lock.sh | 22 ------------------ scripts/uv-run-with-index.sh | 43 +++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 27 deletions(-) delete mode 100755 scripts/pre-commit-uv-lock.sh create mode 100755 scripts/uv-run-with-index.sh diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index c82b5b491..fe9f0c2b0 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -78,7 +78,7 @@ repos: name: uv-lock additional_dependencies: - uv==0.7.20 - entry: ./scripts/pre-commit-uv-lock.sh + entry: ./scripts/uv-run-with-index.sh lock language: python pass_filenames: false require_serial: true @@ -87,7 +87,7 @@ repos: name: Distribution Template Codegen additional_dependencies: - uv==0.7.8 - entry: uv run --group codegen ./scripts/distro_codegen.py + entry: ./scripts/uv-run-with-index.sh run --group codegen ./scripts/distro_codegen.py language: python pass_filenames: false require_serial: true @@ -96,7 +96,7 @@ repos: name: Provider Codegen additional_dependencies: - uv==0.7.8 - entry: uv run --group codegen ./scripts/provider_codegen.py + entry: ./scripts/uv-run-with-index.sh run --group codegen ./scripts/provider_codegen.py language: python pass_filenames: false require_serial: true @@ -105,7 +105,7 @@ repos: name: API Spec Codegen additional_dependencies: - uv==0.7.8 - entry: sh -c 'uv run ./docs/openapi_generator/run_openapi_generator.sh > /dev/null' + entry: sh -c './scripts/uv-run-with-index.sh run ./docs/openapi_generator/run_openapi_generator.sh > /dev/null' language: python pass_filenames: false require_serial: true @@ -146,7 +146,7 @@ repos: name: Generate CI documentation additional_dependencies: - uv==0.7.8 - entry: uv run ./scripts/gen-ci-docs.py + entry: ./scripts/uv-run-with-index.sh run ./scripts/gen-ci-docs.py language: python pass_filenames: false require_serial: true diff --git a/scripts/pre-commit-uv-lock.sh b/scripts/pre-commit-uv-lock.sh deleted file mode 100755 index 0ae7f1901..000000000 --- a/scripts/pre-commit-uv-lock.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/bash -# Copyright (c) Meta Platforms, Inc. and affiliates. -# All rights reserved. -# -# This source code is licensed under the terms described in the LICENSE file in -# the root directory of this source tree. - -set -euo pipefail - -# Detect current branch -BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "") - -# On release branches, use test.pypi as extra index for RC versions -if [[ "$BRANCH" =~ ^release-[0-9]+\.[0-9]+\.x$ ]]; then - echo "Detected release branch: $BRANCH" - echo "Setting UV_EXTRA_INDEX_URL=https://test.pypi.org/simple/" - export UV_EXTRA_INDEX_URL="https://test.pypi.org/simple/" - export UV_INDEX_STRATEGY="unsafe-best-match" -fi - -# Run uv lock -exec uv lock "$@" diff --git a/scripts/uv-run-with-index.sh b/scripts/uv-run-with-index.sh new file mode 100755 index 000000000..00d6ac591 --- /dev/null +++ b/scripts/uv-run-with-index.sh @@ -0,0 +1,43 @@ +#!/bin/bash +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the terms described in the LICENSE file in +# the root directory of this source tree. + +set -euo pipefail + +# Detect current branch and target branch +BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "") +# For feature branches, check what branch they're merging into +TARGET_BRANCH=$(git rev-parse --abbrev-ref HEAD@{upstream} 2>/dev/null | sed 's|origin/||' || echo "") + +echo "[uv-run-with-index] Current branch: '$BRANCH'" >&2 +echo "[uv-run-with-index] Target branch: '$TARGET_BRANCH'" >&2 +echo "[uv-run-with-index] PWD: $PWD" >&2 +echo "[uv-run-with-index] Command: uv $*" >&2 + +# Check if on a release branch or targeting one, or LLAMA_STACK_RELEASE_MODE is set +IS_RELEASE=false +if [[ "$BRANCH" =~ ^release-[0-9]+\.[0-9]+\.x$ ]]; then + echo "[uv-run-with-index] ✓ On release branch: $BRANCH" >&2 + IS_RELEASE=true +elif [[ "$TARGET_BRANCH" =~ ^release-[0-9]+\.[0-9]+\.x$ ]]; then + echo "[uv-run-with-index] ✓ Targeting release branch: $TARGET_BRANCH" >&2 + IS_RELEASE=true +elif [[ "${LLAMA_STACK_RELEASE_MODE:-}" == "true" ]]; then + echo "[uv-run-with-index] ✓ LLAMA_STACK_RELEASE_MODE=true" >&2 + IS_RELEASE=true +else + echo "[uv-run-with-index] ✗ Not a release branch" >&2 +fi + +# On release branches, use test.pypi as extra index for RC versions +if [[ "$IS_RELEASE" == "true" ]]; then + echo "[uv-run-with-index] Setting UV_EXTRA_INDEX_URL=https://test.pypi.org/simple/" >&2 + export UV_EXTRA_INDEX_URL="https://test.pypi.org/simple/" + export UV_INDEX_STRATEGY="unsafe-best-match" +fi + +# Run uv with all arguments passed through +exec uv "$@"