From a1aeedd5d559b079ff9c1978d9349738e41596d5 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Thu, 17 Jul 2025 20:52:46 +0200 Subject: [PATCH] Fix showing range diff across a rename when filtering by path We need to pass the union of filter paths at both ends of the range. --- pkg/gui/controllers/helpers/diff_helper.go | 16 ++++++++++++++-- .../show_diffs_for_renamed_file.go | 14 -------------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/pkg/gui/controllers/helpers/diff_helper.go b/pkg/gui/controllers/helpers/diff_helper.go index 0ef23de36..1fd73081a 100644 --- a/pkg/gui/controllers/helpers/diff_helper.go +++ b/pkg/gui/controllers/helpers/diff_helper.go @@ -57,8 +57,20 @@ func (self *DiffHelper) GetUpdateTaskForRenderingCommitsDiff(commit *models.Comm from, to := refRange.From, refRange.To args := []string{from.ParentRefName(), to.RefName(), "--stat", "-p"} args = append(args, "--") - if path := self.c.Modes().Filtering.GetPath(); path != "" { - args = append(args, path) + if filterPath := self.c.Modes().Filtering.GetPath(); filterPath != "" { + // If both refs are commits, filter by the union of their paths. This is useful for + // example when diffing a range of commits in filter-by-path mode across a rename. + fromCommit, ok1 := from.(*models.Commit) + toCommit, ok2 := to.(*models.Commit) + if ok1 && ok2 { + paths := append(self.FilterPathsForCommit(fromCommit), self.FilterPathsForCommit(toCommit)...) + args = append(args, lo.Uniq(paths)...) + } else { + // If either ref is not a commit (which is possible in sticky diff mode, when + // diffing against a branch or tag), we just filter by the filter path; that's the + // best we can do in this case. + args = append(args, filterPath) + } } cmdObj := self.c.Git().Diff.DiffCmdObj(args) prefix := style.FgYellow.Sprintf("%s %s-%s\n\n", self.c.Tr.ShowingDiffForRange, from.ShortRefName(), to.ShortRefName()) diff --git a/pkg/integration/tests/filter_by_path/show_diffs_for_renamed_file.go b/pkg/integration/tests/filter_by_path/show_diffs_for_renamed_file.go index eae09acd2..720a681e1 100644 --- a/pkg/integration/tests/filter_by_path/show_diffs_for_renamed_file.go +++ b/pkg/integration/tests/filter_by_path/show_diffs_for_renamed_file.go @@ -113,7 +113,6 @@ var ShowDiffsForRenamedFile = NewIntegrationTest(NewIntegrationTestArgs{ Press(keys.Universal.RangeSelectUp) t.Views().Main().ContainsLines( - /* EXPECTED: Contains("Showing diff for range"), Equals(""), Equals(" oldFile => newFile | 2 +-"), @@ -131,19 +130,6 @@ var ShowDiffsForRenamedFile = NewIntegrationTest(NewIntegrationTestArgs{ Equals("+y"), Equals(" b"), Equals(" c"), - ACTUAL: */ - Equals(" newFile | 3 +++"), - Equals(" 1 file changed, 3 insertions(+)"), - Equals(""), - Equals("diff --git a/newFile b/newFile"), - Equals("new file mode 100644"), - Contains("index"), - Equals("--- /dev/null"), - Equals("+++ b/newFile"), - Equals("@@ -0,0 +1,3 @@"), - Equals("+y"), - Equals("+b"), - Equals("+c"), ) }, })