From 67b8ef449cf13ad7ac6dc52829331ad9803fefff Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sat, 18 Feb 2023 16:12:41 +0100 Subject: [PATCH] Edit by breaking after current commit Instead of rebasing from the commit below the current one and then setting the current one to "edit", we rebase from the current one and insert a "break" after it. In most cases the behavior is exactly the same as before, except that the new method also works if the current commit is a merge commit. This is useful if you want to create a new commit at the very beginning of your branch (by editing the last commit before your branch). --- pkg/commands/git_commands/rebase.go | 16 +++++++++++++++- pkg/gui/controllers/local_commits_controller.go | 3 ++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/pkg/commands/git_commands/rebase.go b/pkg/commands/git_commands/rebase.go index a99686220..66dbcf45f 100644 --- a/pkg/commands/git_commands/rebase.go +++ b/pkg/commands/git_commands/rebase.go @@ -117,6 +117,16 @@ func (self *RebaseCommands) InteractiveRebase(commits []*models.Commit, index in return self.PrepareInteractiveRebaseCommand(sha, todo, true).Run() } +func (self *RebaseCommands) InteractiveRebaseBreakAfter(commits []*models.Commit, index int) error { + todo, sha, err := self.BuildSingleActionTodo(commits, index-1, "pick") + if err != nil { + return err + } + + todo = append(todo, TodoLine{Action: "break", Commit: nil}) + return self.PrepareInteractiveRebaseCommand(sha, todo, true).Run() +} + // PrepareInteractiveRebaseCommand returns the cmd for an interactive rebase // we tell git to run lazygit to edit the todo list, and we pass the client // lazygit a todo string to write to the todo file @@ -400,5 +410,9 @@ type TodoLine struct { } func (self *TodoLine) ToString() string { - return self.Action + " " + self.Commit.Sha + " " + self.Commit.Name + "\n" + if self.Action == "break" { + return self.Action + "\n" + } else { + return self.Action + " " + self.Commit.Sha + " " + self.Commit.Name + "\n" + } } diff --git a/pkg/gui/controllers/local_commits_controller.go b/pkg/gui/controllers/local_commits_controller.go index 3a9aff86b..a9fe7a190 100644 --- a/pkg/gui/controllers/local_commits_controller.go +++ b/pkg/gui/controllers/local_commits_controller.go @@ -298,7 +298,8 @@ func (self *LocalCommitsController) edit(commit *models.Commit) error { return self.c.WithWaitingStatus(self.c.Tr.RebasingStatus, func() error { self.c.LogAction(self.c.Tr.Actions.EditCommit) - return self.interactiveRebase("edit") + err := self.git.Rebase.InteractiveRebaseBreakAfter(self.model.Commits, self.context().GetSelectedLineIdx()) + return self.helpers.MergeAndRebase.CheckMergeOrRebase(err) }) }