Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit ee3a651

Browse files
authored
Merge pull request #2009 from github/fixes/1908-unicode-error
Don't change file encoding unnecessarily.
2 parents 82a2ccd + f1bab19 commit ee3a651

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/GitHub.App/Services/PullRequestService.cs

+11-3
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ public Encoding GetEncoding(ILocalRepositoryModel repository, string relativePat
769769
}
770770
}
771771

772-
return Encoding.Default;
772+
return null;
773773
}
774774

775775
static bool HasPreamble(string file, Encoding encoding)
@@ -877,7 +877,15 @@ async Task ExtractToTempFile(
877877
}
878878

879879
Directory.CreateDirectory(Path.GetDirectoryName(tempFilePath));
880-
File.WriteAllText(tempFilePath, contents, encoding);
880+
881+
if (encoding != null)
882+
{
883+
File.WriteAllText(tempFilePath, contents, encoding);
884+
}
885+
else
886+
{
887+
File.WriteAllText(tempFilePath, contents);
888+
}
881889
}
882890

883891
IEnumerable<string> GetLocalBranchesInternal(
@@ -963,7 +971,7 @@ static string CalculateTempFileName(string relativePath, string commitSha, Encod
963971
{
964972
// The combination of relative path, commit SHA and encoding should be sufficient to uniquely identify a file.
965973
var relativeDir = Path.GetDirectoryName(relativePath) ?? string.Empty;
966-
var key = relativeDir + '|' + encoding.WebName;
974+
var key = relativeDir + '|' + (encoding?.WebName ?? "unknown");
967975
var relativePathHash = key.GetSha256Hash();
968976
var tempDir = Path.Combine(Path.GetTempPath(), "GitHubVisualStudio", "FileContents", relativePathHash);
969977
var tempFileName = Invariant($"{Path.GetFileNameWithoutExtension(relativePath)}@{commitSha}{Path.GetExtension(relativePath)}");

src/GitHub.Exports.Reactive/Services/IPullRequestService.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,12 @@ IObservable<IPullRequestModel> CreatePullRequest(IModelService modelService,
176176
IObservable<Tuple<string, int>> GetPullRequestForCurrentBranch(ILocalRepositoryModel repository);
177177

178178
/// <summary>
179-
/// Gets the encoding for the specified file.
179+
/// Gets the encoding for the specified local file.
180180
/// </summary>
181181
/// <param name="repository">The repository.</param>
182182
/// <param name="relativePath">The relative path to the file in the repository.</param>
183183
/// <returns>
184-
/// The file's encoding or <see cref="Encoding.Default"/> if the file doesn't exist.
184+
/// The file's encoding or null if the file doesn't exist locally.
185185
/// </returns>
186186
Encoding GetEncoding(ILocalRepositoryModel repository, string relativePath);
187187

@@ -192,7 +192,9 @@ IObservable<IPullRequestModel> CreatePullRequest(IModelService modelService,
192192
/// <param name="pullRequest">The pull request details.</param>
193193
/// <param name="relativePath">The path to the file, relative to the repository root.</param>
194194
/// <param name="commitSha">The SHA of the commit.</param>
195-
/// <param name="encoding">The encoding to use.</param>
195+
/// <param name="encoding">
196+
/// The encoding to save the file with. If null, will use the file's original encoding.
197+
/// </param>
196198
/// <returns>The path to the temporary file.</returns>
197199
Task<string> ExtractToTempFile(
198200
ILocalRepositoryModel repository,

0 commit comments

Comments
 (0)