@@ -876,3 +876,67 @@ it.effect("GitVcsDriver checks out the requested commit and records the worktree
876876 yield * driver . removeWorktree ( { cwd : repo , path : worktreePath , force : true } ) ;
877877 } ) . pipe ( Effect . scoped , Effect . provide ( GitContractLayer ) ) ,
878878) ;
879+
880+ it . effect ( "GitVcsDriver switchRef resolves remote-tracking refs to local branches" , ( ) =>
881+ Effect . gen ( function * ( ) {
882+ const fileSystem = yield * FileSystem . FileSystem ;
883+ const path = yield * Path . Path ;
884+ const driver = yield * GitVcsDriver . GitVcsDriver ;
885+ const fixtureRoot = yield * fileSystem . makeTempDirectoryScoped ( {
886+ prefix : "t3-git-switch-ref-" ,
887+ } ) ;
888+ const upstream = path . join ( fixtureRoot , "upstream" ) ;
889+ const repo = path . join ( fixtureRoot , "repo" ) ;
890+ yield * fileSystem . makeDirectory ( upstream ) ;
891+ yield * fileSystem . makeDirectory ( repo ) ;
892+ yield * runGit ( upstream , [ "init" , "--initial-branch=main" ] ) ;
893+ yield * runGit ( upstream , [ "config" , "user.email" , "[email protected] " ] ) ; 894+ yield * runGit ( upstream , [ "config" , "user.name" , "Test" ] ) ;
895+ yield * fileSystem . writeFileString ( path . join ( upstream , "README.md" ) , "base\n" ) ;
896+ yield * runGit ( upstream , [ "add" , "README.md" ] ) ;
897+ yield * runGit ( upstream , [ "commit" , "-m" , "Initial" ] ) ;
898+ yield * runGit ( upstream , [ "branch" , "feature/only-remote" ] ) ;
899+ yield * runGit ( upstream , [ "branch" , "feature/existing-local" ] ) ;
900+ yield * runGit ( upstream , [ "branch" , "feature/twin" ] ) ;
901+ yield * runGit ( upstream , [ "branch" , "feature/untracked" ] ) ;
902+ yield * runGit ( repo , [ "clone" , upstream , "." ] ) ;
903+ yield * runGit ( repo , [ "config" , "user.email" , "[email protected] " ] ) ; 904+ yield * runGit ( repo , [ "config" , "user.name" , "Test" ] ) ;
905+ yield * runGit ( repo , [ "branch" , "feature/existing-local" , "origin/feature/existing-local" ] ) ;
906+ yield * runGit ( repo , [ "branch" , "twin-alias" , "origin/feature/twin" ] ) ;
907+ yield * runGit ( repo , [ "branch" , "feature/untracked" , "origin/feature/untracked" ] ) ;
908+ yield * runGit ( repo , [ "branch" , "--unset-upstream" , "feature/untracked" ] ) ;
909+
910+ // Remote-only ref: creates and checks out a local branch tracking it.
911+ const created = yield * driver . switchRef ( { cwd : repo , refName : "origin/feature/only-remote" } ) ;
912+ assert . strictEqual ( created . refName , "feature/only-remote" ) ;
913+ assert . strictEqual (
914+ yield * readGit ( repo , [ "rev-parse" , "--abbrev-ref" , "--symbolic-full-name" , "@{upstream}" ] ) ,
915+ "origin/feature/only-remote" ,
916+ ) ;
917+
918+ // Remote ref tracked by a differently-named local branch: checks that out.
919+ const alias = yield * driver . switchRef ( { cwd : repo , refName : "origin/feature/twin" } ) ;
920+ assert . strictEqual ( alias . refName , "twin-alias" ) ;
921+
922+ // Local twin exists but does not track the remote ref: upstream checks out
923+ // the remote ref directly (detached HEAD, so no branch name comes back).
924+ const untracked = yield * driver . switchRef ( { cwd : repo , refName : "origin/feature/untracked" } ) ;
925+ assert . strictEqual ( untracked . refName , null ) ;
926+
927+ // Remote ref tracked by a same-named local branch: checks that out.
928+ const existing = yield * driver . switchRef ( {
929+ cwd : repo ,
930+ refName : "origin/feature/existing-local" ,
931+ } ) ;
932+ assert . strictEqual ( existing . refName , "feature/existing-local" ) ;
933+ assert . strictEqual (
934+ yield * readGit ( repo , [ "symbolic-ref" , "--short" , "HEAD" ] ) ,
935+ "feature/existing-local" ,
936+ ) ;
937+
938+ // Plain local ref: unchanged behavior.
939+ const local = yield * driver . switchRef ( { cwd : repo , refName : "main" } ) ;
940+ assert . strictEqual ( local . refName , "main" ) ;
941+ } ) . pipe ( Effect . scoped , Effect . provide ( GitContractLayer ) ) ,
942+ ) ;
0 commit comments