提交 da534515 编写于 作者: J Jingwen Owen Ou

Move PushTarget to Branch

上级 d1674543
......@@ -9,6 +9,7 @@ import (
)
type Branch struct {
Repo *GitHubRepo
Name string
}
......@@ -22,6 +23,29 @@ func (b *Branch) LongName() string {
return reg.ReplaceAllString(b.Name, "")
}
func (b *Branch) PushTarget(owner string) (branch *Branch) {
var err error
pushDefault, _ := git.Config("push.default")
if pushDefault == "upstream" || pushDefault == "tracking" {
branch, err = b.Upstream()
if err != nil {
return
}
} else {
shortName := b.ShortName()
remotes := b.Repo.remotesForPublish(owner)
for _, remote := range remotes {
if git.HasFile("refs", "remotes", remote.Name, shortName) {
name := fmt.Sprintf("refs/remotes/%s/%s", remote.Name, shortName)
branch = &Branch{b.Repo, name}
break
}
}
}
return
}
func (b *Branch) RemoteName() string {
reg := regexp.MustCompile("^refs/remotes/([^/]+)")
if reg.MatchString(b.Name) {
......@@ -37,14 +61,13 @@ func (b *Branch) Upstream() (u *Branch, err error) {
return
}
u = &Branch{name}
u = &Branch{b.Repo, name}
return
}
func (b *Branch) IsMaster() bool {
localRepo := LocalRepo()
masterName := localRepo.MasterBranch().ShortName()
masterName := b.Repo.MasterBranch().ShortName()
return b.ShortName() == masterName
}
......
......@@ -6,27 +6,27 @@ import (
)
func TestBranch_ShortName(t *testing.T) {
b := Branch{"refs/heads/master"}
b := Branch{LocalRepo(), "refs/heads/master"}
assert.Equal(t, "master", b.ShortName())
}
func TestBranch_LongName(t *testing.T) {
b := Branch{"refs/heads/master"}
b := Branch{LocalRepo(), "refs/heads/master"}
assert.Equal(t, "heads/master", b.LongName())
b = Branch{"refs/remotes/origin/master"}
b = Branch{LocalRepo(), "refs/remotes/origin/master"}
assert.Equal(t, "origin/master", b.LongName())
}
func TestBranch_RemoteName(t *testing.T) {
b := Branch{"refs/remotes/origin/master"}
b := Branch{LocalRepo(), "refs/remotes/origin/master"}
assert.Equal(t, "origin", b.RemoteName())
b = Branch{"refs/head/master"}
b = Branch{LocalRepo(), "refs/head/master"}
assert.Equal(t, "", b.RemoteName())
}
func TestBranch_IsRemote(t *testing.T) {
b := Branch{"refs/remotes/origin/master"}
b := Branch{LocalRepo(), "refs/remotes/origin/master"}
assert.T(t, b.IsRemote())
}
......@@ -2,6 +2,7 @@ package github
import (
"fmt"
"github.com/github/hub/git"
)
......@@ -76,7 +77,7 @@ func (r *GitHubRepo) CurrentBranch() (branch *Branch, err error) {
return
}
branch = &Branch{head}
branch = &Branch{r, head}
return
}
......@@ -91,7 +92,7 @@ func (r *GitHubRepo) MasterBranch() (branch *Branch) {
name = "refs/heads/master"
}
branch = &Branch{name}
branch = &Branch{r, name}
return
}
......@@ -107,25 +108,9 @@ func (r *GitHubRepo) RemoteBranchAndProject(owner string) (branch *Branch, proje
return
}
pushDefault, _ := git.Config("push.default")
if pushDefault == "upstream" || pushDefault == "tracking" {
branch, err = branch.Upstream()
if err != nil {
return
}
} else {
shortName := branch.ShortName()
remotes := r.remotesForPublish(owner)
for _, remote := range remotes {
if git.HasFile("refs", "remotes", remote.Name, shortName) {
name := fmt.Sprintf("refs/remotes/%s/%s", remote.Name, shortName)
branch = &Branch{name}
break
}
}
}
branch = branch.PushTarget(owner)
if branch.IsRemote() {
if branch != nil && branch.IsRemote() {
remote, e := r.RemoteByName(branch.RemoteName())
if e == nil {
project, err = remote.Project()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册