2019-11-16 07:38:38 +02:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/fatih/color"
|
|
|
|
"github.com/jesseduffield/gocui"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
// list panel functions
|
|
|
|
|
|
|
|
func (gui *Gui) getSelectedRemote() *commands.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-18 14:02:35 +02:00
|
|
|
return gui.refreshMain(refreshMainOpts{
|
|
|
|
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-08-19 11:31:58 +02:00
|
|
|
return gui.postRefreshUpdate(gui.contextForContextKey(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 {
|
|
|
|
branchesView := gui.getBranchesView()
|
2020-08-15 08:38:16 +02:00
|
|
|
return gui.prompt(branchesView, gui.Tr.SLocalize("newRemoteName"), "", func(remoteName string) error {
|
|
|
|
return gui.prompt(branchesView, gui.Tr.SLocalize("newRemoteUrl"), "", 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-08-15 08:36:39 +02:00
|
|
|
returnToView: v,
|
|
|
|
returnFocusOnClose: true,
|
|
|
|
title: gui.Tr.SLocalize("removeRemote"),
|
|
|
|
prompt: gui.Tr.SLocalize("removeRemotePrompt") + " '" + remote.Name + "'?",
|
|
|
|
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 {
|
|
|
|
branchesView := gui.getBranchesView()
|
|
|
|
remote := gui.getSelectedRemote()
|
|
|
|
if remote == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
editNameMessage := gui.Tr.TemplateLocalize(
|
|
|
|
"editRemoteName",
|
|
|
|
Teml{
|
|
|
|
"remoteName": remote.Name,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2020-08-15 08:38:16 +02:00
|
|
|
return gui.prompt(branchesView, editNameMessage, "", 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
editUrlMessage := gui.Tr.TemplateLocalize(
|
|
|
|
"editRemoteUrl",
|
|
|
|
Teml{
|
|
|
|
"remoteName": updatedRemoteName,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2020-08-15 08:38:16 +02:00
|
|
|
return gui.prompt(branchesView, editUrlMessage, "", 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
|
|
|
|
}
|
|
|
|
|
|
|
|
return gui.WithWaitingStatus(gui.Tr.SLocalize("FetchingRemoteStatus"), func() error {
|
|
|
|
if err := gui.GitCommand.FetchRemote(remote.Name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-28 01:27:34 +02:00
|
|
|
return gui.refreshSidePanels(refreshOptions{scope: []int{BRANCHES, REMOTES}})
|
2019-12-07 07:20:22 +02:00
|
|
|
})
|
|
|
|
}
|