Skip to content

Commit 7fe73c1

Browse files
committed
When entering a commit in path filtering mode, select the filtered path
1 parent 5811f29 commit 7fe73c1

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-0
lines changed

pkg/gui/controllers/switch_to_diff_files_controller.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package controllers
22

33
import (
4+
"path/filepath"
5+
46
"github.com/jesseduffield/lazygit/pkg/commands/models"
57
"github.com/jesseduffield/lazygit/pkg/gui/types"
68
)
@@ -90,6 +92,15 @@ func (self *SwitchToDiffFilesController) enter() error {
9092
Scope: []types.RefreshableView{types.COMMIT_FILES},
9193
})
9294

95+
if filterPath := self.c.Modes().Filtering.GetPath(); filterPath != "" {
96+
path, err := filepath.Rel(self.c.Git().RepoPaths.RepoPath(), filterPath)
97+
if err != nil {
98+
path = filterPath
99+
}
100+
commitFilesContext.CommitFileTreeViewModel.SelectPath(
101+
filepath.ToSlash(path), self.c.UserConfig().Gui.ShowRootItemInFileTree)
102+
}
103+
93104
self.c.Context().Push(commitFilesContext, types.OnFocusOpts{})
94105
return nil
95106
}

pkg/gui/filetree/commit_file_tree_view_model.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,15 @@ 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.
198+
// ToggleCollapsed. It must be a relative path (relative to the repo root), and it must contain
199+
// forward slashes rather than backslashes even on Windows.
200+
func (self *CommitFileTreeViewModel) SelectPath(filepath string, showRootItem bool) {
201+
index, found := self.GetIndexForPath(InternalTreePathForFilePath(filepath, showRootItem))
202+
if found {
203+
self.SetSelection(index)
204+
}
205+
}
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
@@ -242,6 +242,8 @@ var tests = []*components.IntegrationTest{
242242
filter_by_path.KeepSameCommitSelectedOnExit,
243243
filter_by_path.RewordCommitInFilteringMode,
244244
filter_by_path.SelectFile,
245+
filter_by_path.SelectFilteredFileWhenEnteringCommit,
246+
filter_by_path.SelectFilteredFileWhenEnteringCommitNoRootItem,
245247
filter_by_path.ShowDiffsForRenamedFile,
246248
filter_by_path.TypeFile,
247249
interactive_rebase.AdvancedInteractiveRebase,

0 commit comments

Comments
 (0)