github.com/andrewrech/lazygit@v0.8.1/pkg/gui/rebase_options_panel.go (about)

     1  package gui
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/jesseduffield/gocui"
     8  )
     9  
    10  type option struct {
    11  	value string
    12  }
    13  
    14  // GetDisplayStrings is a function.
    15  func (r *option) GetDisplayStrings(isFocused bool) []string {
    16  	return []string{r.value}
    17  }
    18  
    19  func (gui *Gui) handleCreateRebaseOptionsMenu(g *gocui.Gui, v *gocui.View) error {
    20  	options := []*option{
    21  		{value: "continue"},
    22  		{value: "abort"},
    23  	}
    24  
    25  	if gui.State.WorkingTreeState == "rebasing" {
    26  		options = append(options, &option{value: "skip"})
    27  	}
    28  
    29  	handleMenuPress := func(index int) error {
    30  		command := options[index].value
    31  		return gui.genericMergeCommand(command)
    32  	}
    33  
    34  	var title string
    35  	if gui.State.WorkingTreeState == "merging" {
    36  		title = gui.Tr.SLocalize("MergeOptionsTitle")
    37  	} else {
    38  		title = gui.Tr.SLocalize("RebaseOptionsTitle")
    39  	}
    40  
    41  	return gui.createMenu(title, options, len(options), handleMenuPress)
    42  }
    43  
    44  func (gui *Gui) genericMergeCommand(command string) error {
    45  	status := gui.State.WorkingTreeState
    46  
    47  	if status != "merging" && status != "rebasing" {
    48  		return gui.createErrorPanel(gui.g, gui.Tr.SLocalize("NotMergingOrRebasing"))
    49  	}
    50  
    51  	commandType := strings.Replace(status, "ing", "e", 1)
    52  	// we should end up with a command like 'git merge --continue'
    53  
    54  	// it's impossible for a rebase to require a commit so we'll use a subprocess only if it's a merge
    55  	if status == "merging" && command != "abort" && gui.Config.GetUserConfig().GetBool("git.merging.manualCommit") {
    56  		sub := gui.OSCommand.PrepareSubProcess("git", commandType, fmt.Sprintf("--%s", command))
    57  		if sub != nil {
    58  			gui.SubProcess = sub
    59  			return gui.Errors.ErrSubProcess
    60  		}
    61  		return nil
    62  	}
    63  	result := gui.GitCommand.GenericMerge(commandType, command)
    64  	if err := gui.handleGenericMergeCommandResult(result); err != nil {
    65  		return err
    66  	}
    67  	return nil
    68  }
    69  
    70  func (gui *Gui) handleGenericMergeCommandResult(result error) error {
    71  	if err := gui.refreshSidePanels(gui.g); err != nil {
    72  		return err
    73  	}
    74  	if result == nil {
    75  		return nil
    76  	} else if result == gui.Errors.ErrSubProcess {
    77  		return result
    78  	} else if strings.Contains(result.Error(), "No changes - did you forget to use") {
    79  		return gui.genericMergeCommand("skip")
    80  	} else if strings.Contains(result.Error(), "When you have resolved this problem") || strings.Contains(result.Error(), "fix conflicts") || strings.Contains(result.Error(), "Resolve all conflicts manually") {
    81  		return gui.createConfirmationPanel(gui.g, gui.getFilesView(), gui.Tr.SLocalize("FoundConflictsTitle"), gui.Tr.SLocalize("FoundConflicts"),
    82  			func(g *gocui.Gui, v *gocui.View) error {
    83  				return nil
    84  			}, func(g *gocui.Gui, v *gocui.View) error {
    85  				return gui.genericMergeCommand("abort")
    86  			},
    87  		)
    88  	} else {
    89  		return gui.createErrorPanel(gui.g, result.Error())
    90  	}
    91  }