diff --git a/features/pull_request.feature b/features/pull_request.feature index 7726a23ab9d4b5caf48b2425f7a58b6d2a676c6e..504aa56e2e0249900e4cf46662f2bf6b4302f3a6 100644 --- a/features/pull_request.feature +++ b/features/pull_request.feature @@ -255,6 +255,20 @@ Feature: hub pull-request When I successfully run `hub pull-request -b develop -m message` Then the output should contain exactly "the://url\n" + Scenario: Implicit base by detecting main branch + Given the default branch for "origin" is "develop" + And I am on the "master" branch + Given the GitHub API server: + """ + post('/repos/mislav/coral/pulls') { + assert :base => 'develop', + :head => 'mislav:master' + json :html_url => "the://url" + } + """ + When I successfully run `hub pull-request -m message` + Then the output should contain exactly "the://url\n" + Scenario: Explicit base with owner Given I am on the "master" branch Given the GitHub API server: diff --git a/features/steps.rb b/features/steps.rb index b175ba1b561607744b10dca1c363f3851f07d6bd..be6c5dda21a92a4c70de61e21325d9c87cd21641 100644 --- a/features/steps.rb +++ b/features/steps.rb @@ -74,6 +74,16 @@ Given(/^I am on the "([^"]+)" branch(?: with upstream "([^"]+)")?$/) do |name, u run_silent %(git checkout --quiet -B #{name} --track #{upstream}) end +Given(/^the default branch for "([^"]+)" is "([^"]+)"$/) do |remote, branch| + empty_commit + ref_file = ".git/refs/remotes/#{remote}/#{branch}" + in_current_dir do + FileUtils.mkdir_p File.dirname(ref_file) + FileUtils.cp '.git/refs/heads/master', ref_file + end + run_silent %(git remote set-head #{remote} #{branch}) +end + Given(/^I am in detached HEAD$/) do empty_commit empty_commit diff --git a/lib/hub/context.rb b/lib/hub/context.rb index bbf8b029c5ddeeef755d7abf10d799f75ff1a96b..022d12524e088fe0494c6f32e737169574475ad2 100644 --- a/lib/hub/context.rb +++ b/lib/hub/context.rb @@ -159,7 +159,10 @@ module Hub end def master_branch - Branch.new self, 'refs/heads/master' + if remote = origin_remote + default_branch = git_command("rev-parse --symbolic-full-name #{remote}") + end + Branch.new(self, default_branch || 'refs/heads/master') end def remotes diff --git a/test/hub_test.rb b/test/hub_test.rb index b2ed7688c81251812361cd58660e2c5ae10cbb6f..1a3c9278d1603167e794491eb2647d8fe9e9cec3 100644 --- a/test/hub_test.rb +++ b/test/hub_test.rb @@ -260,6 +260,7 @@ class HubTest < Test::Unit::TestCase def test_pullrequest_from_branch_tracking_local stub_branch('refs/heads/feature') stub_tracking('feature', 'refs/heads/master') + stub_command_output('rev-parse --symbolic-full-name origin', 'refs/remotes/origin/master') stub_request(:post, "https://api.github.com/repos/defunkt/hub/pulls"). with(:body => {'base' => "master", 'head' => "tpw:feature", 'title' => "hereyougo" }). @@ -274,6 +275,7 @@ class HubTest < Test::Unit::TestCase stub_repo_url('git@git.my.org:defunkt/hub.git') stub_branch('refs/heads/feature') stub_tracking_nothing('feature') + stub_command_output('rev-parse --symbolic-full-name origin', 'refs/remotes/origin/master') stub_command_output "rev-list --cherry-pick --right-only --no-merges origin/feature...", nil edit_hub_config do |data| data['git.my.org'] = [{'user'=>'myfiname', 'oauth_token' => 'FITOKEN'}]