From f9a3dc7be734b38051568bef8123b9263d2e8555 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Fri, 9 Oct 2026 19:20:50 +0200 Subject: [PATCH] Add a test for editing a file of a branch checked out in another worktree If you enter a branch in the branches panel, and that branch is checked out in another worktree, pressing `e` on a line of a commit's diff opens the file in the current worktree. The same happens for `e` on a file in the commit files panel entered from there. The current worktree may not have the file at all, as in this test, or have it in a different state than the branch has it. The line number is carried forward to the current worktree's version of the file too, and this gives line 0 here. Co-Authored-By: Claude Opus 5.5 (1M context) --- pkg/integration/tests/test_list.go | 1 + .../edit_file_of_branch_in_other_worktree.go | 77 +++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 pkg/integration/tests/worktree/edit_file_of_branch_in_other_worktree.go diff --git a/pkg/integration/tests/test_list.go b/pkg/integration/tests/test_list.go index 496789e0c..d031d7d5b 100644 --- a/pkg/integration/tests/test_list.go +++ b/pkg/integration/tests/test_list.go @@ -694,6 +694,7 @@ var tests = []*components.IntegrationTest{ worktree.DetachWorktreeFromBranch, worktree.DotfileBareRepo, worktree.DoubleNestedLinkedSubmodule, + worktree.EditFileOfBranchInOtherWorktree, worktree.ExcludeFileInWorktree, worktree.FastForwardWorktreeBranch, worktree.FastForwardWorktreeBranchShouldNotPolluteCurrentWorktree, diff --git a/pkg/integration/tests/worktree/edit_file_of_branch_in_other_worktree.go b/pkg/integration/tests/worktree/edit_file_of_branch_in_other_worktree.go new file mode 100644 index 000000000..b1fb31576 --- /dev/null +++ b/pkg/integration/tests/worktree/edit_file_of_branch_in_other_worktree.go @@ -0,0 +1,77 @@ +package worktree + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +var EditFileOfBranchInOtherWorktree = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Edit a file of a commit of a branch that is checked out in another worktree", + ExtraCmdArgs: []string{}, + Skip: false, + SetupConfig: func(config *config.AppConfig) { + config.GetUserConfig().Gui.UseHunkModeInDiffView = false + config.GetUserConfig().OS.Edit = "echo {{filename}} > edit-command" + config.GetUserConfig().OS.EditAtLine = "echo {{filename}}:{{line}} > edit-command" + }, + SetupRepo: func(shell *Shell) { + shell.CreateFileAndAdd("README.md", "hello world") + shell.Commit("initial commit") + shell.NewBranch("mybranch") + shell.CreateFileAndAdd("file.txt", "1\n2\n3\n") + shell.Commit("add file") + shell.Checkout("master") + shell.AddWorktreeCheckout("mybranch", "../linked-worktree") + + // Move the lines of the file down in the linked worktree, so that a line of + // the commit's diff is at a different line of the file there + shell.UpdateFile("../linked-worktree/file.txt", "0\n1\n2\n3\n") + }, + Run: func(t *TestDriver, keys config.KeybindingConfig) { + t.Views().Branches(). + Focus(). + Lines( + Contains("master").IsSelected(), + Contains("mybranch (worktree linked-worktree)"), + ). + NavigateToLine(Contains("mybranch")). + PressEnter() + + t.Views().SubCommits(). + IsFocused(). + Lines( + Contains("add file").IsSelected(), + Contains("initial commit"), + ). + Press(keys.Universal.FocusMainView) + + t.Views().Main(). + IsFocused(). + NavigateToLine(Contains("+2")). + Press(keys.Universal.Edit). + Tap(func() { + /* EXPECTED: + t.FileSystem().FileContent("edit-command", Contains("/linked-worktree/file.txt:3\n")) + ACTUAL: */ + t.FileSystem().FileContent("edit-command", Contains("/repo/file.txt:0\n")) + }). + PressEscape() + + t.Views().SubCommits(). + IsFocused(). + PressEnter() + + t.Views().CommitFiles(). + IsFocused(). + Lines( + Contains("A file.txt").IsSelected(), + ). + Press(keys.Universal.Edit). + Tap(func() { + /* EXPECTED: + t.FileSystem().FileContent("edit-command", Contains("/linked-worktree/file.txt\n")) + ACTUAL: */ + t.FileSystem().FileContent("edit-command", Contains("/repo/file.txt\n")) + }) + }, +})