@@ -6,6 +6,12 @@ import type {
66 Chunk ,
77 ChunkRange ,
88} from './types' ;
9+ import {
10+ ExtendedHeader ,
11+ ExtendedHeaderValues ,
12+ FileType ,
13+ LineType ,
14+ } from './constants' ;
915
1016export default function parseGitDiff ( diff : string ) : GitDiff {
1117 const ctx = new Context ( diff ) ;
@@ -45,15 +51,15 @@ function parseFileChange(ctx: Context): AnyFileChange | undefined {
4551 if ( ! extHeader ) {
4652 break ;
4753 }
48- if ( extHeader . type === 'deleted' ) isDeleted = true ;
49- if ( extHeader . type === 'new file' ) isNew = true ;
50- if ( extHeader . type === 'rename from' ) {
54+ if ( extHeader . type === ExtendedHeader . Deleted ) isDeleted = true ;
55+ if ( extHeader . type === ExtendedHeader . NewFile ) isNew = true ;
56+ if ( extHeader . type === ExtendedHeader . RenameFrom ) {
5157 isRename = true ;
52- pathBefore = extHeader . path ;
58+ pathBefore = extHeader . path as string ;
5359 }
54- if ( extHeader . type === 'rename to' ) {
60+ if ( extHeader . type === ExtendedHeader . RenameTo ) {
5561 isRename = true ;
56- pathAfter = extHeader . path ;
62+ pathAfter = extHeader . path as string ;
5763 }
5864 }
5965
@@ -62,26 +68,26 @@ function parseFileChange(ctx: Context): AnyFileChange | undefined {
6268
6369 if ( isDeleted && changeMarkers ) {
6470 return {
65- type : 'DeletedFile' ,
71+ type : FileType . Deleted ,
6672 chunks,
6773 path : changeMarkers . deleted ,
6874 } ;
6975 } else if ( isNew && changeMarkers ) {
7076 return {
71- type : 'AddedFile' ,
77+ type : FileType . Added ,
7278 chunks,
7379 path : changeMarkers . added ,
7480 } ;
7581 } else if ( isRename ) {
7682 return {
77- type : 'RenamedFile' ,
83+ type : FileType . Renamed ,
7884 pathAfter,
7985 pathBefore,
8086 chunks,
8187 } ;
8288 } else if ( changeMarkers ) {
8389 return {
84- type : 'ChangedFile' ,
90+ type : FileType . Changed ,
8591 chunks,
8692 path : changeMarkers . added ,
8793 } ;
@@ -125,49 +131,22 @@ function parseChunk(context: Context): Chunk | undefined {
125131 } ;
126132}
127133
128- const UNHANDLED_EXTENDED_HEADERS = new Set ( [
129- 'index' ,
130- 'old' ,
131- 'copy' ,
132- 'similarity' ,
133- 'dissimilarity' ,
134- ] ) ;
135-
136- const startsWith = ( str : string , target : string ) => {
137- return str . indexOf ( target ) === 0 ;
138- } ;
139-
140134function parseExtendedHeader ( ctx : Context ) {
141135 const line = ctx . getCurLine ( ) ;
142- const type = line . slice ( 0 , line . indexOf ( ' ' ) ) ;
136+ const type = ExtendedHeaderValues . find ( ( v ) => line . startsWith ( v ) ) ;
143137
144- if ( UNHANDLED_EXTENDED_HEADERS . has ( type ) ) {
138+ if ( type ) {
145139 ctx . nextLine ( ) ;
146- return {
147- type : 'unhandled' ,
148- } as const ;
149140 }
150- if ( startsWith ( line , 'deleted ' ) ) {
151- ctx . nextLine ( ) ;
152- return {
153- type : 'deleted' ,
154- } as const ;
155- } else if ( startsWith ( line , 'new file ' ) ) {
156- ctx . nextLine ( ) ;
157- return {
158- type : 'new file' ,
159- } as const ;
160- } else if ( startsWith ( line , 'rename from ' ) ) {
161- ctx . nextLine ( ) ;
141+
142+ if ( type === ExtendedHeader . RenameFrom || type === ExtendedHeader . RenameTo ) {
162143 return {
163- type : 'rename from' ,
164- path : line . slice ( 'rename from ' . length ) ,
144+ type,
145+ path : line . slice ( ` ${ type } ` . length ) ,
165146 } as const ;
166- } else if ( startsWith ( line , 'rename to ' ) ) {
167- ctx . nextLine ( ) ;
147+ } else if ( type ) {
168148 return {
169- type : 'rename to' ,
170- path : line . slice ( 'rename to ' . length ) ,
149+ type,
171150 } as const ;
172151 }
173152
@@ -219,9 +198,9 @@ function parseMarker(context: Context, marker: string): string | null {
219198type LineType = AnyLineChange [ 'type' ] ;
220199
221200const CHAR_TYPE_MAP : Record < string , LineType > = {
222- '+' : 'AddedLine' ,
223- '-' : 'DeletedLine' ,
224- ' ' : 'UnchangedLine' ,
201+ '+' : LineType . Added ,
202+ '-' : LineType . Deleted ,
203+ ' ' : LineType . Unchanged ,
225204} ;
226205
227206function parseChanges (
@@ -244,25 +223,25 @@ function parseChanges(
244223 let change : AnyLineChange ;
245224 const content = line . slice ( 1 ) ;
246225 switch ( type ) {
247- case 'AddedLine' : {
226+ case LineType . Added : {
248227 change = {
249- type : 'AddedLine' ,
228+ type,
250229 lineAfter : lineAfter ++ ,
251230 content,
252231 } ;
253232 break ;
254233 }
255- case 'DeletedLine' : {
234+ case LineType . Deleted : {
256235 change = {
257- type : 'DeletedLine' ,
236+ type,
258237 lineBefore : lineBefore ++ ,
259238 content,
260239 } ;
261240 break ;
262241 }
263- case 'UnchangedLine' : {
242+ case LineType . Unchanged : {
264243 change = {
265- type : 'UnchangedLine' ,
244+ type,
266245 lineBefore : lineBefore ++ ,
267246 lineAfter : lineAfter ++ ,
268247 content,
0 commit comments