2018-12-08 07:54:54 +02:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2020-11-16 11:38:26 +02:00
|
|
|
|
2021-12-30 04:35:10 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/types/enums"
|
2018-12-08 07:54:54 +02:00
|
|
|
)
|
|
|
|
|
2021-10-31 23:58:58 +02:00
|
|
|
type RebaseOption string
|
|
|
|
|
|
|
|
const (
|
|
|
|
REBASE_OPTION_CONTINUE = "continue"
|
|
|
|
REBASE_OPTION_ABORT = "abort"
|
|
|
|
REBASE_OPTION_SKIP = "skip"
|
|
|
|
)
|
|
|
|
|
2020-08-15 09:23:16 +02:00
|
|
|
func (gui *Gui) handleCreateRebaseOptionsMenu() error {
|
2021-10-31 23:58:58 +02:00
|
|
|
options := []string{REBASE_OPTION_CONTINUE, REBASE_OPTION_ABORT}
|
2018-12-08 07:54:54 +02:00
|
|
|
|
2021-12-30 04:35:10 +02:00
|
|
|
if gui.GitCommand.WorkingTreeState() == enums.REBASE_MODE_REBASING {
|
2021-10-31 23:58:58 +02:00
|
|
|
options = append(options, REBASE_OPTION_SKIP)
|
2018-12-08 07:54:54 +02:00
|
|
|
}
|
|
|
|
|
2020-02-14 14:29:41 +02:00
|
|
|
menuItems := make([]*menuItem, len(options))
|
|
|
|
for i, option := range options {
|
2020-02-25 13:08:03 +02:00
|
|
|
// note to self. Never, EVER, close over loop variables in a function
|
2020-03-09 02:34:10 +02:00
|
|
|
option := option
|
2020-02-14 14:29:41 +02:00
|
|
|
menuItems[i] = &menuItem{
|
2020-03-09 02:34:10 +02:00
|
|
|
displayString: option,
|
2020-02-14 14:29:41 +02:00
|
|
|
onPress: func() error {
|
2020-03-09 02:34:10 +02:00
|
|
|
return gui.genericMergeCommand(option)
|
2020-02-14 14:29:41 +02:00
|
|
|
},
|
2019-11-10 07:20:35 +02:00
|
|
|
}
|
2018-12-08 07:54:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var title string
|
2021-12-30 04:35:10 +02:00
|
|
|
if gui.GitCommand.WorkingTreeState() == enums.REBASE_MODE_MERGING {
|
2020-10-04 02:00:48 +02:00
|
|
|
title = gui.Tr.MergeOptionsTitle
|
2018-12-08 07:54:54 +02:00
|
|
|
} else {
|
2020-10-04 02:00:48 +02:00
|
|
|
title = gui.Tr.RebaseOptionsTitle
|
2018-12-08 07:54:54 +02:00
|
|
|
}
|
|
|
|
|
2020-02-14 14:39:02 +02:00
|
|
|
return gui.createMenu(title, menuItems, createMenuOptions{showCancel: true})
|
2018-12-08 07:54:54 +02:00
|
|
|
}
|
|
|
|
|
2019-02-16 12:01:17 +02:00
|
|
|
func (gui *Gui) genericMergeCommand(command string) error {
|
2020-03-28 03:08:13 +02:00
|
|
|
status := gui.GitCommand.WorkingTreeState()
|
2018-12-08 07:54:54 +02:00
|
|
|
|
2021-12-30 04:35:10 +02:00
|
|
|
if status != enums.REBASE_MODE_MERGING && status != enums.REBASE_MODE_REBASING {
|
2020-10-04 02:00:48 +02:00
|
|
|
return gui.createErrorPanel(gui.Tr.NotMergingOrRebasing)
|
2018-12-08 07:54:54 +02:00
|
|
|
}
|
|
|
|
|
2022-01-05 03:01:59 +02:00
|
|
|
gui.logAction(fmt.Sprintf("Merge/Rebase: %s", command))
|
2021-04-10 08:01:46 +02:00
|
|
|
|
2021-12-30 03:10:09 +02:00
|
|
|
commandType := ""
|
|
|
|
switch status {
|
2021-12-30 04:35:10 +02:00
|
|
|
case enums.REBASE_MODE_MERGING:
|
2021-12-30 03:10:09 +02:00
|
|
|
commandType = "merge"
|
2021-12-30 04:35:10 +02:00
|
|
|
case enums.REBASE_MODE_REBASING:
|
2021-12-30 03:10:09 +02:00
|
|
|
commandType = "rebase"
|
|
|
|
}
|
|
|
|
|
2018-12-08 07:54:54 +02:00
|
|
|
// we should end up with a command like 'git merge --continue'
|
|
|
|
|
2018-12-11 13:16:48 +02:00
|
|
|
// it's impossible for a rebase to require a commit so we'll use a subprocess only if it's a merge
|
2021-12-30 04:35:10 +02:00
|
|
|
if status == enums.REBASE_MODE_MERGING && command != REBASE_OPTION_ABORT && gui.UserConfig.Git.Merging.ManualCommit {
|
2022-01-05 02:57:32 +02:00
|
|
|
sub := gui.GitCommand.Cmd.New("git " + commandType + " --" + command)
|
2018-12-11 13:16:48 +02:00
|
|
|
if sub != nil {
|
2021-04-10 03:40:42 +02:00
|
|
|
return gui.runSubprocessWithSuspenseAndRefresh(sub)
|
2018-12-11 13:16:48 +02:00
|
|
|
}
|
|
|
|
return nil
|
2018-12-08 07:54:54 +02:00
|
|
|
}
|
2022-01-05 02:57:32 +02:00
|
|
|
result := gui.GitCommand.GenericMergeOrRebaseAction(commandType, command)
|
2019-02-16 12:01:17 +02:00
|
|
|
if err := gui.handleGenericMergeCommandResult(result); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-12-08 07:54:54 +02:00
|
|
|
|
2021-10-20 13:21:16 +02:00
|
|
|
var conflictStrings = []string{
|
|
|
|
"Failed to merge in the changes",
|
|
|
|
"When you have resolved this problem",
|
|
|
|
"fix conflicts",
|
|
|
|
"Resolve all conflicts manually",
|
|
|
|
}
|
|
|
|
|
|
|
|
func isMergeConflictErr(errStr string) bool {
|
|
|
|
for _, str := range conflictStrings {
|
|
|
|
if strings.Contains(errStr, str) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-02-16 12:01:17 +02:00
|
|
|
func (gui *Gui) handleGenericMergeCommandResult(result error) error {
|
2020-03-27 10:12:15 +02:00
|
|
|
if err := gui.refreshSidePanels(refreshOptions{mode: ASYNC}); err != nil {
|
2019-02-16 12:01:17 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if result == nil {
|
|
|
|
return nil
|
|
|
|
} else if strings.Contains(result.Error(), "No changes - did you forget to use") {
|
2021-10-31 23:58:58 +02:00
|
|
|
return gui.genericMergeCommand(REBASE_OPTION_SKIP)
|
2019-11-05 04:02:04 +02:00
|
|
|
} else if strings.Contains(result.Error(), "The previous cherry-pick is now empty") {
|
2021-10-31 23:58:58 +02:00
|
|
|
return gui.genericMergeCommand(REBASE_OPTION_CONTINUE)
|
2020-03-28 03:52:45 +02:00
|
|
|
} else if strings.Contains(result.Error(), "No rebase in progress?") {
|
|
|
|
// assume in this case that we're already done
|
|
|
|
return nil
|
2021-10-20 13:21:16 +02:00
|
|
|
} else if isMergeConflictErr(result.Error()) {
|
2020-08-15 08:38:16 +02:00
|
|
|
return gui.ask(askOpts{
|
2020-10-04 02:00:48 +02:00
|
|
|
title: gui.Tr.FoundConflictsTitle,
|
|
|
|
prompt: gui.Tr.FoundConflicts,
|
2020-08-23 11:28:59 +02:00
|
|
|
handlersManageFocus: true,
|
2020-08-15 08:36:39 +02:00
|
|
|
handleConfirm: func() error {
|
2021-04-03 06:56:11 +02:00
|
|
|
return gui.pushContext(gui.State.Contexts.Files)
|
2020-08-15 08:36:39 +02:00
|
|
|
},
|
|
|
|
handleClose: func() error {
|
2020-08-23 11:28:59 +02:00
|
|
|
if err := gui.returnFromContext(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-31 23:58:58 +02:00
|
|
|
return gui.genericMergeCommand(REBASE_OPTION_ABORT)
|
2019-02-16 12:01:17 +02:00
|
|
|
},
|
2020-08-15 08:36:39 +02:00
|
|
|
})
|
2019-02-16 12:01:17 +02:00
|
|
|
} else {
|
2020-03-28 02:47:54 +02:00
|
|
|
return gui.createErrorPanel(result.Error())
|
2019-02-16 12:01:17 +02:00
|
|
|
}
|
2018-12-08 07:54:54 +02:00
|
|
|
}
|
2021-10-31 23:58:58 +02:00
|
|
|
|
|
|
|
func (gui *Gui) abortMergeOrRebaseWithConfirm() error {
|
|
|
|
// prompt user to confirm that they want to abort, then do it
|
|
|
|
mode := gui.workingTreeStateNoun()
|
|
|
|
return gui.ask(askOpts{
|
|
|
|
title: fmt.Sprintf(gui.Tr.AbortTitle, mode),
|
|
|
|
prompt: fmt.Sprintf(gui.Tr.AbortPrompt, mode),
|
|
|
|
handleConfirm: func() error {
|
|
|
|
return gui.genericMergeCommand(REBASE_OPTION_ABORT)
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) workingTreeStateNoun() string {
|
|
|
|
workingTreeState := gui.GitCommand.WorkingTreeState()
|
|
|
|
switch workingTreeState {
|
2021-12-30 04:35:10 +02:00
|
|
|
case enums.REBASE_MODE_NONE:
|
2021-10-31 23:58:58 +02:00
|
|
|
return ""
|
2021-12-30 04:35:10 +02:00
|
|
|
case enums.REBASE_MODE_MERGING:
|
2021-10-31 23:58:58 +02:00
|
|
|
return "merge"
|
|
|
|
default:
|
|
|
|
return "rebase"
|
|
|
|
}
|
|
|
|
}
|