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) <[email protected]>
This commit is contained in:
Stefan Haller
2026-10-10 13:58:05 +02:00
co-authored by Claude Opus 5.5
parent 690509a8cc
commit 6cd6e4532d
3 changed files with 7 additions and 5 deletions
@@ -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
}
@@ -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
@@ -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"))
})
},
})