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

Commit e3b8168

Browse files
committed
add async method GetLatestSha
1 parent 30a4d0b commit e3b8168

File tree

8 files changed

+39
-8
lines changed

8 files changed

+39
-8
lines changed

src/GitHub.Exports/Models/ILocalRepositoryModel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using GitHub.Primitives;
22
using System.ComponentModel;
3+
using System.Threading.Tasks;
34

45
namespace GitHub.Models
56
{
@@ -31,6 +32,6 @@ public interface ILocalRepositoryModel : IRepositoryModel, INotifyPropertyChange
3132
/// <param name="startLine">A specific line, or (if specifying the <paramref name="endLine"/> as well) the start of a range</param>
3233
/// <param name="endLine">The end of a line range on the specified file.</param>
3334
/// <returns>An UriString with the generated url, or null if the repository has no remote server configured or if it can't be found locally</returns>
34-
UriString GenerateUrl(string path = null, int startLine = -1, int endLine = -1);
35+
Task<UriString> GenerateUrl(string path = null, int startLine = -1, int endLine = -1);
3536
}
3637
}

src/GitHub.Exports/Models/LocalRepositoryModel.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using GitHub.UI;
88
using GitHub.Services;
99
using GitHub.Extensions;
10+
using System.Threading.Tasks;
1011

1112
namespace GitHub.Models
1213
{
@@ -60,12 +61,12 @@ public void Refresh()
6061
/// <param name="startLine">A specific line, or (if specifying the <paramref name="endLine"/> as well) the start of a range</param>
6162
/// <param name="endLine">The end of a line range on the specified file.</param>
6263
/// <returns>An UriString with the generated url, or null if the repository has no remote server configured or if it can't be found locally</returns>
63-
public UriString GenerateUrl(string path = null, int startLine = -1, int endLine = -1)
64+
public async Task<UriString> GenerateUrl(string path = null, int startLine = -1, int endLine = -1)
6465
{
6566
if (CloneUrl == null)
6667
return null;
6768

68-
var sha = HeadSha;
69+
var sha = await GitService.GitServiceHelper.GetLatestPushedSha(path);
6970
// this also incidentally checks whether the repo has a valid LocalPath
7071
if (String.IsNullOrEmpty(sha))
7172
return CloneUrl.ToRepositoryUrl().AbsoluteUri;

src/GitHub.Exports/Services/GitService.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Threading.Tasks;
66
using GitHub.Models;
7+
using System.Linq;
78

89
namespace GitHub.Services
910
{
@@ -70,5 +71,29 @@ public UriString GetRemoteUri(IRepository repo, string remote = "origin")
7071
}
7172

7273
public static IGitService GitServiceHelper => VisualStudio.Services.DefaultExportProvider.GetExportedValueOrDefault<IGitService>() ?? new GitService();
74+
75+
public Task<string> GetLatestPushedSha(string path)
76+
{
77+
var repo = GetRepository(path);
78+
79+
if (repo.Head.IsTracking && repo.Head.Tip.Sha == repo.Head.TrackedBranch.Tip.Sha)
80+
{
81+
return Task.FromResult(repo.Head.Tip.Sha);
82+
}
83+
84+
return Task.Factory.StartNew(() =>
85+
{
86+
var remoteHeads = repo.Refs.Where(r => r.IsRemoteTrackingBranch).ToList();
87+
88+
foreach (var c in repo.Commits)
89+
{
90+
if (repo.Refs.ReachableFrom(remoteHeads, new[] { c }).Any())
91+
{
92+
return c.Sha;
93+
}
94+
}
95+
return null;
96+
});
97+
}
7398
}
7499
}

src/GitHub.Exports/Services/IGitService.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Threading.Tasks;
12
using GitHub.Models;
23
using GitHub.Primitives;
34
using LibGit2Sharp;
@@ -44,5 +45,7 @@ public interface IGitService
4445
/// <param name="repo"></param>
4546
/// <returns></returns>
4647
UriString GetRemoteUri(IRepository repo, string remote = "origin");
48+
49+
Task<string> GetLatestPushedSha(string path);
4750
}
4851
}

src/GitHub.VisualStudio/Menus/CopyLink.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public async void Activate([AllowNull]object data = null)
3333
if (!isgithub)
3434
return;
3535

36-
var link = GenerateLink();
36+
var link = await GenerateLink();
3737
if (link == null)
3838
return;
3939
try

src/GitHub.VisualStudio/Menus/LinkMenuBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using GitHub.Extensions;
33
using GitHub.Primitives;
44
using System;
5+
using System.Threading.Tasks;
56

67
namespace GitHub.VisualStudio.Menus
78
{
@@ -12,7 +13,7 @@ public LinkMenuBase(IServiceProvider serviceProvider, ISimpleApiClientFactory ap
1213
{
1314
}
1415

15-
protected UriString GenerateLink()
16+
protected Task<UriString> GenerateLink()
1617
{
1718
var repo = ActiveRepo;
1819
var activeDocument = ServiceProvider.GetExportedValue<IActiveDocumentSnapshot>();

src/GitHub.VisualStudio/Menus/OpenLink.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public async void Activate([AllowNull]object data = null)
3131
if (!isgithub)
3232
return;
3333

34-
var link = GenerateLink();
34+
var link = await GenerateLink();
3535
if (link == null)
3636
return;
3737
var browser = ServiceProvider.GetExportedValue<IVisualStudioBrowser>();

src/UnitTests/GitHub.Exports/LocalRepositoryModelTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ static void SetupRepository(string sha)
5454
[InlineData(19, false, "[email protected]/foo/bar", "123123", @"src\dir\file1.cs", -1, -1, "https://github.com/foo/bar/blob/123123/src/dir/file1.cs")]
5555
[InlineData(20, false, "[email protected]/foo/bar", "123123", @"src\dir\File1.cs", -1, -1, "https://github.com/foo/bar/blob/123123/src/dir/File1.cs")]
5656
[InlineData(21, false, "[email protected]/foo/bar", "123123", @"src\dir\ThisIsFile1.cs", -1, -1, "https://github.com/foo/bar/blob/123123/src/dir/ThisIsFile1.cs")]
57-
public void GenerateUrl(int testid, bool createRootedPath, string baseUrl, string sha, string path, int startLine, int endLine, string expected)
57+
public async void GenerateUrl(int testid, bool createRootedPath, string baseUrl, string sha, string path, int startLine, int endLine, string expected)
5858
{
5959
using (var temp = new TempDirectory())
6060
{
@@ -68,7 +68,7 @@ public void GenerateUrl(int testid, bool createRootedPath, string baseUrl, strin
6868
model = new LocalRepositoryModel("bar", new UriString(baseUrl), basePath.FullName);
6969
else
7070
model = new LocalRepositoryModel(basePath.FullName);
71-
var result = model.GenerateUrl(path, startLine, endLine);
71+
var result = await model.GenerateUrl(path, startLine, endLine);
7272
Assert.Equal(expected, result?.ToString());
7373
}
7474
}

0 commit comments

Comments
 (0)