@@ -217,13 +217,15 @@ function normalizeGitPatchPrefixes(patchText: string) {
217217function rewriteGitDiffHeader ( line : string ) : { line : string ; changed : boolean } {
218218 const rest = line . slice ( "diff --git " . length ) . trimEnd ( ) ;
219219
220- const quotedMatch = rest . match ( / ^ " ( [ ^ " ] * ) " " ( [ ^ " ] * ) " $ / ) ;
220+ const quotedMatch = rest . match ( / ^ " ( (?: \\ . | [ ^ " \\ ] ) * ) " " ( (?: \\ . | [ ^ " \\ ] ) * ) " $ / ) ;
221221 if ( quotedMatch ) {
222- const [ , oldPath , newPath ] = quotedMatch ;
223- if ( oldPath ?. startsWith ( "a/" ) && newPath ?. startsWith ( "b/" ) ) {
224- return { line, changed : false } ;
225- }
226- return { line : `diff --git "a/${ oldPath } " "b/${ newPath } "` , changed : true } ;
222+ const [ , oldPath = "" , newPath = "" ] = quotedMatch ;
223+ // Pierre's git header parser does not currently handle the quoted `"a/..." "b/..."`
224+ // form, so canonicalize quoted paths to the unquoted form even when prefixes exist.
225+ return {
226+ line : `diff --git ${ withGitPrefix ( oldPath , "a/" ) } ${ withGitPrefix ( newPath , "b/" ) } ` ,
227+ changed : true ,
228+ } ;
227229 }
228230
229231 const tokens = rest . split ( " " ) ;
@@ -255,16 +257,23 @@ function rewriteGitDiffHeader(line: string): { line: string; changed: boolean }
255257 return { line, changed : false } ;
256258}
257259
260+ /** Return a path with the expected Git side prefix while avoiding double-prefixing. */
261+ function withGitPrefix ( path : string , prefix : "a/" | "b/" ) {
262+ return path . startsWith ( prefix ) ? path : `${ prefix } ${ path } ` ;
263+ }
264+
258265/** Insert the canonical `a/` or `b/` prefix on a unified-diff header that is missing it. */
259266function rewriteUnifiedFileLine ( line : string , marker : "--- " | "+++ " , prefix : "a/" | "b/" ) {
260267 const path = line . slice ( marker . length ) ;
261- if ( path === "/dev/null" || path . startsWith ( "/dev/null\t" ) ) {
268+ const quotedPath = path . match ( / ^ " ( (?: \\ .| [ ^ " \\ ] ) * ) " ( .* ) $ / ) ;
269+ const pathName = quotedPath ?. [ 1 ] ?? path ;
270+ const suffix = quotedPath ?. [ 2 ] ?? "" ;
271+
272+ if ( pathName === "/dev/null" || pathName . startsWith ( "/dev/null\t" ) ) {
262273 return line ;
263274 }
264- if ( path . startsWith ( '"' ) ) {
265- return `${ marker } "${ prefix } ${ path . slice ( 1 ) } ` ;
266- }
267- return `${ marker } ${ prefix } ${ path } ` ;
275+
276+ return `${ marker } ${ withGitPrefix ( pathName , prefix ) } ${ suffix } ` ;
268277}
269278
270279/** Escape only the filename characters that break unified-diff header parsing. */
0 commit comments