@@ -605,77 +605,103 @@ export class WorktreeManager {
605605 }
606606
607607 private async getDefaultBranch ( ) : Promise < string > {
608- try {
609- const remoteBranch = await this . runGitCommand (
610- "symbolic-ref refs/remotes/origin/HEAD" ,
611- ) ;
612- return remoteBranch . replace ( "refs/remotes/origin/" , "" ) ;
613- } catch {
614- // Fallback: check if main exists, otherwise use master
615- try {
616- await this . runGitCommand ( "rev-parse --verify main" ) ;
617- return "main" ;
618- } catch {
619- try {
620- await this . runGitCommand ( "rev-parse --verify master" ) ;
621- return "master" ;
622- } catch {
623- throw new Error (
624- "Cannot determine default branch. No main or master branch found." ,
625- ) ;
626- }
627- }
608+ // Try all methods in parallel for speed
609+ const [ symbolicRef , mainExists , masterExists ] = await Promise . allSettled ( [
610+ this . runGitCommand ( "symbolic-ref refs/remotes/origin/HEAD" ) ,
611+ this . runGitCommand ( "rev-parse --verify main" ) ,
612+ this . runGitCommand ( "rev-parse --verify master" ) ,
613+ ] ) ;
614+
615+ // Prefer symbolic ref (most accurate)
616+ if ( symbolicRef . status === "fulfilled" ) {
617+ return symbolicRef . value . replace ( "refs/remotes/origin/" , "" ) ;
618+ }
619+
620+ // Fallback to main if it exists
621+ if ( mainExists . status === "fulfilled" ) {
622+ return "main" ;
623+ }
624+
625+ // Fallback to master if it exists
626+ if ( masterExists . status === "fulfilled" ) {
627+ return "master" ;
628628 }
629+
630+ throw new Error (
631+ "Cannot determine default branch. No main or master branch found." ,
632+ ) ;
629633 }
630634
631635 async createWorktree ( options ?: {
632636 baseBranch ?: string ;
633637 } ) : Promise < WorktreeInfo > {
638+ const totalStart = Date . now ( ) ;
639+
640+ // Run setup tasks in parallel for speed
641+ const setupPromises : Promise < unknown > [ ] = [ ] ;
642+
634643 // Only modify .git/info/exclude when using in-repo storage
635644 if ( ! this . usesExternalPath ( ) ) {
636- await this . ensureArrayDirIgnored ( ) ;
637- }
638-
639- // Ensure the worktree folder exists when using external path
640- if ( this . usesExternalPath ( ) ) {
645+ setupPromises . push ( this . ensureArrayDirIgnored ( ) ) ;
646+ } else {
647+ // Ensure the worktree folder exists when using external path
641648 const folderPath = this . getWorktreeFolderPath ( ) ;
642- await fs . mkdir ( folderPath , { recursive : true } ) ;
649+ setupPromises . push ( fs . mkdir ( folderPath , { recursive : true } ) ) ;
643650 }
644651
645- // Generate unique worktree name
646- const worktreeName = await this . generateUniqueWorktreeName ( ) ;
652+ // Generate unique worktree name (in parallel with above)
653+ const worktreeNamePromise = this . generateUniqueWorktreeName ( ) ;
654+ setupPromises . push ( worktreeNamePromise ) ;
655+
656+ // Get default branch in parallel if not provided
657+ const baseBranchPromise = options ?. baseBranch
658+ ? Promise . resolve ( options . baseBranch )
659+ : this . getDefaultBranch ( ) ;
660+ setupPromises . push ( baseBranchPromise ) ;
661+
662+ // Wait for all setup to complete
663+ await Promise . all ( setupPromises ) ;
664+ const setupTime = Date . now ( ) - totalStart ;
665+
666+ const worktreeName = await worktreeNamePromise ;
667+ const baseBranch = await baseBranchPromise ;
647668 const worktreePath = this . getWorktreePath ( worktreeName ) ;
648669 const branchName = `array/${ worktreeName } ` ;
649- const baseBranch = options ?. baseBranch ?? ( await this . getDefaultBranch ( ) ) ;
650670
651671 this . logger . info ( "Creating worktree" , {
652672 worktreeName,
653673 worktreePath,
654674 branchName,
655675 baseBranch,
656676 external : this . usesExternalPath ( ) ,
677+ setupTimeMs : setupTime ,
657678 } ) ;
658679
659680 // Create the worktree with a new branch
681+ const gitStart = Date . now ( ) ;
660682 if ( this . usesExternalPath ( ) ) {
661683 // Use absolute path for external worktrees
662684 await this . runGitCommand (
663- `worktree add -b "${ branchName } " "${ worktreePath } " "${ baseBranch } "` ,
685+ `worktree add --quiet - b "${ branchName } " "${ worktreePath } " "${ baseBranch } "` ,
664686 ) ;
665687 } else {
666688 // Use relative path from repo root for in-repo worktrees
667689 const relativePath = `${ WORKTREE_FOLDER_NAME } /${ worktreeName } ` ;
668690 await this . runGitCommand (
669- `worktree add -b "${ branchName } " "./${ relativePath } " "${ baseBranch } "` ,
691+ `worktree add --quiet - b "${ branchName } " "./${ relativePath } " "${ baseBranch } "` ,
670692 ) ;
671693 }
694+ const gitTime = Date . now ( ) - gitStart ;
672695
673696 const createdAt = new Date ( ) . toISOString ( ) ;
674697
675698 this . logger . info ( "Worktree created successfully" , {
676699 worktreeName,
677700 worktreePath,
678701 branchName,
702+ setupTimeMs : setupTime ,
703+ gitWorktreeAddMs : gitTime ,
704+ totalMs : Date . now ( ) - totalStart ,
679705 } ) ;
680706
681707 return {
0 commit comments