diff --git a/commands/checkout.go b/commands/checkout.go index abee3181..fd29425c 100644 --- a/commands/checkout.go +++ b/commands/checkout.go @@ -115,8 +115,16 @@ func transformCheckoutArgs(args *Args, pullRequest *github.PullRequest, newBranc } newArgs = append(newArgs, newBranchName) + b, errB := repo.CurrentBranch() + isCurrentBranch := errB == nil && b.ShortName() == newBranchName + ref := fmt.Sprintf("refs/pull/%d/head", pullRequest.Number) - args.Before("git", "fetch", baseRemote.Name, fmt.Sprintf("%s:%s", ref, newBranchName)) + if isCurrentBranch { + args.Before("git", "fetch", baseRemote.Name, ref) + args.After("git", "merge", "--ff-only", "FETCH_HEAD") + } else { + args.Before("git", "fetch", baseRemote.Name, fmt.Sprintf("%s:%s", ref, newBranchName)) + } remote := baseRemote.Name mergeRef := ref diff --git a/features/checkout.feature b/features/checkout.feature index 773d5638..786c18c2 100644 --- a/features/checkout.feature +++ b/features/checkout.feature @@ -251,6 +251,35 @@ Feature: hub checkout And "git checkout -f fixes -q" should be run And "fixes" should merge "refs/heads/fixes" from remote "git@github.com:mislav/jekyll.git" + Scenario: Modifiable fork into current branch + Given the GitHub API server: + """ + get('/repos/mojombo/jekyll/pulls/77') { + json :number => 77, :head => { + :ref => "fixes", + :repo => { + :owner => { :login => "mislav" }, + :name => "jekyll", + :html_url => "https://github.com/mislav/jekyll.git", + :private => false + }, + }, :base => { + :repo => { + :name => 'jekyll', + :html_url => 'https://github.com/mojombo/jekyll', + :owner => { :login => "mojombo" }, + } + }, :maintainer_can_modify => true + } + """ + And I am on the "fixes" branch + And there is a git FETCH_HEAD + When I successfully run `hub checkout https://github.com/mojombo/jekyll/pull/77` + Then "git fetch origin refs/pull/77/head" should be run + And "git checkout fixes" should be run + And "git merge --ff-only FETCH_HEAD" should be run + And "fixes" should merge "refs/heads/fixes" from remote "git@github.com:mislav/jekyll.git" + Scenario: Modifiable fork with HTTPS Given the GitHub API server: """