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

Commit ebd710d

Browse files
committed
Use CalculateHistoryDivergence to find nearest
Use CalculateHistoryDivergence to find the remote branch which it behind by the least number of commits.
1 parent 22a0373 commit ebd710d

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

src/GitHub.Exports/Services/GitService.cs

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

135-
// This is the less common case where a branch was forked from a local branch
136-
// which has since had new commits pulled to it. It also covers cases where a
137-
// branch was forked from a reference rather than a branch that is tracking
138-
// a remote (e.g. from the head of a PR `refs/pull/#/head`).
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.
137+
var nearestCommonAncestor = repo.Branches
138+
.Where(b => b.IsRemote && b.RemoteName == remote)
139+
.Select(b => repo.ObjectDatabase.CalculateHistoryDivergence(b.Tip, repo.Head.Tip))
140+
.Where(hd => hd.AheadBy != null)
141+
.OrderBy(hd => hd.BehindBy)
142+
.Select(hd => hd.CommonAncestor)
143+
.FirstOrDefault();
144+
if (nearestCommonAncestor != null)
145+
{
146+
return nearestCommonAncestor.Sha;
147+
}
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`).
139151
var branchPrefix = $"refs/remotes/{remote}/";
140152
var pullPrefix = "refs/pull/";
141153
var remoteHeads = repo.Refs.Where(r =>

0 commit comments

Comments
 (0)