@@ -98,14 +98,14 @@ export function binaryFileDummyDetails(fromFile: string, toFile: string, status:
98
98
99
99
const 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;
100
100
101
- function parseHeader (
102
- patch : string ,
103
- fromFile : string ,
104
- toFile : string ,
105
- ) : {
101
+ type BasicHeader = {
102
+ fromFile : string ;
103
+ toFile : string ;
106
104
status : FileStatus ;
107
105
binary : boolean ;
108
- } {
106
+ } ;
107
+
108
+ function parseHeader ( patch : string , fromFile : string , toFile : string ) : BasicHeader {
109
109
let status : FileStatus = "modified" ;
110
110
if ( fromFile !== toFile ) {
111
111
status = "renamed_modified" ;
@@ -141,34 +141,42 @@ function parseHeader(
141
141
lineStart = lineEnd + 1 ;
142
142
}
143
143
144
- return { status, binary } ;
144
+ return { fromFile , toFile , status, binary } ;
145
145
}
146
146
147
- export function splitMultiFilePatch (
147
+ export function parseMultiFilePatch (
148
148
patchContent : string ,
149
149
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 ) ;
163
163
continue ;
164
164
}
165
- } else {
166
- patches . push ( binaryFileDummyDetails ( fromFile , toFile , status ) ) ;
167
- continue ;
168
165
}
166
+
167
+ yield makeTextDetails ( header . fromFile , header . toFile , header . status , content ) ;
169
168
}
169
+ }
170
+ return detailsGenerator ( ) ;
171
+ }
170
172
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 ] ) ;
172
180
}
173
181
return patches ;
174
182
}
0 commit comments