Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add squash merge support #3130

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/Config.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ keybinding:
rebaseBranch: 'r'
renameBranch: 'R'
mergeIntoCurrentBranch: 'M'
squashIntoWorkingTree: 'S'
viewGitFlowOptions: 'i'
fastForward: 'f' # fast-forward this branch from its upstream
createTag: 'T'
Expand Down
2 changes: 2 additions & 0 deletions pkg/commands/git_commands/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,15 @@ func (self *BranchCommands) Rename(oldName string, newName string) error {

type MergeOpts struct {
FastForwardOnly bool
Squash bool
}

func (self *BranchCommands) Merge(branchName string, opts MergeOpts) error {
cmdArgs := NewGitCmd("merge").
Arg("--no-edit").
ArgIf(self.UserConfig.Git.Merging.Args != "", self.UserConfig.Git.Merging.Args).
ArgIf(opts.FastForwardOnly, "--ff-only").
ArgIf(opts.Squash, "--squash").
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to check that FastForwardOnly and Squash aren't both true, because that combination doesn't work. (I think it would be appropriate to panic in that case, as that's a programming error; like an assert in other languages.)

Also, it would be good to also add --ff if Squash is true, because it doesn't work otherwise. Yes, that's the default, but people might have merge.ff false in their git config (I do), in which case the command would fail. Now you might argue that this is a bug in git (--squash should imply --ff), but if we can easily work around it, we should.

And finally, we have tests for this function (in branch_test.go), would be good to extend these for the new option.

Arg(branchName).
ToArgv()

Expand Down
2 changes: 2 additions & 0 deletions pkg/config/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ type KeybindingBranchesConfig struct {
RebaseBranch string `yaml:"rebaseBranch"`
RenameBranch string `yaml:"renameBranch"`
MergeIntoCurrentBranch string `yaml:"mergeIntoCurrentBranch"`
SquashIntoWorkingTree string `yaml:"squashIntoWorkingTree"`
ViewGitFlowOptions string `yaml:"viewGitFlowOptions"`
FastForward string `yaml:"fastForward"`
CreateTag string `yaml:"createTag"`
Expand Down Expand Up @@ -761,6 +762,7 @@ func GetDefaultConfig() *UserConfig {
RebaseBranch: "r",
RenameBranch: "R",
MergeIntoCurrentBranch: "M",
SquashIntoWorkingTree: "S",
ViewGitFlowOptions: "i",
FastForward: "f",
CreateTag: "T",
Expand Down
10 changes: 10 additions & 0 deletions pkg/gui/controllers/branches_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ func (self *BranchesController) GetKeybindings(opts types.KeybindingsOpts) []*ty
Handler: opts.Guards.OutsideFilterMode(self.merge),
Description: self.c.Tr.MergeIntoCurrentBranch,
},
{
Key: opts.GetKey(opts.Config.Branches.SquashIntoWorkingTree),
Handler: opts.Guards.OutsideFilterMode(self.squash),
Description: self.c.Tr.SquashIntoWorkingTree,
},
{
Key: opts.GetKey(opts.Config.Branches.FastForward),
Handler: self.checkSelectedAndReal(self.fastForward),
Expand Down Expand Up @@ -548,6 +553,11 @@ func (self *BranchesController) merge() error {
return self.c.Helpers().MergeAndRebase.MergeRefIntoCheckedOutBranch(selectedBranchName)
}

func (self *BranchesController) squash() error {
selectedBranchName := self.context().GetSelected().Name
return self.c.Helpers().MergeAndRebase.SquashRefIntoWorkingTree(selectedBranchName)
}

func (self *BranchesController) rebase() error {
selectedBranchName := self.context().GetSelected().Name
return self.c.Helpers().MergeAndRebase.RebaseOntoRef(selectedBranchName)
Expand Down
24 changes: 24 additions & 0 deletions pkg/gui/controllers/helpers/merge_and_rebase_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,30 @@ func (self *MergeAndRebaseHelper) MergeRefIntoCheckedOutBranch(refName string) e
})
}

func (self *MergeAndRebaseHelper) SquashRefIntoWorkingTree(refName string) error {
checkedOutBranchName := self.refsHelper.GetCheckedOutRef().Name
if checkedOutBranchName == refName {
return self.c.ErrorMsg(self.c.Tr.CantMergeBranchIntoItself)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our new way of dealing with situations like this is to set a DisabledReason on the menu item, so that you can't even invoke it. We just didn't get around yet to converting all existing commands to that new scheme yet. Now I do realize that you just copied this from MergeRefIntoCheckedOutBranch, so you might argue it's out of scope for this PR, but if you feel like doing that extra work, you could add a separate commit (before yours) that converts MergeRefIntoCheckedOutBranch to the new style (it also uses a non-translated text for when you are trying to merge a detached head, which could be fixed at the same time), and use the new pattern for your new command too.

}

prompt := utils.ResolvePlaceholderString(
self.c.Tr.ConfirmSquash,
map[string]string{
"selectedBranch": refName,
},
)

return self.c.Confirm(types.ConfirmOpts{
Title: self.c.Tr.SquashConfirmTitle,
Prompt: prompt,
HandleConfirm: func() error {
self.c.LogAction(self.c.Tr.Actions.SquashBranch)
err := self.c.Git().Branch.Merge(refName, git_commands.MergeOpts{Squash: true})
return self.CheckMergeOrRebase(err)
},
})
}

func (self *MergeAndRebaseHelper) ResetMarkedBaseCommit() error {
self.c.Modes().MarkedBaseCommit.Reset()
return self.c.PostRefreshUpdate(self.c.Contexts().LocalCommits)
Expand Down
9 changes: 9 additions & 0 deletions pkg/gui/controllers/remote_branches_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ func (self *RemoteBranchesController) GetKeybindings(opts types.KeybindingsOpts)
Handler: opts.Guards.OutsideFilterMode(self.checkSelected(self.merge)),
Description: self.c.Tr.MergeIntoCurrentBranch,
},
{
Key: opts.GetKey(opts.Config.Branches.SquashIntoWorkingTree),
Handler: opts.Guards.OutsideFilterMode(self.checkSelected(self.squash)),
Description: self.c.Tr.SquashIntoWorkingTree,
},
{
Key: opts.GetKey(opts.Config.Branches.RebaseBranch),
Handler: opts.Guards.OutsideFilterMode(self.checkSelected(self.rebase)),
Expand Down Expand Up @@ -113,6 +118,10 @@ func (self *RemoteBranchesController) delete(selectedBranch *models.RemoteBranch
return self.c.Helpers().BranchesHelper.ConfirmDeleteRemote(selectedBranch.RemoteName, selectedBranch.Name)
}

func (self *RemoteBranchesController) squash(selectedBranch *models.RemoteBranch) error {
return self.c.Helpers().MergeAndRebase.SquashRefIntoWorkingTree(selectedBranch.FullName())
}

func (self *RemoteBranchesController) merge(selectedBranch *models.RemoteBranch) error {
return self.c.Helpers().MergeAndRebase.MergeRefIntoCheckedOutBranch(selectedBranch.FullName())
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/i18n/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type TranslationSet struct {
StagingTitle string
MergingTitle string
MergeConfirmTitle string
SquashConfirmTitle string
NormalTitle string
LogTitle string
CommitSummary string
Expand Down Expand Up @@ -166,6 +167,7 @@ type TranslationSet struct {
ExcludeFile string
RefreshFiles string
MergeIntoCurrentBranch string
SquashIntoWorkingTree string
ConfirmQuit string
SwitchRepo string
AllBranchesLogGraph string
Expand Down Expand Up @@ -220,6 +222,7 @@ type TranslationSet struct {
InteractiveRebase string
InteractiveRebaseTooltip string
ConfirmMerge string
ConfirmSquash string
FwdNoUpstream string
FwdNoLocalUpstream string
FwdCommitsToPush string
Expand Down Expand Up @@ -668,6 +671,7 @@ type Actions struct {
DeleteLocalBranch string
DeleteBranch string
Merge string
SquashBranch string
RebaseBranch string
RenameBranch string
CreateBranch string
Expand Down Expand Up @@ -819,6 +823,7 @@ func EnglishTranslationSet() TranslationSet {
StagedChanges: "Staged changes",
MainTitle: "Main",
MergeConfirmTitle: "Merge",
SquashConfirmTitle: "Squash",
StagingTitle: "Main panel (staging)",
MergingTitle: "Main panel (merging)",
NormalTitle: "Main panel (normal)",
Expand Down Expand Up @@ -963,6 +968,7 @@ func EnglishTranslationSet() TranslationSet {
ExcludeFile: `Add to .git/info/exclude`,
RefreshFiles: `Refresh files`,
MergeIntoCurrentBranch: `Merge into currently checked out branch`,
SquashIntoWorkingTree: `Squash into working tree`,
ConfirmQuit: `Are you sure you want to quit?`,
SwitchRepo: `Switch to a recent repo`,
AllBranchesLogGraph: `Show all branch logs`,
Expand Down Expand Up @@ -1021,6 +1027,7 @@ func EnglishTranslationSet() TranslationSet {
InteractiveRebase: "Interactive rebase",
InteractiveRebaseTooltip: "Begin an interactive rebase with a break at the start, so you can update the TODO commits before continuing",
ConfirmMerge: "Are you sure you want to merge '{{.selectedBranch}}' into '{{.checkedOutBranch}}'?",
ConfirmSquash: "Are you sure you want to squash '{{.selectedBranch}}' into working tree?",
FwdNoUpstream: "Cannot fast-forward a branch with no upstream",
FwdNoLocalUpstream: "Cannot fast-forward a branch whose remote is not registered locally",
FwdCommitsToPush: "Cannot fast-forward a branch with commits to push",
Expand Down