Skip to content

Commit 7d8848c

Browse files
authored
Remote#branch and #merge should default to current branch instead of "master" (#639)
Signed-off-by: James Couball <jcouball@yahoo.com>
1 parent 3dda040 commit 7d8848c

File tree

2 files changed

+64
-13
lines changed

2 files changed

+64
-13
lines changed

lib/git/remote.rb

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,38 @@
11
module Git
22
class Remote < Path
3-
3+
44
attr_accessor :name, :url, :fetch_opts
5-
5+
66
def initialize(base, name)
77
@base = base
88
config = @base.lib.config_remote(name)
99
@name = name
1010
@url = config['url']
1111
@fetch_opts = config['fetch']
1212
end
13-
13+
1414
def fetch(opts={})
1515
@base.fetch(@name, opts)
1616
end
17-
17+
1818
# merge this remote locally
19-
def merge(branch = 'master')
20-
@base.merge("#{@name}/#{branch}")
19+
def merge(branch = @base.current_branch)
20+
remote_tracking_branch = "#{@name}/#{branch}"
21+
@base.merge(remote_tracking_branch)
2122
end
22-
23-
def branch(branch = 'master')
24-
Git::Branch.new(@base, "#{@name}/#{branch}")
23+
24+
def branch(branch = @base.current_branch)
25+
remote_tracking_branch = "#{@name}/#{branch}"
26+
Git::Branch.new(@base, remote_tracking_branch)
2527
end
26-
28+
2729
def remove
28-
@base.lib.remote_remove(@name)
30+
@base.lib.remote_remove(@name)
2931
end
30-
32+
3133
def to_s
3234
@name
3335
end
34-
36+
3537
end
3638
end

tests/units/test_remotes.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,53 @@ def test_push
232232
assert(rem.tag('test-tag'))
233233
end
234234
end
235+
236+
test 'Remote#branch with no args' do
237+
in_temp_dir do
238+
Dir.mkdir 'git'
239+
Git.init('git', initial_branch: 'first', bare: true)
240+
r1 = Git.clone('git', 'r1')
241+
File.write('r1/file1.txt', 'hello world')
242+
r1.add('file1.txt')
243+
r1.commit('first commit')
244+
r1.push
245+
246+
r2 = Git.clone('git', 'r2')
247+
248+
File.write('r1/file2.txt', 'hello world')
249+
r1.add('file2.txt')
250+
r1.commit('second commit')
251+
r1.push
252+
253+
branch = r2.remote('origin').branch
254+
255+
assert_equal('origin/first', branch.full)
256+
end
257+
end
258+
259+
test 'Remote#merge with no args' do
260+
in_temp_dir do
261+
Dir.mkdir 'git'
262+
Git.init('git', initial_branch: 'first', bare: true)
263+
r1 = Git.clone('git', 'r1')
264+
File.write('r1/file1.txt', 'hello world')
265+
r1.add('file1.txt')
266+
r1.commit('first commit')
267+
r1.push
268+
269+
r2 = Git.clone('git', 'r2')
270+
271+
File.write('r1/file2.txt', 'hello world')
272+
r1.add('file2.txt')
273+
r1.commit('second commit')
274+
r1.push
275+
276+
remote = r2.remote('origin')
277+
278+
remote.fetch
279+
remote.merge
280+
281+
assert(File.exist?('r2/file2.txt'))
282+
end
283+
end
235284
end

0 commit comments

Comments
 (0)