diff --git a/pkg/gui/controllers/switch_to_diff_files_controller.go b/pkg/gui/controllers/switch_to_diff_files_controller.go index f704f8bd4..86e927558 100644 --- a/pkg/gui/controllers/switch_to_diff_files_controller.go +++ b/pkg/gui/controllers/switch_to_diff_files_controller.go @@ -1,6 +1,8 @@ package controllers import ( + "path/filepath" + "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/gui/types" ) @@ -90,6 +92,15 @@ func (self *SwitchToDiffFilesController) enter() error { Scope: []types.RefreshableView{types.COMMIT_FILES}, }) + if filterPath := self.c.Modes().Filtering.GetPath(); filterPath != "" { + path, err := filepath.Rel(self.c.Git().RepoPaths.RepoPath(), filterPath) + if err != nil { + path = filterPath + } + commitFilesContext.CommitFileTreeViewModel.SelectPath( + filepath.ToSlash(path), self.c.UserConfig().Gui.ShowRootItemInFileTree) + } + self.c.Context().Push(commitFilesContext, types.OnFocusOpts{}) return nil } diff --git a/pkg/gui/filetree/commit_file_tree_view_model.go b/pkg/gui/filetree/commit_file_tree_view_model.go index 82c478a3e..02b0fff9a 100644 --- a/pkg/gui/filetree/commit_file_tree_view_model.go +++ b/pkg/gui/filetree/commit_file_tree_view_model.go @@ -191,3 +191,15 @@ func (self *CommitFileTreeViewModel) ExpandAll() { self.SetSelectedLineIdx(index) } } + +// Try to select the given path if present. If it doesn't exist, or one of the parent directories is +// collapsed, do nothing. +// Note that filepath is an actual file path, not an internal tree path as with e.g. +// ToggleCollapsed. It must be a relative path (relative to the repo root), and it must contain +// forward slashes rather than backslashes even on Windows. +func (self *CommitFileTreeViewModel) SelectPath(filepath string, showRootItem bool) { + index, found := self.GetIndexForPath(InternalTreePathForFilePath(filepath, showRootItem)) + if found { + self.SetSelection(index) + } +} diff --git a/pkg/integration/tests/filter_by_path/select_filtered_file_when_entering_commit.go b/pkg/integration/tests/filter_by_path/select_filtered_file_when_entering_commit.go new file mode 100644 index 000000000..5b73d00a5 --- /dev/null +++ b/pkg/integration/tests/filter_by_path/select_filtered_file_when_entering_commit.go @@ -0,0 +1,47 @@ +package filter_by_path + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +var SelectFilteredFileWhenEnteringCommit = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Filter commits by file path, then enter a commit and ensure the file is selected", + ExtraCmdArgs: []string{}, + Skip: false, + SetupConfig: func(config *config.AppConfig) { + }, + SetupRepo: func(shell *Shell) { + shell.CreateFileAndAdd("file1", "") + shell.CreateFileAndAdd("dir/file2", "") + shell.Commit("add files") + }, + Run: func(t *TestDriver, keys config.KeybindingConfig) { + t.GlobalPress(keys.Universal.FilteringMenu) + t.ExpectPopup().Menu(). + Title(Equals("Filtering")). + Select(Contains("Enter path to filter by")). + Confirm() + + t.ExpectPopup().Prompt(). + Title(Equals("Enter path:")). + Type("dir/file2"). + Confirm() + + t.Views().Commits(). + Focus(). + Lines( + Contains("add files").IsSelected(), + ). + PressEnter() + + t.Views().CommitFiles(). + IsFocused(). + Lines( + Equals("▼ /"), + Equals(" ▼ dir"), + Equals(" A file2").IsSelected(), + Equals(" A file1"), + ) + }, +}) diff --git a/pkg/integration/tests/filter_by_path/select_filtered_file_when_entering_commit_no_root_item.go b/pkg/integration/tests/filter_by_path/select_filtered_file_when_entering_commit_no_root_item.go new file mode 100644 index 000000000..9846d109a --- /dev/null +++ b/pkg/integration/tests/filter_by_path/select_filtered_file_when_entering_commit_no_root_item.go @@ -0,0 +1,47 @@ +package filter_by_path + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +var SelectFilteredFileWhenEnteringCommitNoRootItem = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Filter commits by file path, then enter a commit and ensure the file is selected (with the show root item config off)", + ExtraCmdArgs: []string{}, + Skip: false, + SetupConfig: func(config *config.AppConfig) { + config.GetUserConfig().Gui.ShowRootItemInFileTree = false + }, + SetupRepo: func(shell *Shell) { + shell.CreateFileAndAdd("file1", "") + shell.CreateFileAndAdd("dir/file2", "") + shell.Commit("add files") + }, + Run: func(t *TestDriver, keys config.KeybindingConfig) { + t.GlobalPress(keys.Universal.FilteringMenu) + t.ExpectPopup().Menu(). + Title(Equals("Filtering")). + Select(Contains("Enter path to filter by")). + Confirm() + + t.ExpectPopup().Prompt(). + Title(Equals("Enter path:")). + Type("dir/file2"). + Confirm() + + t.Views().Commits(). + Focus(). + Lines( + Contains("add files").IsSelected(), + ). + PressEnter() + + t.Views().CommitFiles(). + IsFocused(). + Lines( + Equals("▼ dir"), + Equals(" A file2").IsSelected(), + Equals("A file1"), + ) + }, +}) diff --git a/pkg/integration/tests/test_list.go b/pkg/integration/tests/test_list.go index ed0299b18..6c36d9172 100644 --- a/pkg/integration/tests/test_list.go +++ b/pkg/integration/tests/test_list.go @@ -242,6 +242,8 @@ var tests = []*components.IntegrationTest{ filter_by_path.KeepSameCommitSelectedOnExit, filter_by_path.RewordCommitInFilteringMode, filter_by_path.SelectFile, + filter_by_path.SelectFilteredFileWhenEnteringCommit, + filter_by_path.SelectFilteredFileWhenEnteringCommitNoRootItem, filter_by_path.ShowDiffsForRenamedFile, filter_by_path.TypeFile, interactive_rebase.AdvancedInteractiveRebase,