Skip to content

Commit

Permalink
Add start_point option for checkout command
Browse files Browse the repository at this point in the history
Co-authored-by: V.Kolesnikov <re.vkolesnikov@gmail.com>
Signed-off-by: Vasily Fedoseyev <vasilyfedoseyev@gmail.com>
  • Loading branch information
Vasfed and v-kolesnikov committed Oct 5, 2022
1 parent ff6dcf4 commit 307530d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions lib/git/lib.rb
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions tests/units/test_lib.rb
Expand Up @@ -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
Expand Down

0 comments on commit 307530d

Please sign in to comment.