Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/Stack.Tests/Commands/Helpers/StackHelpersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -472,4 +472,38 @@ public void PushChanges_WhenSomeLocalBranchesAreAhead_OnlyPushesChangesForBranch
// Assert
branchesPushedToRemote.ToArray().Should().BeEquivalentTo([branchAheadOfRemote]);
}

[Fact]
public void PullChanges_WhenSomeBranchesHaveChanges_AndOthersDoNot_OnlyPullsChangesForBranchesThatNeedIt()
{
// Arrange
var sourceBranch = Some.BranchName();
var branchWithRemoteChanges = Some.BranchName();
var branchWithoutRemoteChanges = Some.BranchName();

var gitClient = Substitute.For<IGitClient>();

var branchStatus = new Dictionary<string, GitBranchStatus>
{
{ sourceBranch, new GitBranchStatus(sourceBranch, $"origin/{sourceBranch}", true, false, 0, 0, new Commit(Some.Sha(), Some.Name())) },
{ branchWithRemoteChanges, new GitBranchStatus(branchWithRemoteChanges, $"origin/{branchWithRemoteChanges}", true, false, 0, 3, new Commit(Some.Sha(), Some.Name())) },
{ branchWithoutRemoteChanges, new GitBranchStatus(branchWithoutRemoteChanges, $"origin/{branchWithoutRemoteChanges}", true, false, 0, 0, new Commit(Some.Sha(), Some.Name())) }
};

gitClient.GetBranchStatuses(Arg.Any<string[]>()).Returns(branchStatus);

var stack = new TestStackBuilder()
.WithSourceBranch(sourceBranch)
.WithBranch(b => b.WithName(branchWithRemoteChanges))
.WithBranch(b => b.WithName(branchWithoutRemoteChanges))
.Build();

// Act
StackHelpers.PullChanges(stack, gitClient, new TestLogger(testOutputHelper));

// Assert
gitClient.DidNotReceive().PullBranch(sourceBranch);
gitClient.Received().PullBranch(branchWithRemoteChanges);
gitClient.DidNotReceive().PullBranch(branchWithoutRemoteChanges);
}
}
6 changes: 5 additions & 1 deletion src/Stack/Commands/Helpers/StackHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,11 @@ public static void PullChanges(Config.Stack stack, IGitClient gitClient, ILogger
List<string> allBranchesInStacks = [stack.SourceBranch, .. stack.AllBranchNames];
var branchStatus = gitClient.GetBranchStatuses([.. allBranchesInStacks]);

foreach (var branch in allBranchesInStacks.Where(b => branchStatus.ContainsKey(b) && branchStatus[b].RemoteBranchExists))
foreach (var branch in allBranchesInStacks
.Where(b =>
branchStatus.ContainsKey(b) &&
branchStatus[b].RemoteBranchExists &&
branchStatus[b].Behind > 0))
{
logger.Information($"Pulling changes for {branch.Branch()} from remote");
gitClient.ChangeBranch(branch);
Expand Down