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

Commit 7de9572

Browse files
committed
Use CalculateHistoryDivergence for all remotes
Use CalculateHistoryDivergence to search all remotes. The original implementation of this method searched all remotes but not `refs/pull/*`. Keep the original behaviour for the moment.
1 parent ebd710d commit 7de9572

2 files changed

Lines changed: 7 additions & 49 deletions

File tree

src/GitHub.Exports/Services/GitService.cs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,13 @@ public Task<string> GetLatestPushedSha(string path, string remote = "origin")
132132
}
133133
}
134134

135-
// This is a less common case where a branch was forked from a local branch
136-
// which has since had new commits pulled to it.
135+
// This is a less common case where a branch was forked from a branch
136+
// which has since had new commits added to it.
137137
var nearestCommonAncestor = repo.Branches
138-
.Where(b => b.IsRemote && b.RemoteName == remote)
139-
.Select(b => repo.ObjectDatabase.CalculateHistoryDivergence(b.Tip, repo.Head.Tip))
138+
.Where(b => b.IsRemote)
139+
.Select(b => b.Tip)
140+
.Distinct()
141+
.Select(c => repo.ObjectDatabase.CalculateHistoryDivergence(c, repo.Head.Tip))
140142
.Where(hd => hd.AheadBy != null)
141143
.OrderBy(hd => hd.BehindBy)
142144
.Select(hd => hd.CommonAncestor)
@@ -145,22 +147,6 @@ public Task<string> GetLatestPushedSha(string path, string remote = "origin")
145147
{
146148
return nearestCommonAncestor.Sha;
147149
}
148-
149-
// This is a less case where a branch was forked from a reference rather than a
150-
// branch that is tracking a remote (e.g. from the head of a PR `refs/pull/#/head`).
151-
var branchPrefix = $"refs/remotes/{remote}/";
152-
var pullPrefix = "refs/pull/";
153-
var remoteHeads = repo.Refs.Where(r =>
154-
r.CanonicalName.StartsWith(branchPrefix, StringComparison.Ordinal) ||
155-
r.CanonicalName.StartsWith(pullPrefix, StringComparison.Ordinal))
156-
.ToList();
157-
foreach (var commit in repo.Commits.QueryBy(sortByTopological))
158-
{
159-
if (repo.Refs.ReachableFrom(remoteHeads, new[] { commit }).Any())
160-
{
161-
return commit.Sha;
162-
}
163-
}
164150
}
165151

166152
return null;

test/GitHub.Exports.UnitTests/GitServiceTests.cs

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public async Task TowPossibleRemoteBranches_ReturnNearestCommitSha()
202202
}
203203

204204
[TestCase("origin", true)]
205-
[TestCase("jcansdale", false)]
205+
[TestCase("jcansdale", true, Description = "Search all remotes")]
206206
public async Task BehindRemoteBranch_ReturnRemoteCommitSha(string remoteName, bool expectFound)
207207
{
208208
using (var temp = new TempDirectory())
@@ -228,34 +228,6 @@ public async Task BehindRemoteBranch_ReturnRemoteCommitSha(string remoteName, bo
228228
}
229229
}
230230

231-
[TestCase("refs/remotes/origin/master", true)]
232-
[TestCase("refs/pull/777/head", true)]
233-
[TestCase("refs/heads/other", false)]
234-
[TestCase("refs/remotes/other/master", false)]
235-
public async Task BehindRefWithCanonicalName_ReturnRemoteCommitSha(string canonicalName, bool expectFound)
236-
{
237-
using (var temp = new TempDirectory())
238-
{
239-
string expectSha;
240-
var dir = temp.Directory.FullName;
241-
using (var repo = new Repository(Repository.Init(dir)))
242-
{
243-
AddCommit(repo); // First commit
244-
var commit1 = AddCommit(repo);
245-
var commit2 = AddCommit(repo);
246-
repo.Reset(ResetMode.Hard, commit1);
247-
expectSha = expectFound ? commit1.Sha : null;
248-
repo.Refs.Add(canonicalName, commit2.Id);
249-
}
250-
251-
var target = new GitService(new RepositoryFacade());
252-
253-
var sha = await target.GetLatestPushedSha(dir).ConfigureAwait(false);
254-
255-
Assert.That(sha, Is.EqualTo(expectSha));
256-
}
257-
}
258-
259231
static Commit AddCommit(Repository repo)
260232
{
261233
var dir = repo.Info.WorkingDirectory;

0 commit comments

Comments
 (0)