Skip to content

Commit 5ba8d42

Browse files
committed
Fix the bug described in the previous commit
What happens here is that when stopping on an "edit" todo entry, we rely on the assumption that if the .git/rebase-merge/amend file exists, the command was successful, and if it doesn't, there was a conflict. The problem is that when you stop on an edit command, and then run a multi-commit cherry-pick or rebase, this will delete the amend file. You may or may not consider this a bug in git; to work around it, we also check the existence of the rebase-merge/message file, which will be deleted as well by the cherry-pick or revert.
1 parent 9b88052 commit 5ba8d42

File tree

4 files changed

+35
-35
lines changed

4 files changed

+35
-35
lines changed

pkg/commands/git_commands/commit_loader.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -388,15 +388,13 @@ func (self *CommitLoader) getConflictedCommit(todos []todo.Todo) *models.Commit
388388
return nil
389389
}
390390

391-
amendFileExists := false
392-
if _, err := os.Stat(filepath.Join(self.repoPaths.WorktreeGitDirPath(), "rebase-merge/amend")); err == nil {
393-
amendFileExists = true
394-
}
391+
amendFileExists, _ := self.os.FileExists(filepath.Join(self.repoPaths.WorktreeGitDirPath(), "rebase-merge/amend"))
392+
messageFileExists, _ := self.os.FileExists(filepath.Join(self.repoPaths.WorktreeGitDirPath(), "rebase-merge/message"))
395393

396-
return self.getConflictedCommitImpl(todos, doneTodos, amendFileExists)
394+
return self.getConflictedCommitImpl(todos, doneTodos, amendFileExists, messageFileExists)
397395
}
398396

