diff options
author | Stefan Haller <stefan@haller-berlin.de> | 2023-04-02 19:16:28 +0200 |
---|---|---|
committer | Stefan Haller <stefan@haller-berlin.de> | 2023-04-15 08:36:03 +0200 |
commit | 120dd1530ae329199928c3494ea6063d741fc54d (patch) | |
tree | a983a4f448d80988e10abe67c6e807ba5f053f7a /pkg/commands | |
parent | 860a8d102b7eac47aaefc3ae17b443e93cd10c9f (diff) |
Make EditRebaseTodo more robust
It used to work on the assumption that rebasing commits in lazygit's model
correspond one-to-one to lines in the git-rebase-todo file, which isn't
necessarily true (e.g. when users use "git rebase --edit-todo" at the custom
command prompt and add a "break" between lines).
Diffstat (limited to 'pkg/commands')
-rw-r--r-- | pkg/commands/git_commands/rebase.go | 29 |
1 files changed, 16 insertions, 13 deletions
diff --git a/pkg/commands/git_commands/rebase.go b/pkg/commands/git_commands/rebase.go index 1207bdd56..27b5a6ba4 100644 --- a/pkg/commands/git_commands/rebase.go +++ b/pkg/commands/git_commands/rebase.go @@ -12,6 +12,7 @@ import ( "github.com/jesseduffield/lazygit/pkg/app/daemon" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/commands/oscommands" + "github.com/jesseduffield/lazygit/pkg/utils" ) type RebaseCommands struct { @@ -202,25 +203,27 @@ func (self *RebaseCommands) AmendTo(commit *models.Commit) error { return self.SquashAllAboveFixupCommits(commit) } -// EditRebaseTodo sets the action at a given index in the git-rebase-todo file -func (self *RebaseCommands) EditRebaseTodo(index int, action todo.TodoCommand) error { +// EditRebaseTodo sets the action for a given rebase commit in the git-rebase-todo file +func (self *RebaseCommands) EditRebaseTodo(commit *models.Commit, action todo.TodoCommand) error { fileName := filepath.Join(self.dotGitDir, "rebase-merge/git-rebase-todo") - bytes, err := os.ReadFile(fileName) + todos, err := utils.ReadRebaseTodoFile(fileName) if err != nil { return err } - content := strings.Split(string(bytes), "\n") - commitCount := self.getTodoCommitCount(content) - - // we have the most recent commit at the bottom whereas the todo file has - // it at the bottom, so we need to subtract our index from the commit count - contentIndex := commitCount - 1 - index - splitLine := strings.Split(content[contentIndex], " ") - content[contentIndex] = action.String() + " " + strings.Join(splitLine[1:], " ") - result := strings.Join(content, "\n") + for i := range todos { + t := &todos[i] + // Comparing just the sha is not enough; we need to compare both the + // action and the sha, as the sha could appear multiple times (e.g. in a + // pick and later in a merge) + if t.Command == commit.Action && t.Commit == commit.Sha { + t.Command = action + return utils.WriteRebaseTodoFile(fileName, todos) + } + } - return os.WriteFile(fileName, []byte(result), 0o644) + // Should never get here + return fmt.Errorf("Todo %s not found in git-rebase-todo", commit.Sha) } func (self *RebaseCommands) getTodoCommitCount(content []string) int { |