From 84c37c98272095fdf5473d2df3c67b45bfe9807a Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Tue, 23 Oct 2018 15:56:01 +0200 Subject: [PATCH] Don't change file encoding unnecessarily. When a file doesn't exist locally, don't change it's encoding to `Encoding.Default`, just leave it be. Fixes #1908. --- src/GitHub.App/Services/PullRequestService.cs | 14 +++++++++++--- .../Services/IPullRequestService.cs | 8 +++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/GitHub.App/Services/PullRequestService.cs b/src/GitHub.App/Services/PullRequestService.cs index 1790a43cd8..9b8015cb43 100644 --- a/src/GitHub.App/Services/PullRequestService.cs +++ b/src/GitHub.App/Services/PullRequestService.cs @@ -769,7 +769,7 @@ public Encoding GetEncoding(ILocalRepositoryModel repository, string relativePat } } - return Encoding.Default; + return null; } static bool HasPreamble(string file, Encoding encoding) @@ -877,7 +877,15 @@ async Task ExtractToTempFile( } Directory.CreateDirectory(Path.GetDirectoryName(tempFilePath)); - File.WriteAllText(tempFilePath, contents, encoding); + + if (encoding != null) + { + File.WriteAllText(tempFilePath, contents, encoding); + } + else + { + File.WriteAllText(tempFilePath, contents); + } } IEnumerable GetLocalBranchesInternal( @@ -963,7 +971,7 @@ static string CalculateTempFileName(string relativePath, string commitSha, Encod { // The combination of relative path, commit SHA and encoding should be sufficient to uniquely identify a file. var relativeDir = Path.GetDirectoryName(relativePath) ?? string.Empty; - var key = relativeDir + '|' + encoding.WebName; + var key = relativeDir + '|' + (encoding?.WebName ?? "unknown"); var relativePathHash = key.GetSha256Hash(); var tempDir = Path.Combine(Path.GetTempPath(), "GitHubVisualStudio", "FileContents", relativePathHash); var tempFileName = Invariant($"{Path.GetFileNameWithoutExtension(relativePath)}@{commitSha}{Path.GetExtension(relativePath)}"); diff --git a/src/GitHub.Exports.Reactive/Services/IPullRequestService.cs b/src/GitHub.Exports.Reactive/Services/IPullRequestService.cs index 72ac6ec532..48837ce2d6 100644 --- a/src/GitHub.Exports.Reactive/Services/IPullRequestService.cs +++ b/src/GitHub.Exports.Reactive/Services/IPullRequestService.cs @@ -176,12 +176,12 @@ IObservable CreatePullRequest(IModelService modelService, IObservable> GetPullRequestForCurrentBranch(ILocalRepositoryModel repository); /// - /// Gets the encoding for the specified file. + /// Gets the encoding for the specified local file. /// /// The repository. /// The relative path to the file in the repository. /// - /// The file's encoding or if the file doesn't exist. + /// The file's encoding or null if the file doesn't exist locally. /// Encoding GetEncoding(ILocalRepositoryModel repository, string relativePath); @@ -192,7 +192,9 @@ IObservable CreatePullRequest(IModelService modelService, /// The pull request details. /// The path to the file, relative to the repository root. /// The SHA of the commit. - /// The encoding to use. + /// + /// The encoding to save the file with. If null, will use the file's original encoding. + /// /// The path to the temporary file. Task ExtractToTempFile( ILocalRepositoryModel repository,