|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# ============================================================================= |
| 4 | +# LUCA POST-CHECKOUT GIT HOOK |
| 5 | +# ============================================================================= |
| 6 | +# |
| 7 | +# This hook script runs after a git checkout or git switch command. |
| 8 | +# It ensures that CLI tools are synchronized with the Lucafile specification |
| 9 | +# after switching branches or checking out commits. |
| 10 | +# |
| 11 | +# Git passes three arguments to this hook: |
| 12 | +# $1 = ref of the previous HEAD |
| 13 | +# $2 = ref of the new HEAD |
| 14 | +# $3 = flag indicating checkout type: |
| 15 | +# 1 = branch checkout (git checkout <branch>, git switch <branch>) |
| 16 | +# 0 = file checkout (git checkout -- <file>) |
| 17 | +# |
| 18 | +# Installation: |
| 19 | +# Copy this file to .git/hooks/post-checkout in your repository |
| 20 | +# Or install Luca with the install.sh script |
| 21 | +# |
| 22 | +# ============================================================================= |
| 23 | + |
| 24 | +# Configuration |
| 25 | +TOOL_NAME="Luca" |
| 26 | +TOOL_FOLDER=".luca" |
| 27 | +LUCAFILE="Lucafile" |
| 28 | +INSTALL_SCRIPT_URL="https://raw.githubusercontent.com/LucaTools/LucaScripts/HEAD/install.sh" |
| 29 | + |
| 30 | +# ============================================================================= |
| 31 | +# HELPER FUNCTIONS |
| 32 | +# ============================================================================= |
| 33 | + |
| 34 | +# Print a message with Luca prefix |
| 35 | +log_info() { |
| 36 | + echo "[$TOOL_NAME] $1" |
| 37 | +} |
| 38 | + |
| 39 | +log_success() { |
| 40 | + echo "[$TOOL_NAME] ✅ $1" |
| 41 | +} |
| 42 | + |
| 43 | +log_warning() { |
| 44 | + echo "[$TOOL_NAME] ⚠️ $1" |
| 45 | +} |
| 46 | + |
| 47 | +log_error() { |
| 48 | + echo "[$TOOL_NAME] ❌ $1" |
| 49 | +} |
| 50 | + |
| 51 | +# Detect the user's shell RC file for sourcing |
| 52 | +get_shell_rc_file() { |
| 53 | + case "$SHELL" in |
| 54 | + */bash) |
| 55 | + echo "$HOME/.bashrc" |
| 56 | + ;; |
| 57 | + */zsh) |
| 58 | + echo "$HOME/.zshrc" |
| 59 | + ;; |
| 60 | + *) |
| 61 | + # Default to bashrc if shell detection fails |
| 62 | + echo "$HOME/.bashrc" |
| 63 | + ;; |
| 64 | + esac |
| 65 | +} |
| 66 | + |
| 67 | +# Check if luca command is available |
| 68 | +is_luca_installed() { |
| 69 | + command -v luca >/dev/null 2>&1 |
| 70 | +} |
| 71 | + |
| 72 | +# ============================================================================= |
| 73 | +# MAIN HOOK LOGIC |
| 74 | +# ============================================================================= |
| 75 | + |
| 76 | +# Get the checkout type flag (third argument) |
| 77 | +CHECKOUT_TYPE="${3:-0}" |
| 78 | + |
| 79 | +# Only run on branch checkouts, not file checkouts |
| 80 | +if [ "$CHECKOUT_TYPE" != "1" ]; then |
| 81 | + # File checkout, skip processing |
| 82 | + exit 0 |
| 83 | +fi |
| 84 | + |
| 85 | +# Get the refs for logging (optional) |
| 86 | +PREV_HEAD="$1" |
| 87 | +NEW_HEAD="$2" |
| 88 | + |
| 89 | +log_info "Branch checkout detected" |
| 90 | + |
| 91 | +# Check if Lucafile exists in the repository root |
| 92 | +REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null)" |
| 93 | +if [ -z "$REPO_ROOT" ]; then |
| 94 | + log_warning "Could not determine repository root" |
| 95 | + exit 0 |
| 96 | +fi |
| 97 | + |
| 98 | +LUCAFILE_PATH="$REPO_ROOT/$LUCAFILE" |
| 99 | + |
| 100 | +# Only proceed if Lucafile exists |
| 101 | +if [ ! -f "$LUCAFILE_PATH" ]; then |
| 102 | + # No Lucafile in this repository, nothing to do |
| 103 | + exit 0 |
| 104 | +fi |
| 105 | + |
| 106 | +log_info "Found $LUCAFILE, synchronizing tools..." |
| 107 | + |
| 108 | +# ============================================================================= |
| 109 | +# LUCA INSTALLATION CHECK |
| 110 | +# ============================================================================= |
| 111 | + |
| 112 | +# Check if Luca is installed, install if not |
| 113 | +if ! is_luca_installed; then |
| 114 | + log_info "Luca not found, installing..." |
| 115 | + |
| 116 | + if command -v curl >/dev/null 2>&1; then |
| 117 | + /bin/bash -c "$(curl -fsSL $INSTALL_SCRIPT_URL)" |
| 118 | + INSTALL_RESULT=$? |
| 119 | + |
| 120 | + if [ $INSTALL_RESULT -ne 0 ]; then |
| 121 | + log_error "Failed to install Luca" |
| 122 | + exit 1 |
| 123 | + fi |
| 124 | + |
| 125 | + log_success "Luca installed" |
| 126 | + else |
| 127 | + log_error "curl is required to install Luca" |
| 128 | + exit 1 |
| 129 | + fi |
| 130 | +fi |
| 131 | + |
| 132 | +# ============================================================================= |
| 133 | +# TOOL INSTALLATION FROM LUCAFILE |
| 134 | +# ============================================================================= |
| 135 | + |
| 136 | +log_info "Installing CLI tools from $LUCAFILE..." |
| 137 | + |
| 138 | +# Change to repo root to ensure correct context |
| 139 | +cd "$REPO_ROOT" || exit 1 |
| 140 | + |
| 141 | +# Install tools specified in Lucafile |
| 142 | +if is_luca_installed; then |
| 143 | + luca install --quiet --spec "$LUCAFILE" |
| 144 | + INSTALL_RESULT=$? |
| 145 | + |
| 146 | + if [ $INSTALL_RESULT -eq 0 ]; then |
| 147 | + log_success "Tools synchronized successfully" |
| 148 | + else |
| 149 | + log_warning "Some tools may have failed to install (exit code: $INSTALL_RESULT)" |
| 150 | + fi |
| 151 | +else |
| 152 | + log_error "Luca command not available after installation" |
| 153 | + exit 1 |
| 154 | +fi |
| 155 | + |
| 156 | +exit 0 |
0 commit comments