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

Commit de5726f

Browse files
committed
Make ResolveBlob return if commitish is a SHA
1 parent 3029ccd commit de5726f

File tree

5 files changed

+46
-31
lines changed

5 files changed

+46
-31
lines changed

src/GitHub.App/Services/GitHubContextService.cs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ public GitHubContext FindContextFromWindowTitle(string windowTitle)
237237

238238
public bool TryOpenFile(string repositoryDir, GitHubContext context)
239239
{
240-
var (commitish, path) = ResolveBlob(repositoryDir, context);
240+
var (commitish, path, isSha) = ResolveBlob(repositoryDir, context);
241241
if (path == null)
242242
{
243243
return false;
@@ -249,7 +249,7 @@ public bool TryOpenFile(string repositoryDir, GitHubContext context)
249249
return true;
250250
}
251251

252-
public (string commitish, string path) ResolveBlob(string repositoryDir, GitHubContext context)
252+
public (string commitish, string path, bool isSha) ResolveBlob(string repositoryDir, GitHubContext context)
253253
{
254254
Guard.ArgumentNotNull(repositoryDir, nameof(repositoryDir));
255255
Guard.ArgumentNotNull(context, nameof(context));
@@ -259,34 +259,42 @@ 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);
262+
return (null, null, false);
263263
}
264264

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

271271
var objectishPath = $"{context.TreeishPath}/{context.BlobName}";
272-
foreach (var objectish in ToObjectish(objectishPath))
272+
foreach (var (commitish, path) in ToObjectish(objectishPath))
273273
{
274-
var commit = repository.Lookup(objectish.commitish);
275-
if (commit == null)
274+
bool isSha;
275+
if (ObjectId.TryParse(commitish, out ObjectId objectId) && repository.Lookup(objectId) != null)
276+
{
277+
isSha = true;
278+
}
279+
else if (repository.Lookup(commitish) != null)
280+
{
281+
isSha = false;
282+
}
283+
else
276284
{
277285
continue;
278286
}
279287

280-
var blob = repository.Lookup($"{objectish.commitish}:{objectish.path}");
281-
if (blob == null)
288+
if (repository.Lookup($"{commitish}:{path}") == null)
282289
{
283-
return (objectish.commitish, null);
290+
// Resolved commitish but not path
291+
return (commitish, null, isSha);
284292
}
285293

286-
return objectish;
294+
return (commitish, path, isSha);
287295
}
288296

289-
return (null, null);
297+
return (null, null, false);
290298
}
291299

292300
IEnumerable<(string commitish, string path)> ToObjectish(string treeishPath)
@@ -314,7 +322,7 @@ public bool HasChangesInWorkingDirectory(string repositoryDir, string commitish,
314322

315323
public async Task<bool> TryAnnotateFile(string repositoryDir, string currentBranch, GitHubContext context)
316324
{
317-
var (commitish, path) = ResolveBlob(repositoryDir, context);
325+
var (commitish, path, isSha) = ResolveBlob(repositoryDir, context);
318326
if (path == null)
319327
{
320328
return false;

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) ResolveBlob(string repositoryDir, GitHubContext context);
17+
(string commitish, string path, bool isSha) ResolveBlob(string repositoryDir, GitHubContext context);
1818
bool HasChangesInWorkingDirectory(string repositoryDir, string commitish, string path);
1919
}
2020
}

src/GitHub.VisualStudio/Commands/OpenFromClipboardCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public override async Task Execute(string url)
6868
return;
6969
}
7070

