Fix priority of loading origin remotes

This commit is contained in:
Jingwen Owen Ou 2014-04-08 15:17:43 -07:00
Родитель bc5c12fa0d
Коммит 8bb46c2d84
5 изменённых файлов: 47 добавлений и 29 удалений

Просмотреть файл

@ -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)

Просмотреть файл

@ -130,7 +130,6 @@ func pullRequest(cmd *Command, args *Args) {
utils.Check(e)
}
}
}
if head == "" {

Просмотреть файл

@ -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) {

Просмотреть файл

@ -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())
}

Просмотреть файл

@ -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")
}