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

Commit 19be672

Browse files
committed
Return commit SHA from ResolveBlob
The commit SHA is required when opening the specific version of blob with `AnnotateFile`.
1 parent ab3e7e9 commit 19be672

File tree

4 files changed

+34
-29
lines changed

4 files changed

+34
-29
lines changed

src/GitHub.App/Services/GitHubContextService.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ public bool TryOpenFile(string repositoryDir, GitHubContext context)
249249
return true;
250250
}
251251

252-
public (string commitish, string path, bool isSha) ResolveBlob(string repositoryDir, GitHubContext context, string remoteName = "origin")
252+
public (string commitish, string path, string commitSha) ResolveBlob(string repositoryDir, GitHubContext context, string remoteName = "origin")
253253
{
254254
Guard.ArgumentNotNull(repositoryDir, nameof(repositoryDir));
255255
Guard.ArgumentNotNull(context, nameof(context));
@@ -259,13 +259,13 @@ public bool TryOpenFile(string repositoryDir, GitHubContext context)
259259
if (context.TreeishPath == null)
260260
{
261261
// Blobs without a TreeishPath aren't currently supported
262-
return (null, null, false);
262+
return (null, null, null);
263263
}
264264

265265
if (context.BlobName == null)
266266
{
267267
// Not a blob
268-
return (null, null, false);
268+
return (null, null, null);
269269
}
270270

271271
var objectishPath = $"{context.TreeishPath}/{context.BlobName}";
@@ -275,28 +275,30 @@ public bool TryOpenFile(string repositoryDir, GitHubContext context)
275275
{
276276
if (repository.Lookup($"{commitSha}:{pathSha}") != null)
277277
{
278-
return (commitSha, pathSha, true);
278+
return (commitSha, pathSha, commitSha);
279279
}
280280
}
281281

282282
foreach (var (commitish, path) in objectish)
283283
{
284284
var remoteRef = $"refs/remotes/{remoteName}/{commitish}";
285-
if (repository.Lookup(remoteRef) == null)
285+
var commit = repository.Lookup(remoteRef);
286+
if (commit == null)
286287
{
287288
continue;
288289
}
289290

290-
if (repository.Lookup($"{remoteRef}:{path}") == null)
291+
var blob = repository.Lookup($"{remoteRef}:{path}");
292+
if (blob == null)
291293
{
292294
// Resolved commitish but not path
293-
return (remoteRef, null, false);
295+
return (remoteRef, null, commit.Sha);
294296
}
295297

296-
return (remoteRef, path, false);
298+
return (remoteRef, path, commit.Sha);
297299
}
298300

299-
return (null, null, false);
301+
return (null, null, null);
300302
}
301303

302304
IEnumerable<(string commitish, string path)> ToObjectish(string treeishPath)
@@ -324,13 +326,13 @@ public bool HasChangesInWorkingDirectory(string repositoryDir, string commitish,
324326

325327
public async Task<bool> TryAnnotateFile(string repositoryDir, string currentBranch, GitHubContext context)
326328
{
327-
var (commitish, path, isSha) = ResolveBlob(repositoryDir, context);
329+
var (commitish, path, commitSha) = ResolveBlob(repositoryDir, context);
328330
if (path == null)
329331
{
330332
return false;
331333
}
332334

333-
if (!AnnotateFile(repositoryDir, currentBranch, path, commitish))
335+
if (!AnnotateFile(repositoryDir, currentBranch, path, commitSha))
334336
{
335337
return false;
336338
}

src/GitHub.Exports/Services/IGitHubContextService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public interface IGitHubContextService
1414
Uri ToRepositoryUrl(GitHubContext context);
1515
bool TryOpenFile(string repositoryDir, GitHubContext context);
1616
Task<bool> TryAnnotateFile(string repositoryDir, string currentBranch, GitHubContext context);
17-
(string commitish, string path, bool isSha) ResolveBlob(string repositoryDir, GitHubContext context, string remoteName = "origin");
17+
(string commitish, string path, string commitSha) ResolveBlob(string repositoryDir, GitHubContext context, string remoteName = "origin");
1818
bool HasChangesInWorkingDirectory(string repositoryDir, string commitish, string path);
1919
}
2020
}

