diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2024-06-30 11:05:09 +1000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-30 11:05:09 +1000 |
commit | 1285554cb28262a5b4d559ac75ac6742a8d0989b (patch) | |
tree | 097e5a811b6cc536e4695f8d9960d17ee966f571 /pkg/gui/controllers | |
parent | 89900263586c87880af12ffcbb62c976f7592e8e (diff) | |
parent | 232be05785c4f1b12c7d0ddda2c64f12ec525562 (diff) |
Add Squash merge (#3566)
- **PR Description**
Hello,
This PR add merge --squash. A PR already exist
https://github.com/jesseduffield/lazygit/pull/3130, but the author
abandoned it, so I remake it.
I modified to fit most of the comment made except this one
https://github.com/jesseduffield/lazygit/pull/3130/files#r1404808121. I
didn't find an existing example and thus didn't know to modify the code
to fit it to the new way of doing things.
There's still the choice box to commit or not to do as discussed
https://github.com/jesseduffield/lazygit/pull/3130#issuecomment-2112324990.
I'll do it when I have time, I first need to read the code to see how it
really works.
Also only english has been made for now.
![image](https://github.com/jesseduffield/lazygit/assets/94681915/f648ca13-3d16-4703-a074-a83fe9a1eb0f)
- **Please check if the PR fulfills these requirements**
* [x] Cheatsheets are up-to-date (run `go generate ./...`)
* [x] Code has been formatted (see
[here](https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#code-formatting))
* [x] Tests have been added/updated (see
[here](https://github.com/jesseduffield/lazygit/blob/master/pkg/integration/README.md)
for the integration test guide)
* [x] Text is internationalised (see
[here](https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#internationalisation))
* [x] Docs (specifically `docs/Config.md`) have been updated if
necessary
* [x] You've read through your own file changes for silly mistakes etc
<!--
Be sure to name your PR with an imperative e.g. 'Add worktrees view'
see https://github.com/jesseduffield/lazygit/releases/tag/v0.40.0 for
examples
-->
Diffstat (limited to 'pkg/gui/controllers')
-rw-r--r-- | pkg/gui/controllers/branches_controller.go | 14 | ||||
-rw-r--r-- | pkg/gui/controllers/helpers/merge_and_rebase_helper.go | 87 |
2 files changed, 86 insertions, 15 deletions
diff --git a/pkg/gui/controllers/branches_controller.go b/pkg/gui/controllers/branches_controller.go index 8b4a5d395..9f5ba7f05 100644 --- a/pkg/gui/controllers/branches_controller.go +++ b/pkg/gui/controllers/branches_controller.go @@ -111,10 +111,11 @@ func (self *BranchesController) GetKeybindings(opts types.KeybindingsOpts) []*ty { Key: opts.GetKey(opts.Config.Branches.MergeIntoCurrentBranch), Handler: opts.Guards.OutsideFilterMode(self.merge), - GetDisabledReason: self.require(self.singleItemSelected()), + GetDisabledReason: self.require(self.singleItemSelected(self.notMergingIntoYourself)), Description: self.c.Tr.Merge, Tooltip: self.c.Tr.MergeBranchTooltip, DisplayOnScreen: true, + OpensMenu: true, }, { Key: opts.GetKey(opts.Config.Branches.FastForward), @@ -826,3 +827,14 @@ func (self *BranchesController) branchIsReal(branch *models.Branch) *types.Disab return nil } + +func (self *BranchesController) notMergingIntoYourself(branch *models.Branch) *types.DisabledReason { + selectedBranchName := branch.Name + checkedOutBranch := self.c.Helpers().Refs.GetCheckedOutRef().Name + + if checkedOutBranch == selectedBranchName { + return &types.DisabledReason{Text: self.c.Tr.CantMergeBranchIntoItself} + } + + return nil +} diff --git a/pkg/gui/controllers/helpers/merge_and_rebase_helper.go b/pkg/gui/controllers/helpers/merge_and_rebase_helper.go index c5ad78c47..a5554aa58 100644 --- a/pkg/gui/controllers/helpers/merge_and_rebase_helper.go +++ b/pkg/gui/controllers/helpers/merge_and_rebase_helper.go @@ -369,25 +369,84 @@ func (self *MergeAndRebaseHelper) MergeRefIntoCheckedOutBranch(refName string) e if checkedOutBranchName == refName { return errors.New(self.c.Tr.CantMergeBranchIntoItself) } - prompt := utils.ResolvePlaceholderString( - self.c.Tr.ConfirmMerge, - map[string]string{ - "checkedOutBranch": checkedOutBranchName, - "selectedBranch": refName, - }, - ) - return self.c.Confirm(types.ConfirmOpts{ - Title: self.c.Tr.MergeConfirmTitle, - Prompt: prompt, - HandleConfirm: func() error { - self.c.LogAction(self.c.Tr.Actions.Merge) - err := self.c.Git().Branch.Merge(refName, git_commands.MergeOpts{}) - return self.CheckMergeOrRebase(err) + return self.c.Menu(types.CreateMenuOptions{ + Title: self.c.Tr.Merge, + Items: []*types.MenuItem{ + { + Label: self.c.Tr.RegularMerge, + OnPress: self.RegularMerge(refName), + Key: 'm', + Tooltip: utils.ResolvePlaceholderString( + self.c.Tr.RegularMergeTooltip, + map[string]string{ + "checkedOutBranch": checkedOutBranchName, + "selectedBranch": refName, + }, + ), + }, + { + Label: self.c.Tr.SquashMergeUncommittedTitle, + OnPress: self.SquashMergeUncommitted(refName), + Key: 's', + Tooltip: utils.ResolvePlaceholderString( + self.c.Tr.SquashMergeUncommitted, + map[string]string{ + "selectedBranch": refName, + }, + ), + }, + { + Label: self.c.Tr.SquashMergeCommittedTitle, + OnPress: self.SquashMergeCommitted(refName, checkedOutBranchName), + Key: 'S', + Tooltip: utils.ResolvePlaceholderString( + self.c.Tr.SquashMergeCommitted, + map[string]string{ + "checkedOutBranch": checkedOutBranchName, + "selectedBranch": refName, + }, + ), + }, }, }) } +func (self *MergeAndRebaseHelper) RegularMerge(refName string) func() error { + return func() error { + self.c.LogAction(self.c.Tr.Actions.Merge) + err := self.c.Git().Branch.Merge(refName, git_commands.MergeOpts{}) + return self.CheckMergeOrRebase(err) + } +} + +func (self *MergeAndRebaseHelper) SquashMergeUncommitted(refName string) func() error { + return func() error { + self.c.LogAction(self.c.Tr.Actions.SquashMerge) + err := self.c.Git().Branch.Merge(refName, git_commands.MergeOpts{Squash: true}) + return self.CheckMergeOrRebase(err) + } +} + +func (self *MergeAndRebaseHelper) SquashMergeCommitted(refName, checkedOutBranchName string) func() error { + return func() error { + self.c.LogAction(self.c.Tr.Actions.SquashMerge) + err := self.c.Git().Branch.Merge(refName, git_commands.MergeOpts{Squash: true}) + if err = self.CheckMergeOrRebase(err); err != nil { + return err + } + message := utils.ResolvePlaceholderString(self.c.UserConfig.Git.Merging.SquashMergeMessage, map[string]string{ + "selectedRef": refName, + "currentBranch": checkedOutBranchName, + }) + err = self.c.Git().Commit.CommitCmdObj(message, "").Run() + if err != nil { + return err + } + return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC}) + } +} + func (self *MergeAndRebaseHelper) ResetMarkedBaseCommit() error { self.c.Modes().MarkedBaseCommit.Reset() return self.c.PostRefreshUpdate(self.c.Contexts().LocalCommits) |