Add preference flag_defaults.remote
, refactor
#466
|
@ -17,10 +17,20 @@ import (
|
|||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
// FlagDefaults defines all flags that can be overridden with a default value
|
||||
// via the config file
|
||||
type FlagDefaults struct {
|
||||
// Prefer a specific git remote to use for selecting a repository on gitea,
|
||||
// instead of relying on the remote associated with main/master/trunk branch.
|
||||
// The --remote flag still has precedence over this value.
|
||||
Remote string `yaml:"remote"`
|
||||
}
|
||||
|
||||
// Preferences that are stored in and read from the config file
|
||||
type Preferences struct {
|
||||
// Prefer using an external text editor over inline multiline prompts
|
||||
Editor bool `yaml:"editor"`
|
||||
Editor bool `yaml:"editor"`
|
||||
FlagDefaults FlagDefaults `yaml:"flag_defaults"`
|
||||
}
|
||||
|
||||
// LocalConfig represents local configurations
|
||||
|
@ -64,6 +74,7 @@ func GetConfigPath() string {
|
|||
|
||||
// GetPreferences returns preferences based on the config file
|
||||
func GetPreferences() Preferences {
|
||||
loadConfig()
|
||||
return config.Prefs
|
||||
}
|
||||
|
||||
|
|
|
@ -99,6 +99,10 @@ func InitCommand(ctx *cli.Context) *TeaContext {
|
|||
}
|
||||
}
|
||||
|
||||
if len(remoteFlag) == 0 {
|
||||
remoteFlag = config.GetPreferences().FlagDefaults.Remote
|
||||
}
|
||||
|
||||
// try to read local git repo & extract context: if repoFlag specifies a valid path, read repo in that dir,
|
||||
// otherwise attempt PWD. if no repo is found, continue with default login
|
||||
if c.LocalRepo, c.Login, c.RepoSlug, err = contextFromLocalRepo(repoPath, remoteFlag); err != nil {
|
||||
|
@ -158,28 +162,34 @@ func contextFromLocalRepo(repoPath, remoteValue string) (*git.TeaRepo, *config.L
|
|||
return repo, nil, "", errors.New("No remote(s) found in this Git repository")
|
||||
}
|
||||
|
||||
// if only one remote exists
|
||||
if len(gitConfig.Remotes) >= 1 && len(remoteValue) == 0 {
|
||||
for remote := range gitConfig.Remotes {
|
||||
remoteValue = remote
|
||||
}
|
||||
if len(gitConfig.Remotes) > 1 {
|
||||
// prefer origin if there is multiple remotes
|
||||
_, ok := gitConfig.Remotes["origin"]
|
||||
// When no preferred value is given, choose a remote to find a
|
||||
// matching login based on its URL.
|
||||
if len(gitConfig.Remotes) > 1 && len(remoteValue) == 0 {
|
||||
// if master branch is present, use it as the default remote
|
||||
mainBranches := []string{"main", "master", "trunk"}
|
||||
for _, b := range mainBranches {
|
||||
masterBranch, ok := gitConfig.Branches[b]
|
||||
if ok {
|
||||
if len(masterBranch.Remote) > 0 {
|
||||
remoteValue = masterBranch.Remote
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
// if no branch has matched, default to origin or upstream remote.
|
||||
if len(remoteValue) == 0 {
|
||||
if _, ok := gitConfig.Remotes["upstream"]; ok {
|
||||
remoteValue = "upstream"
|
||||
} else if _, ok := gitConfig.Remotes["origin"]; ok {
|
||||
remoteValue = "origin"
|
||||
}
|
||||
// if master branch is present, use it as the default remote
|
||||
mainBranches := []string{"main", "master", "trunk"}
|
||||
for _, b := range mainBranches {
|
||||
masterBranch, ok := gitConfig.Branches[b]
|
||||
if ok {
|
||||
if len(masterBranch.Remote) > 0 {
|
||||
remoteValue = masterBranch.Remote
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// make sure a remote is selected
|
||||
if len(remoteValue) == 0 {
|
||||
for remote := range gitConfig.Remotes {
|
||||
remoteValue = remote
|
||||
6543 marked this conversation as resolved
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user
Maybe we can break after the first remote, there is no need to iterate through all of them.
this is breaking though for little benefit in 99% of cases