mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-04-21 12:16:54 +02:00
I can only guess here: maybe they were added to more clearly document the public interface of the classes? If so, I don't think that works. Developers who are not familiar with the convention will just add a new public method to the class without updating the interface.
79 lines
2.0 KiB
Go
79 lines
2.0 KiB
Go
package helpers
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
|
)
|
|
|
|
type UpstreamHelper struct {
|
|
c *HelperCommon
|
|
|
|
getRemoteBranchesSuggestionsFunc func(string) func(string) []*types.Suggestion
|
|
}
|
|
|
|
func NewUpstreamHelper(
|
|
c *HelperCommon,
|
|
getRemoteBranchesSuggestionsFunc func(string) func(string) []*types.Suggestion,
|
|
) *UpstreamHelper {
|
|
return &UpstreamHelper{
|
|
c: c,
|
|
getRemoteBranchesSuggestionsFunc: getRemoteBranchesSuggestionsFunc,
|
|
}
|
|
}
|
|
|
|
func (self *UpstreamHelper) ParseUpstream(upstream string) (string, string, error) {
|
|
var upstreamBranch, upstreamRemote string
|
|
split := strings.Split(upstream, " ")
|
|
if len(split) != 2 {
|
|
return "", "", errors.New(self.c.Tr.InvalidUpstream)
|
|
}
|
|
|
|
upstreamRemote = split[0]
|
|
upstreamBranch = split[1]
|
|
|
|
return upstreamRemote, upstreamBranch, nil
|
|
}
|
|
|
|
func (self *UpstreamHelper) promptForUpstream(initialContent string, onConfirm func(string) error) error {
|
|
self.c.Prompt(types.PromptOpts{
|
|
Title: self.c.Tr.EnterUpstream,
|
|
InitialContent: initialContent,
|
|
FindSuggestionsFunc: self.getRemoteBranchesSuggestionsFunc(" "),
|
|
HandleConfirm: onConfirm,
|
|
})
|
|
|
|
return nil
|
|
}
|
|
|
|
func (self *UpstreamHelper) PromptForUpstreamWithInitialContent(currentBranch *models.Branch, onConfirm func(string) error) error {
|
|
suggestedRemote := self.GetSuggestedRemote()
|
|
initialContent := suggestedRemote + " " + currentBranch.Name
|
|
|
|
return self.promptForUpstream(initialContent, onConfirm)
|
|
}
|
|
|
|
func (self *UpstreamHelper) PromptForUpstreamWithoutInitialContent(_ *models.Branch, onConfirm func(string) error) error {
|
|
return self.promptForUpstream("", onConfirm)
|
|
}
|
|
|
|
func (self *UpstreamHelper) GetSuggestedRemote() string {
|
|
return getSuggestedRemote(self.c.Model().Remotes)
|
|
}
|
|
|
|
func getSuggestedRemote(remotes []*models.Remote) string {
|
|
if len(remotes) == 0 {
|
|
return "origin"
|
|
}
|
|
|
|
for _, remote := range remotes {
|
|
if remote.Name == "origin" {
|
|
return remote.Name
|
|
}
|
|
}
|
|
|
|
return remotes[0].Name
|
|
}
|