From ac7d8ad02574771a4ca4f78c6fe7cb700065d86a Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Mon, 24 Mar 2025 15:11:25 +0100 Subject: [PATCH 1/3] Add test for checking out a file from a commit This works, we just didn't have a test for it. --- .../tests/commit/checkout_file_from_commit.go | 55 +++++++++++++++++++ pkg/integration/tests/test_list.go | 1 + 2 files changed, 56 insertions(+) create mode 100644 pkg/integration/tests/commit/checkout_file_from_commit.go diff --git a/pkg/integration/tests/commit/checkout_file_from_commit.go b/pkg/integration/tests/commit/checkout_file_from_commit.go new file mode 100644 index 000000000..40a8ee146 --- /dev/null +++ b/pkg/integration/tests/commit/checkout_file_from_commit.go @@ -0,0 +1,55 @@ +package commit + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +var CheckoutFileFromCommit = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Checkout a file from a commit", + ExtraCmdArgs: []string{}, + Skip: false, + SetupConfig: func(config *config.AppConfig) {}, + SetupRepo: func(shell *Shell) { + shell.CreateFileAndAdd("file.txt", "one\n") + shell.Commit("one") + shell.CreateFileAndAdd("file.txt", "two\n") + shell.Commit("two") + shell.CreateFileAndAdd("file.txt", "three\n") + shell.Commit("three") + shell.CreateFileAndAdd("file.txt", "four\n") + shell.Commit("four") + }, + Run: func(t *TestDriver, keys config.KeybindingConfig) { + t.Views().Commits(). + Focus(). + Lines( + Contains("four").IsSelected(), + Contains("three"), + Contains("two"), + Contains("one"), + ). + NavigateToLine(Contains("three")). + Tap(func() { + t.Views().Main().ContainsLines( + Contains("-two"), + Contains("+three"), + ) + }). + PressEnter() + + t.Views().CommitFiles(). + IsFocused(). + Lines( + Equals("M file.txt"), + ). + Press(keys.CommitFiles.CheckoutCommitFile) + + t.Views().Files(). + Lines( + Equals("M file.txt"), + ) + + t.FileSystem().FileContent("file.txt", Equals("three\n")) + }, +}) diff --git a/pkg/integration/tests/test_list.go b/pkg/integration/tests/test_list.go index cce631956..c65063901 100644 --- a/pkg/integration/tests/test_list.go +++ b/pkg/integration/tests/test_list.go @@ -89,6 +89,7 @@ var tests = []*components.IntegrationTest{ commit.AmendWhenThereAreConflictsAndContinue, commit.AutoWrapMessage, commit.Checkout, + commit.CheckoutFileFromCommit, commit.Commit, commit.CommitMultiline, commit.CommitSkipHooks, From 61c56c7822cb06b0b845f2d55102369a589fb7b7 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Mon, 24 Mar 2025 15:24:48 +0100 Subject: [PATCH 2/3] Add test for checking out a file from a range selection of commits The test shows a misbehavior: even though the diff shows "-one" and "+three", meaning that "three" is the state we want to check out, we get "one". The reason is that the checkout file command doesn't pay attention to range selections, it only looks at the "moving end" of the range. Had we created the range by selecting "two" and then pressed shift-up to "three", we would have gotten the expected result. --- ...ut_file_from_range_selection_of_commits.go | 59 +++++++++++++++++++ pkg/integration/tests/test_list.go | 1 + 2 files changed, 60 insertions(+) create mode 100644 pkg/integration/tests/commit/checkout_file_from_range_selection_of_commits.go diff --git a/pkg/integration/tests/commit/checkout_file_from_range_selection_of_commits.go b/pkg/integration/tests/commit/checkout_file_from_range_selection_of_commits.go new file mode 100644 index 000000000..899458da1 --- /dev/null +++ b/pkg/integration/tests/commit/checkout_file_from_range_selection_of_commits.go @@ -0,0 +1,59 @@ +package commit + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +var CheckoutFileFromRangeSelectionOfCommits = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Checkout a file from a range selection of commits", + ExtraCmdArgs: []string{}, + Skip: false, + SetupConfig: func(config *config.AppConfig) {}, + SetupRepo: func(shell *Shell) { + shell.CreateFileAndAdd("file.txt", "one\n") + shell.Commit("one") + shell.CreateFileAndAdd("file.txt", "two\n") + shell.Commit("two") + shell.CreateFileAndAdd("file.txt", "three\n") + shell.Commit("three") + shell.CreateFileAndAdd("file.txt", "four\n") + shell.Commit("four") + }, + Run: func(t *TestDriver, keys config.KeybindingConfig) { + t.Views().Commits(). + Focus(). + Lines( + Contains("four").IsSelected(), + Contains("three"), + Contains("two"), + Contains("one"), + ). + NavigateToLine(Contains("three")). + Press(keys.Universal.RangeSelectDown). + Tap(func() { + t.Views().Main().ContainsLines( + Contains("-one"), + Contains("+three"), + ) + }). + PressEnter() + + t.Views().CommitFiles(). + IsFocused(). + Lines( + Equals("M file.txt"), + ). + Press(keys.CommitFiles.CheckoutCommitFile) + + t.Views().Files(). + Lines( + Equals("M file.txt"), + ) + + /* EXPECTED: + t.FileSystem().FileContent("file.txt", Equals("three\n")) + ACTUAL: */ + t.FileSystem().FileContent("file.txt", Equals("two\n")) + }, +}) diff --git a/pkg/integration/tests/test_list.go b/pkg/integration/tests/test_list.go index c65063901..b7e204fd2 100644 --- a/pkg/integration/tests/test_list.go +++ b/pkg/integration/tests/test_list.go @@ -90,6 +90,7 @@ var tests = []*components.IntegrationTest{ commit.AutoWrapMessage, commit.Checkout, commit.CheckoutFileFromCommit, + commit.CheckoutFileFromRangeSelectionOfCommits, commit.Commit, commit.CommitMultiline, commit.CommitSkipHooks, From d2059df543c0378170988f1f2277052d01b0e343 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Mon, 24 Mar 2025 15:29:03 +0100 Subject: [PATCH 3/3] Fix the bug mentioned in the previous commit --- pkg/gui/controllers/commits_files_controller.go | 3 ++- .../commit/checkout_file_from_range_selection_of_commits.go | 3 --- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/pkg/gui/controllers/commits_files_controller.go b/pkg/gui/controllers/commits_files_controller.go index a9db34b7e..68a826eb8 100644 --- a/pkg/gui/controllers/commits_files_controller.go +++ b/pkg/gui/controllers/commits_files_controller.go @@ -292,7 +292,8 @@ func (self *CommitFilesController) openCopyMenu() error { func (self *CommitFilesController) checkout(node *filetree.CommitFileNode) error { self.c.LogAction(self.c.Tr.Actions.CheckoutFile) - if err := self.c.Git().WorkingTree.CheckoutFile(self.context().GetRef().RefName(), node.GetPath()); err != nil { + _, to := self.context().GetFromAndToForDiff() + if err := self.c.Git().WorkingTree.CheckoutFile(to, node.GetPath()); err != nil { return err } diff --git a/pkg/integration/tests/commit/checkout_file_from_range_selection_of_commits.go b/pkg/integration/tests/commit/checkout_file_from_range_selection_of_commits.go index 899458da1..28b8f966c 100644 --- a/pkg/integration/tests/commit/checkout_file_from_range_selection_of_commits.go +++ b/pkg/integration/tests/commit/checkout_file_from_range_selection_of_commits.go @@ -51,9 +51,6 @@ var CheckoutFileFromRangeSelectionOfCommits = NewIntegrationTest(NewIntegrationT Equals("M file.txt"), ) - /* EXPECTED: t.FileSystem().FileContent("file.txt", Equals("three\n")) - ACTUAL: */ - t.FileSystem().FileContent("file.txt", Equals("two\n")) }, })