@@ -152,6 +152,26 @@ func branchExists(branch string) bool {
152152 return cmd .Run () == nil
153153}
154154
155+ func ensureWorktreePath (repo , branch string ) (string , error ) {
156+ targetRoot := filepath .Join (worktreeRoot , repo )
157+
158+ info , err := os .Stat (targetRoot )
159+ switch {
160+ case err == nil :
161+ if ! info .IsDir () {
162+ return "" , fmt .Errorf ("WORKTREE_ROOT path %s is not a directory" , targetRoot )
163+ }
164+ case os .IsNotExist (err ):
165+ if err := os .MkdirAll (targetRoot , 0o755 ); err != nil {
166+ return "" , fmt .Errorf ("failed to create WORKTREE_ROOT directory %s: %w" , targetRoot , err )
167+ }
168+ default :
169+ return "" , fmt .Errorf ("failed to access WORKTREE_ROOT directory %s: %w" , targetRoot , err )
170+ }
171+
172+ return filepath .Join (targetRoot , branch ), nil
173+ }
174+
155175func printCDMarker (path string ) {
156176 fmt .Printf ("TREE_ME_CD:%s\n " , path )
157177}
@@ -309,8 +329,6 @@ var checkoutCmd = &cobra.Command{
309329 return err
310330 }
311331
312- path := filepath .Join (worktreeRoot , repo , branch )
313-
314332 // Check if worktree already exists
315333 if existingPath , exists := worktreeExists (branch ); exists {
316334 fmt .Printf ("✓ Worktree already exists: %s\n " , existingPath )
@@ -323,6 +341,11 @@ var checkoutCmd = &cobra.Command{
323341 return fmt .Errorf ("branch '%s' does not exist\n Use 'wt create %s' to create a new branch" , branch , branch )
324342 }
325343
344+ path , err := ensureWorktreePath (repo , branch )
345+ if err != nil {
346+ return err
347+ }
348+
326349 // Create worktree
327350 gitCmd := exec .Command ("git" , "worktree" , "add" , path , branch )
328351 gitCmd .Stdout = os .Stdout
@@ -353,15 +376,18 @@ var createCmd = &cobra.Command{
353376 return err
354377 }
355378
356- path := filepath .Join (worktreeRoot , repo , branch )
357-
358379 // Check if worktree already exists
359380 if existingPath , exists := worktreeExists (branch ); exists {
360381 fmt .Printf ("✓ Worktree already exists: %s\n " , existingPath )
361382 printCDMarker (existingPath )
362383 return nil
363384 }
364385
386+ path , err := ensureWorktreePath (repo , branch )
387+ if err != nil {
388+ return err
389+ }
390+
365391 // Create new branch and worktree
366392 gitCmd := exec .Command ("git" , "worktree" , "add" , path , "-b" , branch , base )
367393 gitCmd .Stdout = os .Stdout
@@ -493,7 +519,6 @@ func checkoutPROrMR(input string, remoteType RemoteType) error {
493519 }
494520
495521 branch := fmt .Sprintf ("%s-%s" , prefix , prNumber )
496- path := filepath .Join (worktreeRoot , repo , branch )
497522
498523 // Check if worktree already exists
499524 if existingPath , exists := worktreeExists (branch ); exists {
@@ -502,6 +527,11 @@ func checkoutPROrMR(input string, remoteType RemoteType) error {
502527 return nil
503528 }
504529
530+ path , err := ensureWorktreePath (repo , branch )
531+ if err != nil {
532+ return err
533+ }
534+
505535 // Fetch the PR/MR
506536 fetchCmd := exec .Command ("git" , "fetch" , "origin" , fmt .Sprintf ("%s:%s" , refSpec , branch ))
507537 fetchCmd .Stderr = os .Stderr
0 commit comments