2019-11-16 07:38:38 +02:00
|
|
|
package gui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2020-09-29 12:28:39 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
2021-07-27 15:00:37 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/style"
|
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]
|
|
|
|
}
|
|
|
|
|
2021-11-21 03:48:49 +02:00
|
|
|
func (gui *Gui) remotesRenderToMain() 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 {
|
2021-04-04 15:51:59 +02:00
|
|
|
task = NewRenderStringTask("No remotes")
|
2020-08-18 14:02:35 +02:00
|
|
|
} else {
|
2021-07-27 15:00:37 +02:00
|
|
|
task = NewRenderStringTask(fmt.Sprintf("%s\nUrls:\n%s", style.FgGreen.Sprint(remote.Name), 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()
|
|
|
|
|
2022-01-02 01:21:32 +02:00
|
|
|
remotes, err := gui.GitCommand.Loaders.Remotes.GetRemotes()
|
2019-11-16 07:38:38 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-04 15:51:59 +02:00
|
|
|
return gui.postRefreshUpdate(gui.mustContextForContextKey(ContextKey(gui.Views.Branches.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
|
|
|
|
2021-04-03 06:56:11 +02:00
|
|
|
return gui.pushContext(gui.State.Contexts.RemoteBranches)
|
2019-11-16 08:35:59 +02:00
|
|
|
}
|
2019-11-17 03:02:39 +02:00
|
|
|
|
2021-04-02 10:20:40 +02:00
|
|
|
func (gui *Gui) handleAddRemote() error {
|
2020-11-28 04:35:58 +02:00
|
|
|
return gui.prompt(promptOpts{
|
|
|
|
title: gui.Tr.LcNewRemoteName,
|
|
|
|
handleConfirm: func(remoteName string) error {
|
|
|
|
return gui.prompt(promptOpts{
|
|
|
|
title: gui.Tr.LcNewRemoteUrl,
|
|
|
|
handleConfirm: func(remoteUrl string) error {
|
2022-01-05 02:57:32 +02:00
|
|
|
gui.logSpan(gui.Tr.Spans.AddRemote)
|
|
|
|
if err := gui.GitCommand.AddRemote(remoteName, remoteUrl); err != nil {
|
2020-11-28 04:35:58 +02:00
|
|
|
return err
|
|
|
|
}
|
2021-03-31 14:55:06 +02:00
|
|
|
return gui.refreshSidePanels(refreshOptions{scope: []RefreshableView{REMOTES}})
|
2020-11-28 04:35:58 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
2019-11-17 03:02:39 +02:00
|
|
|
})
|
2020-11-28 04:35:58 +02:00
|
|
|
|
2019-11-17 03:02:39 +02:00
|
|
|
}
|
|
|
|
|
2021-04-02 10:20:40 +02:00
|
|
|
func (gui *Gui) handleRemoveRemote() error {
|
2019-11-17 03:02:39 +02:00
|
|
|
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 {
|
2022-01-05 02:57:32 +02:00
|
|
|
gui.logSpan(gui.Tr.Spans.RemoveRemote)
|
|
|
|
if err := gui.GitCommand.RemoveRemote(remote.Name); err != nil {
|
2020-11-05 11:16:08 +02:00
|
|
|
return gui.surfaceError(err)
|
2020-08-15 08:36:39 +02:00
|
|
|
}
|
2019-11-17 03:02:39 +02:00
|
|
|
|
2021-03-31 14:55:06 +02:00
|
|
|
return gui.refreshSidePanels(refreshOptions{scope: []RefreshableView{BRANCHES, REMOTES}})
|
2020-08-15 08:36:39 +02:00
|
|
|
},
|
|
|
|
})
|
2019-11-17 03:02:39 +02:00
|
|
|
}
|
2019-11-17 09:15:32 +02:00
|
|
|
|
2021-04-02 10:20:40 +02:00
|
|
|
func (gui *Gui) handleEditRemote() error {
|
2019-11-17 09:15:32 +02:00
|
|
|
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-11-28 04:35:58 +02:00
|
|
|
return gui.prompt(promptOpts{
|
|
|
|
title: editNameMessage,
|
|
|
|
initialContent: remote.Name,
|
|
|
|
handleConfirm: func(updatedRemoteName string) error {
|
|
|
|
if updatedRemoteName != remote.Name {
|
2022-01-05 02:57:32 +02:00
|
|
|
gui.logSpan(gui.Tr.Spans.UpdateRemote)
|
|
|
|
if err := gui.GitCommand.RenameRemote(remote.Name, updatedRemoteName); err != nil {
|
2020-11-28 04:35:58 +02:00
|
|
|
return gui.surfaceError(err)
|
|
|
|
}
|
2019-11-17 09:15:32 +02:00
|
|
|
}
|
|
|
|
|
2020-11-28 04:35:58 +02:00
|
|
|
editUrlMessage := utils.ResolvePlaceholderString(
|
|
|
|
gui.Tr.LcEditRemoteUrl,
|
|
|
|
map[string]string{
|
|
|
|
"remoteName": updatedRemoteName,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
urls := remote.Urls
|
|
|
|
url := ""
|
|
|
|
if len(urls) > 0 {
|
|
|
|
url = urls[0]
|
2019-11-17 09:15:32 +02:00
|
|
|
}
|
2020-11-28 04:35:58 +02:00
|
|
|
|
|
|
|
return gui.prompt(promptOpts{
|
|
|
|
title: editUrlMessage,
|
|
|
|
initialContent: url,
|
|
|
|
handleConfirm: func(updatedRemoteUrl string) error {
|
2022-01-05 02:57:32 +02:00
|
|
|
gui.logSpan(gui.Tr.Spans.UpdateRemote)
|
|
|
|
if err := gui.GitCommand.UpdateRemoteUrl(updatedRemoteName, updatedRemoteUrl); err != nil {
|
2020-11-28 04:35:58 +02:00
|
|
|
return gui.surfaceError(err)
|
|
|
|
}
|
2021-03-31 14:55:06 +02:00
|
|
|
return gui.refreshSidePanels(refreshOptions{scope: []RefreshableView{BRANCHES, REMOTES}})
|
2020-11-28 04:35:58 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
2019-11-17 09:15:32 +02:00
|
|
|
})
|
|
|
|
}
|
2019-12-07 07:20:22 +02:00
|
|
|
|
2021-04-02 10:20:40 +02:00
|
|
|
func (gui *Gui) handleFetchRemote() error {
|
2019-12-07 07:20:22 +02:00
|
|
|
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-07 12:45:57 +02:00
|
|
|
gui.Mutexes.FetchMutex.Lock()
|
|
|
|
defer gui.Mutexes.FetchMutex.Unlock()
|
2020-08-23 13:25:39 +02:00
|
|
|
|
2020-09-29 12:03:39 +02:00
|
|
|
err := gui.GitCommand.FetchRemote(remote.Name, gui.promptUserForCredential)
|
|
|
|
gui.handleCredentialsPopup(err)
|
2019-12-07 07:20:22 +02:00
|
|
|
|
2021-03-31 14:55:06 +02:00
|
|
|
return gui.refreshSidePanels(refreshOptions{scope: []RefreshableView{BRANCHES, REMOTES}})
|
2019-12-07 07:20:22 +02:00
|
|
|
})
|
|
|
|
}
|