Skip to content

Commit

Permalink
Fix multi selection stage/discard not working for files with substrin…
Browse files Browse the repository at this point in the history
…gs (#3599)

- **PR Description**

I found an issue with multi selection stage/discard. Here is a minimal
repro:
```bash
git init
touch a
touch aa
lazygit
```

Select both files using shift and hit space. Only `a` is staged.

- **Please check if the PR fulfills these requirements**

* [ ] Cheatsheets are up-to-date (run `go generate ./...`)
* [x] Code has been formatted (see
[here](https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#code-formatting))
* [x] Tests have been added/updated (see
[here](https://github.com/jesseduffield/lazygit/blob/master/pkg/integration/README.md)
for the integration test guide)
* [ ] Text is internationalised (see
[here](https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#internationalisation))
* [ ] Docs have been updated if necessary
* [x] You've read through your own file changes for silly mistakes etc

<!--
Be sure to name your PR with an imperative e.g. 'Add worktrees view'
see https://github.com/jesseduffield/lazygit/releases/tag/v0.40.0 for
examples
-->
  • Loading branch information
jesseduffield authored Jul 6, 2024
2 parents 94cf2cc + 38aa5b8 commit 34214af
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/gui/controllers/files_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1009,10 +1009,14 @@ func normalisedSelectedNodes(selectedNodes []*filetree.FileNode) []*filetree.Fil

func isDescendentOfSelectedNodes(node *filetree.FileNode, selectedNodes []*filetree.FileNode) bool {
for _, selectedNode := range selectedNodes {
if selectedNode.IsFile() {
continue
}

selectedNodePath := selectedNode.GetPath()
nodePath := node.GetPath()

if strings.HasPrefix(nodePath, selectedNodePath) && nodePath != selectedNodePath {
if strings.HasPrefix(nodePath, selectedNodePath+"/") {
return true
}
}
Expand Down
45 changes: 45 additions & 0 deletions pkg/integration/tests/file/stage_children_range_select.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package file

import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)

var StageChildrenRangeSelect = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Stage a range of files/folders and their children using range select",
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(config *config.AppConfig) {
},
SetupRepo: func(shell *Shell) {
shell.CreateFile("foo", "")
shell.CreateFile("foobar", "")
shell.CreateFile("baz/file", "")
shell.CreateFile("bazbam/file", "")
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
t.Views().Files().
IsFocused().
Lines(
Contains("▼ baz").IsSelected(),
Contains(" ??").Contains("file"),
Contains("▼ bazbam"),
Contains(" ??").Contains("file"),
Contains("??").Contains("foo"),
Contains("??").Contains("foobar"),
).
// Select everything
Press(keys.Universal.ToggleRangeSelect).
NavigateToLine(Contains("foobar")).
// Stage
PressPrimaryAction().
Lines(
Contains("▼ baz").IsSelected(),
Contains(" A ").Contains("file").IsSelected(),
Contains("▼ bazbam").IsSelected(),
Contains(" A ").Contains("file").IsSelected(),
Contains("A ").Contains("foo").IsSelected(),
Contains("A ").Contains("foobar").IsSelected(),
)
},
})
1 change: 1 addition & 0 deletions pkg/integration/tests/test_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ var tests = []*components.IntegrationTest{
file.DiscardVariousChangesRangeSelect,
file.Gitignore,
file.RememberCommitMessageAfterFail,
file.StageChildrenRangeSelect,
file.StageRangeSelect,
filter_and_search.FilterCommitFiles,
filter_and_search.FilterFiles,
Expand Down

0 comments on commit 34214af

Please sign in to comment.