2018-12-08 16:54:54 +11:00
package gui
import (
"fmt"
"strings"
2020-11-16 20:38:26 +11:00
"github.com/jesseduffield/lazygit/pkg/commands"
2018-12-08 16:54:54 +11:00
)
2020-08-15 17:23:16 +10:00
func ( gui * Gui ) handleCreateRebaseOptionsMenu ( ) error {
2020-02-14 23:29:41 +11:00
options := [ ] string { "continue" , "abort" }
2018-12-08 16:54:54 +11:00
2020-11-16 20:38:26 +11:00
if gui . GitCommand . WorkingTreeState ( ) == commands . REBASE_MODE_REBASING {
2020-02-14 23:29:41 +11:00
options = append ( options , "skip" )
2018-12-08 16:54:54 +11:00
}
2020-02-14 23:29:41 +11:00
menuItems := make ( [ ] * menuItem , len ( options ) )
for i , option := range options {
2020-02-25 22:08:03 +11:00
// note to self. Never, EVER, close over loop variables in a function
2020-03-09 11:34:10 +11:00
option := option
2020-02-14 23:29:41 +11:00
menuItems [ i ] = & menuItem {
2020-03-09 11:34:10 +11:00
displayString : option ,
2020-02-14 23:29:41 +11:00
onPress : func ( ) error {
2020-03-09 11:34:10 +11:00
return gui . genericMergeCommand ( option )
2020-02-14 23:29:41 +11:00
} ,
2019-11-10 16:20:35 +11:00
}
2018-12-08 16:54:54 +11:00
}
var title string
2020-11-16 20:38:26 +11:00
if gui . GitCommand . WorkingTreeState ( ) == commands . REBASE_MODE_MERGING {
2020-10-04 11:00:48 +11:00
title = gui . Tr . MergeOptionsTitle
2018-12-08 16:54:54 +11:00
} else {
2020-10-04 11:00:48 +11:00
title = gui . Tr . RebaseOptionsTitle
2018-12-08 16:54:54 +11:00
}
2020-02-14 23:39:02 +11:00
return gui . createMenu ( title , menuItems , createMenuOptions { showCancel : true } )
2018-12-08 16:54:54 +11:00
}
2019-02-16 21:01:17 +11:00
func ( gui * Gui ) genericMergeCommand ( command string ) error {
2020-03-28 12:08:13 +11:00
status := gui . GitCommand . WorkingTreeState ( )
2018-12-08 16:54:54 +11:00
2020-11-16 20:38:26 +11:00
if status != commands . REBASE_MODE_MERGING && status != commands . REBASE_MODE_REBASING {
2020-10-04 11:00:48 +11:00
return gui . createErrorPanel ( gui . Tr . NotMergingOrRebasing )
2018-12-08 16:54:54 +11:00
}
2021-04-10 17:31:23 +10:00
gitCommand := gui . GitCommand . WithSpan ( fmt . Sprintf ( "Merge/Rebase: %s" , command ) )
2021-04-10 16:01:46 +10:00
2018-12-08 16:54:54 +11:00
commandType := strings . Replace ( status , "ing" , "e" , 1 )
// we should end up with a command like 'git merge --continue'
2018-12-11 22:16:48 +11:00
// it's impossible for a rebase to require a commit so we'll use a subprocess only if it's a merge
2020-11-16 20:38:26 +11:00
if status == commands . REBASE_MODE_MERGING && command != "abort" && gui . Config . GetUserConfig ( ) . Git . Merging . ManualCommit {
2021-04-10 16:01:46 +10:00
sub := gitCommand . OSCommand . PrepareSubProcess ( "git" , commandType , fmt . Sprintf ( "--%s" , command ) )
2018-12-11 22:16:48 +11:00
if sub != nil {
2021-04-10 11:40:42 +10:00
return gui . runSubprocessWithSuspenseAndRefresh ( sub )
2018-12-11 22:16:48 +11:00
}
return nil
2018-12-08 16:54:54 +11:00
}
2021-04-10 16:01:46 +10:00
result := gitCommand . GenericMergeOrRebaseAction ( commandType , command )
2019-02-16 21:01:17 +11:00
if err := gui . handleGenericMergeCommandResult ( result ) ; err != nil {
return err
}
return nil
}
2018-12-08 16:54:54 +11:00
2019-02-16 21:01:17 +11:00
func ( gui * Gui ) handleGenericMergeCommandResult ( result error ) error {
2020-03-27 19:12:15 +11:00
if err := gui . refreshSidePanels ( refreshOptions { mode : ASYNC } ) ; err != nil {
2019-02-16 21:01:17 +11:00
return err
}
if result == nil {
return nil
} else if strings . Contains ( result . Error ( ) , "No changes - did you forget to use" ) {
return gui . genericMergeCommand ( "skip" )
2019-11-05 13:02:04 +11:00
} else if strings . Contains ( result . Error ( ) , "The previous cherry-pick is now empty" ) {
return gui . genericMergeCommand ( "continue" )
2020-03-28 12:52:45 +11:00
} else if strings . Contains ( result . Error ( ) , "No rebase in progress?" ) {
// assume in this case that we're already done
return nil
2019-03-02 20:56:54 +11:00
} 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" ) {
2020-08-15 16:38:16 +10:00
return gui . ask ( askOpts {
2020-10-04 11:00:48 +11:00
title : gui . Tr . FoundConflictsTitle ,
prompt : gui . Tr . FoundConflicts ,
2020-08-23 19:28:59 +10:00
handlersManageFocus : true ,
2020-08-15 16:36:39 +10:00
handleConfirm : func ( ) error {
2021-04-03 15:56:11 +11:00
return gui . pushContext ( gui . State . Contexts . Files )
2020-08-15 16:36:39 +10:00
} ,
handleClose : func ( ) error {
2020-08-23 19:28:59 +10:00
if err := gui . returnFromContext ( ) ; err != nil {
return err
}
2019-02-16 21:01:17 +11:00
return gui . genericMergeCommand ( "abort" )
} ,
2020-08-15 16:36:39 +10:00
} )
2019-02-16 21:01:17 +11:00
} else {
2020-03-28 11:47:54 +11:00
return gui . createErrorPanel ( result . Error ( ) )
2019-02-16 21:01:17 +11:00
}
2018-12-08 16:54:54 +11:00
}