8000 Be consistent about how we use relative paths and Git paths by jcansdale · Pull Request #2386 · github/VisualStudio · GitHub
[go: up one dir, main page]

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

Be consistent about how we use relative paths and Git paths #2386

Merged
merged 11 commits into from
Aug 9, 2019
Merged
Prev Previous commit
Next Next commit
Factor out ToGitPath and ToRelativePath
  • Loading branch information
jcansdale committed Jun 13, 2019
commit f8c52cd127690f8728ed2fd5f6840a6ecd642b1e
6 changes: 3 additions & 3 deletions src/GitHub.App/Services/GitClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public Task<string> 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<Commit>(commitSha);
Expand All @@ -285,7 +285,7 @@ public Task<byte[]> 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<Commit>(commitSha);
Expand Down Expand Up @@ -315,7 +315,7 @@ public Task<bool> 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)
Expand Down
5 changes: 3 additions & 2 deletions src/GitHub.App/Services/GitHubContextService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Commit>(commitish);
Expand Down
9 changes: 5 additions & 4 deletions src/GitHub.App/Services/PullRequestEditorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -482,7 +483,7 @@ public static int FindNearestMatchingLine(IList<string> fromLines, IList<string>
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);
}

Expand All @@ -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}'");
Expand Down Expand Up @@ -654,10 +655,10 @@ async Task<string> 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;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/GitHub.App/Services/PullRequestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ public async Task<string> ExtractToTempFile(
Encoding encoding)
{
var tempFilePath = CalculateTempFileName(relativePath, commitSha, encoding);
var gitPath = relativePath.TrimStart('/').Replace('\\', '/');
var gitPath = Paths.ToGitPath(relativePath);

if (!File.Exists(tempFilePath))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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));
}
Expand Down
20 changes: 20 additions & 0 deletions src/GitHub.Exports/Primitives/Paths.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
4 changes: 2 additions & 2 deletions src/GitHub.Exports/Services/GitService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public Task<Patch> 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<Commit>(sha1);
Expand Down Expand Up @@ -269,7 +269,7 @@ public Task<ContentChanges> 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<Commit>(sha1);
Expand Down
2 changes: 1 addition & 1 deletion src/GitHub.InlineReviews/Services/PullRequestSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public async Task<IPullRequestSessionFile> 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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public IReadOnlyList<InlineAnnotationModel> 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 }))
Expand All @@ -114,7 +114,7 @@ public IReadOnlyList<IInlineCommentThreadModel> BuildCommentThreads(
IReadOnlyList<DiffChunk> diff,
string headSha)
{
var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/');
var gitPath = Paths.ToGitPath(relativePath);

var threadsByPosition = pullRequest.Threads
.Where(x => x.Path == gitPath)
Expand Down
0