@@ -98,14 +98,14 @@ export function binaryFileDummyDetails(fromFile: string, toFile: string, status:
9898
9999const fileRegex = / d i f f - - g i t a \/ ( \S + ) b \/ ( \S + ) \r ? \n (?: .+ \r ? \n ) * ?(? = - - \r ? \n | d i f f - - g i t | $ ) / g;
100100
101- function parseHeader (
102- patch : string ,
103- fromFile : string ,
104- toFile : string ,
105- ) : {
101+ type BasicHeader = {
102+ fromFile : string ;
103+ toFile : string ;
106104 status : FileStatus ;
107105 binary : boolean ;
108- } {
106+ } ;
107+
108+ function parseHeader ( patch : string , fromFile : string , toFile : string ) : BasicHeader {
109109 let status : FileStatus = "modified" ;
110110 if ( fromFile !== toFile ) {
111111 status = "renamed_modified" ;
@@ -141,34 +141,42 @@ function parseHeader(
141141 lineStart = lineEnd + 1 ;
142142 }
143143
144- return { status, binary } ;
144+ return { fromFile , toFile , status, binary } ;
145145}
146146
147- export function splitMultiFilePatch (
147+ export function parseMultiFilePatch (
148148 patchContent : string ,
149149 imageFactory ?: ( fromFile : string , toFile : string , status : FileStatus ) => ImageFileDetails | null ,
150- ) : FileDetails [ ] {
151- const patches : FileDetails [ ] = [ ] ;
152- // Process each file in the diff
153- let fileMatch ;
154- while ( ( fileMatch = fileRegex . exec ( patchContent ) ) !== null ) {
155- const [ fullFileMatch , fromFile , toFile ] = fileMatch ;
156- const { status , binary } = parseHeader ( fullFileMatch , fromFile , toFile ) ;
157-
158- if ( binary ) {
159- if ( imageFactory !== undefined && isImageFile ( fromFile ) && isImageFile ( toFile ) ) {
160- const imageDetails = imageFactory ( fromFile , toFile , status ) ;
161- if ( imageDetails != null ) {
162- patches . push ( imageDetails ) ;
150+ ) : AsyncGenerator < FileDetails > {
151+ const split = splitMultiFilePatch ( patchContent ) ;
152+ async function * detailsGenerator ( ) {
153+ for ( const [ header , content ] of split ) {
154+ if ( header . binary ) {
155+ if ( imageFactory !== undefined && isImageFile ( header . fromFile ) && isImageFile ( header . toFile ) ) {
156+ const imageDetails = imageFactory ( header . fromFile , header . toFile , header . status ) ;
157+ if ( imageDetails != null ) {
158+ yield imageDetails ;
159+ continue ;
160+ }
161+ } else {
162+ yield binaryFileDummyDetails ( header . fromFile , header . toFile , header . status ) ;
163163 continue ;
164164 }
165- } else {
166- patches . push ( binaryFileDummyDetails ( fromFile , toFile , status ) ) ;
167- continue ;
168165 }
166+
167+ yield makeTextDetails ( header . fromFile , header . toFile , header . status , content ) ;
169168 }
169+ }
170+ return detailsGenerator ( ) ;
171+ }
170172
171- patches . push ( makeTextDetails ( fromFile , toFile , status , fullFileMatch ) ) ;
173+ export function splitMultiFilePatch ( patchContent : string ) : [ BasicHeader , string ] [ ] {
174+ const patches : [ BasicHeader , string ] [ ] = [ ] ;
175+ let fileMatch ;
176+ while ( ( fileMatch = fileRegex . exec ( patchContent ) ) !== null ) {
177+ const [ fullFileMatch , fromFile , toFile ] = fileMatch ;
178+ const header = parseHeader ( fullFileMatch , fromFile , toFile ) ;
179+ patches . push ( [ header , fullFileMatch ] ) ;
172180 }
173181 return patches ;
174182}
0 commit comments