-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcreate.go
More file actions
90 lines (76 loc) · 2.12 KB
/
create.go
File metadata and controls
90 lines (76 loc) · 2.12 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
package cmd
import (
"fmt"
"os"
"os/exec"
"github.com/spf13/cobra"
)
var createCmd = &cobra.Command{
Use: "create <branch> [base-branch]",
Short: "Create new branch in worktree (default: main/master)",
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) error {
branch := args[0]
base := getDefaultBase()
if len(args) > 1 {
base = args[1]
}
// Resolve case-insensitive match against remote branches
resolved := resolveRemoteBranchCase(branch)
if resolved != branch {
fmt.Fprintf(os.Stderr, "Note: using remote branch name %q (you typed %q)\n", resolved, branch)
branch = resolved
}
info, err := getRepoInfo()
if err != nil {
return err
}
// Check if worktree already exists
if existingPath, exists := worktreeExists(branch); exists {
if isJSONOutput() {
return emitJSONSuccess(cmd, map[string]any{
"status": "exists",
"branch": branch,
"base": base,
"path": existingPath,
"navigate_to": existingPath,
})
}
fmt.Printf("✓ Worktree already exists: %s\n", existingPath)
printCDMarker(existingPath)
return nil
}
path, err := buildWorktreePath(info, branch)
if err != nil {
return err
}
hookEnv := buildHookEnv(info, branch, path)
// Run pre-create hooks
if err := runHooks("pre_create", getHooks("pre_create"), hookEnv); err != nil {
return fmt.Errorf("pre-create hook failed: %w", err)
}
// Create new branch and worktree
gitCmd := exec.Command("git", "worktree", "add", path, "-b", branch, base)
if !isJSONOutput() {
gitCmd.Stdout = os.Stdout
gitCmd.Stderr = os.Stderr
}
if err := gitCmd.Run(); err != nil {
return fmt.Errorf("failed to create worktree: %w", err)
}
// Run post-create hooks (warn only)
_ = runHooks("post_create", getHooks("post_create"), hookEnv)
if isJSONOutput() {
return emitJSONSuccess(cmd, map[string]any{
"status": "created",
"branch": branch,
"base": base,
"path": path,
"navigate_to": path,
})
}
fmt.Printf("✓ Worktree created at: %s\n", path)
printCDMarker(path)
return nil
},
}