Skip to content

Commit d33d563

Browse files
authored
#checkout without args should do same as git checkout with no args (#637)
Signed-off-by: James Couball <jcouball@yahoo.com>
1 parent 0c908da commit d33d563

File tree

3 files changed

+91
-4
lines changed

3 files changed

+91
-4
lines changed

lib/git/base.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,8 @@ def commit_all(message, opts = {})
350350
end
351351

352352
# checks out a branch as the new git working directory
353-
def checkout(branch = 'master', opts = {})
354-
self.lib.checkout(branch, opts)
353+
def checkout(*args, **options)
354+
self.lib.checkout(*args, **options)
355355
end
356356

357357
# checks out an old version of a file

lib/git/lib.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -772,11 +772,16 @@ def branch_delete(branch)
772772
#
773773
# @param [String] branch
774774
# @param [Hash] opts
775-
def checkout(branch, opts = {})
775+
def checkout(branch = nil, opts = {})
776+
if branch.is_a?(Hash) && opts == {}
777+
opts = branch
778+
branch = nil
779+
end
780+
776781
arr_opts = []
777782
arr_opts << '-b' if opts[:new_branch] || opts[:b]
778783
arr_opts << '--force' if opts[:force] || opts[:f]
779-
arr_opts << branch
784+
arr_opts << branch if branch
780785
arr_opts << opts[:start_point] if opts[:start_point] && arr_opts.include?('-b')
781786

782787
command('checkout', *arr_opts)

tests/units/test_checkout.rb

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
require 'test_helper'
2+
3+
# Runs checkout command to checkout or create branch
4+
#
5+
# accepts options:
6+
# :new_branch
7+
# :force
8+
# :start_point
9+
#
10+
# @param [String] branch
11+
# @param [Hash] opts
12+
# def checkout(branch, opts = {})
13+
14+
class TestCheckout < Test::Unit::TestCase
15+
test 'checkout with no args' do
16+
expected_command_line = ['checkout']
17+
git_cmd = :checkout
18+
git_cmd_args = []
19+
assert_command_line(expected_command_line, git_cmd, git_cmd_args)
20+
end
21+
22+
test 'checkout with no args and options' do
23+
expected_command_line = ['checkout', '--force']
24+
git_cmd = :checkout
25+
git_cmd_args = [force: true]
26+
assert_command_line(expected_command_line, git_cmd, git_cmd_args)
27+
end
28+
29+
test 'checkout with branch' do
30+
expected_command_line = ['checkout', 'feature1']
31+
git_cmd = :checkout
32+
git_cmd_args = ['feature1']
33+
assert_command_line(expected_command_line, git_cmd, git_cmd_args)
34+
end
35+
36+
test 'checkout with branch and options' do
37+
expected_command_line = ['checkout', '--force', 'feature1']
38+
git_cmd = :checkout
39+
git_cmd_args = ['feature1', force: true]
40+
assert_command_line(expected_command_line, git_cmd, git_cmd_args)
41+
end
42+
43+
test 'checkout with branch name and new_branch: true' do
44+
expected_command_line = ['checkout', '-b', 'feature1']
45+
git_cmd = :checkout
46+
git_cmd_args = ['feature1', new_branch: true]
47+
assert_command_line(expected_command_line, git_cmd, git_cmd_args)
48+
end
49+
50+
test 'checkout with force: true' do
51+
expected_command_line = ['checkout', '--force', 'feature1']
52+
git_cmd = :checkout
53+
git_cmd_args = ['feature1', force: true]
54+
assert_command_line(expected_command_line, git_cmd, git_cmd_args)
55+
end
56+
57+
test 'checkout with branch name and new_branch: true and start_point: "sha"' do
58+
expected_command_line = ['checkout', '-b', 'feature1', 'sha']
59+
git_cmd = :checkout
60+
git_cmd_args = ['feature1', new_branch: true, start_point: 'sha']
61+
assert_command_line(expected_command_line, git_cmd, git_cmd_args)
62+
end
63+
64+
65+
test 'when checkout succeeds an error should not be raised' do
66+
in_temp_dir do
67+
git = Git.init('.', initial_branch: 'master')
68+
File.write('file1.txt', 'file1')
69+
git.add('file1.txt')
70+
git.commit('commit1')
71+
assert_nothing_raised { git.checkout('master') }
72+
end
73+
end
74+
75+
test 'when checkout fails a Git::FailedError should be raised' do
76+
in_temp_dir do
77+
git = Git.init('.', initial_branch: 'master')
78+
# fails because there are no commits
79+
assert_raises(Git::FailedError) { git.checkout('master') }
80+
end
81+
end
82+
end

0 commit comments

Comments
 (0)