Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add start_point option for checkout command #597

Merged
merged 1 commit into from Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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