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

Commit 5fe36d6

Browse files
committed
Use relativePath and gitPath in a consistent way
Use Guard.ArgumentIsRelativePath instead of Guard.ArgumentIsGitPath Automatically convert to Git path where required.
1 parent 8ae29c8 commit 5fe36d6

File tree

12 files changed

+57
-58
lines changed

12 files changed

+57
-58
lines changed

src/GitHub.App/SampleData/GitServiceDesigner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class GitServiceDesigner : IGitService
1515
public IRepository GetRepository(string path) => null;
1616
public UriString GetUri(string path, string remote = "origin") => null;
1717
public UriString GetUri(IRepository repository, string remote = "origin") => null;
18-
public Task<Patch> Compare(IRepository repository, string sha1, string sha2, string path) => null;
18+
public Task<Patch> Compare(IRepository repository, string sha1, string sha2, string relativePath) => null;
1919
public Task<ContentChanges> CompareWith(IRepository repository, string sha1, string sha2, string path, byte[] contents) => null;
2020
public Task<TreeChanges> Compare(IRepository repository, string sha1, string sha2, bool detectRenames = false) => null;
2121
}

src/GitHub.App/Services/GitClient.cs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -259,40 +259,42 @@ public Task<Remote> GetHttpRemote(IRepository repo, string remote)
259259
});
260260
}
261261

262-
public Task<string> ExtractFile(IRepository repository, string commitSha, string fileName)
262+
public Task<string> ExtractFile(IRepository repository, string commitSha, string relativePath)
263263
{
264264
Guard.ArgumentNotNull(repository, nameof(repository));
265265
Guard.ArgumentNotEmptyString(commitSha, nameof(commitSha));
266-
Guard.ArgumentIsGitPath(fileName, nameof(fileName));
266+
Guard.ArgumentIsRelativePath(relativePath, nameof(relativePath));
267267

268+
var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/');
268269
return Task.Run(() =>
269270
{
270271
var commit = repository.Lookup<Commit>(commitSha);
271272
if (commit == null)
272273
{
273-
throw new FileNotFoundException("Couldn't find '" + fileName + "' at commit " + commitSha + ".");
274+
throw new FileNotFoundException("Couldn't find '" + gitPath + "' at commit " + commitSha + ".");
274275
}
275276

276-
var blob = commit[fileName]?.Target as Blob;
277+
var blob = commit[gitPath]?.Target as Blob;
277278
return blob?.GetContentText();
278279
});
279280
}
280281

