diff --git a/features/fork.feature b/features/fork.feature index a96eef8e7db0fcdfb653b4f451a25d3c5b32b8f9..6e6b8570049cb9f5224c4140a46b86abc099ce80 100644 --- a/features/fork.feature +++ b/features/fork.feature @@ -17,6 +17,20 @@ Feature: hub fork And "git remote set-url mislav git@github.com:mislav/dotfiles.git" should be run And the url for "mislav" should be "git@github.com:mislav/dotfiles.git" + Scenario: Fork the repository when origin URL is private + Given the "origin" remote has url "git@github.com:evilchelu/dotfiles.git" + Given the GitHub API server: + """ + before { halt 401 unless request.env['HTTP_AUTHORIZATION'] == 'token OTOKEN' } + get('/repos/mislav/dotfiles', :host_name => 'api.github.com') { 404 } + post('/repos/evilchelu/dotfiles/forks', :host_name => 'api.github.com') { '' } + """ + When I successfully run `hub fork` + Then the output should contain exactly "new remote: mislav\n" + And "git remote add -f mislav ssh://git@github.com/evilchelu/dotfiles.git" should be run + And "git remote set-url mislav git@github.com:mislav/dotfiles.git" should be run + And the url for "mislav" should be "git@github.com:mislav/dotfiles.git" + Scenario: --no-remote Given the GitHub API server: """ diff --git a/lib/hub/commands.rb b/lib/hub/commands.rb index bd42be53654a71eb2fba7d9b4ed83e8146ebe138..c24a9caaa93a09da212ec830bf8e7a68777f3561 100644 --- a/lib/hub/commands.rb +++ b/lib/hub/commands.rb @@ -556,8 +556,9 @@ module Hub if args.include?('--no-remote') exit else + origin_url = project.remote.github_url url = forked_project.git_url(:private => true, :https => https_protocol?) - args.replace %W"remote add -f #{forked_project.owner} #{project.git_url}" + args.replace %W"remote add -f #{forked_project.owner} #{origin_url}" args.after %W"remote set-url #{forked_project.owner} #{url}" args.after 'echo', ['new remote:', forked_project.owner] end diff --git a/lib/hub/context.rb b/lib/hub/context.rb index f9b75eba9c563aaa0a4a358d5376b7add44d47fd..efc01cb93f99d4aee2025c3f7676d3bc82381f79 100644 --- a/lib/hub/context.rb +++ b/lib/hub/context.rb @@ -397,6 +397,10 @@ module Hub nil end + def github_url + urls.detect {|url| local_repo.known_host?(url.host) } + end + def urls @urls ||= raw_urls.map do |url| with_normalized_url(url) do |normalized|