@@ -892,42 +892,66 @@ const make = Effect.gen(function* () {
892892 : `settled for ${ settledDays } days` ;
893893 if ( ! eligible && ( settings . worktreeUnchanged || settings . worktreeOnMerge ) ) {
894894 const repositoryCwd = path . resolve ( project . workspaceRoot ) ;
895- const remote = yield * git . resolvePrimaryRemoteName ( repositoryCwd ) ;
896- const defaultBranchKey = `${ repositoryCwd } \0${ remote } ` ;
897- let branch = defaultBranches . get ( defaultBranchKey ) ;
898- if ( branch === undefined ) {
899- branch = yield * git . resolveDefaultBranchName ( repositoryCwd , remote ) ;
900- // Without a local HEAD ref, only the remote can identify its default;
901- // a conventional name may be a feature branch.
902- if ( branch === null ) {
903- branch = yield * git
904- . execute ( {
905- operation : "StorageCleanup.resolveDefaultBranch" ,
906- cwd : repositoryCwd ,
907- args : [ "ls-remote" , "--symref" , "--" , remote , "HEAD" ] ,
908- timeoutMs : 20_000 ,
909- } )
910- . pipe (
911- Effect . map (
912- ( result ) =>
913- / ^ r e f : r e f s \/ h e a d s \/ ( .+ ) \t H E A D \r ? $ / m. exec ( result . stdout ) ?. [ 1 ] ?? null ,
914- ) ,
915- Effect . orElseSucceed ( ( ) => null ) ,
916- ) ;
917- }
918- defaultBranches . set ( defaultBranchKey , branch ) ;
919- }
920- if ( branch === null ) return keep ( "default branch is unavailable" ) ;
921- const defaultRef = `refs/remotes/${ remote } /${ branch } ` ;
922- const refreshed = refreshedDefaultRefs . get ( repositoryCwd ) ?? new Set < string > ( ) ;
923- if ( ! refreshed . has ( defaultRef ) ) {
924- yield * git . fetchRemoteTrackingBranch ( {
895+ const remotes = yield * git . execute ( {
896+ operation : "StorageCleanup.listRemotes" ,
897+ cwd : repositoryCwd ,
898+ args : [ "remote" ] ,
899+ } ) ;
900+ const remote =
901+ remotes . stdout . trim ( ) === ""
902+ ? null
903+ : yield * git . resolvePrimaryRemoteName ( repositoryCwd ) ;
904+ let branch : string | null | undefined ;
905+ let defaultRef : string ;
906+ if ( remote === null ) {
907+ if ( ! settings . worktreeUnchanged )
908+ return keep ( "no remote is configured for merged-worktree cleanup" ) ;
909+ const projectHead = yield * git . execute ( {
910+ operation : "StorageCleanup.resolveProjectBranch" ,
925911 cwd : repositoryCwd ,
926- remoteName : remote ,
927- remoteBranch : branch ,
912+ args : [ "symbolic-ref" , "--quiet" , "HEAD" ] ,
913+ allowNonZeroExit : true ,
928914 } ) ;
929- refreshed . add ( defaultRef ) ;
930- refreshedDefaultRefs . set ( repositoryCwd , refreshed ) ;
915+ if ( projectHead . exitCode !== 0 ) return keep ( "project branch is unavailable" ) ;
916+ defaultRef = projectHead . stdout . trim ( ) ;
917+ branch = defaultRef . slice ( "refs/heads/" . length ) ;
918+ } else {
919+ const defaultBranchKey = `${ repositoryCwd } \0${ remote } ` ;
920+ branch = defaultBranches . get ( defaultBranchKey ) ;
921+ if ( branch === undefined ) {
922+ branch = yield * git . resolveDefaultBranchName ( repositoryCwd , remote ) ;
923+ // Without a local HEAD ref, only the remote can identify its default;
924+ // a conventional name may be a feature branch.
925+ if ( branch === null ) {
926+ branch = yield * git
927+ . execute ( {
928+ operation : "StorageCleanup.resolveDefaultBranch" ,
929+ cwd : repositoryCwd ,
930+ args : [ "ls-remote" , "--symref" , "--" , remote , "HEAD" ] ,
931+ timeoutMs : 20_000 ,
932+ } )
933+ . pipe (
934+ Effect . map (
935+ ( result ) =>
936+ / ^ r e f : r e f s \/ h e a d s \/ ( .+ ) \t H E A D \r ? $ / m. exec ( result . stdout ) ?. [ 1 ] ?? null ,
937+ ) ,
938+ Effect . orElseSucceed ( ( ) => null ) ,
939+ ) ;
940+ }
941+ defaultBranches . set ( defaultBranchKey , branch ) ;
942+ }
943+ if ( branch === null ) return keep ( "default branch is unavailable" ) ;
944+ defaultRef = `refs/remotes/${ remote } /${ branch } ` ;
945+ const refreshed = refreshedDefaultRefs . get ( repositoryCwd ) ?? new Set < string > ( ) ;
946+ if ( ! refreshed . has ( defaultRef ) ) {
947+ yield * git . fetchRemoteTrackingBranch ( {
948+ cwd : repositoryCwd ,
949+ remoteName : remote ,
950+ remoteBranch : branch ,
951+ } ) ;
952+ refreshed . add ( defaultRef ) ;
953+ refreshedDefaultRefs . set ( repositoryCwd , refreshed ) ;
954+ }
931955 }
932956 const base = yield * git . resolveCommit ( {
933957 cwd : worktreePath ,
@@ -941,8 +965,12 @@ const make = Effect.gen(function* () {
941965 } ) ;
942966 const integrated = ancestor . exitCode === 0 ;
943967 eligible = integrated && settings . worktreeUnchanged ;
944- if ( eligible ) removalReason = "no commits beyond the default branch" ;
945- if ( ! eligible && settings . worktreeOnMerge && thread . branch !== null ) {
968+ if ( eligible )
969+ removalReason =
970+ remote === null
971+ ? "no commits beyond the project branch"
972+ : "no commits beyond the default branch" ;
973+ if ( ! eligible && remote !== null && settings . worktreeOnMerge && thread . branch !== null ) {
946974 const pullRequest = yield * gitManager . branchPullRequest (
947975 { cwd : worktreePath , branch : thread . branch } ,
948976 { refresh : true } ,
@@ -955,6 +983,7 @@ const make = Effect.gen(function* () {
955983 } ) ;
956984 if ( eligible ) removalReason = "pull request was merged" ;
957985 }
986+ if ( ! eligible && remote === null ) return keep ( "has commits beyond the project branch" ) ;
958987 }
959988 if ( ! eligible )
960989 return keep (
0 commit comments