281-
public Task<byte[]> ExtractFileBinary(IRepository repository, string commitSha, string fileName)
282+
public Task<byte[]> ExtractFileBinary(IRepository repository, string commitSha, string relativePath)
282283
{
283284
Guard.ArgumentNotNull(repository, nameof(repository));
284285
Guard.ArgumentNotEmptyString(commitSha, nameof(commitSha));
285-
Guard.ArgumentIsGitPath(fileName, nameof(fileName));
286+
Guard.ArgumentIsRelativePath(relativePath, nameof(relativePath));
286287

288+
var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/');
287289
return Task.Run(() =>
288290
{
289291
var commit = repository.Lookup<Commit>(commitSha);
290292
if (commit == null)
291293
{
292-
throw new FileNotFoundException("Couldn't find '" + fileName + "' at commit " + commitSha + ".");
294+
throw new FileNotFoundException("Couldn't find '" + gitPath + "' at commit " + commitSha + ".");
293295
}
294296

295-
var blob = commit[fileName]?.Target as Blob;
297+
var blob = commit[gitPath]?.Target as Blob;
296298

297299
if (blob != null)
298300
{
@@ -308,16 +310,17 @@ public Task<byte[]> ExtractFileBinary(IRepository repository, string commitSha,
308310
});
309311
}
310312

311-
public Task<bool> IsModified(IRepository repository, string path, byte[] contents)
313+
public Task<bool> IsModified(IRepository repository, string relativePath, byte[] contents)
312314
{
313315
Guard.ArgumentNotNull(repository, nameof(repository));
314-
Guard.ArgumentIsGitPath(path, nameof(path));
316+
Guard.ArgumentIsRelativePath(relativePath, nameof(relativePath));
315317

318+
var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/');
316319
return Task.Run(() =>
317320
{
318-
if (repository.RetrieveStatus(path) == FileStatus.Unaltered)
321+
if (repository.RetrieveStatus(gitPath) == FileStatus.Unaltered)
319322
{
320-
var treeEntry = repository.Head[path];
323+
var treeEntry = repository.Head[gitPath];
321324
if (treeEntry?.TargetType != TreeEntryTargetType.Blob)
322325
{
323326
return false;
@@ -326,7 +329,7 @@ public Task<bool> IsModified(IRepository repository, string path, byte[] content
326329
var blob1 = (Blob)treeEntry.Target;
327330
using (var s = contents != null ? new MemoryStream(contents) : new MemoryStream())
328331
{
329-
var blob2 = repository.ObjectDatabase.CreateBlob(s, path);
332+
var blob2 = repository.ObjectDatabase.CreateBlob(s, gitPath);
330333
var diff = repository.Diff.Compare(blob1, blob2);
331334
return diff.LinesAdded != 0 || diff.LinesDeleted != 0;
332335
}

src/GitHub.App/Services/GitHubContextService.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -404,16 +404,17 @@ public string FindObjectishForTFSTempFile(string tempFile)
404404
}
405405

406406
/// <inheritdoc/>
407-
public bool HasChangesInWorkingDirectory(string repositoryDir, string commitish, string path)
407+
public bool HasChangesInWorkingDirectory(string repositoryDir, string commitish, string relativePath)
408408
{
409-
Guard.ArgumentNotNull(path, nameof(repositoryDir));
410-
Guard.ArgumentNotNull(path, nameof(commitish));
411-
Guard.ArgumentIsGitPath(path, nameof(path));
409+
Guard.ArgumentNotNull(repositoryDir, nameof(repositoryDir));
410+
Guard.ArgumentNotNull(commitish, nameof(commitish));
411+
Guard.ArgumentIsRelativePath(relativePath, nameof(relativePath));
412412

413+
var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/');
413414
using (var repo = gitService.GetRepository(repositoryDir))
414415
{
415416
var commit = repo.Lookup<Commit>(commitish);
416-
var paths = new[] { path };
417+
var paths = new[] { gitPath };
417418

418419
return repo.Diff.Compare<Patch>(commit.Tree, DiffTargets.WorkingDirectory, paths).Count() > 0;
419420
}

src/GitHub.App/Services/PullRequestEditorService.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -573,23 +573,21 @@ void ShowErrorInStatusBar(string message, Exception e)
573573
void AddBufferTag(
574574
ITextBuffer buffer,
575575
IPullRequestSession session,
576-
string gitPath,
576+
string relativePath,
577577
string commitSha,
578578
DiffSide? side)
579579
{
580-
Guard.ArgumentIsGitPath(gitPath, nameof(gitPath));
581-
582580
buffer.Properties.GetOrCreateSingletonProperty(
583581
typeof(PullRequestTextBufferInfo),
584-
() => new PullRequestTextBufferInfo(session, gitPath, commitSha, side));
582+
() => new PullRequestTextBufferInfo(session, relativePath, commitSha, side));
585583

586584
var projection = buffer as IProjectionBuffer;
587585

588586
if (projection != null)
589587
{
590588
foreach (var source in projection.SourceBuffers)
591589
{
592-
AddBufferTag(source, session, gitPath, commitSha, side);
590+
AddBufferTag(source, session, relativePath, commitSha, side);
593591
}
594592
}
595593
}
@@ -656,9 +654,10 @@ async Task<string> GetBaseFileName(IPullRequestSession session, IPullRequestSess
656654
session.LocalRepository,
657655
session.PullRequest))
658656
{
659-
var fileChange = changes.FirstOrDefault(x => x.Path == file.RelativePath);
657+
var gitPath = file.RelativePath.Replace(Path.DirectorySeparatorChar, '/');
658+
var fileChange = changes.FirstOrDefault(x => x.Path == gitPath);
660659
return fileChange?.Status == LibGit2Sharp.ChangeKind.Renamed ?
661-
fileChange.OldPath : file.RelativePath;
660+
fileChange.OldPath.Replace('/', Path.DirectorySeparatorChar) : file.RelativePath;
662661
}
663662
}
664663

src/GitHub.App/Services/PullRequestService.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -922,24 +922,22 @@ async Task ExtractToTempFile(
922922
IRepository repo,
923923
int pullRequestNumber,
924924
string commitSha,
925-
string path,
925+
string relativePath,
926926
Encoding encoding,
927927
string tempFilePath)
928928
{
929-
Guard.ArgumentIsGitPath(path, nameof(path));
930-
931929
string contents;
932930

933931
try
934932
{
935-
contents = await gitClient.ExtractFile(repo, commitSha, path) ?? string.Empty;
933+
contents = await gitClient.ExtractFile(repo, commitSha, relativePath) ?? string.Empty;
936934
}
937935
catch (FileNotFoundException)
938936
{
939937
var pullHeadRef = $"refs/pull/{pullRequestNumber}/head";
940938
var remote = await gitClient.GetHttpRemote(repo, "origin");
941939
await gitClient.Fetch(repo, remote.Name, commitSha, pullHeadRef);
942-
contents = await gitClient.ExtractFile(repo, commitSha, path) ?? string.Empty;
940+
contents = await gitClient.ExtractFile(repo, commitSha, relativePath) ?? string.Empty;
943941
}
944942

945943
Directory.CreateDirectory(Path.GetDirectoryName(tempFilePath));

src/GitHub.App/ViewModels/PullRequestReviewCommentThreadViewModel.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.ComponentModel.Composition;
44
using System.Globalization;
5+
using System.IO;
56
using System.Linq;
67
using System.Reactive.Linq;
78
using System.Threading.Tasks;
@@ -197,7 +198,7 @@ public override async Task PostComment(ICommentViewModel comment)
197198
await Session.PostReviewComment(
198199
comment.Body,
199200
File.CommitSha,
200-
File.RelativePath.Replace("\\", "/"),
201+
File.RelativePath.Replace(Path.DirectorySeparatorChar, '/'),
201202
File.Diff,
202203
diffPosition.DiffLineNumber).ConfigureAwait(false);
203204
}
@@ -234,8 +235,8 @@ public static (string key, string secondaryKey) GetDraftKeys(
234235
string relativePath,
235236
int lineNumber)
236237
{
237-
relativePath = relativePath.Replace("\\", "/");
238-
var key = Invariant($"pr-review-comment|{cloneUri}|{pullRequestNumber}|{relativePath}");
238+
var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/');
239+
var key = Invariant($"pr-review-comment|{cloneUri}|{pullRequestNumber}|{gitPath}");
239240
return (key, lineNumber.ToString(CultureInfo.InvariantCulture));
240241
}
241242

src/GitHub.Exports/Services/GitService.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -234,13 +234,14 @@ public Task<Patch> Compare(
234234
IRepository repository,
235235
string sha1,
236236
string sha2,
237-
string path)
237+
string relativePath)
238238
{
239239
Guard.ArgumentNotNull(repository, nameof(repository));
240240
Guard.ArgumentNotEmptyString(sha1, nameof(sha1));
241241
Guard.ArgumentNotEmptyString(sha2, nameof(sha2));
242-
Guard.ArgumentIsGitPath(path, nameof(path));
242+
Guard.ArgumentIsRelativePath(relativePath, nameof(relativePath));
243243

244+
var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/');
244245
return Task.Run(() =>
245246
{
246247
var commit1 = repository.Lookup<Commit>(sha1);
@@ -251,7 +252,7 @@ public Task<Patch> Compare(
251252
return repository.Diff.Compare<Patch>(
252253
commit1.Tree,
253254
commit2.Tree,
254-
new[] { path },
255+
new[] { gitPath },
255256
defaultCompareOptions);
256257
}
257258
else
@@ -261,27 +262,28 @@ public Task<Patch> Compare(
261262
});
262263
}
263264

264-
public Task<ContentChanges> CompareWith(IRepository repository, string sha1, string sha2, string path, byte[] contents)
265+
public Task<ContentChanges> CompareWith(IRepository repository, string sha1, string sha2, string relativePath, byte[] contents)
265266
{
266267
Guard.ArgumentNotNull(repository, nameof(repository));
267268
Guard.ArgumentNotEmptyString(sha1, nameof(sha1));
268269
Guard.ArgumentNotEmptyString(sha2, nameof(sha1));
269-
Guard.ArgumentIsGitPath(path, nameof(path));
270+
Guard.ArgumentIsRelativePath(relativePath, nameof(relativePath));
270271

272+
var gitPath = relativePath.Replace(Path.DirectorySeparatorChar, '/');
271273
return Task.Run(() =>
272274
{
273275
var commit1 = repository.Lookup<Commit>(sha1);
274276
var commit2 = repository.Lookup<Commit>(sha2);
275277

276278
var treeChanges = repository.Diff.Compare<TreeChanges>(commit1.Tree, commit2.Tree, defaultCompareOptions);
277-
var change = treeChanges.FirstOrDefault(x => x.Path == path);
279+
var change = treeChanges.FirstOrDefault(x => x.Path == gitPath);
278280
var oldPath = change?.OldPath;
279281

280282
if (commit1 != null && oldPath != null)
281283
{
282284
var contentStream = contents != null ? new MemoryStream(contents) : new MemoryStream();
283285
var blob1 = commit1[oldPath]?.Target as Blob ?? repository.ObjectDatabase.CreateBlob(new MemoryStream());
284-
var blob2 = repository.ObjectDatabase.CreateBlob(contentStream, path);
286+
var blob2 = repository.ObjectDatabase.CreateBlob(contentStream, gitPath);
285287
return repository.Diff.Compare(blob1, blob2, defaultCompareOptions);
286288
}
287289

src/GitHub.Exports/Services/IGitService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ public interface IGitService
9090
/// <param name="repository">The repository</param>
9191
/// <param name="sha1">The SHA of the first commit.</param>
9292
/// <param name="sha2">The SHA of the second commit.</param>
93-
/// <param name="path">The relative path to the file (using '/' directory separator).</param>
93+
/// <param name="path">The relative path to the file.</param>
9494
/// <param name="contents">The contents to compare with the file.</param>
9595
/// <returns>
9696
/// A <see cref="Patch"/> object or null if the commit could not be found in the repository.
9797
/// </returns>
9898
/// <exception cref="ArgumentException">If <paramref name="path"/> contains a '\'.</exception>
99-
Task<ContentChanges> CompareWith(IRepository repository, string sha1, string sha2, string path, byte[] contents);
99+
Task<ContentChanges> CompareWith(IRepository repository, string sha1, string sha2, string relativePath, byte[] contents);
100100

101101
/// <summary>
102102
/// Compares two commits.

src/GitHub.Extensions/Guard.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,10 @@ namespace GitHub.Extensions
77
{
88
public static class Guard
99
{
10-
public static void ArgumentIsGitPath(string value, string name)
10+
public static void ArgumentIsRelativePath(string value, string name)
1111
{
1212
ArgumentNotNull(value, name);
1313

14-
if (value.Contains('\\'))
15-
{
16-
throw new ArgumentException($"The value '{value}' must use '/' not '\\' as directory separator", name);
17-
}
18-
1914
if (Path.IsPathRooted(value))
2015
{
2116
throw new ArgumentException($"The value '{value}' must not be rooted", name);

src/GitHub.InlineReviews/Services/DiffService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public async Task<IReadOnlyList<DiffChunk>> Diff(
2929
IRepository repo,
3030
string baseSha,
3131
string headSha,
32-
string path)
32+
string relativePath)
3333
{
34-
var patch = await gitService.Compare(repo, baseSha, headSha, path);
34+
var patch = await gitService.Compare(repo, baseSha, headSha, relativePath);
3535

3636
if (patch != null)
3737
{

0 commit comments

Comments
 (0)