@@ -148,6 +148,30 @@ function statusUpstreamRefreshFailureCooldown(consecutiveFailures: number): Dura
148148 return Duration . min ( Duration . millis ( cooldownMs ) , STATUS_UPSTREAM_REFRESH_FAILURE_MAX_COOLDOWN ) ;
149149}
150150
151+ // PR checkouts keep the PR's branch name, so without a cap the folder name can
152+ // be any length. On Windows every character comes out of the 260-character
153+ // budget that tools without long-path support (and git's own `.git/worktrees`
154+ // bookkeeping, even with core.longpaths) have for files inside the worktree.
155+ // Long names keep a readable prefix and a hash of the full branch.
156+ const MAX_WORKTREE_DIRECTORY_NAME_LENGTH = 40 ;
157+
158+ function worktreeDirectoryName ( branch : string ) : string {
159+ const name = branch . replace ( / \/ / g, "-" ) ;
160+ if ( name . length <= MAX_WORKTREE_DIRECTORY_NAME_LENGTH ) return name ;
161+ let hash = 0x811c9dc5 ;
162+ for ( let index = 0 ; index < branch . length ; index ++ ) {
163+ hash = Math . imul ( hash ^ branch . charCodeAt ( index ) , 0x01000193 ) ;
164+ }
165+ const suffix = ( hash >>> 0 ) . toString ( 16 ) . padStart ( 8 , "0" ) ;
166+ const prefix = name
167+ . slice ( 0 , MAX_WORKTREE_DIRECTORY_NAME_LENGTH - suffix . length - 1 )
168+ // A cut emoji leaves a lone surrogate, which git lists back as U+FFFD, so
169+ // T3's stored path would stop matching git's.
170+ . replace ( / [ \uD800 - \uDBFF ] $ / , "" )
171+ . replace ( / [ . - ] + $ / , "" ) ;
172+ return `${ prefix } -${ suffix } ` ;
173+ }
174+
151175class GitRefsSnapshotCacheKey extends Data . Class < {
152176 gitCommonDir : string ;
153177 epoch : number ;
@@ -3689,7 +3713,6 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
36893713 yield * refuseOptionLikeRefName ( "GitVcsDriver.createWorktree" , input . cwd , input . refName ) ;
36903714 yield * refuseOptionLikeRefName ( "GitVcsDriver.createWorktree" , input . cwd , input . newRefName ) ;
36913715 const targetBranch = input . newRefName ?? input . refName ;
3692- const sanitizedBranch = targetBranch . replace ( / \/ / g, "-" ) ;
36933716 // resolve: a cwd like `<repo>/sub/..` must not name the folder `..`.
36943717 const repoName = path . basename ( path . resolve ( input . cwd ) ) ;
36953718 let worktreePath = input . path ;
@@ -3708,7 +3731,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
37083731 detail : `The worktree location "${ options ?. worktreesDirectory } " must be an absolute folder on this machine, not a drive root. Change it in Settings → Storage.` ,
37093732 } ) ;
37103733 }
3711- worktreePath = path . join ( parentDir , repoName , sanitizedBranch ) ;
3734+ worktreePath = path . join ( parentDir , repoName , worktreeDirectoryName ( targetBranch ) ) ;
37123735 }
37133736 const args = input . newRefName
37143737 ? [ "worktree" , "add" , "-b" , input . newRefName , "--" , worktreePath , input . refName ]
0 commit comments