@@ -14,29 +14,52 @@ import (
14
14
15
15
// Setup creates a new worktree for the session
16
16
func (g * GitWorktree ) Setup () error {
17
- // Check if branch exists first
18
- repo , err := git . PlainOpen ( g . repoPath )
17
+ // Ensure worktrees directory exists early (can be done in parallel with branch check)
18
+ worktreesDir , err := getWorktreeDirectory ( )
19
19
if err != nil {
20
- return fmt .Errorf ("failed to open repository : %w" , err )
20
+ return fmt .Errorf ("failed to get worktree directory : %w" , err )
21
21
}
22
22
23
- branchRef := plumbing .NewBranchReferenceName (g .branchName )
24
- if _ , err := repo .Reference (branchRef , false ); err == nil {
25
- // Branch exists, use SetupFromExistingBranch
26
- return g .SetupFromExistingBranch ()
23
+ // Create directory and check branch existence in parallel
24
+ errChan := make (chan error , 2 )
25
+ var branchExists bool
26
+
27
+ // Goroutine for directory creation
28
+ go func () {
29
+ errChan <- os .MkdirAll (worktreesDir , 0755 )
30
+ }()
31
+
32
+ // Goroutine for branch check
33
+ go func () {
34
+ repo , err := git .PlainOpen (g .repoPath )
35
+ if err != nil {
36
+ errChan <- fmt .Errorf ("failed to open repository: %w" , err )
37
+ return
38
+ }
39
+
40
+ branchRef := plumbing .NewBranchReferenceName (g .branchName )
41
+ if _ , err := repo .Reference (branchRef , false ); err == nil {
42
+ branchExists = true
43
+ }
44
+ errChan <- nil
45
+ }()
46
+
47
+ // Wait for both operations
48
+ for i := 0 ; i < 2 ; i ++ {
49
+ if err := <- errChan ; err != nil {
50
+ return err
51
+ }
27
52
}
28
53
29
- // Branch doesn't exist, create new worktree from HEAD
30
- return g .SetupNewWorktree ()
54
+ if branchExists {
55
+ return g .setupFromExistingBranch ()
56
+ }
57
+ return g .setupNewWorktree ()
31
58
}
32
59
33
- // SetupFromExistingBranch creates a worktree from an existing branch
34
- func (g * GitWorktree ) SetupFromExistingBranch () error {
35
- // Ensure worktrees directory exists
36
- worktreesDir := filepath .Join (g .repoPath , "worktrees" )
37
- if err := os .MkdirAll (worktreesDir , 0755 ); err != nil {
38
- return fmt .Errorf ("failed to create worktrees directory: %w" , err )
39
- }
60
+ // setupFromExistingBranch creates a worktree from an existing branch
61
+ func (g * GitWorktree ) setupFromExistingBranch () error {
62
+ // Directory already created in Setup(), skip duplicate creation
40
63
41
64
// Clean up any existing worktree first
42
65
_ , _ = g .runGitCommand (g .repoPath , "worktree" , "remove" , "-f" , g .worktreePath ) // Ignore error if worktree doesn't exist
@@ -49,8 +72,8 @@ func (g *GitWorktree) SetupFromExistingBranch() error {
49
72
return nil
50
73
}
51
74
52
- // SetupNewWorktree creates a new worktree from HEAD
53
- func (g * GitWorktree ) SetupNewWorktree () error {
75
+ // setupNewWorktree creates a new worktree from HEAD
76
+ func (g * GitWorktree ) setupNewWorktree () error {
54
77
// Ensure worktrees directory exists
55
78
worktreesDir := filepath .Join (g .repoPath , "worktrees" )
56
79
if err := os .MkdirAll (worktreesDir , 0755 ); err != nil {
0 commit comments