From 6cd6e4532da71c7bb7be2a8f0ec44f0f1883b604 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Fri, 9 Oct 2026 20:00:27 +0200 Subject: [PATCH] Copy the absolute path of a branch's file from the worktree that has it If the commit files panel was entered from a branch that another worktree has checked out, "Copy absolute path" in its copy menu gives the file's path in the current worktree. Make it give the path in the worktree that has the branch checked out. This is the file that `e` and `o` open, and the one users most likely want the path of. Co-Authored-By: Claude Opus 5.5 (1M context) --- pkg/gui/controllers/commits_files_controller.go | 3 +-- pkg/gui/controllers/helpers/diff_helper.go | 6 ++++++ .../tests/worktree/open_file_of_branch_in_other_worktree.go | 3 --- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/pkg/gui/controllers/commits_files_controller.go b/pkg/gui/controllers/commits_files_controller.go index b8995e7bd..e945ef8ca 100644 --- a/pkg/gui/controllers/commits_files_controller.go +++ b/pkg/gui/controllers/commits_files_controller.go @@ -3,7 +3,6 @@ package controllers import ( "errors" "fmt" - "path/filepath" "strings" "github.com/jesseduffield/lazygit/pkg/commands/git_commands" @@ -267,7 +266,7 @@ func (self *CommitFilesController) openCopyMenu() error { copyAbsolutePathItem := &types.MenuItem{ Label: self.c.Tr.CopyAbsoluteFilePath, OnPress: func() error { - absPath, err := filepath.Abs(node.GetPath()) + absPath, err := self.c.Helpers().Diff.AbsolutePathOfFileOfDiff(node.GetPath()) if err != nil { return err } diff --git a/pkg/gui/controllers/helpers/diff_helper.go b/pkg/gui/controllers/helpers/diff_helper.go index f620aed66..1e94c9ac7 100644 --- a/pkg/gui/controllers/helpers/diff_helper.go +++ b/pkg/gui/controllers/helpers/diff_helper.go @@ -252,6 +252,12 @@ func (self *DiffHelper) OpenFileOfDiff(path string) error { return self.filesHelper.OpenFile(self.pathInWorktreeOfDiff(path)) } +// AbsolutePathOfFileOfDiff returns the absolute path of a file of the diff that the +// current side panel shows, in the worktree that EditFilesOfDiff opens it from. +func (self *DiffHelper) AbsolutePathOfFileOfDiff(path string) (string, error) { + return filepath.Abs(self.pathInWorktreeOfDiff(path)) +} + // otherWorktreeOfDiff returns the path of the worktree to open files of the diff in // the current side panel from, if that isn't the current worktree. If the diff is of a // commit of a branch, and another worktree has that branch checked out, that is the diff --git a/pkg/integration/tests/worktree/open_file_of_branch_in_other_worktree.go b/pkg/integration/tests/worktree/open_file_of_branch_in_other_worktree.go index 78229d6e4..e0bf265a2 100644 --- a/pkg/integration/tests/worktree/open_file_of_branch_in_other_worktree.go +++ b/pkg/integration/tests/worktree/open_file_of_branch_in_other_worktree.go @@ -58,10 +58,7 @@ var OpenFileOfBranchInOtherWorktree = NewIntegrationTest(NewIntegrationTestArgs{ t.ExpectToast(Equals("File path copied to clipboard")) - /* EXPECTED: t.FileSystem().FileContent("clipboard", Contains("/linked-worktree/file.txt")) - ACTUAL: */ - t.FileSystem().FileContent("clipboard", Contains("/repo/file.txt")) }) }, })