@@ -152,6 +152,30 @@ function statusUpstreamRefreshFailureCooldown(consecutiveFailures: number): Dura
152152 return Duration . min ( Duration . millis ( cooldownMs ) , STATUS_UPSTREAM_REFRESH_FAILURE_MAX_COOLDOWN ) ;
153153}
154154
155+ // PR checkouts keep the PR's branch name, so without a cap the folder name can
156+ // be any length. On Windows every character comes out of the 260-character
157+ // budget that tools without long-path support (and git's own `.git/worktrees`
158+ // bookkeeping, even with core.longpaths) have for files inside the worktree.
159+ // Long names keep a readable prefix and a hash of the full branch.
160+ const MAX_WORKTREE_DIRECTORY_NAME_LENGTH = 40 ;
161+
162+ function worktreeDirectoryName ( branch : string ) : string {
163+ const name = branch . replace ( / \/ / g, "-" ) ;
164+ if ( name . length <= MAX_WORKTREE_DIRECTORY_NAME_LENGTH ) return name ;
165+ let hash = 0x811c9dc5 ;
166+ for ( let index = 0 ; index < branch . length ; index ++ ) {
167+ hash = Math . imul ( hash ^ branch . charCodeAt ( index ) , 0x01000193 ) ;
168+ }
169+ const suffix = ( hash >>> 0 ) . toString ( 16 ) . padStart ( 8 , "0" ) ;
170+ const prefix = name
171+ . slice ( 0 , MAX_WORKTREE_DIRECTORY_NAME_LENGTH - suffix . length - 1 )
172+ // A cut emoji leaves a lone surrogate, which git lists back as U+FFFD, so
173+ // T3's stored path would stop matching git's.
174+ . replace ( / [ \uD800 - \uDBFF ] $ / , "" )
175+ . replace ( / [ . - ] + $ / , "" ) ;
176+ return `${ prefix } -${ suffix } ` ;
177+ }
178+
155179class GitRefsSnapshotCacheKey extends Data . Class < {
156180 gitCommonDir : string ;
157181 epoch : number ;
@@ -3679,7 +3703,6 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
36793703 yield * refuseOptionLikeRefName ( "GitVcsDriver.createWorktree" , input . cwd , input . refName ) ;
36803704 yield * refuseOptionLikeRefName ( "GitVcsDriver.createWorktree" , input . cwd , input . newRefName ) ;
36813705 const targetBranch = input . newRefName ?? input . refName ;
3682- const sanitizedBranch = targetBranch . replace ( / \/ / g, "-" ) ;
36833706 // Resolve sub/.. before taking the basename.
36843707 const repoName = path . basename ( path . resolve ( input . cwd ) ) ;
36853708 let worktreePath = input . path ;
@@ -3698,7 +3721,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
36983721 detail : `The worktree location "${ options ?. worktreesDirectory } " must be an absolute folder on this machine, not a drive root. Change it in Settings → Storage.` ,
36993722 } ) ;
37003723 }
3701- worktreePath = path . join ( parentDir , repoName , sanitizedBranch ) ;
3724+ worktreePath = path . join ( parentDir , repoName , worktreeDirectoryName ( targetBranch ) ) ;
37023725 }
37033726 const args = input . newRefName
37043727 ? [ "worktree" , "add" , "-b" , input . newRefName , "--" , worktreePath , input . refName ]
0 commit comments