From 0515114fa321409705ff8cceb6f977afbaf5cc07 Mon Sep 17 00:00:00 2001 From: 6543 <6543@noreply@gitea.io> Date: Sat, 26 Oct 2019 21:29:37 +0000 Subject: [PATCH] Use Different Remote Repos (#58) --- .gitignore | 3 ++- cmd/config.go | 26 ++++++++++++++++++++++++-- cmd/flags.go | 9 +++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 798baa0..29e86fd 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -tea \ No newline at end of file +tea +.idea/ diff --git a/cmd/config.go b/cmd/config.go index b8c7561..b585555 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -195,9 +195,31 @@ func curGitRepoPath() (*Login, string, error) { if err := gitConfig.Unmarshal(bs); err != nil { return nil, "", err } - remoteConfig, ok := gitConfig.Remotes["origin"] + + // if no remote + if len(gitConfig.Remotes) == 0 { + return nil, "", errors.New("No remote repository set on 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 { + // if master branch is present, use it as the default remote + masterBranch, ok := gitConfig.Branches["master"] + if ok { + if len(masterBranch.Remote) > 0 { + remoteValue = masterBranch.Remote + } + } + } + } + + remoteConfig, ok := gitConfig.Remotes[remoteValue] if !ok || remoteConfig == nil { - return nil, "", errors.New("No remote origin found on this git repository") + return nil, "", errors.New("No remote " + remoteValue + " found on this git repository") } for _, l := range config.Logins { diff --git a/cmd/flags.go b/cmd/flags.go index 5d135fc..dded065 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -16,6 +16,7 @@ var ( loginValue string repoValue string outputValue string + remoteValue string ) // LoginFlag provides flag to specify tea login profile @@ -32,6 +33,13 @@ var RepoFlag = cli.StringFlag{ Destination: &repoValue, } +// RemoteFlag provides flag to specify remote repository +var RemoteFlag = cli.StringFlag{ + Name: "remote, R", + Usage: "Set a specific remote repository, is optional if not set use git default one", + Destination: &remoteValue, +} + // OutputFlag provides flag to specify output type var OutputFlag = cli.StringFlag{ Name: "output, o", @@ -63,6 +71,7 @@ var LoginRepoFlags = []cli.Flag{ // https://github.com/urfave/cli/issues/585 var AllDefaultFlags = append([]cli.Flag{ RepoFlag, + RemoteFlag, }, LoginOutputFlags...) // initCommand returns repository and *Login based on flags