@@ -299,15 +299,53 @@ public IObservable<Tuple<string, string>> ExtractDiffFiles(
299
299
throw new FileNotFoundException ( $ "Couldn't find merge base between { baseSha } and { headSha } .") ;
300
300
}
301
301
302
- // We've found the merge base so these should already be fetched.
303
- var left = await ExtractToTempFile ( repo , mergeBase , fileName ) ;
304
- var right = isPullRequestBranchCheckedOut ?
305
- Path . Combine ( repository . LocalPath , fileName ) : await ExtractToTempFile ( repo , headSha , fileName ) ;
302
+ string left ;
303
+ string right ;
304
+ if ( isPullRequestBranchCheckedOut )
305
+ {
306
+ right = Path . Combine ( repository . LocalPath , fileName ) ;
307
+ left = await ExtractToTempFile ( repo , mergeBase , fileName , GetEncoding ( right ) ) ;
308
+ }
309
+ else
310
+ {
311
+ left = await ExtractToTempFile ( repo , mergeBase , fileName , Encoding . Default ) ;
312
+ right = await ExtractToTempFile ( repo , headSha , fileName , Encoding . Default ) ;
313
+ }
306
314
307
315
return Observable . Return ( Tuple . Create ( left , right ) ) ;
308
316
} ) ;
309
317
}
310
318
319
+ static Encoding GetEncoding ( string file )
320
+ {
321
+ if ( File . Exists ( file ) )
322
+ {
323
+ var encoding = Encoding . UTF8 ;
324
+ if ( IsEncoding ( file , encoding ) )
325
+ {
326
+ return encoding ;
327
+ }
328
+ }
329
+
330
+ return Encoding . Default ;
331
+ }
332
+
333
+ static bool IsEncoding ( string file , Encoding encoding )
334
+ {
335
+ using ( var stream = File . OpenRead ( file ) )
336
+ {
337
+ foreach ( var b in encoding . GetPreamble ( ) )
338
+ {
339
+ if ( b != stream . ReadByte ( ) )
340
+ {
341
+ return false ;
342
+ }
343
+ }
344
+ }
345
+
346
+ return true ;
347
+ }
348
+
311
349
public IObservable < Unit > RemoveUnusedRemotes ( ILocalRepositoryModel repository )
312
350
{
313
351
return Observable . Defer ( async ( ) =>
@@ -362,20 +400,20 @@ string CreateUniqueRemoteName(IRepository repo, string name)
362
400
return uniqueName ;
363
401
}
364
402
365
- async Task < string > ExtractToTempFile ( IRepository repo , string commitSha , string fileName )
403
+ async Task < string > ExtractToTempFile ( IRepository repo , string commitSha , string fileName , Encoding encoding )
366
404
{
367
405
var contents = await gitClient . ExtractFile ( repo , commitSha , fileName ) ?? string . Empty ;
368
- return CreateTempFile ( fileName , commitSha , contents ) ;
406
+ return CreateTempFile ( fileName , commitSha , contents , encoding ) ;
369
407
}
370
408
371
- static string CreateTempFile ( string fileName , string commitSha , string contents )
409
+ static string CreateTempFile ( string fileName , string commitSha , string contents , Encoding encoding )
372
410
{
373
411
var tempDir = Path . Combine ( Path . GetTempPath ( ) , Guid . NewGuid ( ) . ToString ( ) ) ;
374
412
var tempFileName = $ "{ Path . GetFileNameWithoutExtension ( fileName ) } @{ commitSha } { Path . GetExtension ( fileName ) } ";
375
413
var tempFile = Path . Combine ( tempDir , tempFileName ) ;
376
414
377
415
Directory . CreateDirectory ( tempDir ) ;
378
- File . WriteAllText ( tempFile , contents , Encoding . UTF8 ) ;
416
+ File . WriteAllText ( tempFile , contents , encoding ) ;
379
417
return tempFile ;
380
418
}
381
419
0 commit comments