71-
var (commitish, path) = gitHubContextService.Value.ResolveBlob(repositoryDir, context);
71+
var (commitish, path, isSha) = gitHubContextService.Value.ResolveBlob(repositoryDir, context);
7272
if (path == null)
7373
{
7474
vsServices.Value.ShowMessageBoxInfo(NoResolveMessage);

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,22 +331,29 @@ public class TheResolveBlobMethod
331331
[TestCase("https://github.com/github/VisualStudio/blob/fixes/666-bug/src/foo.cs", "fixes/666-bug", "fixes/666-bug:src/foo.cs", "fixes/666-bug", "src/foo.cs")]
332332
[TestCase("https://github.com/github/VisualStudio/blob/fixes/666-bug/A/B/foo.cs", "fixes/666-bug", "fixes/666-bug:A/B/foo.cs", "fixes/666-bug", "A/B/foo.cs")]
333333
[TestCase("https://github.com/github/VisualStudio/blob/master/foo.cs", "master", null, "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")]
334335
[TestCase("https://github.com/github/VisualStudio/commit/8cf9a268c497adb4fc0a14572253165e179dd11e", "8cf9a268c497adb4fc0a14572253165e179dd11e", null, null, null)]
335-
public void ResolveBlob(string url, string commitish, string objectish, string expectCommitish, string expectPath)
336+
public void ResolveBlob(string url, string commitish, string objectish, string expectCommitish, string expectPath, bool expectIsSha = false)
336337
{
337338
var repositoryDir = "repositoryDir";
338339
var repository = Substitute.For<IRepository>();
339340
var commit = Substitute.For<Commit>();
340341
var blob = Substitute.For<Blob>();
341342
repository.Lookup(commitish).Returns(commit);
342343
repository.Lookup(objectish).Returns(blob);
344+
if (ObjectId.TryParse(commitish, out ObjectId objectId))
345+
{
346+
// If it looks like a SHA, allow lookup using its ObjectId
347+
repository.Lookup(objectId).Returns(blob);
348+
}
343349
var target = CreateGitHubContextService(repositoryDir, repository);
344350
var context = target.FindContextFromUrl(url);
345351

346-
var (resolvedCommitish, resolvedPath) = target.ResolveBlob(repositoryDir, context);
352+
var (resolvedCommitish, resolvedPath, isSha) = target.ResolveBlob(repositoryDir, context);
347353

348354
Assert.That(resolvedCommitish, Is.EqualTo(expectCommitish));
349355
Assert.That(resolvedPath, Is.EqualTo(expectPath));
356+
Assert.That(isSha, Is.EqualTo(expectIsSha));
350357
}
351358
}
352359

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

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ public async Task DifferentLocalRepository()
4242
var activeRepositoryName = "activeRepositoryName";
4343
var activeRepositoryDir = "activeRepositoryDir";
4444
var context = new GitHubContext { RepositoryName = targetRepositoryName };
45-
(string, string)? gitObject = null;
45+
(string, string, bool)? resolveBlobResult = null;
4646
var vsServices = Substitute.For<IVSServices>();
4747
var target = CreateOpenFromClipboardCommand(vsServices: vsServices,
48-
contextFromClipboard: context, repositoryDir: activeRepositoryDir, repositoryName: activeRepositoryName, gitObject: gitObject);
48+
contextFromClipboard: context, repositoryDir: activeRepositoryDir, repositoryName: activeRepositoryName, resolveBlobResult: resolveBlobResult);
4949

5050
await target.Execute(null);
5151

@@ -57,10 +57,10 @@ public async Task CouldNotResolve()
5757
{
5858
var context = new GitHubContext();
5959
var repositoryDir = "repositoryDir";
60-
(string, string)? gitObject = null;
60+
(string, string, bool)? resolveBlobResult = null;
6161
var vsServices = Substitute.For<IVSServices>();
6262
var target = CreateOpenFromClipboardCommand(vsServices: vsServices,
63-
contextFromClipboard: context, repositoryDir: repositoryDir, gitObject: gitObject);
63+
contextFromClipboard: context, repositoryDir: repositoryDir, resolveBlobResult: resolveBlobResult);
6464

6565
await target.Execute(null);
6666

