提交 f3d909bb 编写于 作者: M Mislav Marohnić

Merge branch 'checkout-reuse-remote'

......@@ -4,6 +4,7 @@ import (
"fmt"
"regexp"
"github.com/github/hub/git"
"github.com/github/hub/github"
"github.com/github/hub/utils"
)
......@@ -16,8 +17,8 @@ var cmdCheckout = &Command{
## Examples:
$ hub checkout https://github.com/jingweno/gh/pull/73
> git remote add -f --no-tags -t feature git://github:com/jingweno/gh.git
> git checkout --track -B jingweno-feature jingweno/feature
> git fetch origin pull/73/head:jingweno-feature
> git checkout jingweno-feature
## See also:
......@@ -83,21 +84,33 @@ func transformCheckoutArgs(args *Args) error {
return err
}
remote, err := repo.RemoteForRepo(pullRequest.Base.Repo)
baseRemote, err := repo.RemoteForRepo(pullRequest.Base.Repo)
if err != nil {
return err
}
var refSpec string
var headRemote *github.Remote
if pullRequest.IsSameRepo() {
headRemote = baseRemote
} else {
headRemote, _ = repo.RemoteForRepo(pullRequest.Head.Repo)
}
var newArgs []string
if pullRequest.IsSameRepo() {
if headRemote != nil {
if newBranchName == "" {
newBranchName = pullRequest.Head.Ref
}
remoteBranch := fmt.Sprintf("%s/%s", remote.Name, pullRequest.Head.Ref)
refSpec = fmt.Sprintf("+refs/heads/%s:refs/remotes/%s", pullRequest.Head.Ref, remoteBranch)
newArgs = append(newArgs, "-b", newBranchName, "--track", remoteBranch)
remoteBranch := fmt.Sprintf("%s/%s", headRemote.Name, pullRequest.Head.Ref)
refSpec := fmt.Sprintf("+refs/heads/%s:refs/remotes/%s", pullRequest.Head.Ref, remoteBranch)
if git.HasFile("refs", "heads", newBranchName) {
newArgs = append(newArgs, newBranchName)
args.After("git", "merge", "--ff-only", fmt.Sprintf("refs/remotes/%s", remoteBranch))
} else {
newArgs = append(newArgs, "-b", newBranchName, "--track", remoteBranch)
}
args.Before("git", "fetch", headRemote.Name, refSpec)
} else {
if newBranchName == "" {
if pullRequest.Head.Repo == nil {
......@@ -106,11 +119,10 @@ func transformCheckoutArgs(args *Args) error {
newBranchName = fmt.Sprintf("%s-%s", pullRequest.Head.Repo.Owner.Login, pullRequest.Head.Ref)
}
}
refSpec = fmt.Sprintf("refs/pull/%s/head:%s", id, newBranchName)
refSpec := fmt.Sprintf("refs/pull/%s/head:%s", id, newBranchName)
newArgs = append(newArgs, newBranchName)
args.Before("git", "fetch", baseRemote.Name, refSpec)
}
args.Before("git", "fetch", remote.Name, refSpec)
replaceCheckoutParam(args, checkoutURL, newArgs...)
return nil
}
......
......@@ -140,3 +140,55 @@ Feature: hub checkout <PULLREQ-URL>
When I run `hub checkout https://github.com/mojombo/jekyll/pull/77`
Then "git fetch origin refs/pull/77/head:pr-77" should be run
And "git checkout pr-77" should be run
Scenario: Reuse existing remote for head branch
Given the GitHub API server:
"""
get('/repos/mojombo/jekyll/pulls/77') {
json :head => {
:ref => "fixes",
:repo => {
:owner => { :login => "mislav" },
:name => "jekyll",
:private => false
}
}, :base => {
:repo => {
:name => 'jekyll',
:html_url => 'https://github.com/mojombo/jekyll',
:owner => { :login => "mojombo" },
}
}
}
"""
And the "mislav" remote has url "git://github.com/mislav/jekyll.git"
When I run `hub checkout -f https://github.com/mojombo/jekyll/pull/77 -q`
Then "git fetch mislav +refs/heads/fixes:refs/remotes/mislav/fixes" should be run
And "git checkout -f -b fixes --track mislav/fixes -q" should be run
Scenario: Reuse existing remote and branch
Given the GitHub API server:
"""
get('/repos/mojombo/jekyll/pulls/77') {
json :head => {
:ref => "fixes",
:repo => {
:owner => { :login => "mislav" },
:name => "jekyll",
:private => false
}
}, :base => {
:repo => {
:name => 'jekyll',
:html_url => 'https://github.com/mojombo/jekyll',
:owner => { :login => "mojombo" },
}
}
}
"""
And the "mislav" remote has url "git://github.com/mislav/jekyll.git"
And I am on the "fixes" branch
When I run `hub checkout -f https://github.com/mojombo/jekyll/pull/77 -q`
Then "git fetch mislav +refs/heads/fixes:refs/remotes/mislav/fixes" should be run
And "git checkout -f fixes -q" should be run
And "git merge --ff-only refs/remotes/mislav/fixes" should be run
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册