Help us improve
Share bugs, ideas, or general feedback.
From worktree
Auto-detect repository and worktree names from current directory using /workspace/repos path convention
npx claudepluginhub manifoldlogic/claude-code-plugins --plugin worktreeHow this skill is triggered — by the user, by Claude, or both
Slash command
/worktree:worktree-cwd-auto-detectionThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
This skill documents the pattern for automatically detecting the repository name and worktree name from a user's current working directory in worktree lifecycle scripts. The pattern parses the devcontainer path convention `/workspace/repos/<repo>/<worktree>` and includes critical validation to reject the main worktree (where repo == worktree) and handle deeply nested subdirectories.
Use when starting feature work that needs isolation from current workspace or before executing implementation plans - creates isolated git worktrees with smart directory selection and safety verification
Provides behavioral guidelines to reduce common LLM coding mistakes, focusing on simplicity, surgical changes, assumption surfacing, and verifiable success criteria.
Searches, retrieves, and installs Agent Skills from prompts.chat registry using MCP tools like search_skills and get_skill. Activates for finding skills, browsing catalogs, or extending Claude.
Share bugs, ideas, or general feedback.
This skill documents the pattern for automatically detecting the repository name and worktree name from a user's current working directory in worktree lifecycle scripts. The pattern parses the devcontainer path convention /workspace/repos/<repo>/<worktree> and includes critical validation to reject the main worktree (where repo == worktree) and handle deeply nested subdirectories.
This pattern enables worktree scripts to provide a frictionless user experience: when the user is inside a feature worktree, the script can infer the target of the operation without requiring explicit arguments.
Use this pattern when:
Do not use this pattern for:
Extract path components from PWD:
cwd="$(pwd)"
repos_prefix="/workspace/repos/"
# Check if under /workspace/repos/
if [[ "$cwd" != ${repos_prefix}* ]]; then
return 1 # Not in repos directory
fi
# Strip prefix to get relative path
relative_path="${cwd#${repos_prefix}}"
Parse first two path segments:
# Extract repo (first segment) and worktree (second segment)
repo_segment="$(echo "$relative_path" | cut -d'/' -f1)"
worktree_segment="$(echo "$relative_path" | cut -d'/' -f2)"
# Must have both segments
if [[ -z "$repo_segment" ]] || [[ -z "$worktree_segment" ]]; then
return 1
fi
Check for main worktree (critical validation):
# If repo == worktree, user is in main worktree
if [[ "$repo_segment" == "$worktree_segment" ]]; then
error "You appear to be in the main worktree (/workspace/repos/$repo_segment/$worktree_segment)"
error "Navigate to a feature worktree directory, or specify the worktree name explicitly."
exit 3
fi
Validate both names:
# Use validate_worktree_name() from worktree-common.sh
if ! validate_worktree_name "$repo_segment"; then
return 1
fi
if ! validate_worktree_name "$worktree_segment"; then
return 1
fi
Set detected values:
DETECTED_REPO="$repo_segment"
DETECTED_WORKTREE="$worktree_segment"
return 0
The auto-detection function should be called only when positional arguments are missing:
# After parsing all flags, check if worktree name was provided
if [[ -z "$WORKTREE_NAME" ]]; then
if detect_from_cwd; then
WORKTREE_NAME="$DETECTED_WORKTREE"
REPO="${REPO:-$DETECTED_REPO}" # Use detected repo if --repo not specified
info "Auto-detected from cwd: repo=$REPO worktree=$WORKTREE_NAME"
else
error "Could not auto-detect worktree from current directory: $(pwd)"
error "Provide worktree name explicitly: $0 <worktree-name> --repo <repo>"
show_help
exit 3
fi
fi
Deeply nested paths: Works correctly from any subdirectory depth
/workspace/repos/myproject/PANE-001/src/components/Button.tsxMain worktree detection: Correctly identifies and rejects /workspace/repos/<repo>/<repo>
/workspace/repos/crewchief/crewchief/src/index.tsRepo-level paths: Fails gracefully when user is at repo level
/workspace/repos/crewchiefNon-repos paths: Fails gracefully for paths outside /workspace/repos
/home/user/projects/somethingUser is in a feature worktree subdirectory:
$ cd /workspace/repos/myproject/PANE-001/src
$ merge-worktree.sh
# Output: [INFO] Auto-detected from cwd: repo=myproject worktree=PANE-001
The script extracts:
User is in the main worktree:
$ cd /workspace/repos/crewchief/crewchief
$ merge-worktree.sh
# Output:
# [ERROR] You appear to be in the main worktree (/workspace/repos/crewchief/crewchief)
# [ERROR] Navigate to a feature worktree directory, or specify the worktree name explicitly.
The script detects:
User provides explicit arguments, auto-detection is skipped:
$ cd /anywhere
$ merge-worktree.sh PANE-001 --repo myproject
# Auto-detection is not invoked; explicit args used
User is outside the expected path:
$ cd /tmp
$ merge-worktree.sh
# Output:
# [ERROR] Could not auto-detect worktree from current directory: /tmp
# [ERROR] Provide worktree name explicitly: merge-worktree.sh <worktree-name> --repo <repo>
The detect_from_cwd function returns 1, triggering the error path in argument validation.
/workspace/.devcontainer/scripts/merge-worktree.sh lines 295-345 (detect_from_cwd function)/workspace/_SPECS/claude-code-plugins/tickets/WTMERGE_worktree-merge-skill/planning/architecture.md Section "Component 2: CWD Auto-Detection"/workspace/.devcontainer/scripts/test-merge-worktree.sh Category 2 (CWD Auto-Detection Tests)spawn-worktree.sh, cleanup-worktree.sh (candidates for adopting this pattern)