From 9126d28e32577e5af1e17c11e93e5b30cba21f0c Mon Sep 17 00:00:00 2001 From: Ivan Tse Date: Thu, 23 Jan 2014 11:48:21 -0500 Subject: [PATCH] `fork` should fetch from the same "origin" Github URL. --- features/fork.feature | 14 ++++++++++++++ lib/hub/commands.rb | 3 ++- lib/hub/context.rb | 4 ++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/features/fork.feature b/features/fork.feature index a96eef8e..6e6b8570 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 bd42be53..c24a9caa 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 f9b75eba..efc01cb9 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| -- GitLab