test/GitHub.App.UnitTests/Services/GitHubContextServiceTests.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -325,19 +325,22 @@ public void RepositoryHome(string windowTitle, string expectOwner, string expect
325325

326326
public class TheResolveBlobMethod
327327
{
328-
[TestCase("https://github.com/github/VisualStudio/blob/master/foo.cs", "refs/remotes/origin/master", "refs/remotes/origin/master:foo.cs", "refs/remotes/origin/master", "foo.cs")]
329-
[TestCase("https://github.com/github/VisualStudio/blob/master/src/foo.cs", "refs/remotes/origin/master", "refs/remotes/origin/master:src/foo.cs", "refs/remotes/origin/master", "src/foo.cs")]
330-
[TestCase("https://github.com/github/VisualStudio/blob/branch-name/src/foo.cs", "refs/remotes/origin/branch-name", "refs/remotes/origin/branch-name:src/foo.cs", "refs/remotes/origin/branch-name", "src/foo.cs")]
331-
[TestCase("https://github.com/github/VisualStudio/blob/fixes/666-bug/src/foo.cs", "refs/remotes/origin/fixes/666-bug", "refs/remotes/origin/fixes/666-bug:src/foo.cs", "refs/remotes/origin/fixes/666-bug", "src/foo.cs")]
332-
[TestCase("https://github.com/github/VisualStudio/blob/fixes/666-bug/A/B/foo.cs", "refs/remotes/origin/fixes/666-bug", "refs/remotes/origin/fixes/666-bug:A/B/foo.cs", "refs/remotes/origin/fixes/666-bug", "A/B/foo.cs")]
333-
[TestCase("https://github.com/github/VisualStudio/blob/master/foo.cs", "refs/remotes/origin/master", null, "refs/remotes/origin/master", null, Description = "Resolve commit only")]
334-
[TestCase("https://github.com/github/VisualStudio/blob/36d6b0bb6e319337180d523281c42d9611744e66/src/code.cs", "36d6b0bb6e319337180d523281c42d9611744e66", "36d6b0bb6e319337180d523281c42d9611744e66:src/code.cs", "36d6b0bb6e319337180d523281c42d9611744e66", "src/code.cs", true, Description = "Resolve commit only")]
335-
[TestCase("https://github.com/github/VisualStudio/commit/8cf9a268c497adb4fc0a14572253165e179dd11e", "8cf9a268c497adb4fc0a14572253165e179dd11e", null, null, null)]
336-
public void ResolveBlob(string url, string commitish, string objectish, string expectCommitish, string expectPath, bool expectIsSha = false)
328+
const string CommitSha = "36d6b0bb6e319337180d523281c42d9611744e66";
329+
330+
[TestCase("https://github.com/github/VisualStudio/blob/master/foo.cs", "refs/remotes/origin/master", "refs/remotes/origin/master:foo.cs", "refs/remotes/origin/master", "foo.cs", CommitSha)]
331+
[TestCase("https://github.com/github/VisualStudio/blob/master/src/foo.cs", "refs/remotes/origin/master", "refs/remotes/origin/master:src/foo.cs", "refs/remotes/origin/master", "src/foo.cs", CommitSha)]
332+
[TestCase("https://github.com/github/VisualStudio/blob/branch-name/src/foo.cs", "refs/remotes/origin/branch-name", "refs/remotes/origin/branch-name:src/foo.cs", "refs/remotes/origin/branch-name", "src/foo.cs", CommitSha)]
333+
[TestCase("https://github.com/github/VisualStudio/blob/fixes/666-bug/src/foo.cs", "refs/remotes/origin/fixes/666-bug", "refs/remotes/origin/fixes/666-bug:src/foo.cs", "refs/remotes/origin/fixes/666-bug", "src/foo.cs", CommitSha)]
334+
[TestCase("https://github.com/github/VisualStudio/blob/fixes/666-bug/A/B/foo.cs", "refs/remotes/origin/fixes/666-bug", "refs/remotes/origin/fixes/666-bug:A/B/foo.cs", "refs/remotes/origin/fixes/666-bug", "A/B/foo.cs", CommitSha)]
335+
[TestCase("https://github.com/github/VisualStudio/blob/master/foo.cs", "refs/remotes/origin/master", null, "refs/remotes/origin/master", null, CommitSha, Description = "Resolve commit only")]
336+
[TestCase("https://github.com/github/VisualStudio/blob/36d6b0bb6e319337180d523281c42d9611744e66/src/code.cs", CommitSha, CommitSha + ":src/code.cs", CommitSha, "src/code.cs", CommitSha, Description = "Resolve commit only")]
337+
[TestCase("https://github.com/github/VisualStudio/commit/8cf9a268c497adb4fc0a14572253165e179dd11e", "8cf9a268c497adb4fc0a14572253165e179dd11e", null, null, null, null)]
338+
public void ResolveBlob(string url, string commitish, string objectish, string expectCommitish, string expectPath, string expectCommitSha)
337339
{
338340
var repositoryDir = "repositoryDir";
339341
var repository = Substitute.For<IRepository>();
340342
var commit = Substitute.For<Commit>();
343+
commit.Sha.Returns(expectCommitSha);
341344
var blob = Substitute.For<Blob>();
342345
repository.Lookup(commitish).Returns(commit);
343346
repository.Lookup(objectish).Returns(blob);
@@ -349,11 +352,11 @@ public void ResolveBlob(string url, string commitish, string objectish, string e
349352
var target = CreateGitHubContextService(repositoryDir, repository);
350353
var context = target.FindContextFromUrl(url);
351354

352-
var (resolvedCommitish, resolvedPath, isSha) = target.ResolveBlob(repositoryDir, context);
355+
var (resolvedCommitish, resolvedPath, commitSha) = target.ResolveBlob(repositoryDir, context);
353356

354357
Assert.That(resolvedCommitish, Is.EqualTo(expectCommitish));
355358
Assert.That(resolvedPath, Is.EqualTo(expectPath));
356-
Assert.That(isSha, Is.EqualTo(expectIsSha));
359+
Assert.That(commitSha, Is.EqualTo(expectCommitSha));
357360
}
358361
}
359362

test/GitHub.VisualStudio.UnitTests/Commands/OpenFromClipboardCommandTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public async Task DifferentLocalRepository()
4242
var activeRepositoryName = "activeRepositoryName";
4343
var activeRepositoryDir = "activeRepositoryDir";
4444
var context = new GitHubContext { RepositoryName = targetRepositoryName };
45-
(string, string, bool)? resolveBlobResult = null;
45+
(string, string, string)? resolveBlobResult = null;
4646
var vsServices = Substitute.For<IVSServices>();
4747
var target = CreateOpenFromClipboardCommand(vsServices: vsServices,
4848
contextFromClipboard: context, repositoryDir: activeRepositoryDir, repositoryName: activeRepositoryName, resolveBlobResult: resolveBlobResult);
@@ -57,7 +57,7 @@ public async Task CouldNotResolve()
5757
{
5858
var context = new GitHubContext();
5959
var repositoryDir = "repositoryDir";
60-
(string, string, bool)? resolveBlobResult = null;
60+
(string, string, string)? resolveBlobResult = null;
6161
var vsServices = Substitute.For<IVSServices>();
6262
var target = CreateOpenFromClipboardCommand(vsServices: vsServices,
6363
contextFromClipboard: context, repositoryDir: repositoryDir, resolveBlobResult: resolveBlobResult);
@@ -72,7 +72,7 @@ public async Task CouldResolve()
7272
{
7373
var context = new GitHubContext();
7474
var repositoryDir = "repositoryDir";
75-
var resolveBlobResult = ("master", "foo.cs", false);
75+
var resolveBlobResult = ("master", "foo.cs", "");
7676
var vsServices = Substitute.For<IVSServices>();
7777
var target = CreateOpenFromClipboardCommand(vsServices: vsServices,
7878
contextFromClipboard: context, repositoryDir: repositoryDir, resolveBlobResult: resolveBlobResult);
@@ -88,7 +88,7 @@ public async Task NoChangesInWorkingDirectory()
8888
var gitHubContextService = Substitute.For<IGitHubContextService>();
8989
var context = new GitHubContext();
9090
var repositoryDir = "repositoryDir";
91-
var resolveBlobResult = ("master", "foo.cs", false);
91+
var resolveBlobResult = ("master", "foo.cs", "");
9292
var vsServices = Substitute.For<IVSServices>();
9393
var target = CreateOpenFromClipboardCommand(gitHubContextService: gitHubContextService, vsServices: vsServices,
9494
contextFromClipboard: context, repositoryDir: repositoryDir, resolveBlobResult: resolveBlobResult, hasChanges: false);
@@ -109,7 +109,7 @@ public async Task HasChangesInWorkingDirectory(bool annotateFileSupported, strin
109109
var context = new GitHubContext();
110110
var repositoryDir = "repositoryDir";
111111
var currentBranch = "currentBranch";
112-
var resolveBlobResult = ("master", "foo.cs", false);
112+
var resolveBlobResult = ("master", "foo.cs", "");
113113
var vsServices = Substitute.For<IVSServices>();
114114
var target = CreateOpenFromClipboardCommand(gitHubContextService: gitHubContextService, vsServices: vsServices,
115115
contextFromClipboard: context, repositoryDir: repositoryDir, currentBranch: currentBranch, resolveBlobResult: resolveBlobResult, hasChanges: true);
@@ -137,7 +137,7 @@ static OpenFromClipboardCommand CreateOpenFromClipboardCommand(
137137
string repositoryDir = null,
138138
string repositoryName = null,
139139
string currentBranch = null,
140-
(string, string, bool)? resolveBlobResult = null,
140+
(string, string, string)? resolveBlobResult = null,
141141
bool? hasChanges = null)
142142
{
143143
var sp = Substitute.For<IServiceProvider>();

0 commit comments

Comments
 (0)