@@ -851,42 +851,66 @@ const make = Effect.gen(function* () {
851851 : `settled for ${ settledDays } days` ;
852852 if ( ! eligible && ( settings . worktreeUnchanged || settings . worktreeOnMerge ) ) {
853853 const repositoryCwd = path . resolve ( project . workspaceRoot ) ;
854- const remote = yield * git . resolvePrimaryRemoteName ( repositoryCwd ) ;
855- const defaultBranchKey = `${ repositoryCwd } \0${ remote } ` ;
856- let branch = defaultBranches . get ( defaultBranchKey ) ;
857- if ( branch === undefined ) {
858- branch = yield * git . resolveDefaultBranchName ( repositoryCwd , remote ) ;
859- // Without a local HEAD ref, only the remote can identify its default;
860- // a conventional name may be a feature branch.
861- if ( branch === null ) {
862- branch = yield * git
863- . execute ( {
864- operation : "StorageCleanup.resolveDefaultBranch" ,
865- cwd : repositoryCwd ,
866- args : [ "ls-remote" , "--symref" , "--" , remote , "HEAD" ] ,
867- timeoutMs : 20_000 ,
868- } )
869- . pipe (
870- Effect . map (
871- ( result ) =>
872- / ^ r e f : r e f s \/ h e a d s \/ ( .+ ) \t H E A D \r ? $ / m. exec ( result . stdout ) ?. [ 1 ] ?? null ,
873- ) ,
874- Effect . orElseSucceed ( ( ) => null ) ,
875- ) ;
876- }
877- defaultBranches . set ( defaultBranchKey , branch ) ;
878- }
879- if ( branch === null ) return keep ( "default branch is unavailable" ) ;
880- const defaultRef = `refs/remotes/${ remote } /${ branch } ` ;
881- const refreshed = refreshedDefaultRefs . get ( repositoryCwd ) ?? new Set < string > ( ) ;
882- if ( ! refreshed . has ( defaultRef ) ) {
883- yield * git . fetchRemoteTrackingBranch ( {
854+ const remotes = yield * git . execute ( {
855+ operation : "StorageCleanup.listRemotes" ,
856+ cwd : repositoryCwd ,
857+ args : [ "remote" ] ,
858+ } ) ;
859+ const remote =
860+ remotes . stdout . trim ( ) === ""
861+ ? null
862+ : yield * git . resolvePrimaryRemoteName ( repositoryCwd ) ;
863+ let branch : string | null | undefined ;
864+ let defaultRef : string ;
865+ if ( remote === null ) {
866+ if ( ! settings . worktreeUnchanged )
867+ return keep ( "no remote is configured for merged-worktree cleanup" ) ;
868+ const projectHead = yield * git . execute ( {
869+ operation : "StorageCleanup.resolveProjectBranch" ,
884870 cwd : repositoryCwd ,
885- remoteName : remote ,
886- remoteBranch : branch ,
871+ args : [ "symbolic-ref" , "--quiet" , "HEAD" ] ,
872+ allowNonZeroExit : true ,
887873 } ) ;
888- refreshed . add ( defaultRef ) ;
889- refreshedDefaultRefs . set ( repositoryCwd , refreshed ) ;
874+ if ( projectHead . exitCode !== 0 ) return keep ( "project branch is unavailable" ) ;
875+ defaultRef = projectHead . stdout . trim ( ) ;
876+ branch = defaultRef . slice ( "refs/heads/" . length ) ;
877+ } else {
878+ const defaultBranchKey = `${ repositoryCwd } \0${ remote } ` ;
879+ branch = defaultBranches . get ( defaultBranchKey ) ;
880+ if ( branch === undefined ) {
881+ branch = yield * git . resolveDefaultBranchName ( repositoryCwd , remote ) ;
882+ // Without a local HEAD ref, only the remote can identify its default;
883+ // a conventional name may be a feature branch.
884+ if ( branch === null ) {
885+ branch = yield * git
886+ . execute ( {
887+ operation : "StorageCleanup.resolveDefaultBranch" ,
888+ cwd : repositoryCwd ,
889+ args : [ "ls-remote" , "--symref" , "--" , remote , "HEAD" ] ,
890+ timeoutMs : 20_000 ,
891+ } )
892+ . pipe (
893+ Effect . map (
894+ ( result ) =>
895+ / ^ r e f : r e f s \/ h e a d s \/ ( .+ ) \t H E A D \r ? $ / m. exec ( result . stdout ) ?. [ 1 ] ?? null ,
896+ ) ,
897+ Effect . orElseSucceed ( ( ) => null ) ,
898+ ) ;
899+ }
900+ defaultBranches . set ( defaultBranchKey , branch ) ;
901+ }
902+ if ( branch === null ) return keep ( "default branch is unavailable" ) ;
903+ defaultRef = `refs/remotes/${ remote } /${ branch } ` ;
904+ const refreshed = refreshedDefaultRefs . get ( repositoryCwd ) ?? new Set < string > ( ) ;
905+ if ( ! refreshed . has ( defaultRef ) ) {
906+ yield * git . fetchRemoteTrackingBranch ( {
907+ cwd : repositoryCwd ,
908+ remoteName : remote ,
909+ remoteBranch : branch ,
910+ } ) ;
911+ refreshed . add ( defaultRef ) ;
912+ refreshedDefaultRefs . set ( repositoryCwd , refreshed ) ;
913+ }
890914 }
891915 const base = yield * git . resolveCommit ( {
892916 cwd : worktreePath ,
@@ -900,8 +924,12 @@ const make = Effect.gen(function* () {
900924 } ) ;
901925 const integrated = ancestor . exitCode === 0 ;
902926 eligible = integrated && settings . worktreeUnchanged ;
903- if ( eligible ) removalReason = "no commits beyond the default branch" ;
904- if ( ! eligible && settings . worktreeOnMerge && thread . branch !== null ) {
927+ if ( eligible )
928+ removalReason =
929+ remote === null
930+ ? "no commits beyond the project branch"
931+ : "no commits beyond the default branch" ;
932+ if ( ! eligible && remote !== null && settings . worktreeOnMerge && thread . branch !== null ) {
905933 const pullRequest = yield * gitManager . branchPullRequest (
906934 { cwd : worktreePath , branch : thread . branch } ,
907935 { refresh : true } ,
@@ -914,6 +942,7 @@ const make = Effect.gen(function* () {
914942 } ) ;
915943 if ( eligible ) removalReason = "pull request was merged" ;
916944 }
945+ if ( ! eligible && remote === null ) return keep ( "has commits beyond the project branch" ) ;
917946 }
918947 if ( ! eligible )
919948 return keep (
0 commit comments