Permalink
Browse files

improve `checkout` command

Now forwards flags to internal `checkout` command, resets the existing
local branch by default.

Closes #132
  • Loading branch information...
1 parent d0a8e5f commit 7e804aaeb0f6299b7680093f77f523de96f34d68 @mislav mislav committed Jan 28, 2012
Showing with 15 additions and 10 deletions.
  1. +1 −1 README.md
  2. +9 −4 lib/hub/commands.rb
  3. +5 −5 test/hub_test.rb
View
2 README.md
@@ -201,7 +201,7 @@ superpowers:
# $ git checkout https://github.com/defunkt/hub/pull/73
# > git remote add -f -t feature git://github:com/mislav/hub.git
- # > git checkout -b mislav-feature mislav/feature
+ # > git checkout --track -B mislav-feature mislav/feature
# $ git checkout https://github.com/defunkt/hub/pull/73 custom-branch-name
View
13 lib/hub/commands.rb
@@ -303,17 +303,20 @@ def fetch(args)
# $ git checkout https://github.com/defunkt/hub/pull/73
# > git remote add -f -t feature git://github:com/mislav/hub.git
- # > git checkout -b mislav-feature mislav/feature
+ # > git checkout --track -B mislav-feature mislav/feature
def checkout(args)
- if (2..3) === args.length and url = resolve_github_url(args[1]) and url.project_path =~ /^pull\/(\d+)/
+ _, url_arg, new_branch_name = args.words
+ if url = resolve_github_url(url_arg) and url.project_path =~ /^pull\/(\d+)/
pull_id = $1
load_net_http
response = http_request(url.project.api_pullrequest_url(pull_id, 'json'))
pull_data = JSON.parse(response.body)['pull']
+ args.delete new_branch_name
user, branch = pull_data['head']['label'].split(':', 2)
- new_branch_name = args[2] || "#{user}-#{branch}"
+ abort "Error: #{user}'s fork is not available anymore" unless pull_data['head']['repository']
+ new_branch_name ||= "#{user}-#{branch}"
if remotes.include? user
args.before ['remote', 'set-branches', '--add', user, branch]
@@ -323,7 +326,9 @@ def checkout(args)
:https => https_protocol?)
args.before ['remote', 'add', '-f', '-t', branch, user, url]
end
- args[1..-1] = ['-b', new_branch_name, "#{user}/#{branch}"]
+ idx = args.index url_arg
+ args.delete_at idx
+ args.insert idx, '--track', '-B', new_branch_name, "#{user}/#{branch}"
end
end
View
10 test/hub_test.rb
@@ -947,16 +947,16 @@ def test_checkout_pullrequest
to_return(:body => mock_pull_response('blueyed:feature'))
assert_commands 'git remote add -f -t feature blueyed git://github.com/blueyed/hub.git',
- 'git checkout -b blueyed-feature blueyed/feature',
- "checkout https://github.com/defunkt/hub/pull/73/files"
+ 'git checkout -f --track -B blueyed-feature blueyed/feature -q',
+ "checkout -f https://github.com/defunkt/hub/pull/73/files -q"
end
def test_checkout_private_pullrequest
stub_request(:get, "https://#{auth}github.com/api/v2/json/pulls/defunkt/hub/73").
to_return(:body => mock_pull_response('blueyed:feature', :private))
assert_commands 'git remote add -f -t feature blueyed git@github.com:blueyed/hub.git',
- 'git checkout -b blueyed-feature blueyed/feature',
+ 'git checkout --track -B blueyed-feature blueyed/feature',
"checkout https://github.com/defunkt/hub/pull/73/files"
end
@@ -965,7 +965,7 @@ def test_checkout_pullrequest_custom_branch
to_return(:body => mock_pull_response('blueyed:feature'))
assert_commands 'git remote add -f -t feature blueyed git://github.com/blueyed/hub.git',
- 'git checkout -b review blueyed/feature',
+ 'git checkout --track -B review blueyed/feature',
"checkout https://github.com/defunkt/hub/pull/73/files review"
end
@@ -977,7 +977,7 @@ def test_checkout_pullrequest_existing_remote
assert_commands 'git remote set-branches --add blueyed feature',
'git fetch blueyed +refs/heads/feature:refs/remotes/blueyed/feature',
- 'git checkout -b blueyed-feature blueyed/feature',
+ 'git checkout --track -B blueyed-feature blueyed/feature',
"checkout https://github.com/defunkt/hub/pull/73/files"
end

0 comments on commit 7e804aa

Please sign in to comment.