mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-06-15 00:15:32 +02:00
Insert fake todo entry for a conflicting commit that is being applied
When stopping in a rebase because of a conflict, it is nice to see the commit that git is trying to apply. Create a fake todo entry labelled "conflict" for this, and show the "<-- YOU ARE HERE ---" string for that one (in red) instead of for the real current head.
This commit is contained in:
@ -310,6 +310,17 @@ func (self *CommitLoader) getInteractiveRebasingCommits() ([]*models.Commit, err
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// See if the current commit couldn't be applied because it conflicted; if
|
||||
// so, add a fake entry for it
|
||||
if conflictedCommitSha := self.getConflictedCommit(todos); conflictedCommitSha != "" {
|
||||
commits = append(commits, &models.Commit{
|
||||
Sha: conflictedCommitSha,
|
||||
Name: "",
|
||||
Status: models.StatusRebasing,
|
||||
Action: models.ActionConflict,
|
||||
})
|
||||
}
|
||||
|
||||
for _, t := range todos {
|
||||
if t.Command == todo.UpdateRef {
|
||||
t.Msg = strings.TrimPrefix(t.Ref, "refs/heads/")
|
||||
@ -328,6 +339,93 @@ func (self *CommitLoader) getInteractiveRebasingCommits() ([]*models.Commit, err
|
||||
return commits, nil
|
||||
}
|
||||
|
||||
func (self *CommitLoader) getConflictedCommit(todos []todo.Todo) string {
|
||||
bytesContent, err := self.readFile(filepath.Join(self.dotGitDir, "rebase-merge/done"))
|
||||
if err != nil {
|
||||
self.Log.Error(fmt.Sprintf("error occurred reading rebase-merge/done: %s", err.Error()))
|
||||
return ""
|
||||
}
|
||||
|
||||
doneTodos, err := todo.Parse(bytes.NewBuffer(bytesContent))
|
||||
if err != nil {
|
||||
self.Log.Error(fmt.Sprintf("error occurred while parsing rebase-merge/done file: %s", err.Error()))
|
||||
return ""
|
||||
}
|
||||
|
||||
amendFileExists := false
|
||||
if _, err := os.Stat(filepath.Join(self.dotGitDir, "rebase-merge/amend")); err == nil {
|
||||
amendFileExists = true
|
||||
}
|
||||
|
||||
return self.getConflictedCommitImpl(todos, doneTodos, amendFileExists)
|
||||
}
|
||||
|
||||
func (self *CommitLoader) getConflictedCommitImpl(todos []todo.Todo, doneTodos []todo.Todo, amendFileExists bool) string {
|
||||
// Should never be possible, but just to be safe:
|
||||
if len(doneTodos) == 0 {
|
||||
self.Log.Error("no done entries in rebase-merge/done file")
|
||||
return ""
|
||||
}
|
||||
lastTodo := doneTodos[len(doneTodos)-1]
|
||||
if lastTodo.Command == todo.Break || lastTodo.Command == todo.Exec || lastTodo.Command == todo.Reword {
|
||||
return ""
|
||||
}
|
||||
|
||||
// In certain cases, git reschedules commands that failed. One example is if
|
||||
// a patch would overwrite an untracked file (another one is an "exec" that
|
||||
// failed, but we don't care about that here because we dealt with exec
|
||||
// already above). To detect this, compare the last command of the "done"
|
||||
// file against the first command of "git-rebase-todo"; if they are the
|
||||
// same, the command was rescheduled.
|
||||
if len(doneTodos) > 0 && len(todos) > 0 && doneTodos[len(doneTodos)-1] == todos[0] {
|
||||
// Command was rescheduled, no need to display it
|
||||
return ""
|
||||
}
|
||||
|
||||
// Older versions of git have a bug whereby, if a command is rescheduled,
|
||||
// the last successful command is appended to the "done" file again. To
|
||||
// detect this, we need to compare the second-to-last done entry against the
|
||||
// first todo entry, and also compare the last done entry against the
|
||||
// last-but-two done entry; this latter check is needed for the following
|
||||
// case:
|
||||
// pick A
|
||||
// exec make test
|
||||
// pick B
|
||||
// exec make test
|
||||
// If pick B fails with conflicts, then the "done" file contains
|
||||
// pick A
|
||||
// exec make test
|
||||
// pick B
|
||||
// and git-rebase-todo contains
|
||||
// exec make test
|
||||
// Without the last condition we would erroneously treat this as the exec
|
||||
// command being rescheduled, so we wouldn't display our fake entry for
|
||||
// "pick B".
|
||||
if len(doneTodos) >= 3 && len(todos) > 0 && doneTodos[len(doneTodos)-2] == todos[0] &&
|
||||
doneTodos[len(doneTodos)-1] == doneTodos[len(doneTodos)-3] {
|
||||
// Command was rescheduled, no need to display it
|
||||
return ""
|
||||
}
|
||||
|
||||
if lastTodo.Command == todo.Edit {
|
||||
if amendFileExists {
|
||||
// Special case for "edit": if the "amend" file exists, the "edit"
|
||||
// command was successful, otherwise it wasn't
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
// I don't think this is ever possible, but again, just to be safe:
|
||||
if lastTodo.Commit == "" {
|
||||
self.Log.Error("last command in rebase-merge/done file doesn't have a commit")
|
||||
return ""
|
||||
}
|
||||
|
||||
// Any other todo that has a commit associated with it must have failed with
|
||||
// a conflict, otherwise we wouldn't have stopped the rebase:
|
||||
return lastTodo.Commit
|
||||
}
|
||||
|
||||
// assuming the file starts like this:
|
||||
// From e93d4193e6dd45ca9cf3a5a273d7ba6cd8b8fb20 Mon Sep 17 00:00:00 2001
|
||||
// From: Lazygit Tester <test@example.com>
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/fsmiamoto/git-todo-parser/todo"
|
||||
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
||||
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
||||
"github.com/jesseduffield/lazygit/pkg/commands/types/enums"
|
||||
@ -319,3 +320,179 @@ func TestGetCommits(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommitLoader_getConflictedCommitImpl(t *testing.T) {
|
||||
scenarios := []struct {
|
||||
testName string
|
||||
todos []todo.Todo
|
||||
doneTodos []todo.Todo
|
||||
amendFileExists bool
|
||||
expectedSha string
|
||||
}{
|
||||
{
|
||||
testName: "no done todos",
|
||||
todos: []todo.Todo{},
|
||||
doneTodos: []todo.Todo{},
|
||||
amendFileExists: false,
|
||||
expectedSha: "",
|
||||
},
|
||||
{
|
||||
testName: "common case (conflict)",
|
||||
todos: []todo.Todo{},
|
||||
doneTodos: []todo.Todo{
|
||||
{
|
||||
Command: todo.Pick,
|
||||
Commit: "deadbeef",
|
||||
},
|
||||
{
|
||||
Command: todo.Pick,
|
||||
Commit: "fa1afe1",
|
||||
},
|
||||
},
|
||||
amendFileExists: false,
|
||||
expectedSha: "fa1afe1",
|
||||
},
|
||||
{
|
||||
testName: "last command was 'break'",
|
||||
todos: []todo.Todo{},
|
||||
doneTodos: []todo.Todo{
|
||||
{Command: todo.Break},
|
||||
},
|
||||
amendFileExists: false,
|
||||
expectedSha: "",
|
||||
},
|
||||
{
|
||||
testName: "last command was 'exec'",
|
||||
todos: []todo.Todo{},
|
||||
doneTodos: []todo.Todo{
|
||||
{
|
||||
Command: todo.Exec,
|
||||
ExecCommand: "make test",
|
||||
},
|
||||
},
|
||||
amendFileExists: false,
|
||||
expectedSha: "",
|
||||
},
|
||||
{
|
||||
testName: "last command was 'reword'",
|
||||
todos: []todo.Todo{},
|
||||
doneTodos: []todo.Todo{
|
||||
{Command: todo.Reword},
|
||||
},
|
||||
amendFileExists: false,
|
||||
expectedSha: "",
|
||||
},
|
||||
{
|
||||
testName: "'pick' was rescheduled",
|
||||
todos: []todo.Todo{
|
||||
{
|
||||
Command: todo.Pick,
|
||||
Commit: "fa1afe1",
|
||||
},
|
||||
},
|
||||
doneTodos: []todo.Todo{
|
||||
{
|
||||
Command: todo.Pick,
|
||||
Commit: "fa1afe1",
|
||||
},
|
||||
},
|
||||
amendFileExists: false,
|
||||
expectedSha: "",
|
||||
},
|
||||
{
|
||||
testName: "'pick' was rescheduled, buggy git version",
|
||||
todos: []todo.Todo{
|
||||
{
|
||||
Command: todo.Pick,
|
||||
Commit: "fa1afe1",
|
||||
},
|
||||
},
|
||||
doneTodos: []todo.Todo{
|
||||
{
|
||||
Command: todo.Pick,
|
||||
Commit: "deadbeaf",
|
||||
},
|
||||
{
|
||||
Command: todo.Pick,
|
||||
Commit: "fa1afe1",
|
||||
},
|
||||
{
|
||||
Command: todo.Pick,
|
||||
Commit: "deadbeaf",
|
||||
},
|
||||
},
|
||||
amendFileExists: false,
|
||||
expectedSha: "",
|
||||
},
|
||||
{
|
||||
testName: "conflicting 'pick' after 'exec'",
|
||||
todos: []todo.Todo{
|
||||
{
|
||||
Command: todo.Exec,
|
||||
ExecCommand: "make test",
|
||||
},
|
||||
},
|
||||
doneTodos: []todo.Todo{
|
||||
{
|
||||
Command: todo.Pick,
|
||||
Commit: "deadbeaf",
|
||||
},
|
||||
{
|
||||
Command: todo.Exec,
|
||||
ExecCommand: "make test",
|
||||
},
|
||||
{
|
||||
Command: todo.Pick,
|
||||
Commit: "fa1afe1",
|
||||
},
|
||||
},
|
||||
amendFileExists: false,
|
||||
expectedSha: "fa1afe1",
|
||||
},
|
||||
{
|
||||
testName: "'edit' with amend file",
|
||||
todos: []todo.Todo{},
|
||||
doneTodos: []todo.Todo{
|
||||
{
|
||||
Command: todo.Edit,
|
||||
Commit: "fa1afe1",
|
||||
},
|
||||
},
|
||||
amendFileExists: true,
|
||||
expectedSha: "",
|
||||
},
|
||||
{
|
||||
testName: "'edit' without amend file",
|
||||
todos: []todo.Todo{},
|
||||
doneTodos: []todo.Todo{
|
||||
{
|
||||
Command: todo.Edit,
|
||||
Commit: "fa1afe1",
|
||||
},
|
||||
},
|
||||
amendFileExists: false,
|
||||
expectedSha: "fa1afe1",
|
||||
},
|
||||
}
|
||||
for _, scenario := range scenarios {
|
||||
t.Run(scenario.testName, func(t *testing.T) {
|
||||
common := utils.NewDummyCommon()
|
||||
|
||||
builder := &CommitLoader{
|
||||
Common: common,
|
||||
cmd: oscommands.NewDummyCmdObjBuilder(oscommands.NewFakeRunner(t)),
|
||||
getRebaseMode: func() (enums.RebaseMode, error) { return enums.REBASE_MODE_INTERACTIVE, nil },
|
||||
dotGitDir: ".git",
|
||||
readFile: func(filename string) ([]byte, error) {
|
||||
return []byte(""), nil
|
||||
},
|
||||
walkFiles: func(root string, fn filepath.WalkFunc) error {
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
sha := builder.getConflictedCommitImpl(scenario.todos, scenario.doneTodos, scenario.amendFileExists)
|
||||
assert.Equal(t, scenario.expectedSha, sha)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user