2020-09-30 00:27:23 +02:00
package gui
import (
"fmt"
"os"
"github.com/fatih/color"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/utils"
)
func ( gui * Gui ) getSelectedSubmodule ( ) * models . SubmoduleConfig {
selectedLine := gui . State . Panels . Submodules . SelectedLineIdx
if selectedLine == - 1 || len ( gui . State . Submodules ) == 0 {
return nil
}
return gui . State . Submodules [ selectedLine ]
}
func ( gui * Gui ) handleSubmoduleSelect ( ) error {
var task updateTask
submodule := gui . getSelectedSubmodule ( )
if submodule == nil {
task = gui . createRenderStringTask ( "No submodules" )
} else {
// TODO: we want to display the path, name, url, and a diff. We really need to be able to pipe commands together. We can always pipe commands together and just not do it asynchronously, but what if it's an expensive diff to obtain? I think that makes the most sense now though.
2020-09-30 01:06:11 +02:00
2020-09-30 00:27:23 +02:00
task = gui . createRenderStringTask (
fmt . Sprintf (
"Name: %s\nPath: %s\nUrl: %s\n" ,
utils . ColoredString ( submodule . Name , color . FgGreen ) ,
utils . ColoredString ( submodule . Path , color . FgYellow ) ,
utils . ColoredString ( submodule . Url , color . FgCyan ) ,
) ,
)
}
return gui . refreshMainViews ( refreshMainOpts {
main : & viewUpdateOpts {
title : "Submodule" ,
task : task ,
} ,
} )
}
func ( gui * Gui ) handleSubmoduleEnter ( ) error {
submodule := gui . getSelectedSubmodule ( )
if submodule == nil {
return nil
}
return gui . enterSubmodule ( submodule )
}
func ( gui * Gui ) enterSubmodule ( submodule * models . SubmoduleConfig ) error {
wd , err := os . Getwd ( )
if err != nil {
return err
}
gui . State . RepoPathStack = append ( gui . State . RepoPathStack , wd )
return gui . dispatchSwitchToRepo ( submodule . Path )
}
2020-09-30 01:06:11 +02:00
func ( gui * Gui ) handleRemoveSubmodule ( ) error {
submodule := gui . getSelectedSubmodule ( )
if submodule == nil {
return nil
}
2020-09-30 00:27:23 +02:00
2020-09-30 01:06:11 +02:00
return gui . ask ( askOpts {
title : gui . Tr . SLocalize ( "RemoveSubmodule" ) ,
prompt : gui . Tr . SLocalize ( "RemoveSubmodulePrompt" ) + " '" + submodule . Name + "'?" ,
handleConfirm : func ( ) error {
if err := gui . GitCommand . SubmoduleDelete ( submodule ) ; err != nil {
return err
}
return gui . refreshSidePanels ( refreshOptions { scope : [ ] int { SUBMODULES , FILES } } )
} ,
} )
}
func ( gui * Gui ) handleResetSubmodule ( ) error {
return gui . WithWaitingStatus ( gui . Tr . SLocalize ( "resettingSubmoduleStatus" ) , func ( ) error {
submodule := gui . getSelectedSubmodule ( )
if submodule == nil {
return nil
}
2020-09-30 00:27:23 +02:00
2020-09-30 01:06:11 +02:00
return gui . resetSubmodule ( submodule )
} )
}
func ( gui * Gui ) fileForSubmodule ( submodule * models . SubmoduleConfig ) * models . File {
for _ , file := range gui . State . Files {
if file . IsSubmodule ( [ ] * models . SubmoduleConfig { submodule } ) {
return file
}
}
return nil
}
func ( gui * Gui ) resetSubmodule ( submodule * models . SubmoduleConfig ) error {
file := gui . fileForSubmodule ( submodule )
if file != nil {
if err := gui . GitCommand . UnStageFile ( file . Name , file . Tracked ) ; err != nil {
return gui . surfaceError ( err )
}
}
if err := gui . GitCommand . SubmoduleStash ( submodule ) ; err != nil {
return gui . surfaceError ( err )
}
if err := gui . GitCommand . SubmoduleReset ( submodule ) ; err != nil {
return gui . surfaceError ( err )
}
return gui . refreshSidePanels ( refreshOptions { mode : ASYNC , scope : [ ] int { FILES , SUBMODULES } } )
}
// func (gui *Gui) handleAddsubmodule(g *gocui.Gui, v *gocui.View) error {
// return gui.prompt(gui.Tr.SLocalize("newsubmoduleName"), "", func(submoduleName string) error {
// return gui.prompt(gui.Tr.SLocalize("newsubmoduleUrl"), "", func(submoduleUrl string) error {
// if err := gui.GitCommand.Addsubmodule(submoduleName, submoduleUrl); err != nil {
2020-09-30 00:27:23 +02:00
// return err
// }
2020-09-30 01:06:11 +02:00
// return gui.refreshSidePanels(refreshOptions{scope: []int{submoduleS}})
// })
2020-09-30 00:27:23 +02:00
// })
// }
2020-09-30 01:06:11 +02:00
// func (gui *Gui) handleEditsubmodule(g *gocui.Gui, v *gocui.View) error {
// submodule := gui.getSelectedSubmodule()
// if submodule == nil {
2020-09-30 00:27:23 +02:00
// return nil
// }
// editNameMessage := gui.Tr.TemplateLocalize(
2020-09-30 01:06:11 +02:00
// "editsubmoduleName",
2020-09-30 00:27:23 +02:00
// Teml{
2020-09-30 01:06:11 +02:00
// "submoduleName": submodule.Name,
2020-09-30 00:27:23 +02:00
// },
// )
2020-09-30 01:06:11 +02:00
// return gui.prompt(editNameMessage, submodule.Name, func(updatedsubmoduleName string) error {
// if updatedsubmoduleName != submodule.Name {
// if err := gui.GitCommand.Renamesubmodule(submodule.Name, updatedsubmoduleName); err != nil {
2020-09-30 00:27:23 +02:00
// return gui.surfaceError(err)
// }
// }
// editUrlMessage := gui.Tr.TemplateLocalize(
2020-09-30 01:06:11 +02:00
// "editsubmoduleUrl",
2020-09-30 00:27:23 +02:00
// Teml{
2020-09-30 01:06:11 +02:00
// "submoduleName": updatedsubmoduleName,
2020-09-30 00:27:23 +02:00
// },
// )
2020-09-30 01:06:11 +02:00
// urls := submodule.Urls
2020-09-30 00:27:23 +02:00
// url := ""
// if len(urls) > 0 {
// url = urls[0]
// }
2020-09-30 01:06:11 +02:00
// return gui.prompt(editUrlMessage, url, func(updatedsubmoduleUrl string) error {
// if err := gui.GitCommand.UpdatesubmoduleUrl(updatedsubmoduleName, updatedsubmoduleUrl); err != nil {
2020-09-30 00:27:23 +02:00
// return gui.surfaceError(err)
// }
2020-09-30 01:06:11 +02:00
// return gui.refreshSidePanels(refreshOptions{scope: []int{BRANCHES, submoduleS}})
2020-09-30 00:27:23 +02:00
// })
// })
// }