Skip to content

Commit 7ad96db

Browse files
john-mutumajesseduffield
authored andcommitted
Add worktree option to fast forwarding operation
1 parent c3cf48c commit 7ad96db

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed

pkg/commands/git_commands/git_command_builder.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ func (self *GitCommandBuilder) Worktree(path string) *GitCommandBuilder {
7676
return self
7777
}
7878

79+
func (self *GitCommandBuilder) WorktreePathIf(condition bool, path string) *GitCommandBuilder {
80+
if condition {
81+
return self.Worktree(path)
82+
}
83+
84+
return self
85+
}
86+
7987
// Note, you may prefer to use the Dir method instead of this one
8088
func (self *GitCommandBuilder) GitDir(path string) *GitCommandBuilder {
8189
// git dir arg comes before the command

pkg/commands/git_commands/sync.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ type PullOptions struct {
8888
BranchName string
8989
FastForwardOnly bool
9090
WorktreeGitDir string
91+
WorktreePath string
9192
}
9293

9394
func (self *SyncCommands) Pull(task gocui.Task, opts PullOptions) error {
@@ -97,6 +98,7 @@ func (self *SyncCommands) Pull(task gocui.Task, opts PullOptions) error {
9798
ArgIf(opts.RemoteName != "", opts.RemoteName).
9899
ArgIf(opts.BranchName != "", "refs/heads/"+opts.BranchName).
99100
GitDirIf(opts.WorktreeGitDir != "", opts.WorktreeGitDir).
101+
WorktreePathIf(opts.WorktreePath != "", opts.WorktreePath).
100102
ToArgv()
101103

102104
// setting GIT_SEQUENCE_EDITOR to ':' as a way of skipping it, in case the user

pkg/gui/controllers/branches_controller.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,9 +611,11 @@ func (self *BranchesController) fastForward(branch *models.Branch) error {
611611
self.c.LogAction(action)
612612

613613
worktreeGitDir := ""
614+
worktreePath := ""
614615
// if it is the current worktree path, no need to specify the path
615616
if !worktree.IsCurrent {
616617
worktreeGitDir = worktree.GitDir
618+
worktreePath = worktree.Path
617619
}
618620

619621
err := self.c.Git().Sync.Pull(
@@ -623,6 +625,7 @@ func (self *BranchesController) fastForward(branch *models.Branch) error {
623625
BranchName: branch.UpstreamBranch,
624626
FastForwardOnly: true,
625627
WorktreeGitDir: worktreeGitDir,
628+
WorktreePath: worktreePath,
626629
},
627630
)
628631
_ = self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})

pkg/integration/tests/test_list.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ var tests = []*components.IntegrationTest{
373373
worktree.DoubleNestedLinkedSubmodule,
374374
worktree.ExcludeFileInWorktree,
375375
worktree.FastForwardWorktreeBranch,
376+
worktree.FastForwardWorktreeBranchShouldNotPolluteCurrentWorktree,
376377
worktree.ForceRemoveWorktree,
377378
worktree.RemoveWorktreeFromBranch,
378379
worktree.ResetWindowTabs,
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package worktree
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
var FastForwardWorktreeBranchShouldNotPolluteCurrentWorktree = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Fast-forward a linked worktree branch from another worktree",
10+
ExtraCmdArgs: []string{},
11+
Skip: false,
12+
SetupConfig: func(config *config.AppConfig) {},
13+
SetupRepo: func(shell *Shell) {
14+
// both main and linked worktree will have changed to fast-forward
15+
shell.NewBranch("mybranch")
16+
shell.CreateFileAndAdd("README.md", "hello world")
17+
shell.Commit("initial commit")
18+
shell.EmptyCommit("two")
19+
shell.EmptyCommit("three")
20+
shell.NewBranch("newbranch")
21+
22+
shell.CloneIntoRemote("origin")
23+
shell.SetBranchUpstream("mybranch", "origin/mybranch")
24+
shell.SetBranchUpstream("newbranch", "origin/newbranch")
25+
26+
// remove the 'three' commit so that we have something to pull from the remote
27+
shell.HardReset("HEAD^")
28+
shell.Checkout("mybranch")
29+
shell.HardReset("HEAD^")
30+
31+
shell.AddWorktreeCheckout("newbranch", "../linked-worktree")
32+
},
33+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
34+
t.Views().Branches().
35+
Focus().
36+
Lines(
37+
Contains("mybranch").Contains("↓1").IsSelected(),
38+
Contains("newbranch (worktree)").Contains("↓1"),
39+
).
40+
Press(keys.Branches.FastForward).
41+
Lines(
42+
Contains("mybranch").Contains("✓").IsSelected(),
43+
Contains("newbranch (worktree)").Contains("↓1"),
44+
).
45+
NavigateToLine(Contains("newbranch (worktree)")).
46+
Press(keys.Branches.FastForward).
47+
Lines(
48+
Contains("mybranch").Contains("✓"),
49+
Contains("newbranch (worktree)").Contains("✓").IsSelected(),
50+
).
51+
NavigateToLine(Contains("mybranch"))
52+
53+
// check the current worktree that it has no lines in the File changes pane
54+
t.Views().Files().
55+
Focus().
56+
Press(keys.Files.RefreshFiles).
57+
LineCount(EqualsInt(0))
58+
},
59+
})

0 commit comments

Comments
 (0)