Skip to content

Commit b40de70

Browse files
authored
#pull with no options should do the same thing as git pull with no options (#633)
Signed-off-by: James Couball <jcouball@yahoo.com>
1 parent 9c5e0c6 commit b40de70

File tree

3 files changed

+120
-3
lines changed

3 files changed

+120
-3
lines changed

lib/git/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ def each_conflict(&block) # :yields: file, your_version, their_version
399399
# @git.pull('upstream') # pulls from upstream/master
400400
# @git.pull('upstream', 'develope') # pulls from upstream/develop
401401
#
402-
def pull(remote='origin', branch='master')
402+
def pull(remote = nil, branch = nil)
403403
self.lib.pull(remote, branch)
404404
end
405405

lib/git/lib.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -926,8 +926,13 @@ def push(remote, branch = 'master', opts = {})
926926
end
927927
end
928928

929-
def pull(remote='origin', branch='master')
930-
command('pull', remote, branch)
929+
def pull(remote = nil, branch = nil)
930+
raise ArgumentError, "You must specify a remote if a branch is specified" if remote.nil? && !branch.nil?
931+
932+
arr_opts = []
933+
arr_opts << remote if remote
934+
arr_opts << branch if branch
935+
command('pull', *arr_opts)
931936
end
932937

933938
def tag_sha(tag_name)

tests/units/test_pull.rb

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
require 'test_helper'
2+
3+
class TestPull < Test::Unit::TestCase
4+
5+
test 'pull with branch only should raise an ArgumentError' do
6+
in_temp_dir do |path|
7+
Dir.mkdir('remote')
8+
9+
Dir.chdir('remote') do
10+
`git init --initial-branch=branch1`
11+
File.write('README.md', 'Line 1')
12+
`git add README.md`
13+
`git commit -m "Initial commit"`
14+
end
15+
16+
`git clone remote/.git local 2>&1`
17+
18+
Dir.chdir('local') do
19+
git = Git.open('.')
20+
assert_raises(ArgumentError) { git.pull(nil, 'branch1') }
21+
end
22+
end
23+
end
24+
25+
test 'pull with no args should use the default remote and current branch name' do
26+
in_temp_dir do |path|
27+
Dir.mkdir('remote')
28+
29+
Dir.chdir('remote') do
30+
`git init --initial-branch=branch1`
31+
File.write('README.md', 'Line 1')
32+
`git add README.md`
33+
`git commit -m "Initial commit"`
34+
end
35+
36+
`git clone remote/.git local 2>&1`
37+
38+
Dir.chdir('remote') do
39+
File.open('README.md', 'a') { |f| f.write('Line 2') }
40+
`git add README.md`
41+
`git commit -m "Initial commit"`
42+
end
43+
44+
Dir.chdir('local') do
45+
git = Git.open('.')
46+
assert_equal(1, git.log.size)
47+
assert_nothing_raised { git.pull }
48+
assert_equal(2, git.log.size)
49+
end
50+
end
51+
end
52+
53+
test 'pull with one arg should use arg as remote and the current branch name' do
54+
in_temp_dir do |path|
55+
Dir.mkdir('remote')
56+
57+
Dir.chdir('remote') do
58+
`git init --initial-branch=branch1`
59+
File.write('README.md', 'Line 1')
60+
`git add README.md`
61+
`git commit -m "Initial commit"`
62+
end
63+
64+
`git clone remote/.git local 2>&1`
65+
66+
Dir.chdir('remote') do
67+
File.open('README.md', 'a') { |f| f.write('Line 2') }
68+
`git add README.md`
69+
`git commit -m "Initial commit"`
70+
end
71+
72+
Dir.chdir('local') do
73+
git = Git.open('.')
74+
assert_equal(1, git.log.size)
75+
assert_nothing_raised { git.pull('origin') }
76+
assert_equal(2, git.log.size)
77+
end
78+
end
79+
end
80+
81+
test 'pull with both remote and branch should use both' do
82+
in_temp_dir do |path|
83+
Dir.mkdir('remote')
84+
85+
Dir.chdir('remote') do
86+
`git init --initial-branch=master`
87+
File.write('README.md', 'Line 1')
88+
`git add README.md`
89+
`git commit -m "Initial commit"`
90+
end
91+
92+
`git clone remote/.git local 2>&1`
93+
94+
Dir.chdir('remote') do
95+
`git checkout -b feature1 2>&1`
96+
File.write('feature1.md', 'Line 1')
97+
`git add feature1.md`
98+
`git commit -m "Implement feature 1"`
99+
File.open('feature1.md', 'a') { |f| f.write('Line 2') }
100+
`git add feature1.md`
101+
`git commit -m "Implement feature 1, line 2"`
102+
end
103+
104+
Dir.chdir('local') do
105+
git = Git.open('.')
106+
assert_equal(1, git.log.size)
107+
assert_nothing_raised { git.pull('origin', 'feature1') }
108+
assert_equal(3, git.log.size)
109+
end
110+
end
111+
end
112+
end

0 commit comments

Comments
 (0)