11using System . Collections . Generic ;
2- using System . Text ;
2+ using System . IO ;
33using System . Text . RegularExpressions ;
44
55using Avalonia ;
@@ -148,13 +148,13 @@ public void GenerateNewPatchFromSelection(Change change, string fileBlobGuid, Te
148148 var isTracked = ! string . IsNullOrEmpty ( fileBlobGuid ) ;
149149 var fileGuid = isTracked ? fileBlobGuid : "00000000" ;
150150
151- var builder = new StringBuilder ( ) ;
152- builder . Append ( "diff --git a/" ) . Append ( change . Path ) . Append ( " b/" ) . Append ( change . Path ) . Append ( ' \n ' ) ;
151+ using var writer = new StreamWriter ( output ) ;
152+ writer . WriteLine ( $ "diff --git a/{ change . Path } b/{ change . Path } " ) ;
153153 if ( ! revert && ! isTracked )
154- builder . Append ( "new file mode 100644\n " ) ;
155- builder . Append ( "index 00000000..." ) . Append ( fileGuid ) . Append ( ' \n ' ) ;
156- builder . Append ( "--- " ) . Append ( ( revert || isTracked ) ? $ "a/{ change . Path } \n " : "/dev/null\n " ) ;
157- builder . Append ( "+++ b/" ) . Append ( change . Path ) . Append ( ' \n ' ) ;
154+ writer . WriteLine ( "new file mode 100644" ) ;
155+ writer . WriteLine ( $ "index 00000000...{ fileGuid } " ) ;
156+ writer . WriteLine ( $ "--- { ( revert || isTracked ? $ "a/{ change . Path } " : "/dev/null" ) } ") ;
157+ writer . WriteLine ( $ "+++ b/{ change . Path } " ) ;
158158
159159 var additions = selection . EndLine - selection . StartLine ;
160160 if ( selection . StartLine != 1 )
@@ -163,40 +163,40 @@ public void GenerateNewPatchFromSelection(Change change, string fileBlobGuid, Te
163163 if ( revert )
164164 {
165165 var totalLines = Lines . Count - 1 ;
166- builder . Append ( "@@ -0," ) . Append ( totalLines - additions ) . Append ( " +0," ) . Append ( totalLines ) . Append ( " @@") ;
166+ writer . WriteLine ( $ "@@ -0,{ totalLines - additions } +0,{ totalLines } @@") ;
167167 for ( int i = 1 ; i <= totalLines ; i ++ )
168168 {
169169 var line = Lines [ i ] ;
170170 if ( line . Type != TextDiffLineType . Added )
171171 continue ;
172- builder . Append ( selection . IsInRange ( i ) ? "\n +" : "\n " ) . Append ( line . Content ) ;
172+ writer . WriteLine ( $ " { ( selection . IsInRange ( i ) ? "+" : " " ) } { line . Content } " ) ;
173173 }
174174 }
175175 else
176176 {
177- builder . Append ( "@@ -0,0 +0," ) . Append ( additions ) . Append ( " @@") ;
177+ writer . WriteLine ( $ "@@ -0,0 +0,{ additions } @@") ;
178178 for ( int i = selection . StartLine - 1 ; i < selection . EndLine ; i ++ )
179179 {
180180 var line = Lines [ i ] ;
181181 if ( line . Type != TextDiffLineType . Added )
182182 continue ;
183- builder . Append ( " \n +" ) . Append ( line . Content ) ;
183+ writer . WriteLine ( $ "+ { line . Content } " ) ;
184184 }
185185 }
186186
187- builder . Append ( "\n \\ No newline at end of file\n " ) ;
188- System . IO . File . WriteAllText ( output , builder . ToString ( ) ) ;
187+ writer . WriteLine ( "\\ No newline at end of file" ) ;
188+ writer . Flush ( ) ;
189189 }
190190
191191 public void GeneratePatchFromSelection ( Change change , string fileTreeGuid , TextDiffSelection selection , bool revert , string output )
192192 {
193193 var orgFile = ! string . IsNullOrEmpty ( change . OriginalPath ) ? change . OriginalPath : change . Path ;
194194
195- var builder = new StringBuilder ( ) ;
196- builder . Append ( "diff --git a/" ) . Append ( change . Path ) . Append ( " b/" ) . Append ( change . Path ) . Append ( ' \n ' ) ;
197- builder . Append ( "index 00000000..." ) . Append ( fileTreeGuid ) . Append ( " 100644\n ") ;
198- builder . Append ( "--- a/" ) . Append ( orgFile ) . Append ( ' \n ' ) ;
199- builder . Append ( "+++ b/" ) . Append ( change . Path ) ;
195+ using var writer = new StreamWriter ( output ) ;
196+ writer . WriteLine ( $ "diff --git a/{ change . Path } b/{ change . Path } " ) ;
197+ writer . WriteLine ( $ "index 00000000...{ fileTreeGuid } 100644") ;
198+ writer . WriteLine ( $ "--- a/{ orgFile } " ) ;
199+ writer . WriteLine ( $ "+++ b/{ change . Path } " ) ;
200200
201201 // If last line of selection is a change. Find one more line.
202202 var tail = null as string ;
@@ -264,21 +264,21 @@ public void GeneratePatchFromSelection(Change change, string fileTreeGuid, TextD
264264 var line = Lines [ i ] ;
265265 if ( line . Type == TextDiffLineType . Indicator )
266266 {
267- ProcessIndicatorForPatch ( builder , line , i , selection . StartLine , selection . EndLine , ignoreRemoves , ignoreAdds , revert , tail != null ) ;
267+ ProcessIndicatorForPatch ( writer , line , i , selection . StartLine , selection . EndLine , ignoreRemoves , ignoreAdds , revert , tail != null ) ;
268268 }
269269 else if ( line . Type == TextDiffLineType . Added )
270270 {
271271 if ( revert )
272- builder . Append ( " \n " ) . Append ( line . Content ) ;
272+ writer . WriteLine ( $ " { line . Content } " ) ;
273273 }
274274 else if ( line . Type == TextDiffLineType . Deleted )
275275 {
276276 if ( ! revert )
277- builder . Append ( " \n " ) . Append ( line . Content ) ;
277+ writer . WriteLine ( $ " { line . Content } " ) ;
278278 }
279279 else if ( line . Type == TextDiffLineType . Normal )
280280 {
281- builder . Append ( " \n " ) . Append ( line . Content ) ;
281+ writer . WriteLine ( $ " { line . Content } " ) ;
282282 }
283283 }
284284 }
@@ -289,39 +289,38 @@ public void GeneratePatchFromSelection(Change change, string fileTreeGuid, TextD
289289 var line = Lines [ i ] ;
290290 if ( line . Type == TextDiffLineType . Indicator )
291291 {
292- if ( ! ProcessIndicatorForPatch ( builder , line , i , selection . StartLine , selection . EndLine , selection . IgnoredDeletes , selection . IgnoredAdds , revert , tail != null ) )
292+ if ( ! ProcessIndicatorForPatch ( writer , line , i , selection . StartLine , selection . EndLine , selection . IgnoredDeletes , selection . IgnoredAdds , revert , tail != null ) )
293293 {
294294 break ;
295295 }
296296 }
297297 else if ( line . Type == TextDiffLineType . Normal )
298298 {
299- builder . Append ( " \n " ) . Append ( line . Content ) ;
299+ writer . WriteLine ( $ " { line . Content } " ) ;
300300 }
301301 else if ( line . Type == TextDiffLineType . Added )
302302 {
303- builder . Append ( " \n +" ) . Append ( line . Content ) ;
303+ writer . WriteLine ( $ "+ { line . Content } " ) ;
304304 }
305305 else if ( line . Type == TextDiffLineType . Deleted )
306306 {
307- builder . Append ( " \n -" ) . Append ( line . Content ) ;
307+ writer . WriteLine ( $ "- { line . Content } " ) ;
308308 }
309309 }
310310
311- builder . Append ( "\n " ) . Append ( tail ) ;
312- builder . Append ( "\n " ) ;
313- System . IO . File . WriteAllText ( output , builder . ToString ( ) ) ;
311+ writer . WriteLine ( $ " { tail } ") ;
312+ writer . Flush ( ) ;
314313 }
315314
316315 public void GeneratePatchFromSelectionSingleSide ( Change change , string fileTreeGuid , TextDiffSelection selection , bool revert , bool isOldSide , string output )
317316 {
318317 var orgFile = ! string . IsNullOrEmpty ( change . OriginalPath ) ? change . OriginalPath : change . Path ;
319318
320- var builder = new StringBuilder ( ) ;
321- builder . Append ( "diff --git a/" ) . Append ( change . Path ) . Append ( " b/" ) . Append ( change . Path ) . Append ( ' \n ' ) ;
322- builder . Append ( "index 00000000..." ) . Append ( fileTreeGuid ) . Append ( " 100644\n ") ;
323- builder . Append ( "--- a/" ) . Append ( orgFile ) . Append ( ' \n ' ) ;
324- builder . Append ( "+++ b/" ) . Append ( change . Path ) ;
319+ using var writer = new StreamWriter ( output ) ;
320+ writer . WriteLine ( $ "diff --git a/{ change . Path } b/{ change . Path } " ) ;
321+ writer . WriteLine ( $ "index 00000000...{ fileTreeGuid } 100644") ;
322+ writer . WriteLine ( $ "--- a/{ orgFile } " ) ;
323+ writer . WriteLine ( $ "+++ b/{ change . Path } " ) ;
325324
326325 // If last line of selection is a change. Find one more line.
327326 var tail = null as string ;
@@ -389,21 +388,21 @@ public void GeneratePatchFromSelectionSingleSide(Change change, string fileTreeG
389388 var line = Lines [ i ] ;
390389 if ( line . Type == TextDiffLineType . Indicator )
391390 {
392- ProcessIndicatorForPatchSingleSide ( builder , line , i , selection . StartLine , selection . EndLine , ignoreRemoves , ignoreAdds , revert , isOldSide , tail != null ) ;
391+ ProcessIndicatorForPatchSingleSide ( writer , line , i , selection . StartLine , selection . EndLine , ignoreRemoves , ignoreAdds , revert , isOldSide , tail != null ) ;
393392 }
394393 else if ( line . Type == TextDiffLineType . Added )
395394 {
396395 if ( revert )
397- builder . Append ( " \n " ) . Append ( line . Content ) ;
396+ writer . WriteLine ( $ " { line . Content } " ) ;
398397 }
399398 else if ( line . Type == TextDiffLineType . Deleted )
400399 {
401400 if ( ! revert )
402- builder . Append ( " \n " ) . Append ( line . Content ) ;
401+ writer . WriteLine ( $ " { line . Content } " ) ;
403402 }
404403 else if ( line . Type == TextDiffLineType . Normal )
405404 {
406- builder . Append ( " \n " ) . Append ( line . Content ) ;
405+ writer . WriteLine ( $ " { line . Content } " ) ;
407406 }
408407 }
409408 }
@@ -414,22 +413,22 @@ public void GeneratePatchFromSelectionSingleSide(Change change, string fileTreeG
414413 var line = Lines [ i ] ;
415414 if ( line . Type == TextDiffLineType . Indicator )
416415 {
417- if ( ! ProcessIndicatorForPatchSingleSide ( builder , line , i , selection . StartLine , selection . EndLine , selection . IgnoredDeletes , selection . IgnoredAdds , revert , isOldSide , tail != null ) )
416+ if ( ! ProcessIndicatorForPatchSingleSide ( writer , line , i , selection . StartLine , selection . EndLine , selection . IgnoredDeletes , selection . IgnoredAdds , revert , isOldSide , tail != null ) )
418417 {
419418 break ;
420419 }
421420 }
422421 else if ( line . Type == TextDiffLineType . Normal )
423422 {
424- builder . Append ( " \n " ) . Append ( line . Content ) ;
423+ writer . WriteLine ( $ " { line . Content } " ) ;
425424 }
426425 else if ( line . Type == TextDiffLineType . Added )
427426 {
428427 if ( isOldSide )
429428 {
430429 if ( revert )
431430 {
432- builder . Append ( " \n " ) . Append ( line . Content ) ;
431+ writer . WriteLine ( $ " { line . Content } " ) ;
433432 }
434433 else
435434 {
@@ -438,20 +437,20 @@ public void GeneratePatchFromSelectionSingleSide(Change change, string fileTreeG
438437 }
439438 else
440439 {
441- builder . Append ( " \n +" ) . Append ( line . Content ) ;
440+ writer . WriteLine ( $ "+ { line . Content } " ) ;
442441 }
443442 }
444443 else if ( line . Type == TextDiffLineType . Deleted )
445444 {
446445 if ( isOldSide )
447446 {
448- builder . Append ( " \n -" ) . Append ( line . Content ) ;
447+ writer . WriteLine ( $ "- { line . Content } " ) ;
449448 }
450449 else
451450 {
452451 if ( ! revert )
453452 {
454- builder . Append ( " \n " ) . Append ( line . Content ) ;
453+ writer . WriteLine ( $ " { line . Content } " ) ;
455454 }
456455 else
457456 {
@@ -461,12 +460,11 @@ public void GeneratePatchFromSelectionSingleSide(Change change, string fileTreeG
461460 }
462461 }
463462
464- builder . Append ( "\n " ) . Append ( tail ) ;
465- builder . Append ( "\n " ) ;
466- System . IO . File . WriteAllText ( output , builder . ToString ( ) ) ;
463+ writer . WriteLine ( $ " { tail } ") ;
464+ writer . Flush ( ) ;
467465 }
468466
469- private bool ProcessIndicatorForPatch ( StringBuilder builder , TextDiffLine indicator , int idx , int start , int end , int ignoreRemoves , int ignoreAdds , bool revert , bool tailed )
467+ private bool ProcessIndicatorForPatch ( StreamWriter writer , TextDiffLine indicator , int idx , int start , int end , int ignoreRemoves , int ignoreAdds , bool revert , bool tailed )
470468 {
471469 var match = REG_INDICATOR ( ) . Match ( indicator . Content ) ;
472470 var oldStart = int . Parse ( match . Groups [ 1 ] . Value ) ;
@@ -531,11 +529,11 @@ private bool ProcessIndicatorForPatch(StringBuilder builder, TextDiffLine indica
531529 if ( oldCount == 0 && newCount == 0 )
532530 return false ;
533531
534- builder . Append ( $ "\n @@ -{ oldStart } ,{ oldCount } +{ newStart } ,{ newCount } @@") ;
532+ writer . WriteLine ( $ "@@ -{ oldStart } ,{ oldCount } +{ newStart } ,{ newCount } @@") ;
535533 return true ;
536534 }
537535
538- private bool ProcessIndicatorForPatchSingleSide ( StringBuilder builder , TextDiffLine indicator , int idx , int start , int end , int ignoreRemoves , int ignoreAdds , bool revert , bool isOldSide , bool tailed )
536+ private bool ProcessIndicatorForPatchSingleSide ( StreamWriter writer , TextDiffLine indicator , int idx , int start , int end , int ignoreRemoves , int ignoreAdds , bool revert , bool isOldSide , bool tailed )
539537 {
540538 var match = REG_INDICATOR ( ) . Match ( indicator . Content ) ;
541539 var oldStart = int . Parse ( match . Groups [ 1 ] . Value ) ;
@@ -611,7 +609,7 @@ private bool ProcessIndicatorForPatchSingleSide(StringBuilder builder, TextDiffL
611609 if ( oldCount == 0 && newCount == 0 )
612610 return false ;
613611
614- builder . Append ( $ "\n @@ -{ oldStart } ,{ oldCount } +{ newStart } ,{ newCount } @@") ;
612+ writer . WriteLine ( $ "@@ -{ oldStart } ,{ oldCount } +{ newStart } ,{ newCount } @@") ;
615613 return true ;
616614 }
617615
0 commit comments