@@ -6,7 +6,9 @@ import * as Data from "effect/Data";
66import * as Duration from "effect/Duration" ;
77import * as Effect from "effect/Effect" ;
88import * as Exit from "effect/Exit" ;
9+ import * as FileSystem from "effect/FileSystem" ;
910import * as Layer from "effect/Layer" ;
11+ import * as Path from "effect/Path" ;
1012
1113import * as ProcessRunner from "../processRunner.ts" ;
1214import { sourceControlDefinitions } from "../sourceControl/sourceControlDefinitions.ts" ;
@@ -138,8 +140,18 @@ class RepositoryIdentityCacheKey extends Data.Class<{
138140const resolveRepositoryIdentityCacheKey = Effect . fn ( "RepositoryIdentityResolver.resolveCacheKey" ) (
139141 function * ( cwd : string ) {
140142 const processRunner = yield * ProcessRunner . ProcessRunner ;
143+ const fileSystem = yield * FileSystem . FileSystem ;
144+ const path = yield * Path . Path ;
141145 const env = explicitBareRepositoryEnv ( process . env , yield * HostProcess . Platform ) ;
142146
147+ // `git -C` cannot enter a missing folder or a file, so every probe below would fail.
148+ const isFolder = yield * fileSystem . stat ( cwd ) . pipe (
149+ Effect . map ( ( info ) => info . type === "Directory" ) ,
150+ Effect . catchReason ( "PlatformError" , "NotFound" , ( ) => Effect . succeed ( false ) ) ,
151+ Effect . orElseSucceed ( ( ) => true ) ,
152+ ) ;
153+ if ( ! isFolder ) return null ;
154+
143155 // git is always a real executable; cmd shell mode would split spaced paths on re-tokenization.
144156 const topLevelResult = yield * processRunner
145157 . run ( {
@@ -160,7 +172,25 @@ const resolveRepositoryIdentityCacheKey = Effect.fn("RepositoryIdentityResolver.
160172 }
161173
162174 // Explicit Git directories preserve safe.bareRepository=explicit for both layouts.
163- for ( const gitDir of [ "." , ".git" ] ) {
175+ for ( const [ gitDir , entry ] of [
176+ [ "." , "HEAD" ] ,
177+ [ ".git" , ".git" ] ,
178+ ] as const ) {
179+ // An explicit --git-dir fails unless the folder has this entry; a symlink counts even
180+ // when its target is missing, as git reads a symlinked HEAD's target text.
181+ const target = path . join ( cwd , entry ) ;
182+ const hasEntry = yield * fileSystem . exists ( target ) . pipe (
183+ Effect . flatMap ( ( exists ) =>
184+ exists
185+ ? Effect . succeed ( true )
186+ : fileSystem . readLink ( target ) . pipe (
187+ Effect . as ( true ) ,
188+ Effect . orElseSucceed ( ( ) => false ) ,
189+ ) ,
190+ ) ,
191+ Effect . orElseSucceed ( ( ) => true ) ,
192+ ) ;
193+ if ( ! hasEntry ) continue ;
164194 const bareResult = yield * processRunner
165195 . run ( {
166196 command : "git" ,
@@ -224,6 +254,8 @@ export const make = Effect.fn("RepositoryIdentityResolver.make")(function* (
224254 options : RepositoryIdentityResolverOptions = { } ,
225255) {
226256 const processRunner = yield * ProcessRunner . ProcessRunner ;
257+ const fileSystem = yield * FileSystem . FileSystem ;
258+ const path = yield * Path . Path ;
227259 const cacheCapacity = options . cacheCapacity ?? DEFAULT_REPOSITORY_IDENTITY_CACHE_CAPACITY ;
228260 const refine = options . refine ?? Effect . succeed ;
229261 const timeToLive = ( exit : Exit . Exit < unknown > ) =>
@@ -237,6 +269,8 @@ export const make = Effect.fn("RepositoryIdentityResolver.make")(function* (
237269 ( cwd ) =>
238270 resolveRepositoryIdentityCacheKey ( cwd ) . pipe (
239271 Effect . provideService ( ProcessRunner . ProcessRunner , processRunner ) ,
272+ Effect . provideService ( FileSystem . FileSystem , fileSystem ) ,
273+ Effect . provideService ( Path . Path , path ) ,
240274 ) ,
241275 { capacity : cacheCapacity , timeToLive } ,
242276 ) ;
0 commit comments