Skip to content

Commit 200e490

Browse files
authored
Fix checking out a file from a range selection of commits (#4423)
- **PR Description** When selecting a range of commits by selecting the top one and then pressing shift-down to create a range, and then pressing enter and checking out one of the files by pressing `c`, you would get the file checked out with its state at the bottom end of the range, which is not what you want; it is expected to check out the file at the state that the diff shows it changes to. (And if the file was only created in the middle of that range, trying to check it out would result in an error.) Fix this by paying attention to a "from-to" range selection, and checking out the file at the "to" state. Related to #4420.
2 parents 5038fa2 + d2059df commit 200e490

File tree

4 files changed

+115
-1
lines changed

4 files changed

+115
-1
lines changed

pkg/gui/controllers/commits_files_controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,8 @@ func (self *CommitFilesController) openCopyMenu() error {
292292

293293
func (self *CommitFilesController) checkout(node *filetree.CommitFileNode) error {
294294
self.c.LogAction(self.c.Tr.Actions.CheckoutFile)
295-
if err := self.c.Git().WorkingTree.CheckoutFile(self.context().GetRef().RefName(), node.GetPath()); err != nil {
295+
_, to := self.context().GetFromAndToForDiff()
296+
if err := self.c.Git().WorkingTree.CheckoutFile(to, node.GetPath()); err != nil {
296297
return err
297298
}
298299

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package commit
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
var CheckoutFileFromCommit = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Checkout a file from a commit",
10+
ExtraCmdArgs: []string{},
11+
Skip: false,
12+
SetupConfig: func(config *config.AppConfig) {},
13+
SetupRepo: func(shell *Shell) {
14+
shell.CreateFileAndAdd("file.txt", "one\n")
15+
shell.Commit("one")
16+
shell.CreateFileAndAdd("file.txt", "two\n")
17+
shell.Commit("two")
18+
shell.CreateFileAndAdd("file.txt", "three\n")
19+
shell.Commit("three")
20+
shell.CreateFileAndAdd("file.txt", "four\n")
21+
shell.Commit("four")
22+
},
23+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
24+
t.Views().Commits().
25+
Focus().
26+
Lines(
27+
Contains("four").IsSelected(),
28+
Contains("three"),
29+
Contains("two"),
30+
Contains("one"),
31+
).
32+
NavigateToLine(Contains("three")).
33+
Tap(func() {
34+
t.Views().Main().ContainsLines(
35+
Contains("-two"),
36+
Contains("+three"),
37+
)
38+
}).
39+
PressEnter()
40+
41+
t.Views().CommitFiles().
42+
IsFocused().
43+
Lines(
44+
Equals("M file.txt"),
45+
).
46+
Press(keys.CommitFiles.CheckoutCommitFile)
47+
48+
t.Views().Files().
49+
Lines(
50+
Equals("M file.txt"),
51+
)
52+
53+
t.FileSystem().FileContent("file.txt", Equals("three\n"))
54+
},
55+
})
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package commit
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
var CheckoutFileFromRangeSelectionOfCommits = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Checkout a file from a range selection of commits",
10+
ExtraCmdArgs: []string{},
11+
Skip: false,
12+
SetupConfig: func(config *config.AppConfig) {},
13+
SetupRepo: func(shell *Shell) {
14+
shell.CreateFileAndAdd("file.txt", "one\n")
15+
shell.Commit("one")
16+
shell.CreateFileAndAdd("file.txt", "two\n")
17+
shell.Commit("two")
18+
shell.CreateFileAndAdd("file.txt", "three\n")
19+
shell.Commit("three")
20+
shell.CreateFileAndAdd("file.txt", "four\n")
21+
shell.Commit("four")
22+
},
23+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
24+
t.Views().Commits().
25+
Focus().
26+
Lines(
27+
Contains("four").IsSelected(),
28+
Contains("three"),
29+
Contains("two"),
30+
Contains("one"),
31+
).
32+
NavigateToLine(Contains("three")).
33+
Press(keys.Universal.RangeSelectDown).
34+
Tap(func() {
35+
t.Views().Main().ContainsLines(
36+
Contains("-one"),
37+
Contains("+three"),
38+
)
39+
}).
40+
PressEnter()
41+
42+
t.Views().CommitFiles().
43+
IsFocused().
44+
Lines(
45+
Equals("M file.txt"),
46+
).
47+
Press(keys.CommitFiles.CheckoutCommitFile)
48+
49+
t.Views().Files().
50+
Lines(
51+
Equals("M file.txt"),
52+
)
53+
54+
t.FileSystem().FileContent("file.txt", Equals("three\n"))
55+
},
56+
})

pkg/integration/tests/test_list.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ var tests = []*components.IntegrationTest{
8989
commit.AmendWhenThereAreConflictsAndContinue,
9090
commit.AutoWrapMessage,
9191
commit.Checkout,
92+
commit.CheckoutFileFromCommit,
93+
commit.CheckoutFileFromRangeSelectionOfCommits,
9294
commit.Commit,
9395
commit.CommitMultiline,
9496
commit.CommitSkipHooks,

0 commit comments

Comments
 (0)