1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/remotes_panel.go

186 lines
4.6 KiB
Go
Raw Normal View History

2019-11-16 07:38:38 +02:00
package gui
import (
"fmt"
"strings"
"github.com/fatih/color"
"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 {
2021-04-04 15:51:59 +02:00
task = NewRenderStringTask("No remotes")
2020-08-18 14:02:35 +02:00
} else {
2021-04-04 15:51:59 +02:00
task = NewRenderStringTask(fmt.Sprintf("%s\nUrls:\n%s", utils.ColoredString(remote.Name, color.FgGreen), strings.Join(remote.Urls, "\n")))
}
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
}
}
}
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
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 {
2021-04-11 11:35:42 +02:00
if err := gui.GitCommand.WithSpan(gui.Tr.Spans.AddRemote).AddRemote(remoteName, remoteUrl); err != nil {
2020-11-28 04:35:58 +02:00
return err
}
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
}
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 {
2021-04-11 11:35:42 +02:00
if err := gui.GitCommand.WithSpan(gui.Tr.Spans.RemoveRemote).RemoveRemote(remote.Name); err != nil {
return gui.surfaceError(err)
2020-08-15 08:36:39 +02:00
}
2019-11-17 03:02:39 +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
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,
},
)
2021-04-11 11:35:42 +02:00
gitCommand := gui.GitCommand.WithSpan(gui.Tr.Spans.UpdateRemote)
2021-04-10 09:31:23 +02:00
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 {
2021-04-10 09:31:23 +02:00
if err := 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 {
2021-04-10 09:31:23 +02:00
if err := gitCommand.UpdateRemoteUrl(updatedRemoteName, updatedRemoteUrl); err != nil {
2020-11-28 04:35:58 +02:00
return gui.surfaceError(err)
}
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
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 {
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
return gui.refreshSidePanels(refreshOptions{scope: []RefreshableView{BRANCHES, REMOTES}})
2019-12-07 07:20:22 +02:00
})
}