1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-10-30 23:57:43 +02:00

Fix dropping submodule changes from a commit

Our logic to decide if a file needs to be checked out from the previous commit
or deleted because it didn't exist in the previous commit didn't work for
submodules. We were using `git cat-file -e` to ask whether the file existed, but
this returns an error for submodules, so we were always deleting those instead
of reverting them back to their previous state.

Switch to using `git ls-tree -- file` instead, which works for both files and
submodules.
This commit is contained in:
Stefan Haller
2025-10-05 10:51:55 +02:00
parent 0904bf9969
commit c1e52fc807
3 changed files with 12 additions and 8 deletions

View File

@@ -521,10 +521,17 @@ func (self *RebaseCommands) DiscardOldFileChanges(commits []*models.Commit, comm
} }
for _, filePath := range filePaths { for _, filePath := range filePaths {
// check if file exists in previous commit (this command returns an error if the file doesn't exist) doesFileExistInPreviousCommit := false
cmdArgs := NewGitCmd("cat-file").Arg("-e", "HEAD^:"+filePath).ToArgv() if commitIndex < len(commits)-1 {
// check if file exists in previous commit (this command returns an empty string if the file doesn't exist)
if err := self.cmd.New(cmdArgs).Run(); err != nil { cmdArgs := NewGitCmd("ls-tree").Arg("--name-only", "HEAD^", "--", filePath).ToArgv()
output, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
if err != nil {
return err
}
doesFileExistInPreviousCommit = strings.TrimRight(output, "\n") == filePath
}
if !doesFileExistInPreviousCommit {
if err := self.os.Remove(filePath); err != nil { if err := self.os.Remove(filePath); err != nil {
return err return err
} }

View File

@@ -139,7 +139,7 @@ func TestRebaseDiscardOldFileChanges(t *testing.T) {
fileName: []string{"test999.txt"}, fileName: []string{"test999.txt"},
runner: oscommands.NewFakeRunner(t). runner: oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"rebase", "--interactive", "--autostash", "--keep-empty", "--no-autosquash", "--rebase-merges", "abcdef"}, "", nil). ExpectGitArgs([]string{"rebase", "--interactive", "--autostash", "--keep-empty", "--no-autosquash", "--rebase-merges", "abcdef"}, "", nil).
ExpectGitArgs([]string{"cat-file", "-e", "HEAD^:test999.txt"}, "", nil). ExpectGitArgs([]string{"ls-tree", "--name-only", "HEAD^", "--", "test999.txt"}, "test999.txt\n", nil).
ExpectGitArgs([]string{"checkout", "HEAD^", "--", "test999.txt"}, "", nil). ExpectGitArgs([]string{"checkout", "HEAD^", "--", "test999.txt"}, "", nil).
ExpectGitArgs([]string{"commit", "--amend", "--no-edit", "--allow-empty", "--allow-empty-message"}, "", nil). ExpectGitArgs([]string{"commit", "--amend", "--no-edit", "--allow-empty", "--allow-empty-message"}, "", nil).
ExpectGitArgs([]string{"rebase", "--continue"}, "", nil), ExpectGitArgs([]string{"rebase", "--continue"}, "", nil),

View File

@@ -49,9 +49,6 @@ var DiscardSubmoduleChanges = NewIntegrationTest(NewIntegrationTestArgs{
Confirm() Confirm()
t.Shell().RunCommand([]string{"git", "submodule", "update"}) t.Shell().RunCommand([]string{"git", "submodule", "update"})
/* EXPECTED:
t.FileSystem().FileContent("submodule/file", Equals("content")) t.FileSystem().FileContent("submodule/file", Equals("content"))
ACTUAL: */
t.FileSystem().PathNotPresent("submodule/file")
}, },
}) })