From 4f6b57efb03ca76db498539380b75a8d548c748f Mon Sep 17 00:00:00 2001 From: Jamie Cansdale Date: Mon, 10 Jun 2019 16:11:51 +0100 Subject: [PATCH 01/11] Pass Git path to PullRequestTextBufferInfo Consistently new up PullRequestTextBufferInfo with Git relative path. --- .../Services/PullRequestEditorService.cs | 27 +++++++++++++++---- .../Services/PullRequestSession.cs | 16 ----------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/GitHub.App/Services/PullRequestEditorService.cs b/src/GitHub.App/Services/PullRequestEditorService.cs index 597519f7ca..ce3c224396 100644 --- a/src/GitHub.App/Services/PullRequestEditorService.cs +++ b/src/GitHub.App/Services/PullRequestEditorService.cs @@ -119,8 +119,12 @@ public async Task OpenFile( if (!workingDirectory) { - AddBufferTag(wpfTextView.TextBuffer, session, fullPath, commitSha, null); - EnableNavigateToEditor(textView, session); + var gitPath = FindGitPath(session.LocalRepository, fullPath); + if (gitPath != null) + { + AddBufferTag(wpfTextView.TextBuffer, session, gitPath, commitSha, null); + EnableNavigateToEditor(textView, session); + } } } @@ -485,6 +489,17 @@ static string GetAbsolutePath(LocalRepositoryModel localRepository, string relat return Path.Combine(localPath, relativePath); } + static string FindGitPath(LocalRepositoryModel localRepository, string path) + { + var basePath = localRepository.LocalPath + Path.DirectorySeparatorChar; + if (path.StartsWith(basePath, StringComparison.OrdinalIgnoreCase)) + { + return path.Substring(basePath.Length).Replace(Path.DirectorySeparatorChar, '/'); + } + + return null; + } + string GetText(IVsTextView textView) { IVsTextLines buffer; @@ -561,13 +576,15 @@ void ShowErrorInStatusBar(string message, Exception e) void AddBufferTag( ITextBuffer buffer, IPullRequestSession session, - string path, + string gitPath, string commitSha, DiffSide? side) { + Guard.ArgumentIsGitPath(gitPath, nameof(gitPath)); + buffer.Properties.GetOrCreateSingletonProperty( typeof(PullRequestTextBufferInfo), - () => new PullRequestTextBufferInfo(session, path, commitSha, side)); + () => new PullRequestTextBufferInfo(session, gitPath, commitSha, side)); var projection = buffer as IProjectionBuffer; @@ -575,7 +592,7 @@ void AddBufferTag( { foreach (var source in projection.SourceBuffers) { - AddBufferTag(source, session, path, commitSha, side); + AddBufferTag(source, session, gitPath, commitSha, side); } } } diff --git a/src/GitHub.InlineReviews/Services/PullRequestSession.cs b/src/GitHub.InlineReviews/Services/PullRequestSession.cs index 783254237e..284f7cd37c 100644 --- a/src/GitHub.InlineReviews/Services/PullRequestSession.cs +++ b/src/GitHub.InlineReviews/Services/PullRequestSession.cs @@ -110,22 +110,6 @@ public async Task GetMergeBase() return mergeBase; } - /// - public string GetRelativePath(string path) - { - if (Path.IsPathRooted(path)) - { - var basePath = LocalRepository.LocalPath; - - if (path.StartsWith(basePath, StringComparison.OrdinalIgnoreCase) && path.Length > basePath.Length + 1) - { - return path.Substring(basePath.Length + 1); - } - } - - return null; - } - /// public async Task PostReviewComment( string body, From 8ae29c8cddd9dd74a34c897c0ab37d9a4b3716d0 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Wed, 12 Jun 2019 11:06:06 -0400 Subject: [PATCH 02/11] FindGitRelativePath should always return a path or error co-authoried-by: Jamie Cansdale --- src/GitHub.App/Services/PullRequestEditorService.cs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/GitHub.App/Services/PullRequestEditorService.cs b/src/GitHub.App/Services/PullRequestEditorService.cs index ce3c224396..1ab40754ec 100644 --- a/src/GitHub.App/Services/PullRequestEditorService.cs +++ b/src/GitHub.App/Services/PullRequestEditorService.cs @@ -119,12 +119,9 @@ public async Task OpenFile( if (!workingDirectory) { - var gitPath = FindGitPath(session.LocalRepository, fullPath); - if (gitPath != null) - { - AddBufferTag(wpfTextView.TextBuffer, session, gitPath, commitSha, null); - EnableNavigateToEditor(textView, session); - } + var gitPath = ToGitPath(session.LocalRepository, fullPath); + AddBufferTag(wpfTextView.TextBuffer, session, gitPath, commitSha, null); + EnableNavigateToEditor(textView, session); } } @@ -489,7 +486,7 @@ static string GetAbsolutePath(LocalRepositoryModel localRepository, string relat return Path.Combine(localPath, relativePath); } - static string FindGitPath(LocalRepositoryModel localRepository, string path) + static string ToGitPath(LocalRepositoryModel localRepository, string path) { var basePath = localRepository.LocalPath + Path.DirectorySeparatorChar; if (path.StartsWith(basePath, StringComparison.OrdinalIgnoreCase)) @@ -497,7 +494,7 @@ static string FindGitPath(LocalRepositoryModel localRepository, string path) return path.Substring(basePath.Length).Replace(Path.DirectorySeparatorChar, '/'); } - return null; + throw new ArgumentException($"Path '{path}' is not in the working directory '{localRepository.LocalPath}'"); } string GetText(IVsTextView textView) From 5fe36d67889e1d4ec3b7bf545d9525eb3d2bef55 Mon Sep 17 00:00:00 2001 From: Jamie Cansdale Date: Thu, 13 Jun 2019 12:11:10 +0100 Subject: [PATCH 03/11] Use relativePath and gitPath in a consistent way Use Guard.ArgumentIsRelativePath instead of Guard.ArgumentIsGitPath Automatically convert to Git path where required. --- .../SampleData/GitServiceDesigner.cs | 2 +- src/GitHub.App/Services/GitClient.cs | 29 ++++++++++--------- .../Services/GitHubContextService.cs | 11 +++---- .../Services/PullRequestEditorService.cs | 13 ++++----- src/GitHub.App/Services/PullRequestService.cs | 8 ++--- ...PullRequestReviewCommentThreadViewModel.cs | 7 +++-- src/GitHub.Exports/Services/GitService.cs | 16 +++++----- src/GitHub.Exports/Services/IGitService.cs | 4 +-- src/GitHub.Extensions/Guard.cs | 7 +---- .../Services/DiffService.cs | 4 +-- .../Services/PullRequestSession.cs | 6 ++-- .../Services/PullRequestSessionService.cs | 8 ++--- 12 files changed, 57 insertions(+), 58 deletions(-) diff --git a/src/GitHub.App/SampleData/GitServiceDesigner.cs b/src/GitHub.App/SampleData/GitServiceDesigner.cs index 522bde697b..1e125d1a6e 100644 --- a/src/GitHub.App/SampleData/GitServiceDesigner.cs +++ b/src/GitHub.App/SampleData/GitServiceDesigner.cs @@ -15,7 +15,7 @@ class GitServiceDesigner : IGitService public IRepository GetRepository(string path) => null; public UriString GetUri(string path, string remote = "origin") => null; public UriString GetUri(IRepository repository, string remote = "origin") => null; - public Task Compare(IRepository repository, string sha1, string sha2, string path) => null; + public Task Compare(IRepository repository, string sha1, string sha2, string relativePath) => null; public Task CompareWith(IRepository repository, string sha1, string sha2, string path, byte[] contents) => null; public Task Compare(IRepository repository, string sha1, string sha2, bool detectRenames = false) => null; } diff --git a/src/GitHub.App/Services/GitClient.cs b/src/GitHub.App/Services/GitClient.cs index 79dfb1df4c..a5d7efa92c 100644 --- a/src/GitHub.App/Services/GitClient.cs +++ b/src/GitHub.App/Services/GitClient.cs @@ -259,40 +259,42 @@ public Task GetHttpRemote(IRepository repo, string remote) }); } - public Task ExtractFile(IRepository repository, string commitSha, string fileName) + public Task ExtractFile(IRepository repository, string commitSha, string relativePath) { Guard.ArgumentNotNull(repository, nameof(repository)); Guard.ArgumentNotEmptyString(commitSha, nameof(commitSha)); - Guard.ArgumentIsGitPath(fileName, nameof(fileName)); + Guard.ArgumentIsRelativePath(relativePath, nameof(relativePath)); + var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/'); return Task.Run(() => { var commit = repository.Lookup(commitSha); if (commit == null) { - throw new FileNotFoundException("Couldn't find '" + fileName + "' at commit " + commitSha + "."); + throw new FileNotFoundException("Couldn't find '" + gitPath + "' at commit " + commitSha + "."); } - var blob = commit[fileName]?.Target as Blob; + var blob = commit[gitPath]?.Target as Blob; return blob?.GetContentText(); }); } - public Task ExtractFileBinary(IRepository repository, string commitSha, string fileName) + public Task ExtractFileBinary(IRepository repository, string commitSha, string relativePath) { Guard.ArgumentNotNull(repository, nameof(repository)); Guard.ArgumentNotEmptyString(commitSha, nameof(commitSha)); - Guard.ArgumentIsGitPath(fileName, nameof(fileName)); + Guard.ArgumentIsRelativePath(relativePath, nameof(relativePath)); + var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/'); return Task.Run(() => { var commit = repository.Lookup(commitSha); if (commit == null) { - throw new FileNotFoundException("Couldn't find '" + fileName + "' at commit " + commitSha + "."); + throw new FileNotFoundException("Couldn't find '" + gitPath + "' at commit " + commitSha + "."); } - var blob = commit[fileName]?.Target as Blob; + var blob = commit[gitPath]?.Target as Blob; if (blob != null) { @@ -308,16 +310,17 @@ public Task ExtractFileBinary(IRepository repository, string commitSha, }); } - public Task IsModified(IRepository repository, string path, byte[] contents) + public Task IsModified(IRepository repository, string relativePath, byte[] contents) { Guard.ArgumentNotNull(repository, nameof(repository)); - Guard.ArgumentIsGitPath(path, nameof(path)); + Guard.ArgumentIsRelativePath(relativePath, nameof(relativePath)); + var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/'); return Task.Run(() => { - if (repository.RetrieveStatus(path) == FileStatus.Unaltered) + if (repository.RetrieveStatus(gitPath) == FileStatus.Unaltered) { - var treeEntry = repository.Head[path]; + var treeEntry = repository.Head[gitPath]; if (treeEntry?.TargetType != TreeEntryTargetType.Blob) { return false; @@ -326,7 +329,7 @@ public Task IsModified(IRepository repository, string path, byte[] content var blob1 = (Blob)treeEntry.Target; using (var s = contents != null ? new MemoryStream(contents) : new MemoryStream()) { - var blob2 = repository.ObjectDatabase.CreateBlob(s, path); + var blob2 = repository.ObjectDatabase.CreateBlob(s, gitPath); var diff = repository.Diff.Compare(blob1, blob2); return diff.LinesAdded != 0 || diff.LinesDeleted != 0; } diff --git a/src/GitHub.App/Services/GitHubContextService.cs b/src/GitHub.App/Services/GitHubContextService.cs index df14cdbfa2..2237c15af0 100644 --- a/src/GitHub.App/Services/GitHubContextService.cs +++ b/src/GitHub.App/Services/GitHubContextService.cs @@ -404,16 +404,17 @@ public string FindObjectishForTFSTempFile(string tempFile) } /// - public bool HasChangesInWorkingDirectory(string repositoryDir, string commitish, string path) + public bool HasChangesInWorkingDirectory(string repositoryDir, string commitish, string relativePath) { - Guard.ArgumentNotNull(path, nameof(repositoryDir)); - Guard.ArgumentNotNull(path, nameof(commitish)); - Guard.ArgumentIsGitPath(path, nameof(path)); + Guard.ArgumentNotNull(repositoryDir, nameof(repositoryDir)); + Guard.ArgumentNotNull(commitish, nameof(commitish)); + Guard.ArgumentIsRelativePath(relativePath, nameof(relativePath)); + var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/'); using (var repo = gitService.GetRepository(repositoryDir)) { var commit = repo.Lookup(commitish); - var paths = new[] { path }; + var paths = new[] { gitPath }; return repo.Diff.Compare(commit.Tree, DiffTargets.WorkingDirectory, paths).Count() > 0; } diff --git a/src/GitHub.App/Services/PullRequestEditorService.cs b/src/GitHub.App/Services/PullRequestEditorService.cs index 1ab40754ec..383adbad2c 100644 --- a/src/GitHub.App/Services/PullRequestEditorService.cs +++ b/src/GitHub.App/Services/PullRequestEditorService.cs @@ -573,15 +573,13 @@ void ShowErrorInStatusBar(string message, Exception e) void AddBufferTag( ITextBuffer buffer, IPullRequestSession session, - string gitPath, + string relativePath, string commitSha, DiffSide? side) { - Guard.ArgumentIsGitPath(gitPath, nameof(gitPath)); - buffer.Properties.GetOrCreateSingletonProperty( typeof(PullRequestTextBufferInfo), - () => new PullRequestTextBufferInfo(session, gitPath, commitSha, side)); + () => new PullRequestTextBufferInfo(session, relativePath, commitSha, side)); var projection = buffer as IProjectionBuffer; @@ -589,7 +587,7 @@ void AddBufferTag( { foreach (var source in projection.SourceBuffers) { - AddBufferTag(source, session, gitPath, commitSha, side); + AddBufferTag(source, session, relativePath, commitSha, side); } } } @@ -656,9 +654,10 @@ async Task GetBaseFileName(IPullRequestSession session, IPullRequestSess session.LocalRepository, session.PullRequest)) { - var fileChange = changes.FirstOrDefault(x => x.Path == file.RelativePath); + var gitPath = file.RelativePath.Replace(Path.DirectorySeparatorChar, '/'); + var fileChange = changes.FirstOrDefault(x => x.Path == gitPath); return fileChange?.Status == LibGit2Sharp.ChangeKind.Renamed ? - fileChange.OldPath : file.RelativePath; + fileChange.OldPath.Replace('/', Path.DirectorySeparatorChar) : file.RelativePath; } } diff --git a/src/GitHub.App/Services/PullRequestService.cs b/src/GitHub.App/Services/PullRequestService.cs index 7c143409ba..a895d38f67 100644 --- a/src/GitHub.App/Services/PullRequestService.cs +++ b/src/GitHub.App/Services/PullRequestService.cs @@ -922,24 +922,22 @@ async Task ExtractToTempFile( IRepository repo, int pullRequestNumber, string commitSha, - string path, + string relativePath, Encoding encoding, string tempFilePath) { - Guard.ArgumentIsGitPath(path, nameof(path)); - string contents; try { - contents = await gitClient.ExtractFile(repo, commitSha, path) ?? string.Empty; + contents = await gitClient.ExtractFile(repo, commitSha, relativePath) ?? string.Empty; } catch (FileNotFoundException) { var pullHeadRef = $"refs/pull/{pullRequestNumber}/head"; var remote = await gitClient.GetHttpRemote(repo, "origin"); await gitClient.Fetch(repo, remote.Name, commitSha, pullHeadRef); - contents = await gitClient.ExtractFile(repo, commitSha, path) ?? string.Empty; + contents = await gitClient.ExtractFile(repo, commitSha, relativePath) ?? string.Empty; } Directory.CreateDirectory(Path.GetDirectoryName(tempFilePath)); diff --git a/src/GitHub.App/ViewModels/PullRequestReviewCommentThreadViewModel.cs b/src/GitHub.App/ViewModels/PullRequestReviewCommentThreadViewModel.cs index f5ddb65bb6..efcf629a44 100644 --- a/src/GitHub.App/ViewModels/PullRequestReviewCommentThreadViewModel.cs +++ b/src/GitHub.App/ViewModels/PullRequestReviewCommentThreadViewModel.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.ComponentModel.Composition; using System.Globalization; +using System.IO; using System.Linq; using System.Reactive.Linq; using System.Threading.Tasks; @@ -197,7 +198,7 @@ public override async Task PostComment(ICommentViewModel comment) await Session.PostReviewComment( comment.Body, File.CommitSha, - File.RelativePath.Replace("\\", "/"), + File.RelativePath.Replace(Path.DirectorySeparatorChar, '/'), File.Diff, diffPosition.DiffLineNumber).ConfigureAwait(false); } @@ -234,8 +235,8 @@ public static (string key, string secondaryKey) GetDraftKeys( string relativePath, int lineNumber) { - relativePath = relativePath.Replace("\\", "/"); - var key = Invariant($"pr-review-comment|{cloneUri}|{pullRequestNumber}|{relativePath}"); + var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/'); + var key = Invariant($"pr-review-comment|{cloneUri}|{pullRequestNumber}|{gitPath}"); return (key, lineNumber.ToString(CultureInfo.InvariantCulture)); } diff --git a/src/GitHub.Exports/Services/GitService.cs b/src/GitHub.Exports/Services/GitService.cs index e2e886acb7..8474babce5 100644 --- a/src/GitHub.Exports/Services/GitService.cs +++ b/src/GitHub.Exports/Services/GitService.cs @@ -234,13 +234,14 @@ public Task Compare( IRepository repository, string sha1, string sha2, - string path) + string relativePath) { Guard.ArgumentNotNull(repository, nameof(repository)); Guard.ArgumentNotEmptyString(sha1, nameof(sha1)); Guard.ArgumentNotEmptyString(sha2, nameof(sha2)); - Guard.ArgumentIsGitPath(path, nameof(path)); + Guard.ArgumentIsRelativePath(relativePath, nameof(relativePath)); + var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/'); return Task.Run(() => { var commit1 = repository.Lookup(sha1); @@ -251,7 +252,7 @@ public Task Compare( return repository.Diff.Compare( commit1.Tree, commit2.Tree, - new[] { path }, + new[] { gitPath }, defaultCompareOptions); } else @@ -261,27 +262,28 @@ public Task Compare( }); } - public Task CompareWith(IRepository repository, string sha1, string sha2, string path, byte[] contents) + public Task CompareWith(IRepository repository, string sha1, string sha2, string relativePath, byte[] contents) { Guard.ArgumentNotNull(repository, nameof(repository)); Guard.ArgumentNotEmptyString(sha1, nameof(sha1)); Guard.ArgumentNotEmptyString(sha2, nameof(sha1)); - Guard.ArgumentIsGitPath(path, nameof(path)); + Guard.ArgumentIsRelativePath(relativePath, nameof(relativePath)); + var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/'); return Task.Run(() => { var commit1 = repository.Lookup(sha1); var commit2 = repository.Lookup(sha2); var treeChanges = repository.Diff.Compare(commit1.Tree, commit2.Tree, defaultCompareOptions); - var change = treeChanges.FirstOrDefault(x => x.Path == path); + var change = treeChanges.FirstOrDefault(x => x.Path == gitPath); var oldPath = change?.OldPath; if (commit1 != null && oldPath != null) { var contentStream = contents != null ? new MemoryStream(contents) : new MemoryStream(); var blob1 = commit1[oldPath]?.Target as Blob ?? repository.ObjectDatabase.CreateBlob(new MemoryStream()); - var blob2 = repository.ObjectDatabase.CreateBlob(contentStream, path); + var blob2 = repository.ObjectDatabase.CreateBlob(contentStream, gitPath); return repository.Diff.Compare(blob1, blob2, defaultCompareOptions); } diff --git a/src/GitHub.Exports/Services/IGitService.cs b/src/GitHub.Exports/Services/IGitService.cs index 7ad16ad6de..1f183ced17 100644 --- a/src/GitHub.Exports/Services/IGitService.cs +++ b/src/GitHub.Exports/Services/IGitService.cs @@ -90,13 +90,13 @@ public interface IGitService /// The repository /// The SHA of the first commit. /// The SHA of the second commit. - /// The relative path to the file (using '/' directory separator). + /// The relative path to the file. /// The contents to compare with the file. /// /// A object or null if the commit could not be found in the repository. /// /// If contains a '\'. - Task CompareWith(IRepository repository, string sha1, string sha2, string path, byte[] contents); + Task CompareWith(IRepository repository, string sha1, string sha2, string relativePath, byte[] contents); /// /// Compares two commits. diff --git a/src/GitHub.Extensions/Guard.cs b/src/GitHub.Extensions/Guard.cs index 0862f8c3f0..82bf95c509 100644 --- a/src/GitHub.Extensions/Guard.cs +++ b/src/GitHub.Extensions/Guard.cs @@ -7,15 +7,10 @@ namespace GitHub.Extensions { public static class Guard { - public static void ArgumentIsGitPath(string value, string name) + public static void ArgumentIsRelativePath(string value, string name) { ArgumentNotNull(value, name); - if (value.Contains('\\')) - { - throw new ArgumentException($"The value '{value}' must use '/' not '\\' as directory separator", name); - } - if (Path.IsPathRooted(value)) { throw new ArgumentException($"The value '{value}' must not be rooted", name); diff --git a/src/GitHub.InlineReviews/Services/DiffService.cs b/src/GitHub.InlineReviews/Services/DiffService.cs index 8f685f93ac..1dccd7120e 100644 --- a/src/GitHub.InlineReviews/Services/DiffService.cs +++ b/src/GitHub.InlineReviews/Services/DiffService.cs @@ -29,9 +29,9 @@ public async Task> Diff( IRepository repo, string baseSha, string headSha, - string path) + string relativePath) { - var patch = await gitService.Compare(repo, baseSha, headSha, path); + var patch = await gitService.Compare(repo, baseSha, headSha, relativePath); if (patch != null) { diff --git a/src/GitHub.InlineReviews/Services/PullRequestSession.cs b/src/GitHub.InlineReviews/Services/PullRequestSession.cs index 284f7cd37c..b77af013a5 100644 --- a/src/GitHub.InlineReviews/Services/PullRequestSession.cs +++ b/src/GitHub.InlineReviews/Services/PullRequestSession.cs @@ -81,12 +81,12 @@ public async Task GetFile( try { PullRequestSessionFile file; - var normalizedPath = relativePath.Replace("\\", "/"); - var key = normalizedPath + '@' + commitSha; + var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/'); + var key = gitPath + '@' + commitSha; if (!fileIndex.TryGetValue(key, out file)) { - file = new PullRequestSessionFile(normalizedPath, commitSha); + file = new PullRequestSessionFile(relativePath, commitSha); await UpdateFile(file); fileIndex.Add(key, file); } diff --git a/src/GitHub.InlineReviews/Services/PullRequestSessionService.cs b/src/GitHub.InlineReviews/Services/PullRequestSessionService.cs index 368832be71..aa6288004b 100644 --- a/src/GitHub.InlineReviews/Services/PullRequestSessionService.cs +++ b/src/GitHub.InlineReviews/Services/PullRequestSessionService.cs @@ -95,13 +95,13 @@ public IReadOnlyList BuildAnnotations( PullRequestDetailModel pullRequest, string relativePath) { - relativePath = relativePath.Replace("\\", "/"); + var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/'); return pullRequest.CheckSuites ?.SelectMany(checkSuite => checkSuite.CheckRuns.Select(checkRun => new { checkSuite, checkRun })) .SelectMany(arg => arg.checkRun.Annotations - .Where(annotation => annotation.Path == relativePath) + .Where(annotation => annotation.Path == gitPath) .Select(annotation => new InlineAnnotationModel(arg.checkSuite, arg.checkRun, annotation))) .OrderBy(tuple => tuple.StartLine) .ToArray(); @@ -114,10 +114,10 @@ public IReadOnlyList BuildCommentThreads( IReadOnlyList diff, string headSha) { - relativePath = relativePath.Replace("\\", "/"); + var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/'); var threadsByPosition = pullRequest.Threads - .Where(x => x.Path == relativePath) + .Where(x => x.Path == gitPath) .OrderBy(x => x.Id) .GroupBy(x => Tuple.Create(x.OriginalCommitSha, x.OriginalPosition)); var threads = new List(); From f8c52cd127690f8728ed2fd5f6840a6ecd642b1e Mon Sep 17 00:00:00 2001 From: Jamie Cansdale Date: Thu, 13 Jun 2019 12:39:45 +0100 Subject: [PATCH 04/11] Factor out ToGitPath and ToRelativePath --- src/GitHub.App/Services/GitClient.cs | 6 +++--- .../Services/GitHubContextService.cs | 5 +++-- .../Services/PullRequestEditorService.cs | 9 +++++---- src/GitHub.App/Services/PullRequestService.cs | 2 +- ...PullRequestReviewCommentThreadViewModel.cs | 4 ++-- src/GitHub.Exports/Primitives/Paths.cs | 20 +++++++++++++++++++ src/GitHub.Exports/Services/GitService.cs | 4 ++-- .../Services/PullRequestSession.cs | 2 +- .../Services/PullRequestSessionService.cs | 4 ++-- 9 files changed, 39 insertions(+), 17 deletions(-) create mode 100644 src/GitHub.Exports/Primitives/Paths.cs diff --git a/src/GitHub.App/Services/GitClient.cs b/src/GitHub.App/Services/GitClient.cs index a5d7efa92c..96d22e6d97 100644 --- a/src/GitHub.App/Services/GitClient.cs +++ b/src/GitHub.App/Services/GitClient.cs @@ -265,7 +265,7 @@ public Task ExtractFile(IRepository repository, string commitSha, string Guard.ArgumentNotEmptyString(commitSha, nameof(commitSha)); Guard.ArgumentIsRelativePath(relativePath, nameof(relativePath)); - var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/'); + var gitPath = Paths.ToGitPath(relativePath); return Task.Run(() => { var commit = repository.Lookup(commitSha); @@ -285,7 +285,7 @@ public Task ExtractFileBinary(IRepository repository, string commitSha, Guard.ArgumentNotEmptyString(commitSha, nameof(commitSha)); Guard.ArgumentIsRelativePath(relativePath, nameof(relativePath)); - var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/'); + var gitPath = Paths.ToGitPath(relativePath); return Task.Run(() => { var commit = repository.Lookup(commitSha); @@ -315,7 +315,7 @@ public Task IsModified(IRepository repository, string relativePath, byte[] Guard.ArgumentNotNull(repository, nameof(repository)); Guard.ArgumentIsRelativePath(relativePath, nameof(relativePath)); - var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/'); + var gitPath = Paths.ToGitPath(relativePath); return Task.Run(() => { if (repository.RetrieveStatus(gitPath) == FileStatus.Unaltered) diff --git a/src/GitHub.App/Services/GitHubContextService.cs b/src/GitHub.App/Services/GitHubContextService.cs index 2237c15af0..6519283fba 100644 --- a/src/GitHub.App/Services/GitHubContextService.cs +++ b/src/GitHub.App/Services/GitHubContextService.cs @@ -282,7 +282,8 @@ public bool TryOpenFile(string repositoryDir, GitHubContext context) return false; } - var fullPath = Path.Combine(repositoryDir, path.Replace('/', '\\')); + var relativePath = Paths.ToRelativePath(path); + var fullPath = Path.Combine(repositoryDir, relativePath); var textView = OpenDocument(fullPath); SetSelection(textView, context); return true; @@ -410,7 +411,7 @@ public bool HasChangesInWorkingDirectory(string repositoryDir, string commitish, Guard.ArgumentNotNull(commitish, nameof(commitish)); Guard.ArgumentIsRelativePath(relativePath, nameof(relativePath)); - var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/'); + var gitPath = Paths.ToGitPath(relativePath); using (var repo = gitService.GetRepository(repositoryDir)) { var commit = repo.Lookup(commitish); diff --git a/src/GitHub.App/Services/PullRequestEditorService.cs b/src/GitHub.App/Services/PullRequestEditorService.cs index 383adbad2c..a9698a9d57 100644 --- a/src/GitHub.App/Services/PullRequestEditorService.cs +++ b/src/GitHub.App/Services/PullRequestEditorService.cs @@ -7,6 +7,7 @@ using System.Threading.Tasks; using EnvDTE; using GitHub.Commands; +using GitHub.Primitives; using GitHub.Extensions; using GitHub.Models; using GitHub.Models.Drafts; @@ -482,7 +483,7 @@ public static int FindNearestMatchingLine(IList fromLines, IList static string GetAbsolutePath(LocalRepositoryModel localRepository, string relativePath) { var localPath = localRepository.LocalPath; - relativePath = relativePath.Replace('/', Path.DirectorySeparatorChar); + relativePath = Paths.ToRelativePath(relativePath); return Path.Combine(localPath, relativePath); } @@ -491,7 +492,7 @@ static string ToGitPath(LocalRepositoryModel localRepository, string path) var basePath = localRepository.LocalPath + Path.DirectorySeparatorChar; if (path.StartsWith(basePath, StringComparison.OrdinalIgnoreCase)) { - return path.Substring(basePath.Length).Replace(Path.DirectorySeparatorChar, '/'); + return Paths.ToGitPath(path.Substring(basePath.Length)); } throw new ArgumentException($"Path '{path}' is not in the working directory '{localRepository.LocalPath}'"); @@ -654,10 +655,10 @@ async Task GetBaseFileName(IPullRequestSession session, IPullRequestSess session.LocalRepository, session.PullRequest)) { - var gitPath = file.RelativePath.Replace(Path.DirectorySeparatorChar, '/'); + var gitPath = Paths.ToGitPath(file.RelativePath); var fileChange = changes.FirstOrDefault(x => x.Path == gitPath); return fileChange?.Status == LibGit2Sharp.ChangeKind.Renamed ? - fileChange.OldPath.Replace('/', Path.DirectorySeparatorChar) : file.RelativePath; + Paths.ToRelativePath(fileChange.OldPath) : file.RelativePath; } } diff --git a/src/GitHub.App/Services/PullRequestService.cs b/src/GitHub.App/Services/PullRequestService.cs index a895d38f67..54f98852eb 100644 --- a/src/GitHub.App/Services/PullRequestService.cs +++ b/src/GitHub.App/Services/PullRequestService.cs @@ -779,7 +779,7 @@ public async Task ExtractToTempFile( Encoding encoding) { var tempFilePath = CalculateTempFileName(relativePath, commitSha, encoding); - var gitPath = relativePath.TrimStart('/').Replace('\\', '/'); + var gitPath = Paths.ToGitPath(relativePath); if (!File.Exists(tempFilePath)) { diff --git a/src/GitHub.App/ViewModels/PullRequestReviewCommentThreadViewModel.cs b/src/GitHub.App/ViewModels/PullRequestReviewCommentThreadViewModel.cs index efcf629a44..5d6b1a12c6 100644 --- a/src/GitHub.App/ViewModels/PullRequestReviewCommentThreadViewModel.cs +++ b/src/GitHub.App/ViewModels/PullRequestReviewCommentThreadViewModel.cs @@ -198,7 +198,7 @@ public override async Task PostComment(ICommentViewModel comment) await Session.PostReviewComment( comment.Body, File.CommitSha, - File.RelativePath.Replace(Path.DirectorySeparatorChar, '/'), + Paths.ToGitPath(File.RelativePath), File.Diff, diffPosition.DiffLineNumber).ConfigureAwait(false); } @@ -235,7 +235,7 @@ public static (string key, string secondaryKey) GetDraftKeys( string relativePath, int lineNumber) { - var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/'); + var gitPath = Paths.ToGitPath(relativePath); var key = Invariant($"pr-review-comment|{cloneUri}|{pullRequestNumber}|{gitPath}"); return (key, lineNumber.ToString(CultureInfo.InvariantCulture)); } diff --git a/src/GitHub.Exports/Primitives/Paths.cs b/src/GitHub.Exports/Primitives/Paths.cs new file mode 100644 index 0000000000..efe5cc4586 --- /dev/null +++ b/src/GitHub.Exports/Primitives/Paths.cs @@ -0,0 +1,20 @@ +using System; +using System.IO; + +namespace GitHub.Primitives +{ + public static class Paths + { + public const char GitDirectorySeparatorChar = '/'; + + public static string ToGitPath(string relativePath) + { + return relativePath.Replace(Path.DirectorySeparatorChar, GitDirectorySeparatorChar); + } + + public static string ToRelativePath(string relativePath) + { + return relativePath.Replace(GitDirectorySeparatorChar, Path.DirectorySeparatorChar); + } + } +} diff --git a/src/GitHub.Exports/Services/GitService.cs b/src/GitHub.Exports/Services/GitService.cs index 8474babce5..2ae7d650c5 100644 --- a/src/GitHub.Exports/Services/GitService.cs +++ b/src/GitHub.Exports/Services/GitService.cs @@ -241,7 +241,7 @@ public Task Compare( Guard.ArgumentNotEmptyString(sha2, nameof(sha2)); Guard.ArgumentIsRelativePath(relativePath, nameof(relativePath)); - var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/'); + var gitPath = Paths.ToGitPath(relativePath); return Task.Run(() => { var commit1 = repository.Lookup(sha1); @@ -269,7 +269,7 @@ public Task CompareWith(IRepository repository, string sha1, str Guard.ArgumentNotEmptyString(sha2, nameof(sha1)); Guard.ArgumentIsRelativePath(relativePath, nameof(relativePath)); - var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/'); + var gitPath = Paths.ToGitPath(relativePath); return Task.Run(() => { var commit1 = repository.Lookup(sha1); diff --git a/src/GitHub.InlineReviews/Services/PullRequestSession.cs b/src/GitHub.InlineReviews/Services/PullRequestSession.cs index b77af013a5..275780d8dd 100644 --- a/src/GitHub.InlineReviews/Services/PullRequestSession.cs +++ b/src/GitHub.InlineReviews/Services/PullRequestSession.cs @@ -81,7 +81,7 @@ public async Task GetFile( try { PullRequestSessionFile file; - var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/'); + var gitPath = Paths.ToGitPath(relativePath); var key = gitPath + '@' + commitSha; if (!fileIndex.TryGetValue(key, out file)) diff --git a/src/GitHub.InlineReviews/Services/PullRequestSessionService.cs b/src/GitHub.InlineReviews/Services/PullRequestSessionService.cs index aa6288004b..4cdfa9be41 100644 --- a/src/GitHub.InlineReviews/Services/PullRequestSessionService.cs +++ b/src/GitHub.InlineReviews/Services/PullRequestSessionService.cs @@ -95,7 +95,7 @@ public IReadOnlyList BuildAnnotations( PullRequestDetailModel pullRequest, string relativePath) { - var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/'); + var gitPath = Paths.ToGitPath(relativePath); return pullRequest.CheckSuites ?.SelectMany(checkSuite => checkSuite.CheckRuns.Select(checkRun => new { checkSuite, checkRun })) @@ -114,7 +114,7 @@ public IReadOnlyList BuildCommentThreads( IReadOnlyList diff, string headSha) { - var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/'); + var gitPath = Paths.ToGitPath(relativePath); var threadsByPosition = pullRequest.Threads .Where(x => x.Path == gitPath) From eec41c3a3a7df47a96af8f0f2f9cc236cb48c466 Mon Sep 17 00:00:00 2001 From: Jamie Cansdale Date: Thu, 13 Jun 2019 12:54:17 +0100 Subject: [PATCH 05/11] Fix GetOldFileName --- .../ViewModels/GitHubPane/PullRequestFilesViewModel.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/GitHub.App/ViewModels/GitHubPane/PullRequestFilesViewModel.cs b/src/GitHub.App/ViewModels/GitHubPane/PullRequestFilesViewModel.cs index e367d9611b..9be03eb613 100644 --- a/src/GitHub.App/ViewModels/GitHubPane/PullRequestFilesViewModel.cs +++ b/src/GitHub.App/ViewModels/GitHubPane/PullRequestFilesViewModel.cs @@ -12,6 +12,7 @@ using System.Windows.Input; using GitHub.Extensions; using GitHub.Models; +using GitHub.Primitives; using GitHub.Services; using LibGit2Sharp; using ReactiveUI; @@ -224,8 +225,8 @@ static string GetOldFileName(PullRequestFileModel file, TreeChanges changes) { if (file.Status == PullRequestFileStatus.Renamed) { - var fileName = file.FileName.Replace("/", "\\"); - return changes?.Renamed.FirstOrDefault(x => x.Path == fileName)?.OldPath; + var gitPath = Paths.ToGitPath(file.FileName); + return changes?.Renamed.FirstOrDefault(x => x.Path == gitPath)?.OldPath; } return null; From ff60575a0da1395f60f68472318cad22130a4a72 Mon Sep 17 00:00:00 2001 From: Jamie Cansdale Date: Thu, 13 Jun 2019 13:02:42 +0100 Subject: [PATCH 06/11] Use relativeOrGitPath when it doesn't matter --- .../GitHubPane/PullRequestDirectoryNode.cs | 9 +++++---- .../ViewModels/GitHubPane/PullRequestFileNode.cs | 13 +++++++------ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/GitHub.App/ViewModels/GitHubPane/PullRequestDirectoryNode.cs b/src/GitHub.App/ViewModels/GitHubPane/PullRequestDirectoryNode.cs index c270780ff8..9f85b6259e 100644 --- a/src/GitHub.App/ViewModels/GitHubPane/PullRequestDirectoryNode.cs +++ b/src/GitHub.App/ViewModels/GitHubPane/PullRequestDirectoryNode.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using GitHub.Primitives; namespace GitHub.ViewModels.GitHubPane { @@ -12,11 +13,11 @@ public class PullRequestDirectoryNode : IPullRequestDirectoryNode /// /// Initializes a new instance of the class. /// - /// The path to the directory, relative to the repository. - public PullRequestDirectoryNode(string relativePath) + /// The path to the directory, relative to the repository. + public PullRequestDirectoryNode(string relativeOrGitPath) { - DirectoryName = System.IO.Path.GetFileName(relativePath); - RelativePath = relativePath.Replace("/", "\\"); + DirectoryName = System.IO.Path.GetFileName(relativeOrGitPath); + RelativePath = Paths.ToRelativePath(relativeOrGitPath); Directories = new List(); Files = new List(); } diff --git a/src/GitHub.App/ViewModels/GitHubPane/PullRequestFileNode.cs b/src/GitHub.App/ViewModels/GitHubPane/PullRequestFileNode.cs index 374bd131ef..1a0f0a4c4c 100644 --- a/src/GitHub.App/ViewModels/GitHubPane/PullRequestFileNode.cs +++ b/src/GitHub.App/ViewModels/GitHubPane/PullRequestFileNode.cs @@ -3,6 +3,7 @@ using GitHub.App; using GitHub.Extensions; using GitHub.Models; +using GitHub.Primitives; using ReactiveUI; namespace GitHub.ViewModels.GitHubPane @@ -21,7 +22,7 @@ public class PullRequestFileNode : ReactiveObject, IPullRequestFileNode /// Initializes a new instance of the class. /// /// The absolute path to the repository. - /// The path to the file, relative to the repository. + /// The path to the file, relative to the repository. /// The SHA of the file. /// The way the file was changed. /// The string to display in the [message] box next to the filename. @@ -31,17 +32,17 @@ public class PullRequestFileNode : ReactiveObject, IPullRequestFileNode /// public PullRequestFileNode( string repositoryPath, - string relativePath, + string relativeOrGitPath, string sha, PullRequestFileStatus status, string oldPath) { Guard.ArgumentNotEmptyString(repositoryPath, nameof(repositoryPath)); - Guard.ArgumentNotEmptyString(relativePath, nameof(relativePath)); + Guard.ArgumentNotEmptyString(relativeOrGitPath, nameof(relativeOrGitPath)); Guard.ArgumentNotEmptyString(sha, nameof(sha)); - FileName = Path.GetFileName(relativePath); - RelativePath = relativePath.Replace("/", "\\"); + FileName = Path.GetFileName(relativeOrGitPath); + RelativePath = Paths.ToRelativePath(relativeOrGitPath); Sha = sha; Status = status; OldPath = oldPath; @@ -54,7 +55,7 @@ public PullRequestFileNode( { if (oldPath != null) { - StatusDisplay = Path.GetDirectoryName(oldPath) == Path.GetDirectoryName(relativePath) ? + StatusDisplay = Path.GetDirectoryName(oldPath) == Path.GetDirectoryName(relativeOrGitPath) ? Path.GetFileName(oldPath) : oldPath; } else From e46f894a98e9d6a0f3ee760c91097ff4ef5e37b1 Mon Sep 17 00:00:00 2001 From: Jamie Cansdale Date: Thu, 13 Jun 2019 13:13:03 +0100 Subject: [PATCH 07/11] Add xmldoc comments to Paths --- src/GitHub.Exports/Primitives/Paths.cs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/GitHub.Exports/Primitives/Paths.cs b/src/GitHub.Exports/Primitives/Paths.cs index efe5cc4586..d6e52036ec 100644 --- a/src/GitHub.Exports/Primitives/Paths.cs +++ b/src/GitHub.Exports/Primitives/Paths.cs @@ -3,18 +3,31 @@ namespace GitHub.Primitives { + /// + /// Convert to and from Git paths. + /// public static class Paths { public const char GitDirectorySeparatorChar = '/'; + /// + /// Convert from a relative path to a Git path. + /// + /// A relative path. + /// A working directory relative path which uses the '/' directory separator. public static string ToGitPath(string relativePath) { return relativePath.Replace(Path.DirectorySeparatorChar, GitDirectorySeparatorChar); } - public static string ToRelativePath(string relativePath) + /// + /// Convert from a Git path to a relative path. + /// + /// A working directory relative path which uses the '/' directory separator. + /// A relative paht that uses the directory separator. + public static string ToRelativePath(string gitPath) { - return relativePath.Replace(GitDirectorySeparatorChar, Path.DirectorySeparatorChar); + return gitPath.Replace(GitDirectorySeparatorChar, Path.DirectorySeparatorChar); } } } From 472730288b56790bdf4de3b3e913299af2aab93a Mon Sep 17 00:00:00 2001 From: Jamie Cansdale Date: Thu, 13 Jun 2019 13:39:25 +0100 Subject: [PATCH 08/11] AddBufferTag can accept relativePath No need to convert from full path back to gitPath. --- .../Services/PullRequestEditorService.cs | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/src/GitHub.App/Services/PullRequestEditorService.cs b/src/GitHub.App/Services/PullRequestEditorService.cs index a9698a9d57..f900f037b4 100644 --- a/src/GitHub.App/Services/PullRequestEditorService.cs +++ b/src/GitHub.App/Services/PullRequestEditorService.cs @@ -120,8 +120,7 @@ public async Task OpenFile( if (!workingDirectory) { - var gitPath = ToGitPath(session.LocalRepository, fullPath); - AddBufferTag(wpfTextView.TextBuffer, session, gitPath, commitSha, null); + AddBufferTag(wpfTextView.TextBuffer, session, relativePath, commitSha, null); EnableNavigateToEditor(textView, session); } } @@ -487,17 +486,6 @@ static string GetAbsolutePath(LocalRepositoryModel localRepository, string relat return Path.Combine(localPath, relativePath); } - static string ToGitPath(LocalRepositoryModel localRepository, string path) - { - var basePath = localRepository.LocalPath + Path.DirectorySeparatorChar; - if (path.StartsWith(basePath, StringComparison.OrdinalIgnoreCase)) - { - return Paths.ToGitPath(path.Substring(basePath.Length)); - } - - throw new ArgumentException($"Path '{path}' is not in the working directory '{localRepository.LocalPath}'"); - } - string GetText(IVsTextView textView) { IVsTextLines buffer; From cd67be2ae98eec9c624d56238c75d50c04cc67e8 Mon Sep 17 00:00:00 2001 From: Jamie Cansdale Date: Thu, 13 Jun 2019 14:47:01 +0100 Subject: [PATCH 09/11] Remove unnecessary GetAbsolutePath method --- src/GitHub.App/Services/PullRequestEditorService.cs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/GitHub.App/Services/PullRequestEditorService.cs b/src/GitHub.App/Services/PullRequestEditorService.cs index f900f037b4..19adceb489 100644 --- a/src/GitHub.App/Services/PullRequestEditorService.cs +++ b/src/GitHub.App/Services/PullRequestEditorService.cs @@ -90,13 +90,12 @@ public async Task OpenFile( try { - var fullPath = GetAbsolutePath(session.LocalRepository, relativePath); string fileName; string commitSha; if (workingDirectory) { - fileName = fullPath; + fileName = Path.Combine(session.LocalRepository.LocalPath, relativePath); commitSha = null; } else @@ -479,13 +478,6 @@ public static int FindNearestMatchingLine(IList fromLines, IList return matchingLine; } - static string GetAbsolutePath(LocalRepositoryModel localRepository, string relativePath) - { - var localPath = localRepository.LocalPath; - relativePath = Paths.ToRelativePath(relativePath); - return Path.Combine(localPath, relativePath); - } - string GetText(IVsTextView textView) { IVsTextLines buffer; From 286d287d88c20e6348b4e407ef9cba5098265a2e Mon Sep 17 00:00:00 2001 From: Jamie Cansdale Date: Thu, 13 Jun 2019 14:51:14 +0100 Subject: [PATCH 10/11] Add Path_Can_Use_Windows_Directory_Separator test --- .../GitServiceIntegrationTests.cs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/test/GitHub.Exports.UnitTests/GitServiceIntegrationTests.cs b/test/GitHub.Exports.UnitTests/GitServiceIntegrationTests.cs index 30b6a49381..dd13a4c57f 100644 --- a/test/GitHub.Exports.UnitTests/GitServiceIntegrationTests.cs +++ b/test/GitHub.Exports.UnitTests/GitServiceIntegrationTests.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using GitHub.Primitives; using GitHub.Services; using LibGit2Sharp; using NUnit.Framework; @@ -61,7 +62,7 @@ public async Task One_File_Is_Modified(string content1, string content2) [Test] - public void Path_Must_Not_Use_Windows_Directory_Separator() + public async Task Path_Can_Use_Windows_Directory_Separator() { using (var temp = new TempRepository()) { @@ -72,8 +73,10 @@ public void Path_Must_Not_Use_Windows_Directory_Separator() var commit2 = AddCommit(temp.Repository, path, newContent); var target = new GitService(new RepositoryFacade()); - Assert.ThrowsAsync(() => - target.Compare(temp.Repository, commit1.Sha, commit2.Sha, path)); + var patch = await target.Compare(temp.Repository, commit1.Sha, commit2.Sha, path); + + var gitPath = Paths.ToGitPath(path); + Assert.That(patch.Count(c => c.Path == gitPath), Is.EqualTo(1)); } } } @@ -134,7 +137,7 @@ public async Task Can_Handle_Renames(string oldPath, string oldContent, string n } [Test] - public void Path_Must_Not_Use_Windows_Directory_Separator() + public async Task Path_Can_Use_Windows_Directory_Separator() { using (var temp = new TempRepository()) { @@ -146,8 +149,10 @@ public void Path_Must_Not_Use_Windows_Directory_Separator() var contentBytes = new UTF8Encoding(false).GetBytes(newContent); var target = new GitService(new RepositoryFacade()); - Assert.ThrowsAsync(() => - target.CompareWith(temp.Repository, commit1.Sha, commit2.Sha, path, contentBytes)); + var contentChanges = await target.CompareWith(temp.Repository, commit1.Sha, commit2.Sha, path, contentBytes); + + Assert.That(contentChanges.LinesAdded, Is.EqualTo(1)); + Assert.That(contentChanges.LinesDeleted, Is.EqualTo(1)); } } } From 52f2bf09ad19ca69cea4d8389f9d123355152c08 Mon Sep 17 00:00:00 2001 From: Jamie Cansdale Date: Fri, 9 Aug 2019 15:39:53 +0100 Subject: [PATCH 11/11] Rename to Paths.ToWindowsPath from ToRelativePath ToWindowsPath is clearer because it doesn't convert from an absolute path. --- src/GitHub.App/Services/GitHubContextService.cs | 2 +- src/GitHub.App/Services/PullRequestEditorService.cs | 2 +- .../ViewModels/GitHubPane/PullRequestDirectoryNode.cs | 2 +- .../ViewModels/GitHubPane/PullRequestFileNode.cs | 2 +- src/GitHub.Exports/Primitives/Paths.cs | 8 ++++---- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/GitHub.App/Services/GitHubContextService.cs b/src/GitHub.App/Services/GitHubContextService.cs index 6519283fba..5d8944bbb4 100644 --- a/src/GitHub.App/Services/GitHubContextService.cs +++ b/src/GitHub.App/Services/GitHubContextService.cs @@ -282,7 +282,7 @@ public bool TryOpenFile(string repositoryDir, GitHubContext context) return false; } - var relativePath = Paths.ToRelativePath(path); + var relativePath = Paths.ToWindowsPath(path); var fullPath = Path.Combine(repositoryDir, relativePath); var textView = OpenDocument(fullPath); SetSelection(textView, context); diff --git a/src/GitHub.App/Services/PullRequestEditorService.cs b/src/GitHub.App/Services/PullRequestEditorService.cs index 19adceb489..9824f0f0ce 100644 --- a/src/GitHub.App/Services/PullRequestEditorService.cs +++ b/src/GitHub.App/Services/PullRequestEditorService.cs @@ -638,7 +638,7 @@ async Task GetBaseFileName(IPullRequestSession session, IPullRequestSess var gitPath = Paths.ToGitPath(file.RelativePath); var fileChange = changes.FirstOrDefault(x => x.Path == gitPath); return fileChange?.Status == LibGit2Sharp.ChangeKind.Renamed ? - Paths.ToRelativePath(fileChange.OldPath) : file.RelativePath; + Paths.ToWindowsPath(fileChange.OldPath) : file.RelativePath; } } diff --git a/src/GitHub.App/ViewModels/GitHubPane/PullRequestDirectoryNode.cs b/src/GitHub.App/ViewModels/GitHubPane/PullRequestDirectoryNode.cs index 9f85b6259e..bd79e0270e 100644 --- a/src/GitHub.App/ViewModels/GitHubPane/PullRequestDirectoryNode.cs +++ b/src/GitHub.App/ViewModels/GitHubPane/PullRequestDirectoryNode.cs @@ -17,7 +17,7 @@ public class PullRequestDirectoryNode : IPullRequestDirectoryNode public PullRequestDirectoryNode(string relativeOrGitPath) { DirectoryName = System.IO.Path.GetFileName(relativeOrGitPath); - RelativePath = Paths.ToRelativePath(relativeOrGitPath); + RelativePath = Paths.ToWindowsPath(relativeOrGitPath); Directories = new List(); Files = new List(); } diff --git a/src/GitHub.App/ViewModels/GitHubPane/PullRequestFileNode.cs b/src/GitHub.App/ViewModels/GitHubPane/PullRequestFileNode.cs index 1a0f0a4c4c..faf3a7fdb7 100644 --- a/src/GitHub.App/ViewModels/GitHubPane/PullRequestFileNode.cs +++ b/src/GitHub.App/ViewModels/GitHubPane/PullRequestFileNode.cs @@ -42,7 +42,7 @@ public PullRequestFileNode( Guard.ArgumentNotEmptyString(sha, nameof(sha)); FileName = Path.GetFileName(relativeOrGitPath); - RelativePath = Paths.ToRelativePath(relativeOrGitPath); + RelativePath = Paths.ToWindowsPath(relativeOrGitPath); Sha = sha; Status = status; OldPath = oldPath; diff --git a/src/GitHub.Exports/Primitives/Paths.cs b/src/GitHub.Exports/Primitives/Paths.cs index d6e52036ec..9cdd4e0677 100644 --- a/src/GitHub.Exports/Primitives/Paths.cs +++ b/src/GitHub.Exports/Primitives/Paths.cs @@ -21,11 +21,11 @@ public static string ToGitPath(string relativePath) } /// - /// Convert from a Git path to a relative path. + /// Convert from a Git path to a path that uses the Windows directory separator ('\'). /// - /// A working directory relative path which uses the '/' directory separator. - /// A relative paht that uses the directory separator. - public static string ToRelativePath(string gitPath) + /// A relative path that uses the '/' directory separator. + /// A relative path that uses the directory separator ('\' on Windows). + public static string ToWindowsPath(string gitPath) { return gitPath.Replace(GitDirectorySeparatorChar, Path.DirectorySeparatorChar); }