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

Commit 1f0f5fb

Browse files
committed
Return commitish and path from ResolveGitObject
Interface shouldn't depend on LibGit2Sharp and return stale GitObject.
1 parent 92ffa39 commit 1f0f5fb

File tree

9 files changed

+36
-27
lines changed

9 files changed

+36
-27
lines changed

src/GitHub.App/Services/GitHubContextService.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -307,42 +307,40 @@ public string ResolvePath(GitHubContext context)
307307
return null;
308308
}
309309

310-
public GitObject ResolveGitObject(string repositoryDir, GitHubContext context)
310+
public (string commitish, string path) ResolveGitObject(string repositoryDir, GitHubContext context)
311311
{
312312
Guard.ArgumentNotNull(repositoryDir, nameof(repositoryDir));
313313
Guard.ArgumentNotNull(context, nameof(context));
314314

315315
using (var repository = gitService.GetRepository(repositoryDir))
316316
{
317-
var path = context.TreeishPath;
317+
var objectishPath = context.TreeishPath;
318318
if (context.BlobName != null)
319319
{
320-
path += '/' + context.BlobName;
320+
objectishPath += '/' + context.BlobName;
321321
}
322322

323-
foreach (var treeish in ToTreeish(path))
323+
foreach (var objectish in ToObjectish(objectishPath))
324324
{
325-
var gitObject = repository.Lookup(treeish);
325+
var gitObject = repository.Lookup($"{objectish.commitish}:{objectish.path}");
326326
if (gitObject != null)
327327
{
328-
return gitObject;
328+
return objectish;
329329
}
330330
}
331331

332-
return null;
332+
return (null, null);
333333
}
334334
}
335335

336-
static IEnumerable<string> ToTreeish(string treeishPath)
336+
static IEnumerable<(string commitish, string path)> ToObjectish(string treeishPath)
337337
{
338-
yield return treeishPath;
339-
340338
var index = 0;
341339
while ((index = treeishPath.IndexOf('/', index + 1)) != -1)
342340
{
343341
var commitish = treeishPath.Substring(0, index);
344342
var path = treeishPath.Substring(index + 1);
345-
yield return $"{commitish}:{path}";
343+
yield return (commitish, path);
346344
}
347345
}
348346

src/GitHub.Exports/Services/IGitHubContextService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ public interface IGitHubContextService
1414
string ResolvePath(GitHubContext context);
1515
Uri ToRepositoryUrl(GitHubContext context);
1616
bool TryOpenFile(string repositoryDir, GitHubContext context);
17-
GitObject ResolveGitObject(string repositoryDir, GitHubContext context);
17+
(string commitish, string path) ResolveGitObject(string repositoryDir, GitHubContext context);
1818
}
1919
}

src/GitHub.VisualStudio/Commands/OpenFromClipboardCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ public override Task Execute(string url)
5959
return Task.CompletedTask;
6060
}
6161

62-
var gitObject = gitHubContextService.Value.ResolveGitObject(repositoryDir, context);
63-
if (gitObject == null)
62+
var (commitish, path) = gitHubContextService.Value.ResolveGitObject(repositoryDir, context);
63+
if (path == null)
6464
{
6565
vsServices.Value.ShowMessageBoxInfo(NoResolveMessage);
6666
return Task.CompletedTask;

src/GitHub.VisualStudio/GitHub.VisualStudio.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@
303303
<HintPath>..\..\packages\Rx-XAML.2.2.5-custom\lib\net45\System.Reactive.Windows.Threading.dll</HintPath>
304304
<Private>True</Private>
305305
</Reference>
306+
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
307+
<HintPath>..\..\packages\System.ValueTuple.4.5.0\lib\net461\System.ValueTuple.dll</HintPath>
308+
</Reference>
306309
<Reference Include="System.Windows.Forms" />
307310
<Reference Include="System.Windows.Interactivity, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
308311
<HintPath>..\..\packages\Expression.Blend.Sdk.WPF.1.0.1\lib\net45\System.Windows.Interactivity.dll</HintPath>

src/GitHub.VisualStudio/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@
4949
<package id="SerilogAnalyzer" version="0.12.0.0" targetFramework="net461" />
5050
<package id="SQLitePCL.raw_basic" version="0.7.3.0-vs2012" targetFramework="net45" />
5151
<package id="Stateless" version="2.5.56.0" targetFramework="net45" />
52+
<package id="System.ValueTuple" version="4.5.0" targetFramework="net461" />
5253
</packages>

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -325,12 +325,12 @@ 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")]
329-
[TestCase("https://github.com/github/VisualStudio/blob/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")]
331-
[TestCase("https://github.com/github/VisualStudio/blob/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")]
333-
public void ResolveGitObject(string url, string treeish)
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)
334334
{
335335
var repositoryDir = "repositoryDir";
336336
var repository = Substitute.For<IRepository>();
@@ -339,9 +339,10 @@ public void ResolveGitObject(string url, string treeish)
339339
var target = CreateGitHubContextService(repositoryDir, repository);
340340
var context = target.FindContextFromUrl(url);
341341

342-
var gitObject = target.ResolveGitObject(repositoryDir, context);
342+
var (commitish, path) = target.ResolveGitObject(repositoryDir, context);
343343

344-
Assert.That(gitObject, Is.EqualTo(expectGitObject));
344+
Assert.That(commitish, Is.EqualTo(expectCommitish));
345+
Assert.That(path, Is.EqualTo(expectPath));
345346
}
346347
}
347348

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using Microsoft.VisualStudio;
66
using NSubstitute;
77
using NUnit.Framework;
8-
using LibGit2Sharp;
98

