From 8bb46c2d849bc737fe9fdafcd7f56a6d9bf9bad4 Mon Sep 17 00:00:00 2001 From: Jingwen Owen Ou Date: Tue, 8 Apr 2014 15:17:43 -0700 Subject: [PATCH] Fix priority of loading origin remotes --- commands/create.go | 5 ++- commands/pull_request.go | 1 - github/localrepo.go | 22 ++++++++-- github/{remote_test.go => localrepo_test.go} | 5 ++- github/remote.go | 43 ++++++++++---------- 5 files changed, 47 insertions(+), 29 deletions(-) rename github/{remote_test.go => localrepo_test.go} (68%) diff --git a/commands/create.go b/commands/create.go index 3749d7fe..8f3e4e3c 100644 --- a/commands/create.go +++ b/commands/create.go @@ -102,7 +102,10 @@ func create(command *Command, args *Args) { } } - remote, _ := github.OriginRemote() + localRepo, err := github.LocalRepo() + utils.Check(err) + + remote, _ := localRepo.OriginRemote() if remote == nil { url := project.GitURL("", "", true) args.Replace("git", "remote", "add", "-f", "origin", url) diff --git a/commands/pull_request.go b/commands/pull_request.go index c55b31af..cb6f7c41 100644 --- a/commands/pull_request.go +++ b/commands/pull_request.go @@ -130,7 +130,6 @@ func pullRequest(cmd *Command, args *Args) { utils.Check(e) } } - } if head == "" { diff --git a/github/localrepo.go b/github/localrepo.go index e0a7a97a..c7016e7a 100644 --- a/github/localrepo.go +++ b/github/localrepo.go @@ -61,7 +61,7 @@ func (r *GitHubRepo) remotesForPublish(owner string) (remotes []Remote) { } } - names := []string{"origin", "github", "upstream"} + names := OriginNames for _, name := range names { if _, ok := remotesMap[name]; ok { continue @@ -73,7 +73,8 @@ func (r *GitHubRepo) remotesForPublish(owner string) (remotes []Remote) { } } - for _, name := range names { + for i := len(names) - 1; i >= 0; i-- { + name := names[i] if remote, ok := remotesMap[name]; ok { remotes = append(remotes, remote) delete(remotesMap, name) @@ -141,8 +142,21 @@ func (r *GitHubRepo) RemoteBranchAndProject(owner string) (branch *Branch, proje return } -func (r *GitHubRepo) OriginRemote() (*Remote, error) { - return r.RemoteByName("origin") +func (r *GitHubRepo) OriginRemote() (remote *Remote, err error) { + remotes, err := Remotes() + if err != nil { + return + } + + if len(remotes) > 0 { + remote = &remotes[0] + } + + if remote == nil { + err = fmt.Errorf("Can't find git remote origin") + } + + return } func (r *GitHubRepo) MainProject() (project *Project, err error) { diff --git a/github/remote_test.go b/github/localrepo_test.go similarity index 68% rename from github/remote_test.go rename to github/localrepo_test.go index 1c9680b8..85d65047 100644 --- a/github/remote_test.go +++ b/github/localrepo_test.go @@ -7,11 +7,12 @@ import ( "github.com/github/hub/fixtures" ) -func TestOriginRemote(t *testing.T) { +func TestGitHubRepo_OriginRemote(t *testing.T) { repo := fixtures.SetupTestRepo() defer repo.TearDown() - gitRemote, _ := OriginRemote() + localRepo, _ := LocalRepo() + gitRemote, _ := localRepo.OriginRemote() assert.Equal(t, "origin", gitRemote.Name) assert.Equal(t, repo.Remote, gitRemote.URL.String()) } diff --git a/github/remote.go b/github/remote.go index d6ac8051..b64d7127 100644 --- a/github/remote.go +++ b/github/remote.go @@ -2,10 +2,15 @@ package github import ( "fmt" - "github.com/github/hub/git" "net/url" "regexp" "strings" + + "github.com/github/hub/git" +) + +var ( + OriginNames = []string{"upstream", "github", "origin"} ) type Remote struct { @@ -26,6 +31,7 @@ func Remotes() (remotes []Remote, err error) { return } + // build the remotes map remotesMap := make(map[string]string) for _, r := range rs { if re.MatchString(r) { @@ -36,30 +42,25 @@ func Remotes() (remotes []Remote, err error) { } } + // construct remotes in priority order + names := OriginNames + for _, name := range names { + if u, ok := remotesMap[name]; ok { + url, e := git.ParseURL(u) + if e == nil { + remotes = append(remotes, Remote{Name: name, URL: url}) + delete(remotesMap, name) + } + } + } + + // the rest of the remotes for n, u := range remotesMap { url, e := git.ParseURL(u) - if e != nil { - err = e - return + if e == nil { + remotes = append(remotes, Remote{Name: n, URL: url}) } - - remotes = append(remotes, Remote{Name: n, URL: url}) } return } - -func OriginRemote() (*Remote, error) { - remotes, err := Remotes() - if err != nil { - return nil, err - } - - for _, r := range remotes { - if r.Name == "origin" { - return &r, nil - } - } - - return nil, fmt.Errorf("Can't find git remote origin") -}