Created
February 1, 2026 09:50
-
-
Save anonoz/bd5836f079a875dc513286de0406398e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Git post-checkout hook to automatically copy environment files to new worktrees | |
| # This hook runs after git checkout, git switch, and git worktree add | |
| # CONFIGURATION: Modify these lists for your project | |
| # Files to symlink (relative to repo root) | |
| SYMLINK_FILES=( | |
| ".env" | |
| ".env.test" | |
| ) | |
| # Files/directories to copy (relative to repo root) | |
| COPY_FILES=( | |
| "tmp/tunnel" | |
| "tmp/cloudflared-config.yml" | |
| ) | |
| # ============================================================================ | |
| prev_head="$1" | |
| new_head="$2" | |
| branch_checkout="$3" | |
| # Check if this is a worktree creation (previous HEAD is null) | |
| if [[ "$prev_head" == "0000000000000000000000000000000000000000" ]] && [[ "$branch_checkout" == "1" ]]; then | |
| echo "π§ New worktree detected - setting up environment files..." | |
| # Get the main repository path | |
| main_repo_path="$(git rev-parse --git-common-dir)/.." | |
| main_repo_path="$(cd "$main_repo_path" && pwd)" | |
| # Get current worktree path | |
| current_path="$(pwd)" | |
| echo "π Copying from: $main_repo_path" | |
| echo "π Copying to: $current_path" | |
| # Create necessary directories from COPY_FILES | |
| while IFS= read -r dir; do | |
| if [[ ! -d "$current_path/$dir" ]]; then | |
| mkdir -p "$current_path/$dir" | |
| echo "π Created $dir" | |
| fi | |
| done < <(printf '%s\n' "${COPY_FILES[@]}" | xargs -I {} dirname {} | sort -u) | |
| # Symlink files | |
| for file in "${SYMLINK_FILES[@]}"; do | |
| if [[ -f "$main_repo_path/$file" ]]; then | |
| ln -sf "$main_repo_path/$file" "$current_path/$file" | |
| echo "β Symlinked $file" | |
| else | |
| echo "β οΈ $file not found in main repository" | |
| fi | |
| done | |
| # Copy files | |
| for file in "${COPY_FILES[@]}"; do | |
| source_file="$main_repo_path/$file" | |
| target_file="$current_path/$file" | |
| if [[ -f "$source_file" ]]; then | |
| mkdir -p "$(dirname "$target_file")" | |
| cp "$source_file" "$target_file" | |
| echo "β Copied $file" | |
| else | |
| echo "β οΈ $file not found in main repository" | |
| fi | |
| done | |
| echo "π Environment setup complete for new worktree!" | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment