Skip to content

Commit e24e161

Browse files
committed
When entering a commit in path filtering mode, select the filtered path
1 parent 6b6893b commit e24e161

File tree

5 files changed

+111
-0
lines changed

5 files changed

+111
-0
lines changed

pkg/gui/controllers/switch_to_diff_files_controller.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ func (self *SwitchToDiffFilesController) enter() error {
9090
Scope: []types.RefreshableView{types.COMMIT_FILES},
9191
})
9292

93+
if filterPath := self.c.Modes().Filtering.GetPath(); filterPath != "" {
94+
commitFilesContext.CommitFileTreeViewModel.SelectPath(
95+
filterPath, self.c.UserConfig().Gui.ShowRootItemInFileTree)
96+
}
97+
9398
self.c.Context().Push(commitFilesContext, types.OnFocusOpts{})
9499
return nil
95100
}

pkg/gui/filetree/commit_file_tree_view_model.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,13 @@ func (self *CommitFileTreeViewModel) ExpandAll() {
191191
self.SetSelectedLineIdx(index)
192192
}
193193
}
194+
195+
// Try to select the given path if present. If it doesn't exist, or one of the parent directories is
196+
// collapsed, do nothing.
197+
// Note that filepath is an actual file path, not an internal tree path as with e.g. ToggleCollapsed.
198+
func (self *CommitFileTreeViewModel) SelectPath(filepath string, showRootItem bool) {
199+
index, found := self.GetIndexForPath(InternalTreePathForFilePath(filepath, showRootItem))
200+
if found {
201+
self.SetSelection(index)
202+
}
203+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package filter_by_path
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
var SelectFilteredFileWhenEnteringCommit = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Filter commits by file path, then enter a commit and ensure the file is selected",
10+
ExtraCmdArgs: []string{},
11+
Skip: false,
12+
SetupConfig: func(config *config.AppConfig) {
13+
},
14+
SetupRepo: func(shell *Shell) {
15+
shell.CreateFileAndAdd("file1", "")
16+
shell.CreateFileAndAdd("dir/file2", "")
17+
shell.Commit("add files")
18+
},
19+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
20+
t.GlobalPress(keys.Universal.FilteringMenu)
21+
t.ExpectPopup().Menu().
22+
Title(Equals("Filtering")).
23+
Select(Contains("Enter path to filter by")).
24+
Confirm()
25+
26+
t.ExpectPopup().Prompt().
27+
Title(Equals("Enter path:")).
28+
Type("dir/file2").
29+
Confirm()
30+
31+
t.Views().Commits().
32+
Focus().
33+
Lines(
34+
Contains("add files").IsSelected(),
35+
).
36+
PressEnter()
37+
38+
t.Views().CommitFiles().
39+
IsFocused().
40+
Lines(
41+
Equals("▼ /"),
42+
Equals(" ▼ dir"),
43+
Equals(" A file2").IsSelected(),
44+
Equals(" A file1"),
45+
)
46+
},
47+
})
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package filter_by_path
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
var SelectFilteredFileWhenEnteringCommitNoRootItem = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Filter commits by file path, then enter a commit and ensure the file is selected (with the show root item config off)",
10+
ExtraCmdArgs: []string{},
11+
Skip: false,
12+
SetupConfig: func(config *config.AppConfig) {
13+
config.GetUserConfig().Gui.ShowRootItemInFileTree = false
14+
},
15+
SetupRepo: func(shell *Shell) {
16+
shell.CreateFileAndAdd("file1", "")
17+
shell.CreateFileAndAdd("dir/file2", "")
18+
shell.Commit("add files")
19+
},
20+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
21+
t.GlobalPress(keys.Universal.FilteringMenu)
22+
t.ExpectPopup().Menu().
23+
Title(Equals("Filtering")).
24+
Select(Contains("Enter path to filter by")).
25+
Confirm()
26+
27+
t.ExpectPopup().Prompt().
28+
Title(Equals("Enter path:")).
29+
Type("dir/file2").
30+
Confirm()
31+
32+
t.Views().Commits().
33+
Focus().
34+
Lines(
35+
Contains("add files").IsSelected(),
36+
).
37+
PressEnter()
38+
39+
t.Views().CommitFiles().
40+
IsFocused().
41+
Lines(
42+
Equals("▼ dir"),
43+
Equals(" A file2").IsSelected(),
44+
Equals("A file1"),
45+
)
46+
},
47+
})

pkg/integration/tests/test_list.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ var tests = []*components.IntegrationTest{
239239
filter_by_path.KeepSameCommitSelectedOnExit,
240240
filter_by_path.RewordCommitInFilteringMode,
241241
filter_by_path.SelectFile,
242+
filter_by_path.SelectFilteredFileWhenEnteringCommit,
243+
filter_by_path.SelectFilteredFileWhenEnteringCommitNoRootItem,
242244
filter_by_path.ShowDiffsForRenamedFile,
243245
filter_by_path.TypeFile,
244246
interactive_rebase.AdvancedInteractiveRebase,

0 commit comments

Comments
 (0)