Skip to content

Commit 33d4b40

Browse files
committed
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.
1 parent 9488dc2 commit 33d4b40

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

pkg/commands/git_commands/rebase.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -519,10 +519,17 @@ func (self *RebaseCommands) DiscardOldFileChanges(commits []*models.Commit, comm
519519
}
520520

521521
for _, filePath := range filePaths {
522-
// check if file exists in previous commit (this command returns an error if the file doesn't exist)
523-
cmdArgs := NewGitCmd("cat-file").Arg("-e", "HEAD^:"+filePath).ToArgv()
524-
525-
if err := self.cmd.New(cmdArgs).Run(); err != nil {
522+
doesFileExistInPreviousCommit := false
523+
if commitIndex < len(commits)-1 {
524+
// check if file exists in previous commit (this command returns an empty string if the file doesn't exist)
525+
cmdArgs := NewGitCmd("ls-tree").Arg("--name-only", "HEAD^", "--", filePath).ToArgv()
526+
output, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
527+
if err != nil {
528+
return err
529+
}
530+
doesFileExistInPreviousCommit = strings.TrimRight(output, "\n") == filePath
531+
}
532+
if !doesFileExistInPreviousCommit {
526533
if err := self.os.Remove(filePath); err != nil {
527534
return err
528535
}

pkg/commands/git_commands/rebase_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func TestRebaseDiscardOldFileChanges(t *testing.T) {
139139
fileName: []string{"test999.txt"},
140140
runner: oscommands.NewFakeRunner(t).
141141
ExpectGitArgs([]string{"rebase", "--interactive", "--autostash", "--keep-empty", "--no-autosquash", "--rebase-merges", "abcdef"}, "", nil).
142-
ExpectGitArgs([]string{"cat-file", "-e", "HEAD^:test999.txt"}, "", nil).
142+
ExpectGitArgs([]string{"ls-tree", "--name-only", "HEAD^", "--", "test999.txt"}, "test999.txt\n", nil).
143143
ExpectGitArgs([]string{"checkout", "HEAD^", "--", "test999.txt"}, "", nil).
144144
ExpectGitArgs([]string{"commit", "--amend", "--no-edit", "--allow-empty", "--allow-empty-message"}, "", nil).
145145
ExpectGitArgs([]string{"rebase", "--continue"}, "", nil),

pkg/integration/tests/commit/discard_submodule_changes.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ var DiscardSubmoduleChanges = NewIntegrationTest(NewIntegrationTestArgs{
4949
Confirm()
5050

5151
t.Shell().RunCommand([]string{"git", "submodule", "update"})
52-
/* EXPECTED:
5352
t.FileSystem().FileContent("submodule/file", Equals("content"))
54-
ACTUAL: */
55-
t.FileSystem().PathNotPresent("submodule/file")
5653
},
5754
})

0 commit comments

Comments
 (0)