-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove.go
More file actions
43 lines (35 loc) · 1.13 KB
/
remove.go
File metadata and controls
43 lines (35 loc) · 1.13 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
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
)
func remove(name string) error {
wm, err := NewWorktreeManager()
if err != nil {
return err
}
worktreePath := wm.WorktreePath(name)
// Check if we're currently inside the worktree being removed
cwd, err := getwdFn()
insideWorktree := err == nil && (cwd == worktreePath || strings.HasPrefix(cwd, worktreePath+string(filepath.Separator)))
// Remove worktree
fmt.Fprintf(os.Stderr, "Removing worktree %s/%s\n", WorktreesDir, name)
if err := gitCmd(wm.Root(), "worktree", "remove", worktreePath); err != nil {
return fmt.Errorf("failed to remove worktree: %w", err)
}
// Delete branch
fmt.Fprintf(os.Stderr, "Deleting branch %s\n", name)
if err := gitCmd(wm.Root(), "branch", "-D", name); err != nil {
return fmt.Errorf("failed to delete branch: %w", err)
}
fmt.Fprintln(os.Stderr, "Done! Worktree and branch removed")
// Output path to stdout for shell wrapper to cd into
// If we were inside the worktree, output root so shell can cd there
// Otherwise, output empty line (no directory change needed)
if insideWorktree {
fmt.Println(wm.Root())
}
return nil
}