1
1
using System . Collections . Generic ;
2
- using System . Text ;
2
+ using System . IO ;
3
3
using System . Text . RegularExpressions ;
4
4
5
5
using Avalonia ;
@@ -148,13 +148,13 @@ public void GenerateNewPatchFromSelection(Change change, string fileBlobGuid, Te
148
148
var isTracked = ! string . IsNullOrEmpty ( fileBlobGuid ) ;
149
149
var fileGuid = isTracked ? fileBlobGuid : "00000000" ;
150
150
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 } " ) ;
153
153
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 } " ) ;
158
158
159
159
var additions = selection . EndLine - selection . StartLine ;
160
160
if ( selection . StartLine != 1 )
@@ -163,40 +163,40 @@ public void GenerateNewPatchFromSelection(Change change, string fileBlobGuid, Te
163
163
if ( revert )
164
164
{
165
165
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 } @@") ;
167
167
for ( int i = 1 ; i <= totalLines ; i ++ )
168
168
{
169
169
var line = Lines [ i ] ;
170
170
if ( line . Type != TextDiffLineType . Added )
171
171
continue ;
172
- builder . Append ( selection . IsInRange ( i ) ? "\n +" : "\n " ) . Append ( line . Content ) ;
172
+ writer . WriteLine ( $ " { ( selection . IsInRange ( i ) ? "+" : " " ) } { line . Content } " ) ;
173
173
}
174
174
}
175
175
else
176
176
{
177
- builder . Append ( "@@ -0,0 +0," ) . Append ( additions ) . Append ( " @@") ;
177
+ writer . WriteLine ( $ "@@ -0,0 +0,{ additions } @@") ;
178
178
for ( int i = selection . StartLine - 1 ; i < selection . EndLine ; i ++ )
179
179
{
180
180
var line = Lines [ i ] ;
181
181
if ( line . Type != TextDiffLineType . Added )
182
182
continue ;
183
- builder . Append ( " \n +" ) . Append ( line . Content ) ;
183
+ writer . WriteLine ( $ "+ { line . Content } " ) ;
184
184
}
185
185
}
186
186
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 ( ) ;
189
189
}
190
190
191
191
public void GeneratePatchFromSelection ( Change change , string fileTreeGuid , TextDiffSelection selection , bool revert , string output )
192
192
{
193
193
var orgFile = ! string . IsNullOrEmpty ( change . OriginalPath ) ? change . OriginalPath : change . Path ;
194
194
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 } " ) ;
200
200
201
201
// If last line of selection is a change. Find one more line.
202
202
var tail = null as string ;
@@ -264,21 +264,21 @@ public void GeneratePatchFromSelection(Change change, string fileTreeGuid, TextD
264
264
var line = Lines [ i ] ;
265
265
if ( line . Type == TextDiffLineType . Indicator )
266
266
{
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 ) ;
268
268
}
269
269
else if ( line . Type == TextDiffLineType . Added )
270
270
{
271
271
if ( revert )
272
- builder . Append ( " \n " ) . Append ( line . Content ) ;
272
+ writer . WriteLine ( $ " { line . Content } " ) ;
273
273
}
274
274
else if ( line . Type == TextDiffLineType . Deleted )
275
275
{
276
276
if ( ! revert )
277
- builder . Append ( " \n " ) . Append ( line . Content ) ;
277
+ writer . WriteLine ( $ " { line . Content } " ) ;
278
278
}
279
279
else if ( line . Type == TextDiffLineType . Normal )
280
280
{
281
- builder . Append ( " \n " ) . Append ( line . Content ) ;
281
+ writer . WriteLine ( $ " { line . Content } " ) ;
282
282
}
283
283
}
284
284
}
@@ -289,39 +289,38 @@ public void GeneratePatchFromSelection(Change change, string fileTreeGuid, TextD
289
289
var line = Lines [ i ] ;
290
290
if ( line . Type == TextDiffLineType . Indicator )
291
291
{
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 ) )
293
293
{
294
294
break ;
295
295
}
296
296
}
297
297
else if ( line . Type == TextDiffLineType . Normal )
298
298
{
299
- builder . Append ( " \n " ) . Append ( line . Content ) ;
299
+ writer . WriteLine ( $ " { line . Content } " ) ;
300
300
}
301
301
else if ( line . Type == TextDiffLineType . Added )
302
302
{
303
- builder . Append ( " \n +" ) . Append ( line . Content ) ;
303
+ writer . WriteLine ( $ "+ { line . Content } " ) ;
304
304
}
305
305
else if ( line . Type == TextDiffLineType . Deleted )
306
306
{
307
- builder . Append ( " \n -" ) . Append ( line . Content ) ;
307
+ writer . WriteLine ( $ "- { line . Content } " ) ;
308
308
}
309
309
}
310
310
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 ( ) ;
314
313
}
315
314
316
315
public void GeneratePatchFromSelectionSingleSide ( Change change , string fileTreeGuid , TextDiffSelection selection , bool revert , bool isOldSide , string output )
317
316
{
318
317
var orgFile = ! string . IsNullOrEmpty ( change . OriginalPath ) ? change . OriginalPath : change . Path ;
319
318
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 } " ) ;
325
324
326
325
// If last line of selection is a change. Find one more line.
327
326
var tail = null as string ;
@@ -389,21 +388,21 @@ public void GeneratePatchFromSelectionSingleSide(Change change, string fileTreeG
389
388
var line = Lines [ i ] ;
390
389
if ( line . Type == TextDiffLineType . Indicator )
391
390
{
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 ) ;
393
392
}
394
393
else if ( line . Type == TextDiffLineType . Added )
395
394
{
396
395
if ( revert )
397
- builder . Append ( " \n " ) . Append ( line . Content ) ;
396
+ writer . WriteLine ( $ " { line . Content } " ) ;
398
397
}
399
398
else if ( line . Type == TextDiffLineType . Deleted )
400
399
{
401
400
if ( ! revert )
402
- builder . Append ( " \n " ) . Append ( line . Content ) ;
401
+ writer . WriteLine ( $ " { line . Content } " ) ;
403
402
}
404
403
else if ( line . Type == TextDiffLineType . Normal )
405
404
{
406
- builder . Append ( " \n " ) . Append ( line . Content ) ;
405
+ writer . WriteLine ( $ " { line . Content } " ) ;
407
406
}
408
407
}
409
408
}
@@ -414,22 +413,22 @@ public void GeneratePatchFromSelectionSingleSide(Change change, string fileTreeG
414
413
var line = Lines [ i ] ;
415
414
if ( line . Type == TextDiffLineType . Indicator )
416
415
{
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 ) )
418
417
{
419
418
break ;
420
419
}
421
420
}
422
421
else if ( line . Type == TextDiffLineType . Normal )
423
422
{
424
- builder . Append ( " \n " ) . Append ( line . Content ) ;
423
+ writer . WriteLine ( $ " { line . Content } " ) ;
425
424
}
426
425
else if ( line . Type == TextDiffLineType . Added )
427
426
{
428
427
if ( isOldSide )
429
428
{
430
429
if ( revert )
431
430
{
432
- builder . Append ( " \n " ) . Append ( line . Content ) ;
431
+ writer . WriteLine ( $ " { line . Content } " ) ;
433
432
}
434
433
else
435
434
{
@@ -438,20 +437,20 @@ public void GeneratePatchFromSelectionSingleSide(Change change, string fileTreeG
438
437
}
439
438
else
440
439
{
441
- builder . Append ( " \n +" ) . Append ( line . Content ) ;
440
+ writer . WriteLine ( $ "+ { line . Content } " ) ;
442
441
}
443
442
}
444
443
else if ( line . Type == TextDiffLineType . Deleted )
445
444
{
446
445
if ( isOldSide )
447
446
{
448
- builder . Append ( " \n -" ) . Append ( line . Content ) ;
447
+ writer . WriteLine ( $ "- { line . Content } " ) ;
449
448
}
450
449
else
451
450
{
452
451
if ( ! revert )
453
452
{
454
- builder . Append ( " \n " ) . Append ( line . Content ) ;
453
+ writer . WriteLine ( $ " { line . Content } " ) ;
455
454
}
456
455
else
457
456
{
@@ -461,12 +460,11 @@ public void GeneratePatchFromSelectionSingleSide(Change change, string fileTreeG
461
460
}
462
461
}
463
462
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 ( ) ;
467
465
}
468
466
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 )
470
468
{
471
469
var match = REG_INDICATOR ( ) . Match ( indicator . Content ) ;
472
470
var oldStart = int . Parse ( match . Groups [ 1 ] . Value ) ;
@@ -531,11 +529,11 @@ private bool ProcessIndicatorForPatch(StringBuilder builder, TextDiffLine indica
531
529
if ( oldCount == 0 && newCount == 0 )
532
530
return false ;
533
531
534
- builder . Append ( $ "\n @@ -{ oldStart } ,{ oldCount } +{ newStart } ,{ newCount } @@") ;
532
+ writer . WriteLine ( $ "@@ -{ oldStart } ,{ oldCount } +{ newStart } ,{ newCount } @@") ;
535
533
return true ;
536
534
}
537
535
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 )
539
537
{
540
538
var match = REG_INDICATOR ( ) . Match ( indicator . Content ) ;
541
539
var oldStart = int . Parse ( match . Groups [ 1 ] . Value ) ;
@@ -611,7 +609,7 @@ private bool ProcessIndicatorForPatchSingleSide(StringBuilder builder, TextDiffL
611
609
if ( oldCount == 0 && newCount == 0 )
612
610
return false ;
613
611
614
- builder . Append ( $ "\n @@ -{ oldStart } ,{ oldCount } +{ newStart } ,{ newCount } @@") ;
612
+ writer . WriteLine ( $ "@@ -{ oldStart } ,{ oldCount } +{ newStart } ,{ newCount } @@") ;
615
613
return true ;
616
614
}
617
615
0 commit comments