diff options
author | Stefan Haller <stefan@haller-berlin.de> | 2024-01-23 17:03:44 +0100 |
---|---|---|
committer | Stefan Haller <stefan@haller-berlin.de> | 2024-01-29 09:37:47 +0100 |
commit | b133318b40f9863e9952dddd8acfeffa6dc95b71 (patch) | |
tree | 012317915bba9ee915508e5727ef25e2fbd62060 /pkg/gui | |
parent | 24e79a057c6ef39d5153a65cacedcdb419d9c7fd (diff) |
Add command to squash all fixups in the current branch
To do that, change the "Apply fixup commits" command to show a menu with the two
choices "in current branch" and "above the selected commit"; we make "in current
branch" the default, as it's the more useful one most of the time, even though
it is a breaking change for those who are used to "shift-S enter" meaning
"squash above selected".
Diffstat (limited to 'pkg/gui')
-rw-r--r-- | pkg/gui/controllers/local_commits_controller.go | 79 |
1 files changed, 62 insertions, 17 deletions
diff --git a/pkg/gui/controllers/local_commits_controller.go b/pkg/gui/controllers/local_commits_controller.go index 3dc1bae7f..3fadbe9e9 100644 --- a/pkg/gui/controllers/local_commits_controller.go +++ b/pkg/gui/controllers/local_commits_controller.go @@ -167,13 +167,13 @@ func (self *LocalCommitsController) GetKeybindings(opts types.KeybindingsOpts) [ }, { Key: opts.GetKey(opts.Config.Commits.SquashAboveCommits), - Handler: self.withItem(self.squashAllAboveFixupCommits), + Handler: self.squashFixupCommits, GetDisabledReason: self.require( self.notMidRebase(self.c.Tr.AlreadyRebasing), - self.singleItemSelected(), ), Description: self.c.Tr.SquashAboveCommits, Tooltip: self.c.Tr.SquashAboveCommitsTooltip, + OpensMenu: true, }, { Key: opts.GetKey(opts.Config.Commits.MoveDownCommit), @@ -816,25 +816,62 @@ func (self *LocalCommitsController) createFixupCommit(commit *models.Commit) err }) } -func (self *LocalCommitsController) squashAllAboveFixupCommits(commit *models.Commit) error { - prompt := utils.ResolvePlaceholderString( - self.c.Tr.SureSquashAboveCommits, - map[string]string{"commit": commit.Sha}, - ) - - return self.c.Confirm(types.ConfirmOpts{ - Title: self.c.Tr.SquashAboveCommits, - Prompt: prompt, - HandleConfirm: func() error { - return self.c.WithWaitingStatus(self.c.Tr.SquashingStatus, func(gocui.Task) error { - self.c.LogAction(self.c.Tr.Actions.SquashAllAboveFixupCommits) - err := self.c.Git().Rebase.SquashAllAboveFixupCommits(commit) - return self.c.Helpers().MergeAndRebase.CheckMergeOrRebase(err) - }) +func (self *LocalCommitsController) squashFixupCommits() error { + return self.c.Menu(types.CreateMenuOptions{ + Title: self.c.Tr.SquashAboveCommits, + Items: []*types.MenuItem{ + { + Label: self.c.Tr.SquashCommitsInCurrentBranch, + OnPress: self.squashAllFixupsInCurrentBranch, + DisabledReason: self.canFindCommitForSquashFixupsInCurrentBranch(), + Key: 'b', + Tooltip: self.c.Tr.SquashCommitsInCurrentBranchTooltip, + }, + { + Label: self.c.Tr.SquashCommitsAboveSelectedCommit, + OnPress: self.withItem(self.squashAllFixupsAboveSelectedCommit), + DisabledReason: self.singleItemSelected()(), + Key: 'a', + Tooltip: self.c.Tr.SquashCommitsAboveSelectedTooltip, + }, }, }) } +func (self *LocalCommitsController) squashAllFixupsAboveSelectedCommit(commit *models.Commit) error { + return self.c.WithWaitingStatus(self.c.Tr.SquashingStatus, func(gocui.Task) error { + self.c.LogAction(self.c.Tr.Actions.SquashAllAboveFixupCommits) + err := self.c.Git().Rebase.SquashAllAboveFixupCommits(commit) + return self.c.Helpers().MergeAndRebase.CheckMergeOrRebase(err) + }) +} + +func (self *LocalCommitsController) squashAllFixupsInCurrentBranch() error { + commit, err := self.findCommitForSquashFixupsInCurrentBranch() + if err != nil { + return self.c.Error(err) + } + + return self.c.WithWaitingStatus(self.c.Tr.SquashingStatus, func(gocui.Task) error { + self.c.LogAction(self.c.Tr.Actions.SquashAllAboveFixupCommits) + err := self.c.Git().Rebase.SquashAllAboveFixupCommits(commit) + return self.c.Helpers().MergeAndRebase.CheckMergeOrRebase(err) + }) +} + +func (self *LocalCommitsController) findCommitForSquashFixupsInCurrentBranch() (*models.Commit, error) { + commits := self.c.Model().Commits + _, index, ok := lo.FindIndexOf(commits, func(c *models.Commit) bool { + return c.IsMerge() || c.Status == models.StatusMerged + }) + + if !ok || index == 0 { + return nil, errors.New(self.c.Tr.CannotSquashCommitsInCurrentBranch) + } + + return commits[index-1], nil +} + func (self *LocalCommitsController) createTag(commit *models.Commit) error { return self.c.Helpers().Tags.OpenCreateTagPrompt(commit.Sha, func() {}) } @@ -1019,6 +1056,14 @@ func (self *LocalCommitsController) canFindCommitForQuickStart() *types.Disabled return nil } +func (self *LocalCommitsController) canFindCommitForSquashFixupsInCurrentBranch() *types.DisabledReason { + if _, err := self.findCommitForSquashFixupsInCurrentBranch(); err != nil { + return &types.DisabledReason{Text: err.Error()} + } + + return nil +} + func (self *LocalCommitsController) canSquashOrFixup(_selectedCommits []*models.Commit, startIdx int, endIdx int) *types.DisabledReason { if endIdx >= len(self.c.Model().Commits)-1 { return &types.DisabledReason{Text: self.c.Tr.CannotSquashOrFixupFirstCommit} |