Skip to content

Commit 307530d

Browse files
Vasfedv-kolesnikov
andcommitted
Add start_point option for checkout command
Co-authored-by: V.Kolesnikov <re.vkolesnikov@gmail.com> Signed-off-by: Vasily Fedoseyev <vasilyfedoseyev@gmail.com>
1 parent ff6dcf4 commit 307530d

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ g.branch('existing_branch').checkout
265265
g.branch('master').contains?('existing_branch')
266266

267267
g.checkout('new_branch')
268+
g.checkout('new_branch', new_branch: true, start_point: 'master')
268269
g.checkout(g.branch('new_branch'))
269270

270271
g.branch(name).merge(branch2)

lib/git/lib.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,11 +764,21 @@ def branch_delete(branch)
764764
command('branch', '-D', branch)
765765
end
766766

767+
# Runs checkout command to checkout or create branch
768+
#
769+
# accepts options:
770+
# :new_branch
771+
# :force
772+
# :start_point
773+
#
774+
# @param [String] branch
775+
# @param [Hash] opts
767776
def checkout(branch, opts = {})
768777
arr_opts = []
769778
arr_opts << '-b' if opts[:new_branch] || opts[:b]
770779
arr_opts << '--force' if opts[:force] || opts[:f]
771780
arr_opts << branch
781+
arr_opts << opts[:start_point] if opts[:start_point] && arr_opts.include?('-b')
772782

773783
command('checkout', arr_opts)
774784
end

tests/units/test_lib.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,19 @@ def test_checkout
8787
assert(@lib.checkout('master'))
8888
end
8989

90+
def test_checkout_with_start_point
91+
assert(@lib.reset(nil, hard: true)) # to get around worktree status on windows
92+
93+
actual_cmd = nil
94+
@lib.define_singleton_method(:run_command) do |git_cmd, &block|
95+
actual_cmd = git_cmd
96+
super(git_cmd, &block)
97+
end
98+
99+
assert(@lib.checkout('test_checkout_b2', {new_branch: true, start_point: 'master'}))
100+
assert_match(%r/checkout ['"]-b['"] ['"]test_checkout_b2['"] ['"]master['"]/, actual_cmd)
101+
end
102+
90103
# takes parameters, returns array of appropriate commit objects
91104
# :count
92105
# :since

0 commit comments

Comments
 (0)