-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathremove.go
More file actions
120 lines (101 loc) · 2.93 KB
/
Copy pathremove.go
File metadata and controls
120 lines (101 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package cmd
import (
"fmt"
"os"
"os/exec"
"strings"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"github.com/timvw/wt/internal/fuzzy"
)
var removeForce bool
var removeCmd = &cobra.Command{
Use: "remove [branch]",
Aliases: []string{"rm"},
Short: "Remove a worktree",
Args: cobra.RangeArgs(0, 1),
RunE: func(cmd *cobra.Command, args []string) error {
var branch string
jsonMode := isJSONOutput()
// Interactive selection if no branch provided
if len(args) == 0 {
if jsonMode {
return fmt.Errorf("wt remove with --format json requires an explicit branch argument")
}
branches, err := getExistingWorktreeBranches()
if err != nil {
return fmt.Errorf("failed to get worktrees: %w", err)
}
if len(branches) == 0 {
return fmt.Errorf("no worktrees to remove")
}
prompt := promptui.Select{
Label: "Select worktree to remove",
Items: branches,
Searcher: fuzzy.Searcher(branches),
StartInSearchMode: true,
}
_, result, err := prompt.Run()
if err != nil {
return fmt.Errorf("selection cancelled")
}
branch = result
} else {
branch = args[0]
}
existingPath, exists := worktreeExists(branch)
if !exists {
return fmt.Errorf("no worktree found for branch: %s", branch)
}
// Build hook env for remove hooks
info, _ := getRepoInfo()
hookEnv := buildHookEnv(info, branch, existingPath)
// Run pre-remove hooks
if err := runHooks("pre_remove", getHooks("pre_remove"), hookEnv); err != nil {
return fmt.Errorf("pre-remove hook failed: %w", err)
}
// Check if we're currently in the worktree being removed
cwd, err := os.Getwd()
inRemovedWorktree := err == nil && strings.HasPrefix(cwd, existingPath)
// Find the main worktree path (for cd after removal)
var mainWorktreePath string
if inRemovedWorktree {
entries, err := getWorktreeListPorcelain()
if err == nil && len(entries) > 0 {
mainWorktreePath = entries[0].Path
}
}
gitArgs := []string{"worktree", "remove"}
if removeForce {
gitArgs = append(gitArgs, "--force")
}
gitArgs = append(gitArgs, existingPath)
gitCmd := exec.Command("git", gitArgs...)
if !jsonMode {
gitCmd.Stdout = os.Stdout
gitCmd.Stderr = os.Stderr
}
if err := gitCmd.Run(); err != nil {
return fmt.Errorf("failed to remove worktree: %w", err)
}
if err := cleanupWorktreePath(existingPath); err != nil {
return err
}
// Run post-remove hooks (warn only)
_ = runHooks("post_remove", getHooks("post_remove"), hookEnv)
if jsonMode {
return emitJSONSuccess(cmd, map[string]any{
"status": "removed",
"branch": branch,
"path": existingPath,
"navigate_to": mainWorktreePath,
})
}
fmt.Printf("✓ Removed worktree: %s\n", existingPath)
// If we were in the removed worktree, navigate to main
if inRemovedWorktree && mainWorktreePath != "" {
printCDMarker(mainWorktreePath)
}
return nil
},
}