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

Commit f8c52cd

Browse files
committed
Factor out ToGitPath and ToRelativePath
1 parent 5fe36d6 commit f8c52cd

File tree

9 files changed

+39
-17
lines changed

9 files changed

+39
-17
lines changed

src/GitHub.App/Services/GitClient.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ public Task<string> ExtractFile(IRepository repository, string commitSha, string
265265
Guard.ArgumentNotEmptyString(commitSha, nameof(commitSha));
266266
Guard.ArgumentIsRelativePath(relativePath, nameof(relativePath));
267267

268-
var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/');
268+
var gitPath = Paths.ToGitPath(relativePath);
269269
return Task.Run(() =>
270270
{
271271
var commit = repository.Lookup<Commit>(commitSha);
@@ -285,7 +285,7 @@ public Task<byte[]> ExtractFileBinary(IRepository repository, string commitSha,
285285
Guard.ArgumentNotEmptyString(commitSha, nameof(commitSha));
286286
Guard.ArgumentIsRelativePath(relativePath, nameof(relativePath));
287287

288-
var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/');
288+
var gitPath = Paths.ToGitPath(relativePath);
289289
return Task.Run(() =>
290290
{
291291
var commit = repository.Lookup<Commit>(commitSha);
@@ -315,7 +315,7 @@ public Task<bool> IsModified(IRepository repository, string relativePath, byte[]
315315
Guard.ArgumentNotNull(repository, nameof(repository));
316316
Guard.ArgumentIsRelativePath(relativePath, nameof(relativePath));
317317

318-
var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/');
318+
var gitPath = Paths.ToGitPath(relativePath);
319319
return Task.Run(() =>
320320
{
321321
if (repository.RetrieveStatus(gitPath) == FileStatus.Unaltered)

src/GitHub.App/Services/GitHubContextService.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,8 @@ public bool TryOpenFile(string repositoryDir, GitHubContext context)
282282
return false;
283283
}
284284

285-
var fullPath = Path.Combine(repositoryDir, path.Replace('/', '\\'));
285+
var relativePath = Paths.ToRelativePath(path);
286+
var fullPath = Path.Combine(repositoryDir, relativePath);
286287
var textView = OpenDocument(fullPath);
287288
SetSelection(textView, context);
288289
return true;
@@ -410,7 +411,7 @@ public bool HasChangesInWorkingDirectory(string repositoryDir, string commitish,
410411
Guard.ArgumentNotNull(commitish, nameof(commitish));
411412
Guard.ArgumentIsRelativePath(relativePath, nameof(relativePath));
412413

413-
var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/');
414+
var gitPath = Paths.ToGitPath(relativePath);
414415
using (var repo = gitService.GetRepository(repositoryDir))
415416
{
416417
var commit = repo.Lookup<Commit>(commitish);

src/GitHub.App/Services/PullRequestEditorService.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Threading.Tasks;
88
using EnvDTE;
99
using GitHub.Commands;
10+
using GitHub.Primitives;
1011
using GitHub.Extensions;
1112
using GitHub.Models;
1213
using GitHub.Models.Drafts;
@@ -482,7 +483,7 @@ public static int FindNearestMatchingLine(IList<string> fromLines, IList<string>
482483
static string GetAbsolutePath(LocalRepositoryModel localRepository, string relativePath)
483484
{
484485
var localPath = localRepository.LocalPath;
485-
relativePath = relativePath.Replace('/', Path.DirectorySeparatorChar);
486+
relativePath = Paths.ToRelativePath(relativePath);
486487
return Path.Combine(localPath, relativePath);
487488
}
488489

@@ -491,7 +492,7 @@ static string ToGitPath(LocalRepositoryModel localRepository, string path)
491492
var basePath = localRepository.LocalPath + Path.DirectorySeparatorChar;
492493
if (path.StartsWith(basePath, StringComparison.OrdinalIgnoreCase))
493494
{
494-
return path.Substring(basePath.Length).Replace(Path.DirectorySeparatorChar, '/');
495+
return Paths.ToGitPath(path.Substring(basePath.Length));
495496
}
496497

497498
throw new ArgumentException($"Path '{path}' is not in the working directory '{localRepository.LocalPath}'");
@@ -654,10 +655,10 @@ async Task<string> GetBaseFileName(IPullRequestSession session, IPullRequestSess
654655
session.LocalRepository,
655656
session.PullRequest))
656657
{
657-
var gitPath = file.RelativePath.Replace(Path.DirectorySeparatorChar, '/');
658+
var gitPath = Paths.ToGitPath(file.RelativePath);
658659
var fileChange = changes.FirstOrDefault(x => x.Path == gitPath);
659660
return fileChange?.Status == LibGit2Sharp.ChangeKind.Renamed ?
660-
fileChange.OldPath.Replace('/', Path.DirectorySeparatorChar) : file.RelativePath;
661+
Paths.ToRelativePath(fileChange.OldPath) : file.RelativePath;
661662
}
662663
}
663664

src/GitHub.App/Services/PullRequestService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ public async Task<string> ExtractToTempFile(
779779
Encoding encoding)
780780
{
781781
var tempFilePath = CalculateTempFileName(relativePath, commitSha, encoding);
782-
var gitPath = relativePath.TrimStart('/').Replace('\\', '/');
782+
var gitPath = Paths.ToGitPath(relativePath);
783783

784784
if (!File.Exists(tempFilePath))
785785
{

src/GitHub.App/ViewModels/PullRequestReviewCommentThreadViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public override async Task PostComment(ICommentViewModel comment)
198198
await Session.PostReviewComment(
199199
comment.Body,
200200
File.CommitSha,
201-
File.RelativePath.Replace(Path.DirectorySeparatorChar, '/'),
201+
Paths.ToGitPath(File.RelativePath),
202202
File.Diff,
203203
diffPosition.DiffLineNumber).ConfigureAwait(false);
204204
}
@@ -235,7 +235,7 @@ public static (string key, string secondaryKey) GetDraftKeys(
235235
string relativePath,
236236
int lineNumber)
237237
{
238-
var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/');
238+
var gitPath = Paths.ToGitPath(relativePath);
239239
var key = Invariant($"pr-review-comment|{cloneUri}|{pullRequestNumber}|{gitPath}");
240240
return (key, lineNumber.ToString(CultureInfo.InvariantCulture));
241241
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.IO;
3+
4+
namespace GitHub.Primitives
5+
{
6+
public static class Paths
7+
{
8+
public const char GitDirectorySeparatorChar = '/';
9+
10+
public static string ToGitPath(string relativePath)
11+
{
12+
return relativePath.Replace(Path.DirectorySeparatorChar, GitDirectorySeparatorChar);
13+
}
14+
15+
public static string ToRelativePath(string relativePath)
16+
{
17+
return relativePath.Replace(GitDirectorySeparatorChar, Path.DirectorySeparatorChar);
18+
}
19+
}
20+
}

src/GitHub.Exports/Services/GitService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public Task<Patch> Compare(
241241
Guard.ArgumentNotEmptyString(sha2, nameof(sha2));
242242
Guard.ArgumentIsRelativePath(relativePath, nameof(relativePath));
243243

244-
var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/');
244+
var gitPath = Paths.ToGitPath(relativePath);
245245
return Task.Run(() =>
246246
{
247247
var commit1 = repository.Lookup<Commit>(sha1);
@@ -269,7 +269,7 @@ public Task<ContentChanges> CompareWith(IRepository repository, string sha1, str
269269
Guard.ArgumentNotEmptyString(sha2, nameof(sha1));
270270
Guard.ArgumentIsRelativePath(relativePath, nameof(relativePath));
271271

272-
var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/');
272+
var gitPath = Paths.ToGitPath(relativePath);
273273
return Task.Run(() =>
274274
{
275275
var commit1 = repository.Lookup<Commit>(sha1);

src/GitHub.InlineReviews/Services/PullRequestSession.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public async Task<IPullRequestSessionFile> GetFile(
8181
try
8282
{
8383
PullRequestSessionFile file;
84-
var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/');
84+
var gitPath = Paths.ToGitPath(relativePath);
8585
var key = gitPath + '@' + commitSha;
8686

8787
if (!fileIndex.TryGetValue(key, out file))

src/GitHub.InlineReviews/Services/PullRequestSessionService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public IReadOnlyList<InlineAnnotationModel> BuildAnnotations(
9595
PullRequestDetailModel pullRequest,
9696
string relativePath)
9797
{
98-
var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/');
98+
var gitPath = Paths.ToGitPath(relativePath);
9999

100100
return pullRequest.CheckSuites
101101
?.SelectMany(checkSuite => checkSuite.CheckRuns.Select(checkRun => new { checkSuite, checkRun }))
@@ -114,7 +114,7 @@ public IReadOnlyList<IInlineCommentThreadModel> BuildCommentThreads(
114114
IReadOnlyList<DiffChunk> diff,
115115
string headSha)
116116
{
117-
var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/');
117+
var gitPath = Paths.ToGitPath(relativePath);
118118

119119
var threadsByPosition = pullRequest.Threads
120120
.Where(x => x.Path == gitPath)

0 commit comments

Comments
 (0)