提交 ddf79ca0 编写于 作者: C Chris Wanstrath

Tweaks, 80c, docs, -f

上级 c4bda898
......@@ -129,10 +129,11 @@ superpowers:
$ git fork
... hardcore forking action ...
> git remote add YOUR_USER git@github.com:YOUR_USER/CURRENT_REPO.git
> git remote add -f YOUR_USER git@github.com:YOUR_USER/CURRENT_REPO.git
Forks the original repo on GitHub and adds the new remote under your username.
It requires your GitHub token to be present; see "GitHub login" below for details.
Forks the original repo on GitHub and adds the new remote under your
username. It requires your GitHub token to be present; see "GitHub
login" below for details.
### git init
......
......@@ -115,7 +115,7 @@ module Hub
ssh = args.delete('-p')
url = ssh ? PRIVATE : PUBLIC
# user/repo
args.last =~ /\b(.+?)(?:\/(.+))?$/
user, repo = $1, $2 || REPO
......@@ -149,23 +149,26 @@ module Hub
args.after "git remote add origin #{url}"
end
end
# $ hub fork
# ... hardcore forking action ...
# > git remote add -f YOUR_USER git@github.com:YOUR_USER/CURRENT_REPO.git
def fork(args)
require 'net/http'
# can't do anything without token and original owner name
if github_user and github_token and !OWNER.empty?
if github_user && github_token && !OWNER.empty?
if own_repo_exists?
puts "#{github_user}/#{REPO} already exists on GitHub"
else
fork_repo
end
if args.include?('--no-remote')
exit
else
url = PRIVATE % [ github_user, REPO ]
args.replace %W"remote add #{github_user} #{url}"
args.replace %W"remote add -f #{github_user} #{url}"
args.after { puts "new remote: #{github_user}" }
end
end
......@@ -373,7 +376,7 @@ help
USER
end
end
def github_token
if TOKEN.empty?
abort "** No GitHub token set. See #{LGHCONF}"
......@@ -465,12 +468,19 @@ help
write.close
end
end
# Determines whether the current user (you) has a fork of the
# current repo on GitHub.
#
# Returns a Boolean.
def own_repo_exists?
url = API_REPO % [USER, REPO]
Net::HTTPSuccess === Net::HTTP.get_response(URI(url))
end
# Forks the current repo using the GitHub API.
#
# Returns nothing.
def fork_repo
url = API_FORK % [OWNER, REPO]
Net::HTTP.post_form(URI(url), 'login' => USER, 'token' => TOKEN)
......
......@@ -4,7 +4,7 @@ require 'webmock/test_unit'
class HubTest < Test::Unit::TestCase
include WebMock
def setup
Hub::Commands::REPO.replace("hub")
Hub::Commands::USER.replace("tpw")
......@@ -200,31 +200,34 @@ class HubTest < Test::Unit::TestCase
assert_equal "git push origin cool-feature", h.command
assert_equal "git push staging cool-feature; git push qa cool-feature", h.after
end
def test_fork
stub_request(:get, "github.com/api/v2/yaml/repos/show/tpw/hub").to_return(:status => 404)
stub_request(:get, "github.com/api/v2/yaml/repos/show/tpw/hub").
to_return(:status => 404)
stub_request(:post, "github.com/api/v2/yaml/repos/fork/defunkt/hub").with { |req|
params = Hash[*req.body.split(/[&=]/)]
params == { 'login'=>'tpw', 'token'=>'abc123' }
}
expected = "remote add tpw git@github.com:tpw/hub.git\n"
expected = "remote add -f tpw git@github.com:tpw/hub.git\n"
expected << "new remote: tpw\n"
assert_equal expected, hub("fork") { ENV['GIT'] = 'echo' }
end
def test_fork_no_remote
stub_request(:get, "github.com/api/v2/yaml/repos/show/tpw/hub").to_return(:status => 404)
stub_request(:get, "github.com/api/v2/yaml/repos/show/tpw/hub").
to_return(:status => 404)
stub_request(:post, "github.com/api/v2/yaml/repos/fork/defunkt/hub")
assert_equal "", hub("fork --no-remote") { ENV['GIT'] = 'echo' }
end
def test_fork_already_exists
stub_request(:get, "github.com/api/v2/yaml/repos/show/tpw/hub").to_return(:status => 200)
stub_request(:get, "github.com/api/v2/yaml/repos/show/tpw/hub").
to_return(:status => 200)
expected = "tpw/hub already exists on GitHub\n"
expected << "remote add tpw git@github.com:tpw/hub.git\n"
expected << "remote add -f tpw git@github.com:tpw/hub.git\n"
expected << "new remote: tpw\n"
assert_equal expected, hub("fork") { ENV['GIT'] = 'echo' }
end
......@@ -276,7 +279,8 @@ config
end
def test_hub_open_private
assert_command "browse -p bmizerany/sinatra", "open https://github.com/bmizerany/sinatra"
assert_command "browse -p bmizerany/sinatra",
"open https://github.com/bmizerany/sinatra"
end
def test_hub_open_self
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册