jesseduffield.lazygit/pkg/gui/controllers/diff_paths.go
Stefan Haller 40cb4bb24d Extract a single helper for the paths a node's diff is limited to
The files and commit files panels each had their own copy of this, one of
which used to be missing the previous path of a rename. Growing them
apart again is the last thing we want, since the next commit needs to
teach both of them about renames that cross a directory boundary.

The files panel version only returned paths for the filtered case, and
left it to WorktreeFileDiffCmdObj to derive the rest from the node; now
that all callers pass the paths in, that command doesn't need to know
about renames at all.
2026-08-17 09:32:30 +02:00

36 lines
1 KiB
Go

package controllers
import (
"github.com/jesseduffield/lazygit/pkg/gui/filetree"
)
// Both models.File and models.CommitFile satisfy this. Names returns the file's
// path, plus the path it was renamed from if it is a rename.
type fileWithNames[T any] interface {
*T
Names() []string
}
// pathsForDiff returns the paths to limit a diff command to for showing the
// changes of the given node. For a directory this is the directory itself,
// unless a filter is active, in which case we list the files that are visible
// under it.
func pathsForDiff[T any, PT fileWithNames[T]](node *filetree.Node[T], isFiltering bool) []string {
if file := node.GetFile(); file != nil {
return PT(file).Names()
}
if isFiltering {
var paths []string
_ = node.ForEachFile(func(file *T) error {
// For a rename we need to pass both paths so that git detects it as
// a rename rather than an unrelated delete and add.
paths = append(paths, PT(file).Names()...)
return nil
})
return paths
}
return []string{node.GetPath()}
}