mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-11-25 22:32:13 +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:
@@ -521,10 +521,17 @@ func (self *RebaseCommands) DiscardOldFileChanges(commits []*models.Commit, comm
|
||||
}
|
||||
|
||||
for _, filePath := range filePaths {
|
||||
// check if file exists in previous commit (this command returns an error if the file doesn't exist)
|
||||
cmdArgs := NewGitCmd("cat-file").Arg("-e", "HEAD^:"+filePath).ToArgv()
|
||||
|
||||
if err := self.cmd.New(cmdArgs).Run(); err != nil {
|
||||
doesFileExistInPreviousCommit := false
|
||||
if commitIndex < len(commits)-1 {
|
||||
// check if file exists in previous commit (this command returns an empty string if the file doesn't exist)
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user