From dd61c49a1544e44da4602a7643723e4c6c240648 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sun, 19 Feb 2023 15:08:35 +0100 Subject: Better error message for trying to squash or fixup the first commit It's not so much the total number of commits that matters here, it's just whether we are on the first one. (This includes the other condition.) This allows us to get rid of the condition in rebase.go. --- pkg/commands/git_commands/rebase.go | 4 ---- 1 file changed, 4 deletions(-) (limited to 'pkg/commands') diff --git a/pkg/commands/git_commands/rebase.go b/pkg/commands/git_commands/rebase.go index 66dbcf45f..6990d6bd5 100644 --- a/pkg/commands/git_commands/rebase.go +++ b/pkg/commands/git_commands/rebase.go @@ -178,10 +178,6 @@ func (self *RebaseCommands) BuildSingleActionTodo(commits []*models.Commit, acti if action == "squash" || action == "fixup" { baseIndex++ - - if len(commits) <= baseIndex { - return nil, "", errors.New(self.Tr.CannotSquashOntoSecondCommit) - } } todoLines := self.BuildTodoLines(commits[0:baseIndex], func(commit *models.Commit, i int) string { -- cgit v1.2.3 From a349e886ce1c39dd2e279b83eaaefed523df9fa7 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Sun, 19 Feb 2023 15:20:00 +0100 Subject: Allow interactive rebasing all the way down to the first commit Pass --root instead of a sha when we want to rebase down to the initial commit. --- pkg/commands/git_commands/rebase.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'pkg/commands') diff --git a/pkg/commands/git_commands/rebase.go b/pkg/commands/git_commands/rebase.go index 6990d6bd5..0c79e92b7 100644 --- a/pkg/commands/git_commands/rebase.go +++ b/pkg/commands/git_commands/rebase.go @@ -130,7 +130,7 @@ func (self *RebaseCommands) InteractiveRebaseBreakAfter(commits []*models.Commit // 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 -func (self *RebaseCommands) PrepareInteractiveRebaseCommand(baseSha string, todoLines []TodoLine, overrideEditor bool) oscommands.ICmdObj { +func (self *RebaseCommands) PrepareInteractiveRebaseCommand(baseShaOrRoot string, todoLines []TodoLine, overrideEditor bool) oscommands.ICmdObj { todo := self.buildTodo(todoLines) ex := oscommands.GetLazygitPath() @@ -139,7 +139,7 @@ func (self *RebaseCommands) PrepareInteractiveRebaseCommand(baseSha string, todo debug = "TRUE" } - cmdStr := fmt.Sprintf("git rebase --interactive --autostash --keep-empty --no-autosquash %s", baseSha) + cmdStr := fmt.Sprintf("git rebase --interactive --autostash --keep-empty --no-autosquash %s", baseShaOrRoot) self.Log.WithField("command", cmdStr).Debug("RunCommand") cmdObj := self.cmd.New(cmdStr) @@ -172,10 +172,6 @@ func (self *RebaseCommands) PrepareInteractiveRebaseCommand(baseSha string, todo func (self *RebaseCommands) BuildSingleActionTodo(commits []*models.Commit, actionIndex int, action string) ([]TodoLine, string, error) { baseIndex := actionIndex + 1 - if len(commits) <= baseIndex { - return nil, "", errors.New(self.Tr.CannotRebaseOntoFirstCommit) - } - if action == "squash" || action == "fixup" { baseIndex++ } @@ -193,7 +189,12 @@ func (self *RebaseCommands) BuildSingleActionTodo(commits []*models.Commit, acti } }) - return todoLines, commits[baseIndex].Sha, nil + baseSha := "--root" + if baseIndex < len(commits) { + baseSha = commits[baseIndex].Sha + } + + return todoLines, baseSha, nil } // AmendTo amends the given commit with whatever files are staged -- cgit v1.2.3 From c5cd217a6504259c4040402f6044b1d50ca3a410 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Mon, 20 Feb 2023 08:29:43 +0100 Subject: Allow squashing fixups above the first commit of a repo This includes amending changes into a given commit, since that's implemented in terms of the former. --- pkg/commands/git_commands/rebase.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'pkg/commands') diff --git a/pkg/commands/git_commands/rebase.go b/pkg/commands/git_commands/rebase.go index 0c79e92b7..50ab4de7a 100644 --- a/pkg/commands/git_commands/rebase.go +++ b/pkg/commands/git_commands/rebase.go @@ -198,12 +198,12 @@ func (self *RebaseCommands) BuildSingleActionTodo(commits []*models.Commit, acti } // AmendTo amends the given commit with whatever files are staged -func (self *RebaseCommands) AmendTo(sha string) error { - if err := self.commit.CreateFixupCommit(sha); err != nil { +func (self *RebaseCommands) AmendTo(commit *models.Commit) error { + if err := self.commit.CreateFixupCommit(commit.Sha); err != nil { return err } - return self.SquashAllAboveFixupCommits(sha) + return self.SquashAllAboveFixupCommits(commit) } // EditRebaseTodo sets the action at a given index in the git-rebase-todo file @@ -258,12 +258,17 @@ func (self *RebaseCommands) MoveTodoDown(index int) error { } // SquashAllAboveFixupCommits squashes all fixup! commits above the given one -func (self *RebaseCommands) SquashAllAboveFixupCommits(sha string) error { +func (self *RebaseCommands) SquashAllAboveFixupCommits(commit *models.Commit) error { + shaOrRoot := commit.Sha + "^" + if commit.IsFirstCommit() { + shaOrRoot = "--root" + } + return self.runSkipEditorCommand( self.cmd.New( fmt.Sprintf( - "git rebase --interactive --rebase-merges --autostash --autosquash %s^", - sha, + "git rebase --interactive --rebase-merges --autostash --autosquash %s", + shaOrRoot, ), ), ) -- cgit v1.2.3