@@ -72,10 +72,10 @@ public async Task CouldResolve()
7272
{
7373
var context = new GitHubContext();
7474
var repositoryDir = "repositoryDir";
75-
var gitObject = ("master", "foo.cs");
75+
var resolveBlobResult = ("master", "foo.cs", false);
7676
var vsServices = Substitute.For<IVSServices>();
7777
var target = CreateOpenFromClipboardCommand(vsServices: vsServices,
78-
contextFromClipboard: context, repositoryDir: repositoryDir, gitObject: gitObject);
78+
contextFromClipboard: context, repositoryDir: repositoryDir, resolveBlobResult: resolveBlobResult);
7979

8080
await target.Execute(null);
8181

@@ -88,10 +88,10 @@ public async Task NoChangesInWorkingDirectory()
8888
var gitHubContextService = Substitute.For<IGitHubContextService>();
8989
var context = new GitHubContext();
9090
var repositoryDir = "repositoryDir";
91-
var gitObject = ("master", "foo.cs");
91+
var resolveBlobResult = ("master", "foo.cs", false);
9292
var vsServices = Substitute.For<IVSServices>();
9393
var target = CreateOpenFromClipboardCommand(gitHubContextService: gitHubContextService, vsServices: vsServices,
94-
contextFromClipboard: context, repositoryDir: repositoryDir, gitObject: gitObject, hasChanges: false);
94+
contextFromClipboard: context, repositoryDir: repositoryDir, resolveBlobResult: resolveBlobResult, hasChanges: false);
9595

9696
await target.Execute(null);
9797

@@ -109,10 +109,10 @@ public async Task HasChangesInWorkingDirectory(bool annotateFileSupported, strin
109109
var context = new GitHubContext();
110110
var repositoryDir = "repositoryDir";
111111
var currentBranch = "currentBranch";
112-
var gitObject = ("master", "foo.cs");
112+
var resolveBlobResult = ("master", "foo.cs", false);
113113
var vsServices = Substitute.For<IVSServices>();
114114
var target = CreateOpenFromClipboardCommand(gitHubContextService: gitHubContextService, vsServices: vsServices,
115-
contextFromClipboard: context, repositoryDir: repositoryDir, currentBranch: currentBranch, gitObject: gitObject, hasChanges: true);
115+
contextFromClipboard: context, repositoryDir: repositoryDir, currentBranch: currentBranch, resolveBlobResult: resolveBlobResult, hasChanges: true);
116116

117117
await target.Execute(null);
118118

@@ -137,7 +137,7 @@ static OpenFromClipboardCommand CreateOpenFromClipboardCommand(
137137
string repositoryDir = null,
138138
string repositoryName = null,
139139
string currentBranch = null,
140-
(string, string)? gitObject = null,
140+
(string, string, bool)? resolveBlobResult = null,
141141
bool? hasChanges = null)
142142
{
143143
var sp = Substitute.For<IServiceProvider>();
@@ -149,14 +149,14 @@ static OpenFromClipboardCommand CreateOpenFromClipboardCommand(
149149
teamExplorerContext.ActiveRepository.LocalPath.Returns(repositoryDir);
150150
teamExplorerContext.ActiveRepository.Name.Returns(repositoryName);
151151
teamExplorerContext.ActiveRepository.CurrentBranch.Name.Returns(currentBranch);
152-
if (gitObject != null)
152+
if (resolveBlobResult != null)
153153
{
154-
gitHubContextService.ResolveBlob(repositoryDir, contextFromClipboard).Returns(gitObject.Value);
154+
gitHubContextService.ResolveBlob(repositoryDir, contextFromClipboard).Returns(resolveBlobResult.Value);
155155
}
156156

157157
if (hasChanges != null)
158158
{
159-
gitHubContextService.HasChangesInWorkingDirectory(repositoryDir, gitObject.Value.Item1, gitObject.Value.Item2).Returns(hasChanges.Value);
159+
gitHubContextService.HasChangesInWorkingDirectory(repositoryDir, resolveBlobResult.Value.Item1, resolveBlobResult.Value.Item2).Returns(hasChanges.Value);
160160
}
161161

162162
return new OpenFromClipboardCommand(

0 commit comments

Comments
 (0)