@@ -7,6 +7,8 @@ import type {
7
7
ChunkRange ,
8
8
CombinedChunk ,
9
9
AnyChunk ,
10
+ FilledGitDiffOptions ,
11
+ GitDiffOptions ,
10
12
} from './types.js' ;
11
13
import {
12
14
ExtendedHeader ,
@@ -15,8 +17,11 @@ import {
15
17
LineType ,
16
18
} from './constants.js' ;
17
19
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 ) ;
20
25
const files = parseFileChanges ( ctx ) ;
21
26
22
27
return {
@@ -229,8 +234,8 @@ function parseChunkHeader(ctx: Context) {
229
234
ctx . nextLine ( ) ;
230
235
return {
231
236
type : 'BinaryFiles' ,
232
- fileA : fileA . replace ( 'a/' , ' ') ,
233
- fileB : fileB . replace ( 'b/' , ' ') ,
237
+ fileA : getFilePath ( ctx , fileA , 'src ') ,
238
+ fileB : getFilePath ( ctx , fileB , 'dst ') ,
234
239
} as const ;
235
240
}
236
241
@@ -280,8 +285,15 @@ function parseChangeMarkers(context: Context): {
280
285
deleted : string ;
281
286
added : string ;
282
287
} | 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 ;
285
297
return added && deleted ? { added, deleted } : null ;
286
298
}
287
299
@@ -364,3 +376,11 @@ function parseChanges(
364
376
function getLineType ( line : string ) : LineType | null {
365
377
return CHAR_TYPE_MAP [ line [ 0 ] ] || null ;
366
378
}
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