From cb5b0d9991aabd3b9d5ab0a514c7a8dac21d9227 Mon Sep 17 00:00:00 2001 From: Alberto De Bortoli Date: Wed, 11 Mar 2026 14:39:58 +0000 Subject: [PATCH] Add version check in post-checkout hook Check installed luca version against .luca-version on branch checkout, triggering a reinstall when there is a mismatch. Also fixes the shell_hook idempotent PATH test to match only the project-specific .luca/tools entry, avoiding false positives when luca is installed. Co-Authored-By: Claude Sonnet 4.6 --- post-checkout | 29 +++++++++++++++++++++++------ tests/post_checkout.bats | 36 ++++++++++++++++++++++++++++++++++++ tests/shell_hook.bats | 4 ++-- 3 files changed, 61 insertions(+), 8 deletions(-) diff --git a/post-checkout b/post-checkout index 5aaef5b..97b6086 100755 --- a/post-checkout +++ b/post-checkout @@ -69,6 +69,16 @@ is_luca_installed() { command -v luca >/dev/null 2>&1 } +# Check if installed luca version matches .luca-version file +is_luca_version_correct() { + if [ ! -f "$VERSION_FILE" ]; then + return 0 # no version file, nothing to check + fi + REQUIRED_VERSION=$(cat "$VERSION_FILE") + INSTALLED_VERSION=$(luca --version 2>/dev/null) + [ "$INSTALLED_VERSION" = "$REQUIRED_VERSION" ] +} + # ============================================================================= # MAIN HOOK LOGIC # ============================================================================= @@ -95,6 +105,7 @@ if [ -z "$REPO_ROOT" ]; then exit 0 fi +VERSION_FILE="$REPO_ROOT/.luca-version" LUCAFILE_PATH="$REPO_ROOT/$LUCAFILE" # Only proceed if Lucafile exists @@ -109,19 +120,25 @@ log_info "Found $LUCAFILE, synchronizing tools..." # LUCA INSTALLATION CHECK # ============================================================================= -# Check if Luca is installed, install if not -if ! is_luca_installed; then - log_info "Luca not found, installing..." - +# Check if Luca is installed and version is correct, install/update if not +if ! is_luca_installed || ! is_luca_version_correct; then + if ! is_luca_installed; then + log_info "Luca not found, installing..." + else + REQUIRED=$(cat "$VERSION_FILE") + INSTALLED=$(luca --version 2>/dev/null) + log_info "Luca version mismatch (installed: $INSTALLED, required: $REQUIRED), updating..." + fi + if command -v curl >/dev/null 2>&1; then /bin/bash -c "$(curl -fsSL $INSTALL_SCRIPT_URL)" INSTALL_RESULT=$? - + if [ $INSTALL_RESULT -ne 0 ]; then log_error "Failed to install Luca" exit 1 fi - + log_success "Luca installed" else log_error "curl is required to install Luca" diff --git a/tests/post_checkout.bats b/tests/post_checkout.bats index ce553ea..63aec08 100644 --- a/tests/post_checkout.bats +++ b/tests/post_checkout.bats @@ -126,3 +126,39 @@ setup() { assert_success assert_output --partial "Some tools may have failed" } + +# --------------------------------------------------------------------------- +# Luca version check +# --------------------------------------------------------------------------- + +@test "version check: luca version matches .luca-version, curl NOT called to reinstall" { + cp "$FIXTURE_DIR/Lucafile" "$FAKE_REPO/Lucafile" + echo "v1.0.0" > "$FAKE_REPO/.luca-version" # matches MOCK_LUCA_VERSION default + + run "$REPO_ROOT/post-checkout" prev_ref new_ref 1 + + assert_success + refute_output --partial "mismatch" + refute_output --partial "installing" +} + +@test "version check: luca version mismatch triggers reinstall with mismatch message" { + cp "$FIXTURE_DIR/Lucafile" "$FAKE_REPO/Lucafile" + echo "v2.0.0" > "$FAKE_REPO/.luca-version" # differs from MOCK_LUCA_VERSION (v1.0.0) + + run "$REPO_ROOT/post-checkout" prev_ref new_ref 1 + + assert_output --partial "version mismatch" + assert_output --partial "v1.0.0" + assert_output --partial "v2.0.0" +} + +@test "version check: no .luca-version file skips version check" { + cp "$FIXTURE_DIR/Lucafile" "$FAKE_REPO/Lucafile" + # No .luca-version file created + + run "$REPO_ROOT/post-checkout" prev_ref new_ref 1 + + assert_success + refute_output --partial "mismatch" +} diff --git a/tests/shell_hook.bats b/tests/shell_hook.bats index 8bf7a53..2c0bff6 100644 --- a/tests/shell_hook.bats +++ b/tests/shell_hook.bats @@ -39,8 +39,8 @@ setup() { source '$REPO_ROOT/shell_hook.sh' >/dev/null 2>&1 update_path update_path - # Count occurrences of the tools dir in PATH - echo \"\$PATH\" | tr ':' '\n' | grep -c '\.luca/tools' || echo 0 + # Count occurrences of the specific tools dir in PATH + echo \"\$PATH\" | tr ':' '\n' | grep -cxF '$project/.luca/tools' || echo 0 " assert_output "1"