-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.go
More file actions
57 lines (48 loc) · 1.54 KB
/
create.go
File metadata and controls
57 lines (48 loc) · 1.54 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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
)
func create(name, hookPath string) error {
wm, err := NewWorktreeManager()
if err != nil {
return err
}
if err := wm.ValidateWorktreesDir(); err != nil {
return err
}
worktreePath := wm.WorktreePath(name)
// Create worktree with new branch
fmt.Fprintf(os.Stderr, "Creating worktree at %s/%s with branch %s\n", WorktreesDir, name, name)
if err := gitCmd(wm.Root(), "worktree", "add", worktreePath, "-b", name); err != nil {
return fmt.Errorf("failed to create worktree: %w", err)
}
// Create symlink to .claude/ directory if it exists
if wm.ClaudeDirExists() {
fmt.Fprintf(os.Stderr, "Creating symlink to %s/ directory...\n", ClaudeDir)
dstClaudeDir := filepath.Join(worktreePath, ClaudeDir)
if err := os.Symlink(wm.ClaudePath(), dstClaudeDir); err != nil {
return fmt.Errorf("failed to create %s/ symlink: %w", ClaudeDir, err)
}
}
// Run hook if it exists
if wm.HookExists(hookPath) {
fmt.Fprintf(os.Stderr, "Running hook: %s\n", hookPath)
if err := runHook(wm.HookPath(hookPath), worktreePath); err != nil {
return fmt.Errorf("hook failed: %w", err)
}
}
fmt.Fprintf(os.Stderr, "Done! Worktree ready at %s/%s\n", WorktreesDir, name)
// Output path to stdout for shell wrapper to cd into
fmt.Println(worktreePath)
return nil
}
func runHook(hookPath, worktreePath string) error {
cmd := exec.Command(hookPath)
cmd.Dir = worktreePath
cmd.Stdout = os.Stderr // Redirect to stderr to keep stdout clean for worktree path
cmd.Stderr = os.Stderr
return cmd.Run()
}