109
public class OpenFromClipboardCommandTests
1110
{
@@ -41,7 +40,7 @@ public async Task CouldNotResolve()
4140
{
4241
var context = new GitHubContext();
4342
var repositoryDir = "repositoryDir";
44-
var gitObject = null as GitObject;
43+
(string, string)? gitObject = null;
4544
var vsServices = Substitute.For<IVSServices>();
4645
var target = CreateOpenFromClipboardCommand(vsServices: vsServices,
4746
contextFromClipboard: context, repositoryDir: repositoryDir, gitObject: gitObject);
@@ -56,7 +55,7 @@ public async Task CouldResolve()
5655
{
5756
var context = new GitHubContext();
5857
var repositoryDir = "repositoryDir";
59-
var gitObject = Substitute.For<GitObject>();
58+
var gitObject = ("master", "foo.cs");
6059
var vsServices = Substitute.For<IVSServices>();
6160
var target = CreateOpenFromClipboardCommand(vsServices: vsServices,
6261
contextFromClipboard: context, repositoryDir: repositoryDir, gitObject: gitObject);
@@ -72,16 +71,19 @@ static OpenFromClipboardCommand CreateOpenFromClipboardCommand(
7271
IVSServices vsServices = null,
7372
GitHubContext contextFromClipboard = null,
7473
string repositoryDir = null,
75-
GitObject gitObject = null)
74+
(string, string)? gitObject = null)
7675
{
7776
var sp = Substitute.For<IServiceProvider>();
7877
gitHubContextService = gitHubContextService ?? Substitute.For<IGitHubContextService>();
7978
teamExplorerContext = teamExplorerContext ?? Substitute.For<ITeamExplorerContext>();
8079
vsServices = vsServices ?? Substitute.For<IVSServices>();
8180

8281
gitHubContextService.FindContextFromClipboard().Returns(contextFromClipboard);
83-
gitHubContextService.ResolveGitObject(repositoryDir, contextFromClipboard).Returns(gitObject);
8482
teamExplorerContext.ActiveRepository.LocalPath.Returns(repositoryDir);
83+
if (gitObject != null)
84+
{
85+
gitHubContextService.ResolveGitObject(repositoryDir, contextFromClipboard).Returns(gitObject.Value);
86+
}
8587

8688
return new OpenFromClipboardCommand(
8789
new Lazy<IGitHubContextService>(() => gitHubContextService),

test/GitHub.VisualStudio.UnitTests/GitHub.VisualStudio.UnitTests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@
193193
<HintPath>..\..\packages\Rx-XAML.2.2.5-custom\lib\net45\System.Reactive.Windows.Threading.dll</HintPath>
194194
<Private>True</Private>
195195
</Reference>
196+
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
197+
<HintPath>..\..\packages\System.ValueTuple.4.5.0\lib\net461\System.ValueTuple.dll</HintPath>
198+
</Reference>
196199
<Reference Include="System.Xaml" />
197200
<Reference Include="Microsoft.CSharp" />
198201
<Reference Include="System.Xml" />

test/GitHub.VisualStudio.UnitTests/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@
3636
<package id="Rx-PlatformServices" version="2.2.5-custom" targetFramework="net45" />
3737
<package id="Rx-Testing" version="2.2.5-custom" targetFramework="net45" />
3838
<package id="Rx-XAML" version="2.2.5-custom" targetFramework="net45" />
39+
<package id="System.ValueTuple" version="4.5.0" targetFramework="net461" />
3940
</packages>

0 commit comments

Comments
 (0)