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

Commit 1d468d9

Browse files
committed
Let ResolveGitObject resolve commit without path
This will let us give user more meaningful warnings.
1 parent 1f0f5fb commit 1d468d9

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

src/GitHub.App/Services/GitHubContextService.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
using Microsoft.VisualStudio.Shell;
1515
using Microsoft.VisualStudio.Shell.Interop;
1616
using Microsoft.VisualStudio.TextManager.Interop;
17-
using LibGit2Sharp;
1817

1918
namespace GitHub.App.Services
2019
{
@@ -322,11 +321,19 @@ public string ResolvePath(GitHubContext context)
322321

323322
foreach (var objectish in ToObjectish(objectishPath))
324323
{
325-
var gitObject = repository.Lookup($"{objectish.commitish}:{objectish.path}");
326-
if (gitObject != null)
324+
var commit = repository.Lookup(objectish.commitish);
325+
if (commit == null)
327326
{
328-
return objectish;
327+
continue;
329328
}
329+
330+
var blob = repository.Lookup($"{objectish.commitish}:{objectish.path}");
331+
if (blob == null)
332+
{
333+
return (objectish.commitish, null);
334+
}
335+
336+
return objectish;
330337
}
331338

332339
return (null, null);

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

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

326326
public class TheResolveGitObjectMethod
327327
{
328-
[TestCase("https://github.com/github/VisualStudio/blob/master/foo.cs", "master:foo.cs", "master", "foo.cs")]
329-
[TestCase("https://github.com/github/VisualStudio/blob/master/src/foo.cs", "master:src/foo.cs", "master", "src/foo.cs")]
330-
[TestCase("https://github.com/github/VisualStudio/blob/branch-name/src/foo.cs", "branch-name:src/foo.cs", "branch-name", "src/foo.cs")]
331-
[TestCase("https://github.com/github/VisualStudio/blob/fixes/666-bug/src/foo.cs", "fixes/666-bug:src/foo.cs", "fixes/666-bug", "src/foo.cs")]
332-
[TestCase("https://github.com/github/VisualStudio/blob/fixes/666-bug/A/B/foo.cs", "fixes/666-bug:A/B/foo.cs", "fixes/666-bug", "A/B/foo.cs")]
333-
public void ResolveGitObject(string url, string treeish, string expectCommitish, string expectPath)
328+
[TestCase("https://github.com/github/VisualStudio/blob/master/foo.cs", "master", "master:foo.cs", "master", "foo.cs")]
329+
[TestCase("https://github.com/github/VisualStudio/blob/master/src/foo.cs", "master", "master:src/foo.cs", "master", "src/foo.cs")]
330+
[TestCase("https://github.com/github/VisualStudio/blob/branch-name/src/foo.cs", "branch-name", "branch-name:src/foo.cs", "branch-name", "src/foo.cs")]
331+
[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")]
332+
[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")]
333+
[TestCase("https://github.com/github/VisualStudio/blob/master/foo.cs", "master", "", "master", null, Description = "Resolve commit only")]
334+
public void ResolveGitObject(string url, string commitish, string objectish, string expectCommitish, string expectPath)
334335
{
335336
var repositoryDir = "repositoryDir";
336337
var repository = Substitute.For<IRepository>();
337-
var expectGitObject = Substitute.For<GitObject>();
338-
repository.Lookup(treeish).Returns(expectGitObject);
338+
var commit = Substitute.For<Commit>();
339+
var blob = Substitute.For<Blob>();
340+
repository.Lookup(commitish).Returns(commit);
341+
repository.Lookup(objectish).Returns(blob);
339342
var target = CreateGitHubContextService(repositoryDir, repository);
340343
var context = target.FindContextFromUrl(url);
341344

342-
var (commitish, path) = target.ResolveGitObject(repositoryDir, context);
345+
var (resolvedCommitish, resolvedPath) = target.ResolveGitObject(repositoryDir, context);
343346

344-
Assert.That(commitish, Is.EqualTo(expectCommitish));
345-
Assert.That(path, Is.EqualTo(expectPath));
347+
Assert.That(resolvedCommitish, Is.EqualTo(expectCommitish));
348+
Assert.That(resolvedPath, Is.EqualTo(expectPath));
346349
}
347350
}
348351

0 commit comments

Comments
 (0)