Skip to content

Commit 1d65196

Browse files
authored
Dont auto-forward branches that are checked out in another worktree (#4833)
### PR Description This is similar to #2957, except that in that case it was about an explicit fast-forward command, and we solved it by doing the fast-forward in the other work-tree (so that it would fail if that worktree had modified files, for example). In this case it is about the relatively new auto-forward feature (added in v0.50, see #4493), and in this case we fix it by not even trying to auto-forward any branches that are checked out by other worktrees.
2 parents 8beec9a + 235ab15 commit 1d65196

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

pkg/gui/controllers/helpers/branches_helper.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,9 @@ func (self *BranchesHelper) AutoForwardBranches() error {
283283
updateCommands := ""
284284
// The first branch is the currently checked out branch; skip it
285285
for _, branch := range branches[1:] {
286-
if branch.RemoteBranchStoredLocally() && (allBranches || lo.Contains(self.c.UserConfig().Git.MainBranches, branch.Name)) {
286+
if branch.RemoteBranchStoredLocally() &&
287+
!self.checkedOutByOtherWorktree(branch) &&
288+
(allBranches || lo.Contains(self.c.UserConfig().Git.MainBranches, branch.Name)) {
287289
isStrictlyBehind := branch.IsBehindForPull() && !branch.IsAheadForPull()
288290
if isStrictlyBehind {
289291
updateCommands += fmt.Sprintf("update %s %s %s\n", branch.FullRefName(), branch.FullUpstreamRefName(), branch.CommitHash)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package sync
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
var FetchAndAutoForwardBranchesAllBranchesCheckedOutInOtherWorktree = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Fetch from remote and auto-forward branches with config set to 'allBranches'; check that this skips branches checked out by another worktree",
10+
ExtraCmdArgs: []string{},
11+
Skip: false,
12+
SetupConfig: func(config *config.AppConfig) {
13+
config.GetUserConfig().Git.AutoForwardBranches = "allBranches"
14+
config.GetUserConfig().Git.LocalBranchSortOrder = "alphabetical"
15+
},
16+
SetupRepo: func(shell *Shell) {
17+
shell.CreateNCommits(3)
18+
shell.NewBranch("feature")
19+
shell.NewBranch("diverged")
20+
shell.CloneIntoRemote("origin")
21+
shell.SetBranchUpstream("master", "origin/master")
22+
shell.SetBranchUpstream("feature", "origin/feature")
23+
shell.SetBranchUpstream("diverged", "origin/diverged")
24+
shell.Checkout("master")
25+
shell.HardReset("HEAD^")
26+
shell.Checkout("feature")
27+
shell.HardReset("HEAD~2")
28+
shell.Checkout("diverged")
29+
shell.HardReset("HEAD~2")
30+
shell.EmptyCommit("local")
31+
shell.NewBranch("checked-out")
32+
33+
shell.AddWorktreeCheckout("master", "../linked-worktree")
34+
},
35+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
36+
t.Views().Branches().
37+
Lines(
38+
Contains("checked-out").IsSelected(),
39+
Contains("diverged ↓2↑1"),
40+
Contains("feature ↓2").DoesNotContain("↑"),
41+
Contains("master (worktree) ↓1").DoesNotContain("↑"),
42+
)
43+
44+
t.Views().Files().
45+
IsFocused().
46+
Press(keys.Files.Fetch)
47+
48+
// AutoForwardBranches is "allBranches": both master and feature get forwarded
49+
t.Views().Branches().
50+
Lines(
51+
Contains("checked-out").IsSelected(),
52+
Contains("diverged ↓2↑1"),
53+
Contains("feature ✓"),
54+
Contains("master (worktree) ↓1"),
55+
)
56+
},
57+
})

pkg/integration/tests/test_list.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ var tests = []*components.IntegrationTest{
383383
submodule.Reset,
384384
submodule.ResetFolder,
385385
sync.FetchAndAutoForwardBranchesAllBranches,
386+
sync.FetchAndAutoForwardBranchesAllBranchesCheckedOutInOtherWorktree,
386387
sync.FetchAndAutoForwardBranchesNone,
387388
sync.FetchAndAutoForwardBranchesOnlyMainBranches,
388389
sync.FetchPrune,

0 commit comments

Comments
 (0)