Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions plugins/compound-engineering/skills/git-worktree/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ This skill provides a unified interface for managing Git worktrees across your d
- **Interactive confirmations** at each step
- **Automatic .gitignore management** for worktree directory
- **Automatic .env file copying** from main repo to new worktrees
- **Automatic dev tool trusting** for mise and direnv configs (safe: only unchanged configs)

## CRITICAL: Always Use the Manager Script

**NEVER call `git worktree add` directly.** Always use the `worktree-manager.sh` script.

The script handles critical setup that raw git commands don't:
1. Copies `.env`, `.env.local`, `.env.test`, etc. from main repo
2. Ensures `.worktrees` is in `.gitignore`
3. Creates consistent directory structure
2. Trusts dev tool configs (mise, direnv) so hooks and scripts work immediately
3. Ensures `.worktrees` is in `.gitignore`
4. Creates consistent directory structure

```bash
# ✅ CORRECT - Always use the script
Expand Down Expand Up @@ -95,7 +97,8 @@ bash ${CLAUDE_PLUGIN_ROOT}/skills/git-worktree/scripts/worktree-manager.sh creat
2. Updates the base branch from remote
3. Creates new worktree and branch
4. **Copies all .env files from main repo** (.env, .env.local, .env.test, etc.)
5. Shows path for cd-ing to the worktree
5. **Trusts dev tool configs** (mise, direnv) if unchanged from base branch; flags modified configs for manual review
6. Shows path for cd-ing to the worktree

### `list` or `ls`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,93 @@ copy_env_files() {
echo -e " ${GREEN}✓ Copied $copied environment file(s)${NC}"
}

# Trust development tool configs in a new worktree.
# Worktrees get a new filesystem path that tools like mise and direnv
# have never seen. Without trusting, these tools block with interactive
# prompts or refuse to load configs, which breaks hooks and scripts.
#
# Safety: only auto-trusts configs that are unchanged from the base branch.
# Modified configs (e.g., from a PR) are flagged for manual review.
trust_dev_tools() {
local worktree_path="$1"
local from_branch="$2"
local trusted=0
local attempted=0
local skipped=()

# mise: trust this directory if a mise config file is present
if command -v mise &>/dev/null; then
for f in .mise.toml mise.toml .tool-versions; do
if [[ -f "$worktree_path/$f" ]]; then
if _config_unchanged "$f" "$from_branch" "$worktree_path"; then
Comment thread
NimbleEngineer21 marked this conversation as resolved.
Outdated
attempted=$((attempted + 1))
if (cd "$worktree_path" && mise trust --quiet); then
Comment thread
NimbleEngineer21 marked this conversation as resolved.
Outdated
trusted=$((trusted + 1))
else
echo -e " ${YELLOW}Warning: 'mise trust' failed -- run manually in $worktree_path${NC}"
fi
else
skipped+=("$f (mise)")
fi
break
fi
done
fi

# direnv: allow .envrc
if command -v direnv &>/dev/null; then
if [[ -f "$worktree_path/.envrc" ]]; then
if _config_unchanged ".envrc" "$from_branch" "$worktree_path"; then
attempted=$((attempted + 1))
if (cd "$worktree_path" && direnv allow); then
trusted=$((trusted + 1))
else
echo -e " ${YELLOW}Warning: 'direnv allow' failed -- run manually in $worktree_path${NC}"
fi
else
skipped+=(".envrc (direnv)")
fi
fi
fi

if [[ $trusted -gt 0 ]]; then
echo -e " ${GREEN}✓ Trusted $trusted dev tool config(s)${NC}"
elif [[ $attempted -gt 0 ]]; then
echo -e " ${YELLOW}Warning: dev tool trust was attempted but all $attempted config(s) failed${NC}"
echo -e " ${YELLOW}Run 'mise trust' / 'direnv allow' manually in $worktree_path${NC}"
fi

if [[ ${#skipped[@]} -gt 0 ]]; then
echo -e " ${YELLOW}Skipped auto-trust for modified config(s):${NC}"
for item in "${skipped[@]}"; do
echo -e " - $item"
done
echo -e " ${BLUE}Review the diff, then trust manually: cd $worktree_path && mise trust && direnv allow${NC}"
fi
}

# Check if a config file is unchanged from the base branch.
# Returns 0 (true) if the file is identical to the base branch version.
# Returns 1 (false) if the file was added or modified by this branch.
_config_unchanged() {
local file="$1"
local base_branch="$2"
local wt_path="$3"

# If file doesn't exist in base branch, it was added by this branch -- not safe
if ! git show "$base_branch:$file" &>/dev/null; then
return 1
fi

# Compare base branch version to worktree version
local base_hash
base_hash=$(git show "$base_branch:$file" | git hash-object --stdin)
local worktree_hash
worktree_hash=$(git hash-object "$wt_path/$file")

[[ "$base_hash" == "$worktree_hash" ]]
}

# Create a new worktree
create_worktree() {
local branch_name="$1"
Expand Down Expand Up @@ -107,6 +194,10 @@ create_worktree() {
# Copy environment files
copy_env_files "$worktree_path"

# Trust dev tool configs (mise, direnv) so hooks and scripts work immediately
# Only auto-trusts configs unchanged from the base branch
trust_dev_tools "$worktree_path" "$from_branch"

echo -e "${GREEN}✓ Worktree created successfully!${NC}"
echo ""
echo "To switch to this worktree:"
Expand Down Expand Up @@ -321,6 +412,13 @@ Environment Files:
- Creates .backup files if destination already exists
- Use 'copy-env' to refresh env files after main repo changes

Dev Tool Trust:
- Trusts mise config (.mise.toml, mise.toml, .tool-versions) and direnv (.envrc)
- Only auto-trusts configs unchanged from the base branch
- Modified configs are flagged for manual review (safety for PR reviews)
- Only runs if the tool is installed and config exists
- Prevents hooks/scripts from hanging on interactive trust prompts

Examples:
worktree-manager.sh create feature-login
worktree-manager.sh create feature-auth develop
Expand Down