Skip to content

Commit c4a60a8

Browse files
Merge pull request #3 from LucaTools/post-checkout-git-hook
Add `post-checkout` git hook
2 parents ed16511 + b6ff221 commit c4a60a8

2 files changed

Lines changed: 232 additions & 0 deletions

File tree

install.sh

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ REPOSITORY_URL="https://github.com/LucaTools/Luca"
1818
TOOL_DIR="$HOME/$TOOL_FOLDER"
1919
SHELL_HOOK_SCRIPT_PATH="$TOOL_DIR/shell_hook.sh"
2020
SHELL_HOOK_SCRIPT_URL="https://raw.githubusercontent.com/LucaTools/LucaScripts/HEAD/shell_hook.sh"
21+
POST_CHECKOUT_HOOK_PATH="$TOOL_DIR/post-checkout"
22+
POST_CHECKOUT_HOOK_URL="https://raw.githubusercontent.com/LucaTools/LucaScripts/HEAD/post-checkout"
2123

2224
# =============================================================================
2325
# TOOL VERSION DETECTION
@@ -252,6 +254,77 @@ else
252254
fi
253255
fi
254256

257+
# Download the post-checkout hook script to ~/.luca/ for later use
258+
echo "📥 Downloading post-checkout hook script..."
259+
curl -LSsf --output "$POST_CHECKOUT_HOOK_PATH" "$POST_CHECKOUT_HOOK_URL"
260+
261+
POST_CHECKOUT_DOWNLOAD_SUCCESS=$?
262+
if [ $POST_CHECKOUT_DOWNLOAD_SUCCESS -ne 0 ]; then
263+
echo "⚠️ WARNING: Could not download post-checkout hook from $POST_CHECKOUT_HOOK_URL"
264+
else
265+
echo "✅ Post-checkout hook downloaded to $POST_CHECKOUT_HOOK_PATH"
266+
chmod +x "$POST_CHECKOUT_HOOK_PATH"
267+
fi
268+
269+
# =============================================================================
270+
# GIT POST-CHECKOUT HOOK SETUP (OPTIONAL)
271+
# =============================================================================
272+
273+
# Track whether git hook was installed (used in summary)
274+
GIT_HOOK_INSTALLED=false
275+
GIT_HOOK_PATH=""
276+
277+
# Install git hook if we're in a git repository
278+
install_git_hook() {
279+
local git_root
280+
git_root=$(git rev-parse --show-toplevel 2>/dev/null)
281+
282+
if [ -z "$git_root" ]; then
283+
# Not in a git repository, skip
284+
return 0
285+
fi
286+
287+
local hooks_dir="$git_root/.git/hooks"
288+
local hook_file="$hooks_dir/post-checkout"
289+
290+
# Check if hooks directory exists
291+
if [ ! -d "$hooks_dir" ]; then
292+
echo "⚠️ Git hooks directory not found, skipping git hook installation"
293+
return 0
294+
fi
295+
296+
# Check if hook already exists
297+
if [ -f "$hook_file" ]; then
298+
# Check if it's our hook by looking for the Luca identifier
299+
if grep -q "LUCA POST-CHECKOUT GIT HOOK" "$hook_file" 2>/dev/null; then
300+
echo "ℹ️ Luca post-checkout hook already installed"
301+
GIT_HOOK_INSTALLED=true
302+
GIT_HOOK_PATH="$hook_file"
303+
return 0
304+
else
305+
echo "⚠️ A post-checkout hook already exists at $hook_file"
306+
echo " To install Luca's hook, please manually merge or replace it."
307+
echo " The hook script can be found at: $POST_CHECKOUT_HOOK_PATH"
308+
return 0
309+
fi
310+
fi
311+
312+
# Copy from local ~/.luca/post-checkout (already downloaded earlier)
313+
echo "📥 Installing git post-checkout hook..."
314+
if [ -f "$POST_CHECKOUT_HOOK_PATH" ]; then
315+
cp "$POST_CHECKOUT_HOOK_PATH" "$hook_file"
316+
chmod +x "$hook_file"
317+
echo "✅ Git post-checkout hook installed at $hook_file"
318+
GIT_HOOK_INSTALLED=true
319+
GIT_HOOK_PATH="$hook_file"
320+
else
321+
echo "⚠️ Could not install post-checkout hook (source not found)"
322+
fi
323+
}
324+
325+
# Try to install the git hook (optional, won't fail installation if it fails)
326+
install_git_hook
327+
255328
# =============================================================================
256329
# INSTALLATION COMPLETE
257330
# =============================================================================
@@ -263,6 +336,9 @@ echo "📋 Installation Summary:"
263336
echo " • Executable: $EXECUTABLE_FILE"
264337
echo " • Version: $REQUIRED_EXECUTABLE_VERSION"
265338
echo " • Shell Hook: $SHELL_HOOK_SCRIPT_PATH"
339+
if [ "$GIT_HOOK_INSTALLED" = "true" ]; then
340+
echo " • Post-checkout Hook: $POST_CHECKOUT_HOOK_PATH"
341+
fi
266342
echo ""
267343
echo "💡 To start using Luca:"
268344

post-checkout

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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

Comments
 (0)