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

Commit ab3e7e9

Browse files
committed
Change ResolveBlob to resolve remote refs
We need to find the same blob as on GitHub.
1 parent de5726f commit ab3e7e9

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

src/GitHub.App/Services/GitHubContextService.cs

Lines changed: 15 additions & 13 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)
252+
public (string commitish, string path, bool isSha) ResolveBlob(string repositoryDir, GitHubContext context, string remoteName = "origin")
253253
{
254254
Guard.ArgumentNotNull(repositoryDir, nameof(repositoryDir));
255255
Guard.ArgumentNotNull(context, nameof(context));
@@ -269,29 +269,31 @@ public bool TryOpenFile(string repositoryDir, GitHubContext context)
269269
}
270270

271271
var objectishPath = $"{context.TreeishPath}/{context.BlobName}";
272-
foreach (var (commitish, path) in ToObjectish(objectishPath))
272+
var objectish = ToObjectish(objectishPath);
273+
var (commitSha, pathSha) = objectish.First();
274+
if (ObjectId.TryParse(commitSha, out ObjectId objectId) && repository.Lookup(objectId) != null)
273275
{
274-
bool isSha;
275-
if (ObjectId.TryParse(commitish, out ObjectId objectId) && repository.Lookup(objectId) != null)
276+
if (repository.Lookup($"{commitSha}:{pathSha}") != null)
276277
{
277-
isSha = true;
278+
return (commitSha, pathSha, true);
278279
}
279-
else if (repository.Lookup(commitish) != null)
280-
{
281-
isSha = false;
282-
}
283-
else
280+
}
281+
282+
foreach (var (commitish, path) in objectish)
283+
{
284+
var remoteRef = $"refs/remotes/{remoteName}/{commitish}";
285+
if (repository.Lookup(remoteRef) == null)
284286
{
285287
continue;
286288
}
287289

288-
if (repository.Lookup($"{commitish}:{path}") == null)
290+
if (repository.Lookup($"{remoteRef}:{path}") == null)
289291
{
290292
// Resolved commitish but not path
291-
return (commitish, null, isSha);
293+
return (remoteRef, null, false);
292294
}
293295

294-
return (commitish, path, isSha);
296+
return (remoteRef, path, false);
295297
}
296298

297299
return (null, null, 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, bool isSha) ResolveBlob(string repositoryDir, GitHubContext context);
17+
(string commitish, string path, bool isSha) 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: 6 additions & 6 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 TheResolveBlobMethod
327327
{
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", null, "master", null, Description = "Resolve commit only")]
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")]
334334
[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")]
335335
[TestCase("https://github.com/github/VisualStudio/commit/8cf9a268c497adb4fc0a14572253165e179dd11e", "8cf9a268c497adb4fc0a14572253165e179dd11e", null, null, null)]
336336
public void ResolveBlob(string url, string commitish, string objectish, string expectCommitish, string expectPath, bool expectIsSha = false)

0 commit comments

Comments
 (0)