Skip to content

Commit 5d4b34e

Browse files
author
Georgiy Melnikov
committed
allow to pass options to pull comand
Signed-off-by: Georgiy Melnikov <melnikov@evrone.com>
1 parent 15b80f4 commit 5d4b34e

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

lib/git/base.rb

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -409,14 +409,27 @@ def each_conflict(&block) # :yields: file, your_version, their_version
409409
self.lib.conflicts(&block)
410410
end
411411

412-
# pulls the given branch from the given remote into the current branch
413-
#
414-
# @git.pull # pulls from origin/master
415-
# @git.pull('upstream') # pulls from upstream/master
416-
# @git.pull('upstream', 'develope') # pulls from upstream/develop
417-
#
418-
def pull(remote = nil, branch = nil)
419-
self.lib.pull(remote, branch)
412+
# Pulls the given branch from the given remote into the current branch
413+
#
414+
# @param remote [String] the remote repository to pull from
415+
# @param branch [String] the branch to pull from
416+
# @param opts [Hash] options to pass to the pull command
417+
#
418+
# @option opts [Boolean] :allow_unrelated_histories (false) Merges histories of two projects that started their
419+
# lives independently
420+
# @example pulls from origin/master
421+
# @git.pull
422+
# @example pulls from upstream/master
423+
# @git.pull('upstream')
424+
# @example pulls from upstream/develop
425+
# @git.pull('upstream', 'develop')
426+
#
427+
# @return [Void]
428+
#
429+
# @raise [Git::FailedError] if the pull fails
430+
# @raise [ArgumentError] if a branch is given without a remote
431+
def pull(remote = nil, branch = nil, opts = {})
432+
self.lib.pull(remote, branch, opts)
420433
end
421434

422435
# returns an array of Git:Remote objects

lib/git/lib.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1006,10 +1006,11 @@ def push(remote = nil, branch = nil, opts = nil)
10061006
end
10071007
end
10081008

1009-
def pull(remote = nil, branch = nil)
1009+
def pull(remote = nil, branch = nil, opts = {})
10101010
raise ArgumentError, "You must specify a remote if a branch is specified" if remote.nil? && !branch.nil?
10111011

10121012
arr_opts = []
1013+
arr_opts << '--allow-unrelated-histories' if opts[:allow_unrelated_histories]
10131014
arr_opts << remote if remote
10141015
arr_opts << branch if branch
10151016
command('pull', *arr_opts)

tests/units/test_pull.rb

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
class TestPull < Test::Unit::TestCase
44

55
test 'pull with branch only should raise an ArgumentError' do
6-
in_temp_dir do |path|
6+
in_temp_dir do
77
Dir.mkdir('remote')
88

99
Dir.chdir('remote') do
@@ -23,7 +23,7 @@ class TestPull < Test::Unit::TestCase
2323
end
2424

2525
test 'pull with no args should use the default remote and current branch name' do
26-
in_temp_dir do |path|
26+
in_temp_dir do
2727
Dir.mkdir('remote')
2828

2929
Dir.chdir('remote') do
@@ -51,7 +51,7 @@ class TestPull < Test::Unit::TestCase
5151
end
5252

5353
test 'pull with one arg should use arg as remote and the current branch name' do
54-
in_temp_dir do |path|
54+
in_temp_dir do
5555
Dir.mkdir('remote')
5656

5757
Dir.chdir('remote') do
@@ -79,7 +79,7 @@ class TestPull < Test::Unit::TestCase
7979
end
8080

8181
test 'pull with both remote and branch should use both' do
82-
in_temp_dir do |path|
82+
in_temp_dir do
8383
Dir.mkdir('remote')
8484

8585
Dir.chdir('remote') do
@@ -109,4 +109,31 @@ class TestPull < Test::Unit::TestCase
109109
end
110110
end
111111
end
112+
113+
test 'when pull fails a Git::FailedError should be raised' do
114+
in_temp_dir do
115+
Dir.mkdir('remote')
116+
117+
Dir.chdir('remote') do
118+
`git init --initial-branch=master`
119+
File.write('README.md', 'Line 1')
120+
`git add README.md`
121+
`git commit -m "Initial commit"`
122+
end
123+
124+
`git clone remote/.git local 2>&1`
125+
126+
Dir.chdir('local') do
127+
git = Git.open('.')
128+
assert_raises(Git::FailedError) { git.pull('origin', 'none_existing_branch') }
129+
end
130+
end
131+
end
132+
133+
test 'pull with allow_unrelated_histories: true' do
134+
expected_command_line = ['pull', '--allow-unrelated-histories', 'origin', 'feature1', {}]
135+
assert_command_line_eq(expected_command_line) do |git|
136+
git.pull('origin', 'feature1', allow_unrelated_histories: true)
137+
end
138+
end
112139
end

0 commit comments

Comments
 (0)