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
17 changes: 17 additions & 0 deletions internal/commands/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"fmt"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -189,6 +190,16 @@ func runCleanup(cmd *cobra.Command, args []string) error {
}
}

// Check if user is in any of the worktrees being deleted
cwd, _ := os.Getwd()
inDeletedWorktree := false
for _, c := range candidates {
if strings.HasPrefix(cwd, c.path) {
inDeletedWorktree = true
break
}
}

// Delete each candidate
var deleted int
for _, c := range candidates {
Expand Down Expand Up @@ -234,5 +245,11 @@ func runCleanup(cmd *cobra.Command, args []string) error {
}

cmd.Printf("Cleaned up %d worktree(s)\n", deleted)

// If user was in a deleted worktree, output repo root for shell wrapper to cd
if inDeletedWorktree && deleted > 0 {
_, _ = fmt.Fprintln(cmd.OutOrStdout(), repoRoot)
}

return nil
}
13 changes: 8 additions & 5 deletions internal/commands/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,9 @@ func runDelete(cmd *cobra.Command, args []string) error {
}
}

// Warn if user is in the worktree being deleted
// Check if user is in the worktree being deleted
cwd, _ := os.Getwd()
if strings.HasPrefix(cwd, worktreePath) {
cmd.Println("Warning: You are currently in this worktree.")
cmd.Println("After deletion, run 'wt exit' or 'cd' to another directory.")
}
inDeletedWorktree := strings.HasPrefix(cwd, worktreePath)

// Run pre-delete hooks
if err := hooks.RunPreDelete(cfg, env); err != nil {
Expand Down Expand Up @@ -158,6 +155,12 @@ func runDelete(cmd *cobra.Command, args []string) error {
}

cmd.Printf("Worktree %q deleted successfully\n", name)

// If user was in the deleted worktree, output repo root for shell wrapper to cd
if inDeletedWorktree {
_, _ = fmt.Fprintln(cmd.OutOrStdout(), repoRoot)
}

return nil
}

Expand Down
15 changes: 8 additions & 7 deletions internal/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,20 +159,21 @@ wt() {

# Commands that need cd handling
case "$1" in
create)
create|delete|cleanup)
local output
output=$(command wt "$@" 2>&1)
local exit_code=$?

if [[ $exit_code -eq 0 ]]; then
# Print all but last line
echo "$output" | sed '$d'
# cd to path on last line
# cd to path on last line if it's a directory
local target=$(echo "$output" | tail -1)
if [[ -d "$target" ]]; then
cd "$target"
else
echo "$output"
# Last line wasn't a directory, print it too
echo "$target"
fi
else
echo "$output"
Expand Down Expand Up @@ -347,7 +348,7 @@ wt() {
fi

case "$1" in
create)
create|delete|cleanup)
local output
output=$(command wt "$@" 2>&1)
local exit_code=$?
Expand All @@ -358,7 +359,7 @@ wt() {
if [[ -d "$target" ]]; then
cd "$target"
else
echo "$output"
echo "$target"
fi
else
echo "$output"
Expand Down Expand Up @@ -497,7 +498,7 @@ function wt
end

switch $argv[1]
case create
case create delete cleanup
set -l output (command wt $argv 2>&1)
set -l exit_code $status

Expand All @@ -507,7 +508,7 @@ function wt
if test -d "$target"
cd "$target"
else
echo "$output"
echo "$target"
end
else
echo "$output"
Expand Down
Loading