@@ -7,6 +7,8 @@ import type {
77 ChunkRange ,
88 CombinedChunk ,
99 AnyChunk ,
10+ FilledGitDiffOptions ,
11+ GitDiffOptions ,
1012} from './types.js' ;
1113import {
1214 ExtendedHeader ,
@@ -15,8 +17,11 @@ import {
1517 LineType ,
1618} from './constants.js' ;
1719
18- export default function parseGitDiff ( diff : string ) : GitDiff {
19- const ctx = new Context ( diff ) ;
20+ export default function parseGitDiff (
21+ diff : string ,
22+ options ?: GitDiffOptions
23+ ) : GitDiff {
24+ const ctx = new Context ( diff , options ) ;
2025 const files = parseFileChanges ( ctx ) ;
2126
2227 return {
@@ -229,8 +234,8 @@ function parseChunkHeader(ctx: Context) {
229234 ctx . nextLine ( ) ;
230235 return {
231236 type : 'BinaryFiles' ,
232- fileA : fileA . replace ( 'a/' , ' ') ,
233- fileB : fileB . replace ( 'b/' , ' ') ,
237+ fileA : getFilePath ( ctx , fileA , 'src ') ,
238+ fileB : getFilePath ( ctx , fileB , 'dst ') ,
234239 } as const ;
235240 }
236241
@@ -280,8 +285,15 @@ function parseChangeMarkers(context: Context): {
280285 deleted : string ;
281286 added : string ;
282287} | null {
283- const deleted = parseMarker ( context , '--- ' ) ?. replace ( 'a/' , '' ) ;
284- const added = parseMarker ( context , '+++ ' ) ?. replace ( 'b/' , '' ) ;
288+ const deleterMarker = parseMarker ( context , '--- ' ) ;
289+ const deleted = deleterMarker
290+ ? getFilePath ( context , deleterMarker , 'src' )
291+ : deleterMarker ;
292+
293+ const addedMarker = parseMarker ( context , '+++ ' ) ;
294+ const added = addedMarker
295+ ? getFilePath ( context , addedMarker , 'dst' )
296+ : addedMarker ;
285297 return added && deleted ? { added, deleted } : null ;
286298}
287299
@@ -364,3 +376,11 @@ function parseChanges(
364376function getLineType ( line : string ) : LineType | null {
365377 return CHAR_TYPE_MAP [ line [ 0 ] ] || null ;
366378}
379+
380+ function getFilePath ( ctx : Context , input : string , type : 'src' | 'dst' ) {
381+ if ( ctx . options . noPrefix ) {
382+ return input ;
383+ }
384+ if ( type === 'src' ) return input . replace ( / ^ a \/ / , '' ) ;
385+ if ( type === 'dst' ) return input . replace ( / ^ b \/ / , '' ) ;
386+ }
0 commit comments