Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 12 additions & 7 deletions src/wd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ list_worktrees() {
unset _lw_root
}

sanitize_branch() {
echo "$1" | tr '/' '-'
}

write_relative_paths() {
_wrp_root="$1"
_wrp_name="$2"
Expand Down Expand Up @@ -151,20 +155,21 @@ cmd_clone() {
fi

# create worktree
_wt_name=$(sanitize_branch "$_default_branch")
if $_branch_exists; then
git -C "$_project_dir" worktree add "$_default_branch" "$_default_branch"
git -C "$_project_dir" worktree add "$_wt_name" "$_default_branch"
else
git -C "$_project_dir" worktree add --orphan -b "$_default_branch" "$_default_branch"
git -C "$_project_dir" worktree add --orphan -b "$_default_branch" "$_wt_name"
fi
write_relative_paths "$_project_dir" "$_default_branch"
write_relative_paths "$_project_dir" "$_wt_name"

# symlink .devcontainer if present
if [ -d "$_project_dir/$_default_branch/.devcontainer" ]; then
ln -s "$_default_branch/.devcontainer" "$_project_dir/.devcontainer"
if [ -d "$_project_dir/$_wt_name/.devcontainer" ]; then
ln -s "$_wt_name/.devcontainer" "$_project_dir/.devcontainer"
fi

echo "$_project_dir"
unset _branch _url _parsed _project_dir _head_branch _default_branch _branch_exists
unset _branch _url _parsed _project_dir _head_branch _default_branch _branch_exists _wt_name
}

# --- cmd_list ---
Expand Down Expand Up @@ -255,7 +260,7 @@ cmd_add() {
usage
exit 1
}
_wt_dir="wt-$_branch"
_wt_dir="wt-$(sanitize_branch "$_branch")"
[ ! -d "$_project_dir/$_wt_dir" ] || die "worktree '$_branch' already exists"
if $_create; then
git -C "$_project_dir" worktree add "$_wt_dir" -b "$_branch"
Expand Down
9 changes: 9 additions & 0 deletions tests/test_integration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,15 @@ it "creates new branch with -b and wt- prefix"
_result=$(cmd_add -b newbranch 2>&3)
if [ -d "$_proj/wt-newbranch" ]; then _pass; else _fail "worktree not found"; fi

it "handles branch name with slashes"
git -C "$_source" checkout -b features/add-wd 2>&3 >&3
echo "slash" >"$_source/slash.txt"
git -C "$_source" add -A 2>&3 >&3
git -C "$_source" commit -m "slash branch" 2>&3 >&3
git -C "$_proj" fetch origin 2>&3 >&3
_result=$(cmd_add features/add-wd 2>&3)
if [ -d "$_proj/wt-features-add-wd" ]; then _pass; else _fail "worktree not found"; fi

it "adds PR worktree with pr- prefix"
_result=$(cmd_add --pr 12 2>&3)
if [ -d "$_proj/pr-12" ]; then _pass; else _fail "pr-12 directory not found"; fi
Expand Down