399-
func (self *CommitLoader) getConflictedCommitImpl(todos []todo.Todo, doneTodos []todo.Todo, amendFileExists bool) *models.Commit {
397+
func (self *CommitLoader) getConflictedCommitImpl(todos []todo.Todo, doneTodos []todo.Todo, amendFileExists bool, messageFileExists bool) *models.Commit {
400398
// Should never be possible, but just to be safe:
401399
if len(doneTodos) == 0 {
402400
self.Log.Error("no done entries in rebase-merge/done file")
@@ -449,6 +447,14 @@ func (self *CommitLoader) getConflictedCommitImpl(todos []todo.Todo, doneTodos [
449447
// command was successful, otherwise it wasn't
450448
return nil
451449
}
450+
451+
if !messageFileExists {
452+
// As an additional check, see if the "message" file exists; if it
453+
// doesn't, it must be because a multi-commit cherry-pick or revert
454+
// was performed in the meantime, which deleted both the amend file
455+
// and the message file.
456+
return nil
457+
}
452458
}
453459

454460
// I don't think this is ever possible, but again, just to be safe:

pkg/commands/git_commands/commit_loader_test.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -328,11 +328,12 @@ func TestGetCommits(t *testing.T) {
328328

329329
func TestCommitLoader_getConflictedCommitImpl(t *testing.T) {
330330
scenarios := []struct {
331-
testName string
332-
todos []todo.Todo
333-
doneTodos []todo.Todo
334-
amendFileExists bool
335-
expectedResult *models.Commit
331+
testName string
332+
todos []todo.Todo
333+
doneTodos []todo.Todo
334+
amendFileExists bool
335+
messageFileExists bool
336+
expectedResult *models.Commit
336337
}{
337338
{
338339
testName: "no done todos",
@@ -475,21 +476,35 @@ func TestCommitLoader_getConflictedCommitImpl(t *testing.T) {
475476
expectedResult: nil,
476477
},
477478
{
478-
testName: "'edit' without amend file",
479+
testName: "'edit' without amend file but message file",
479480
todos: []todo.Todo{},
480481
doneTodos: []todo.Todo{
481482
{
482483
Command: todo.Edit,
483484
Commit: "fa1afe1",
484485
},
485486
},
486-
amendFileExists: false,
487+
amendFileExists: false,
488+
messageFileExists: true,
487489
expectedResult: &models.Commit{
488490
Hash: "fa1afe1",
489491
Action: todo.Edit,
490492
Status: models.StatusConflicted,
491493
},
492494
},
495+
{
496+
testName: "'edit' without amend and without message file",
497+
todos: []todo.Todo{},
498+
doneTodos: []todo.Todo{
499+
{
500+
Command: todo.Edit,
501+
Commit: "fa1afe1",
502+
},
503+
},
504+
amendFileExists: false,
505+
messageFileExists: false,
506+
expectedResult: nil,
507+
},
493508
}
494509
for _, scenario := range scenarios {
495510
t.Run(scenario.testName, func(t *testing.T) {
@@ -508,7 +523,7 @@ func TestCommitLoader_getConflictedCommitImpl(t *testing.T) {
508523
},
509524
}
510525

511-
hash := builder.getConflictedCommitImpl(scenario.todos, scenario.doneTodos, scenario.amendFileExists)
526+
hash := builder.getConflictedCommitImpl(scenario.todos, scenario.doneTodos, scenario.amendFileExists, scenario.messageFileExists)
512527
assert.Equal(t, scenario.expectedResult, hash)
513528
})
514529
}

pkg/integration/tests/interactive_rebase/revert_during_rebase_when_stopped_on_edit.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,13 @@ var RevertDuringRebaseWhenStoppedOnEdit = NewIntegrationTest(NewIntegrationTestA
4545
).
4646
Press("X").
4747
Lines(
48-
/* EXPECTED:
4948
Contains("pick").Contains("commit 04"),
5049
Contains(`<-- YOU ARE HERE --- Revert "commit 01"`).IsSelected(),
5150
Contains(`Revert "commit 02"`),
5251
Contains("commit 03"),
5352
Contains("commit 02"),
5453
Contains("commit 01"),
5554
Contains("master commit"),
56-
ACTUAL: */
57-
Contains("pick").Contains("commit 04"),
58-
Contains("edit").Contains("<-- CONFLICT --- commit 03").IsSelected(),
59-
Contains(`Revert "commit 01"`),
60-
Contains(`Revert "commit 02"`),
61-
Contains("commit 03"),
62-
Contains("commit 02"),
63-
Contains("commit 01"),
64-
Contains("master commit"),
6555
)
6656
},
6757
})

pkg/integration/tests/interactive_rebase/revert_multiple_commits_in_interactive_rebase.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ var RevertMultipleCommitsInInteractiveRebase = NewIntegrationTest(NewIntegration
8484

8585
t.Views().Commits().
8686
Lines(
87-
/* EXPECTED:
8887
Contains("pick").Contains("CI unrelated change 3"),
8988
Contains("pick").Contains("CI unrelated change 2"),
9089
Contains(`CI ◯ <-- YOU ARE HERE --- Revert "unrelated change 1"`),
@@ -93,16 +92,6 @@ var RevertMultipleCommitsInInteractiveRebase = NewIntegrationTest(NewIntegration
9392
Contains("CI ◯ add first line"),
9493
Contains("CI ◯ unrelated change 1"),
9594
Contains("CI ◯ add empty file"),
96-
ACTUAL: */
97-
Contains("pick").Contains("CI unrelated change 3"),
98-
Contains("pick").Contains("CI unrelated change 2"),
99-
Contains("edit CI <-- CONFLICT --- add second line"),
100-
Contains(`CI ◯ Revert "unrelated change 1"`),
101-
Contains(`CI ◯ Revert "add first line"`),
102-
Contains("CI ◯ add second line"),
103-
Contains("CI ◯ add first line"),
104-
Contains("CI ◯ unrelated change 1"),
105-
Contains("CI ◯ add empty file"),
10695
)
10796

10897
t.Views().Options().Content(Contains("View rebase options: m"))

0 commit comments

Comments
 (0)