2019-11-16 07:38:38 +02:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/fatih/color"
|
|
|
|
"github.com/jesseduffield/gocui"
|
2020-09-29 12:28:39 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
2019-11-16 07:38:38 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
// list panel functions
|
|
|
|
|
2020-09-29 10:41:06 +02:00
|
|
|
func (gui *Gui) getSelectedRemote() *models.Remote {
|
2020-08-20 00:53:10 +02:00
|
|
|
selectedLine := gui.State.Panels.Remotes.SelectedLineIdx
|
2019-11-17 04:47:47 +02:00
|
|
|
if selectedLine == -1 || len(gui.State.Remotes) == 0 {
|
2019-11-16 07:38:38 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return gui.State.Remotes[selectedLine]
|
|
|
|
}
|
|
|
|
|
2020-08-15 08:50:30 +02:00
|
|
|
func (gui *Gui) handleRemoteSelect() error {
|
2020-08-18 14:02:35 +02:00
|
|
|
var task updateTask
|
2019-11-16 07:38:38 +02:00
|
|
|
remote := gui.getSelectedRemote()
|
2019-11-18 00:38:36 +02:00
|
|
|
if remote == nil {
|
2020-08-18 14:02:35 +02:00
|
|
|
task = gui.createRenderStringTask("No remotes")
|
|
|
|
} else {
|
|
|
|
task = gui.createRenderStringTask(fmt.Sprintf("%s\nUrls:\n%s", utils.ColoredString(remote.Name, color.FgGreen), strings.Join(remote.Urls, "\n")))
|
2020-03-29 05:34:17 +02:00
|
|
|
}
|
|
|
|
|
2020-08-23 01:46:28 +02:00
|
|
|
return gui.refreshMainViews(refreshMainOpts{
|
2020-08-18 14:02:35 +02:00
|
|
|
main: &viewUpdateOpts{
|
|
|
|
title: "Remote",
|
|
|
|
task: task,
|
|
|
|
},
|
|
|
|
})
|
2019-11-16 07:38:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) refreshRemotes() error {
|
2019-11-17 04:47:47 +02:00
|
|
|
prevSelectedRemote := gui.getSelectedRemote()
|
|
|
|
|
2019-11-16 07:38:38 +02:00
|
|
|
remotes, err := gui.GitCommand.GetRemotes()
|
|
|
|
if err != nil {
|
2020-03-28 02:47:54 +02:00
|
|
|
return gui.surfaceError(err)
|
2019-11-16 07:38:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
gui.State.Remotes = remotes
|
|
|
|
|
2019-11-17 04:47:47 +02:00
|
|
|
// we need to ensure our selected remote branches aren't now outdated
|
|
|
|
if prevSelectedRemote != nil && gui.State.RemoteBranches != nil {
|
|
|
|
// find remote now
|
|
|
|
for _, remote := range remotes {
|
|
|
|
if remote.Name == prevSelectedRemote.Name {
|
|
|
|
gui.State.RemoteBranches = remote.Branches
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-27 01:37:22 +02:00
|
|
|
return gui.postRefreshUpdate(gui.mustContextForContextKey(gui.getBranchesView().Context))
|
2019-11-17 03:02:39 +02:00
|
|
|
}
|
|
|
|
|
2020-08-15 09:23:16 +02:00
|
|
|
func (gui *Gui) handleRemoteEnter() error {
|
2019-11-16 08:35:59 +02:00
|
|
|
// naive implementation: get the branches and render them to the list, change the context
|
|
|
|
remote := gui.getSelectedRemote()
|
2019-11-18 00:38:36 +02:00
|
|
|
if remote == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2019-11-16 08:35:59 +02:00
|
|
|
|
|
|
|
gui.State.RemoteBranches = remote.Branches
|
|
|
|
|
|
|
|
newSelectedLine := 0
|
|
|
|
if len(remote.Branches) == 0 {
|
|
|
|
newSelectedLine = -1
|
|
|
|
}
|
2020-08-20 00:53:10 +02:00
|
|
|
gui.State.Panels.RemoteBranches.SelectedLineIdx = newSelectedLine
|
2019-11-16 08:35:59 +02:00
|
|
|
|
2020-08-19 01:05:43 +02:00
|
|
|
return gui.switchContext(gui.Contexts.Remotes.Branches.Context)
|
2019-11-16 08:35:59 +02:00
|
|
|
}
|
2019-11-17 03:02:39 +02:00
|
|
|
|
|
|
|
func (gui *Gui) handleAddRemote(g *gocui.Gui, v *gocui.View) error {
|
2020-10-04 02:00:48 +02:00
|
|
|
return gui.prompt(gui.Tr.LcNewRemoteName, "", func(remoteName string) error {
|
|
|
|
return gui.prompt(gui.Tr.LcNewRemoteUrl, "", func(remoteUrl string) error {
|
2019-11-17 03:02:39 +02:00
|
|
|
if err := gui.GitCommand.AddRemote(remoteName, remoteUrl); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-28 01:27:34 +02:00
|
|
|
return gui.refreshSidePanels(refreshOptions{scope: []int{REMOTES}})
|
2019-11-17 03:02:39 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) handleRemoveRemote(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
remote := gui.getSelectedRemote()
|
|
|
|
if remote == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-15 08:38:16 +02:00
|
|
|
return gui.ask(askOpts{
|
2020-10-04 02:00:48 +02:00
|
|
|
title: gui.Tr.LcRemoveRemote,
|
|
|
|
prompt: gui.Tr.LcRemoveRemotePrompt + " '" + remote.Name + "'?",
|
2020-08-15 08:36:39 +02:00
|
|
|
handleConfirm: func() error {
|
|
|
|
if err := gui.GitCommand.RemoveRemote(remote.Name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-17 03:02:39 +02:00
|
|
|
|
2020-08-15 08:36:39 +02:00
|
|
|
return gui.refreshSidePanels(refreshOptions{scope: []int{BRANCHES, REMOTES}})
|
|
|
|
},
|
|
|
|
})
|
2019-11-17 03:02:39 +02:00
|
|
|
}
|
2019-11-17 09:15:32 +02:00
|
|
|
|
|
|
|
func (gui *Gui) handleEditRemote(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
remote := gui.getSelectedRemote()
|
|
|
|
if remote == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-04 02:00:48 +02:00
|
|
|
editNameMessage := utils.ResolvePlaceholderString(
|
|
|
|
gui.Tr.LcEditRemoteName,
|
|
|
|
map[string]string{
|
2019-11-17 09:15:32 +02:00
|
|
|
"remoteName": remote.Name,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2020-08-23 05:54:28 +02:00
|
|
|
return gui.prompt(editNameMessage, remote.Name, func(updatedRemoteName string) error {
|
2019-11-17 09:15:32 +02:00
|
|
|
if updatedRemoteName != remote.Name {
|
|
|
|
if err := gui.GitCommand.RenameRemote(remote.Name, updatedRemoteName); err != nil {
|
2020-03-28 02:47:54 +02:00
|
|
|
return gui.surfaceError(err)
|
2019-11-17 09:15:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-04 02:00:48 +02:00
|
|
|
editUrlMessage := utils.ResolvePlaceholderString(
|
|
|
|
gui.Tr.LcEditRemoteUrl,
|
|
|
|
map[string]string{
|
2019-11-17 09:15:32 +02:00
|
|
|
"remoteName": updatedRemoteName,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2020-08-23 05:54:28 +02:00
|
|
|
urls := remote.Urls
|
|
|
|
url := ""
|
|
|
|
if len(urls) > 0 {
|
|
|
|
url = urls[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return gui.prompt(editUrlMessage, url, func(updatedRemoteUrl string) error {
|
2019-11-17 09:15:32 +02:00
|
|
|
if err := gui.GitCommand.UpdateRemoteUrl(updatedRemoteName, updatedRemoteUrl); err != nil {
|
2020-03-28 02:47:54 +02:00
|
|
|
return gui.surfaceError(err)
|
2019-11-17 09:15:32 +02:00
|
|
|
}
|
2020-03-28 01:27:34 +02:00
|
|
|
return gui.refreshSidePanels(refreshOptions{scope: []int{BRANCHES, REMOTES}})
|
2019-11-17 09:15:32 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2019-12-07 07:20:22 +02:00
|
|
|
|
|
|
|
func (gui *Gui) handleFetchRemote(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
remote := gui.getSelectedRemote()
|
|
|
|
if remote == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-04 02:00:48 +02:00
|
|
|
return gui.WithWaitingStatus(gui.Tr.FetchingRemoteStatus, func() error {
|
2020-10-01 23:56:14 +02:00
|
|
|
gui.State.Mutexes.FetchMutex.Lock()
|
|
|
|
defer gui.State.Mutexes.FetchMutex.Unlock()
|
2020-08-23 13:25:39 +02:00
|
|
|
|
2020-09-29 12:03:39 +02:00
|
|
|
// TODO: test this
|
|
|
|
err := gui.GitCommand.FetchRemote(remote.Name, gui.promptUserForCredential)
|
|
|
|
gui.handleCredentialsPopup(err)
|
2019-12-07 07:20:22 +02:00
|
|
|
|
2020-03-28 01:27:34 +02:00
|
|
|
return gui.refreshSidePanels(refreshOptions{scope: []int{BRANCHES, REMOTES}})
|
2019-12-07 07:20:22 +02:00
|
|
|
})
|
|
|
|
}
|