diff --git a/README.md b/README.md index db38fbf6..df3b3e4b 100644 --- a/README.md +++ b/README.md @@ -265,6 +265,7 @@ g.branch('existing_branch').checkout g.branch('master').contains?('existing_branch') g.checkout('new_branch') +g.checkout('new_branch', new_branch: true, start_point: 'master') g.checkout(g.branch('new_branch')) g.branch(name).merge(branch2) diff --git a/lib/git/lib.rb b/lib/git/lib.rb index f2087a74..e898ebc3 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -764,11 +764,21 @@ def branch_delete(branch) command('branch', '-D', branch) end + # Runs checkout command to checkout or create branch + # + # accepts options: + # :new_branch + # :force + # :start_point + # + # @param [String] branch + # @param [Hash] opts def checkout(branch, opts = {}) arr_opts = [] arr_opts << '-b' if opts[:new_branch] || opts[:b] arr_opts << '--force' if opts[:force] || opts[:f] arr_opts << branch + arr_opts << opts[:start_point] if opts[:start_point] && arr_opts.include?('-b') command('checkout', arr_opts) end diff --git a/tests/units/test_lib.rb b/tests/units/test_lib.rb index 71acd21e..f886a400 100644 --- a/tests/units/test_lib.rb +++ b/tests/units/test_lib.rb @@ -87,6 +87,19 @@ def test_checkout assert(@lib.checkout('master')) end + def test_checkout_with_start_point + assert(@lib.reset(nil, hard: true)) # to get around worktree status on windows + + actual_cmd = nil + @lib.define_singleton_method(:run_command) do |git_cmd, &block| + actual_cmd = git_cmd + super(git_cmd, &block) + end + + assert(@lib.checkout('test_checkout_b2', {new_branch: true, start_point: 'master'})) + assert_match(%r/checkout ['"]-b['"] ['"]test_checkout_b2['"] ['"]master['"]/, actual_cmd) + end + # takes parameters, returns array of appropriate commit objects # :count # :since