Skip to content

Commit c1e52fc

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 0904bf9 commit c1e52fc

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
@@ -521,10 +521,17 @@ func (self *RebaseCommands) DiscardOldFileChanges(commits []*models.Commit, comm
521521
}
522522

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

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)