From feabc9287df1ca2e4c4da2f4463ddefe58165e5c Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sun, 13 Sep 2026 13:22:03 +0200 Subject: [PATCH] Name a diff line's file in the repo's terms in one place A diff line carries the absolute path of its file, and both panels acting on such a line turn it into the repo-relative one git speaks themselves. Opening a line in a pull request needs that path too, to name the file to GitHub by it. Extract repoRelativePath, and have both panels use it. The files panel gains the check for a path outside the repo that the other one had; a path it used to pass on matches no file of the working tree either. Co-Authored-By: Claude Opus 5 (1M context) --- pkg/gui/controllers/commit_diff_actions.go | 8 +------- pkg/gui/controllers/diff_paths.go | 12 ++++++++++++ pkg/gui/controllers/working_tree_diff_actions.go | 7 +++---- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/pkg/gui/controllers/commit_diff_actions.go b/pkg/gui/controllers/commit_diff_actions.go index bf894fb43..7063deb9b 100644 --- a/pkg/gui/controllers/commit_diff_actions.go +++ b/pkg/gui/controllers/commit_diff_actions.go @@ -2,8 +2,6 @@ package controllers import ( "fmt" - "path/filepath" - "strings" "github.com/jesseduffield/generics/set" "github.com/jesseduffield/lazygit/pkg/commands/git_commands" @@ -443,11 +441,7 @@ func (self *CommitDiffActions) patchEndpoints(target *commitDiffTarget) (string, // patchBuilderPath turns the absolute path a diff line carries into the repo-relative // one the patch builder keys a file by, and "" for a path that is no file of this repo. func (self *CommitDiffActions) patchBuilderPath(path string) string { - relativePath, err := filepath.Rel(self.c.Git().RepoPaths.WorktreePath(), path) - if err != nil || strings.HasPrefix(relativePath, "..") { - return "" - } - return filepath.ToSlash(relativePath) + return repoRelativePath(self.c.Git().RepoPaths.WorktreePath(), path) } // indexOfTargetCommit finds the commit the diff belongs to among the commits of the diff --git a/pkg/gui/controllers/diff_paths.go b/pkg/gui/controllers/diff_paths.go index e9c12606f..91215ba2c 100644 --- a/pkg/gui/controllers/diff_paths.go +++ b/pkg/gui/controllers/diff_paths.go @@ -2,6 +2,7 @@ package controllers import ( "path" + "path/filepath" "strings" "github.com/jesseduffield/lazygit/pkg/gui/filetree" @@ -130,3 +131,14 @@ func isInDir(path string, dir string) bool { // "." is the root item, which contains every file return dir == "." || strings.HasPrefix(path, dir+"/") } + +// repoRelativePath turns the absolute path a diff line carries into the one git speaks +// of the file: relative to the worktree, with forward slashes. It is "" for a path that +// is no file of this repo, which a diff renderer's own naming of a line can produce. +func repoRelativePath(worktreePath string, absolutePath string) string { + relativePath, err := filepath.Rel(worktreePath, absolutePath) + if err != nil || strings.HasPrefix(relativePath, "..") { + return "" + } + return filepath.ToSlash(relativePath) +} diff --git a/pkg/gui/controllers/working_tree_diff_actions.go b/pkg/gui/controllers/working_tree_diff_actions.go index 973ee47b4..b9ff5124a 100644 --- a/pkg/gui/controllers/working_tree_diff_actions.go +++ b/pkg/gui/controllers/working_tree_diff_actions.go @@ -2,7 +2,6 @@ package controllers import ( "fmt" - "path/filepath" "strings" "github.com/jesseduffield/generics/set" @@ -262,11 +261,11 @@ func (self *WorkingTreeDiffActions) applyDiffLineSelection( // fileForDiffLinePath maps the absolute path a diff line carries to the working tree // file it belongs to, or nil for a path that is no file of this repo's working tree. func (self *WorkingTreeDiffActions) fileForDiffLinePath(path string) *models.File { - relativePath, err := filepath.Rel(self.c.Git().RepoPaths.WorktreePath(), path) - if err != nil { + relativePath := repoRelativePath(self.c.Git().RepoPaths.WorktreePath(), path) + if relativePath == "" { return nil } - return self.context().FileTreeViewModel.GetFile(filepath.ToSlash(relativePath)) + return self.context().FileTreeViewModel.GetFile(relativePath) } // applyDiffLines applies the given change lines of one file — a line, a hunk, a range —