11import fs from "node:fs" ;
2- import { lstat , readFile } from "node:fs/promises" ;
2+ import { lstat } from "node:fs/promises" ;
33import { join , resolve } from "node:path" ;
4+ import { readRegularFile , readRegularFileText } from "./RegularFileRead.js" ;
45
56export type ReferenceSourceVcsInfo =
67 | {
@@ -29,7 +30,7 @@ export const readReferenceSourceVcs = async (
2930 : { kind : "unknown" , head : null , dirty : null } ;
3031 }
3132 try {
32- const head = await resolveSourceHead ( root ) ;
33+ const head = await resolveSourceHead ( root , signal ) ;
3334 if ( isAborted ( signal ) ) return { kind : "unknown" , head : null , dirty : null } ;
3435 return { kind : "git" , head, dirty : null } ;
3536 } catch ( cause : unknown ) {
@@ -39,39 +40,68 @@ export const readReferenceSourceVcs = async (
3940 }
4041} ;
4142
42- const resolveSourceHead = async ( root : string ) : Promise < string > => {
43+ const resolveSourceHead = async (
44+ root : string ,
45+ signal ?: AbortSignal ,
46+ ) : Promise < string > => {
4347 // isomorphic-git is loaded on first use so CLI and MCP startup skip it.
4448 const { resolveRef } = await import ( "isomorphic-git" ) ;
49+ let failedRead : { readonly cause : unknown } | undefined ;
50+ const referenceFs = {
51+ ...fs ,
52+ promises : {
53+ ...fs . promises ,
54+ // isomorphic-git treats every read failure as absence. Preserve real
55+ // failures so a readable packed ref cannot hide an unreadable loose ref.
56+ readFile : async ( path : string ) => {
57+ try {
58+ return await readRegularFile ( path , { signal } ) ;
59+ } catch ( cause : unknown ) {
60+ if ( errorCode ( cause ) !== "ENOENT" ) failedRead ??= { cause } ;
61+ throw cause ;
62+ }
63+ } ,
64+ } ,
65+ } ;
66+ const readRef = async ( options : {
67+ readonly gitdir ?: string ;
68+ readonly ref : string ;
69+ } ) => {
70+ const head = await resolveRef ( { fs : referenceFs , dir : root , ...options } ) ;
71+ signal ?. throwIfAborted ( ) ;
72+ if ( failedRead !== undefined ) throw failedRead . cause ;
73+ return head ;
74+ } ;
4575 const dotgit = join ( root , ".git" ) ;
46- if ( ! ( await lstat ( dotgit ) ) . isFile ( ) )
47- return resolveRef ( { fs, dir : root , ref : "HEAD" } ) ;
48- const pointer = ( await readFile ( dotgit , "utf8" ) ) . replace ( / \r ? \n $ / u, "" ) ;
76+ if ( ! ( await lstat ( dotgit ) ) . isFile ( ) ) return readRef ( { ref : "HEAD" } ) ;
77+ const pointer = ( await readRegularFileText ( dotgit , { signal } ) ) . replace (
78+ / \r ? \n $ / u,
79+ "" ,
80+ ) ;
4981 if ( ! pointer . startsWith ( "gitdir: " ) || pointer . length === 8 )
5082 throw new Error ( "Invalid Git directory pointer" ) ;
5183 const gitdir = resolve ( root , pointer . slice ( 8 ) ) ;
5284 let commonDirectory : string ;
5385 try {
5486 commonDirectory = resolve (
5587 gitdir ,
56- ( await readFile ( join ( gitdir , "commondir" ) , "utf8" ) ) . replace (
57- / \r ? \n $ / u,
58- "" ,
59- ) ,
88+ (
89+ await readRegularFileText ( join ( gitdir , "commondir" ) , { signal } )
90+ ) . replace ( / \r ? \n $ / u, "" ) ,
6091 ) ;
6192 } catch ( cause : unknown ) {
6293 if ( errorCode ( cause ) !== "ENOENT" ) throw cause ;
63- return resolveRef ( { fs , dir : root , gitdir, ref : "HEAD" } ) ;
94+ return readRef ( { gitdir, ref : "HEAD" } ) ;
6495 }
65- const head = ( await readFile ( join ( gitdir , "HEAD" ) , "utf8" ) ) . trim ( ) ;
66- if ( ! head . startsWith ( "ref: " ) )
67- return resolveRef ( { fs, dir : root , gitdir, ref : "HEAD" } ) ;
96+ const head = (
97+ await readRegularFileText ( join ( gitdir , "HEAD" ) , { signal } )
98+ ) . trim ( ) ;
99+ if ( ! head . startsWith ( "ref: " ) ) return readRef ( { gitdir, ref : "HEAD" } ) ;
68100 const ref = head . slice ( 5 ) ;
69101 const privateRef = [ "refs/bisect/" , "refs/rewritten/" , "refs/worktree/" ] . some (
70102 ( prefix ) => ref . startsWith ( prefix ) ,
71103 ) ;
72- return resolveRef ( {
73- fs,
74- dir : root ,
104+ return readRef ( {
75105 gitdir : privateRef ? gitdir : commonDirectory ,
76106 ref,
77107 } ) ;
0 commit comments