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

Commit aa1fbbb

Browse files
authored
Merge pull request #1156 from github/fixes/1155-fix-isheadpushed
Fix implementation of IsHeadPushed.
2 parents 12024f2 + 6bd963f commit aa1fbbb

File tree

2 files changed

+11
-37
lines changed

2 files changed

+11
-37
lines changed

src/GitHub.App/Services/GitClient.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -424,16 +424,7 @@ public Task<bool> IsHeadPushed(IRepository repo)
424424

425425
return Task.Factory.StartNew(() =>
426426
{
427-
if (repo.Head.IsTracking)
428-
{
429-
var trackedBranchTip = repo.Head.TrackedBranch.Tip;
430-
if (trackedBranchTip != null)
431-
{
432-
return repo.Head.Tip.Sha == trackedBranchTip.Sha;
433-
}
434-
}
435-
436-
return false;
427+
return repo.Head.TrackingDetails.AheadBy == 0;
437428
});
438429
}
439430

src/UnitTests/GitHub.App/Services/GitClientTests.cs

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,29 @@ public class GitClientTests
1414
public class TheIsHeadPushedMethod : TestBaseClass
1515
{
1616
[Theory]
17-
[InlineData(true, "sha", "sha", true)]
18-
[InlineData(true, "xxx", "yyy", false)]
19-
[InlineData(true, "headSha", null, false)]
20-
[InlineData(false, "sha", "sha", false)]
21-
public async Task IsHeadPushed(bool istracking, string headSha, string trackedBranchSha, bool expectHeadPushed)
17+
[InlineData(0, true)]
18+
[InlineData(2, false)]
19+
[InlineData(null, false)]
20+
public async Task IsHeadPushed(int? aheadBy, bool expected)
2221
{
2322
var gitClient = new GitClient(Substitute.For<IGitHubCredentialProvider>());
24-
var repository = MockTrackedBranchRepository(istracking, headSha, trackedBranchSha);
23+
var repository = MockTrackedBranchRepository(aheadBy);
2524

2625
var isHeadPushed = await gitClient.IsHeadPushed(repository);
2726

28-
Assert.Equal(expectHeadPushed, isHeadPushed);
27+
Assert.Equal(expected, isHeadPushed);
2928
}
3029

31-
static IRepository MockTrackedBranchRepository(bool isTracking, string headSha, string trackedBranchSha)
30+
static IRepository MockTrackedBranchRepository(int? aheadBy)
3231
{
33-
var trackedBranch = Substitute.For<Branch>();
34-
var trackedBranchTip = MockCommitOrNull(trackedBranchSha);
35-
trackedBranch.Tip.Returns(trackedBranchTip);
3632
var headBranch = Substitute.For<Branch>();
37-
var headTip = MockCommitOrNull(headSha);
38-
headBranch.Tip.Returns(headTip);
39-
headBranch.IsTracking.Returns(isTracking);
40-
headBranch.TrackedBranch.Returns(trackedBranch);
33+
var trackingDetails = Substitute.For<BranchTrackingDetails>();
34+
trackingDetails.AheadBy.Returns(aheadBy);
35+
headBranch.TrackingDetails.Returns(trackingDetails);
4136
var repository = Substitute.For<IRepository>();
4237
repository.Head.Returns(headBranch);
4338
return repository;
4439
}
45-
46-
static Commit MockCommitOrNull(string sha)
47-
{
48-
if (sha != null)
49-
{
50-
var commit = Substitute.For<Commit>();
51-
commit.Sha.Returns(sha);
52-
return commit;
53-
}
54-
55-
return null;
56-
}
5740
}
5841

5942
public class ThePushMethod : TestBaseClass

0 commit comments

Comments
 (0)