From 9488dc2c9880d7f2eab20d4d5fb7bb1c7214f6bf Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sun, 5 Oct 2025 14:52:28 +0200 Subject: [PATCH] Add test demonstrating the problem When dropping changes to the submodule, we expect it to get rolled back to the previous version; however, it is removed entirely instead. --- pkg/integration/components/shell.go | 14 +++++ .../tests/commit/discard_submodule_changes.go | 57 +++++++++++++++++++ pkg/integration/tests/test_list.go | 1 + 3 files changed, 72 insertions(+) create mode 100644 pkg/integration/tests/commit/discard_submodule_changes.go diff --git a/pkg/integration/components/shell.go b/pkg/integration/components/shell.go index faa6fff13..b89e08d2b 100644 --- a/pkg/integration/components/shell.go +++ b/pkg/integration/components/shell.go @@ -170,6 +170,10 @@ func (self *Shell) Commit(message string) *Shell { return self.RunCommand([]string{"git", "commit", "-m", message}) } +func (self *Shell) CommitInWorktreeOrSubmodule(worktreePath string, message string) *Shell { + return self.RunCommand([]string{"git", "-C", worktreePath, "commit", "-m", message}) +} + func (self *Shell) EmptyCommit(message string) *Shell { return self.RunCommand([]string{"git", "commit", "--allow-empty", "-m", message}) } @@ -438,6 +442,16 @@ func (self *Shell) AddFileInWorktreeOrSubmodule(worktreePath string, filePath st return self } +func (self *Shell) UpdateFileInWorktreeOrSubmodule(worktreePath string, filePath string, content string) *Shell { + self.UpdateFile(filepath.Join(worktreePath, filePath), content) + + self.RunCommand([]string{ + "git", "-C", worktreePath, "add", filePath, + }) + + return self +} + func (self *Shell) MakeExecutable(path string) *Shell { // 0755 sets the executable permission for owner, and read/execute permissions for group and others err := os.Chmod(filepath.Join(self.dir, path), 0o755) diff --git a/pkg/integration/tests/commit/discard_submodule_changes.go b/pkg/integration/tests/commit/discard_submodule_changes.go new file mode 100644 index 000000000..df54dcd87 --- /dev/null +++ b/pkg/integration/tests/commit/discard_submodule_changes.go @@ -0,0 +1,57 @@ +package commit + +import ( + "github.com/jesseduffield/lazygit/pkg/config" + . "github.com/jesseduffield/lazygit/pkg/integration/components" +) + +var DiscardSubmoduleChanges = NewIntegrationTest(NewIntegrationTestArgs{ + Description: "Discarding changes to a submodule from an old commit.", + ExtraCmdArgs: []string{}, + Skip: false, + SetupConfig: func(config *config.AppConfig) {}, + SetupRepo: func(shell *Shell) { + shell.EmptyCommit("Initial commit") + shell.CloneIntoSubmodule("submodule", "submodule") + shell.Commit("Add submodule") + + shell.AddFileInWorktreeOrSubmodule("submodule", "file", "content") + shell.CommitInWorktreeOrSubmodule("submodule", "add file in submodule") + shell.GitAdd("submodule") + shell.Commit("Update submodule") + + shell.UpdateFileInWorktreeOrSubmodule("submodule", "file", "changed content") + shell.CommitInWorktreeOrSubmodule("submodule", "change file in submodule") + shell.GitAdd("submodule") + shell.Commit("Update submodule again") + }, + Run: func(t *TestDriver, keys config.KeybindingConfig) { + t.Views().Commits(). + Focus(). + Lines( + Contains("Update submodule again").IsSelected(), + Contains("Update submodule"), + Contains("Add submodule"), + Contains("Initial commit"), + ). + PressEnter() + + t.Views().CommitFiles(). + IsFocused(). + Lines( + Equals("M submodule").IsSelected(), + ). + Press(keys.Universal.Remove) + + t.ExpectPopup().Confirmation(). + Title(Equals("Discard file changes")). + Content(Contains("Are you sure you want to remove changes to the selected file(s) from this commit?")). + Confirm() + + t.Shell().RunCommand([]string{"git", "submodule", "update"}) + /* EXPECTED: + t.FileSystem().FileContent("submodule/file", Equals("content")) + ACTUAL: */ + t.FileSystem().PathNotPresent("submodule/file") + }, +}) diff --git a/pkg/integration/tests/test_list.go b/pkg/integration/tests/test_list.go index a292227b3..86b9766d3 100644 --- a/pkg/integration/tests/test_list.go +++ b/pkg/integration/tests/test_list.go @@ -119,6 +119,7 @@ var tests = []*components.IntegrationTest{ commit.CreateTag, commit.DisableCopyCommitMessageBody, commit.DiscardOldFileChanges, + commit.DiscardSubmoduleChanges, commit.DoNotShowBranchMarkerForHeadCommit, commit.FailHooksThenCommitNoHooks, commit.FindBaseCommitForFixup,