Skip to content

Commit d507a51

Browse files
committed
Allow users to provide '--refs' to 'ls-remote'
Signed-off-by: Borislav Stanimirov <b.stanimirov@abv.bg>
1 parent 55424e5 commit d507a51

File tree

3 files changed

+39
-27
lines changed

3 files changed

+39
-27
lines changed

lib/git.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,13 @@ def self.init(working_dir = '.', options = {})
146146
# returns a Hash containing information about the references
147147
# of the target repository
148148
#
149+
# options
150+
# :refs
151+
#
149152
# @param [String|NilClass] location the target repository location or nil for '.'
150153
# @return [{String=>Hash}] the available references of the target repo.
151-
def self.ls_remote(location=nil)
152-
Git::Lib.new.ls_remote(location)
154+
def self.ls_remote(location = nil, options = {})
155+
Git::Lib.new.ls_remote(location, options)
153156
end
154157

155158
# open an existing git working directory

lib/git/lib.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,10 +448,13 @@ def ls_files(location=nil)
448448
hsh
449449
end
450450

451-
def ls_remote(location=nil)
452-
location ||= '.'
451+
def ls_remote(location=nil, opts={})
452+
arr_opts = []
453+
arr_opts << ['--refs'] if opts[:refs]
454+
arr_opts << (location || '.')
455+
453456
Hash.new{ |h,k| h[k] = {} }.tap do |hsh|
454-
command_lines('ls-remote', location).each do |line|
457+
command_lines('ls-remote', arr_opts).each do |line|
455458
(sha, info) = line.split("\t")
456459
(ref, type, name) = info.split('/', 3)
457460
type ||= 'head'

tests/units/test_lib.rb

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,17 @@ def test_log_commits
9090
a = @lib.log_commits :count => 10
9191
assert(a.first.is_a?(String))
9292
assert_equal(10, a.size)
93-
93+
9494
a = @lib.log_commits :count => 20, :since => "#{Date.today.year - 2006} years ago"
9595
assert(a.first.is_a?(String))
9696
assert_equal(20, a.size)
97-
97+
9898
a = @lib.log_commits :count => 20, :since => '1 second ago'
9999
assert_equal(0, a.size)
100-
100+
101101
a = @lib.log_commits :count => 20, :between => ['v2.5', 'v2.6']
102102
assert_equal(2, a.size)
103-
103+
104104
a = @lib.log_commits :count => 20, :path_limiter => 'ex_dir/'
105105
assert_equal(1, a.size)
106106

@@ -150,58 +150,58 @@ def test_revparse
150150
assert_equal('94c827875e2cadb8bc8d4cdd900f19aa9e8634c7', @lib.revparse('1cc8667014381^{tree}')) #tree
151151
assert_equal('ba492c62b6227d7f3507b4dcc6e6d5f13790eabf', @lib.revparse('v2.5:example.txt')) #blob
152152
end
153-
153+
154154
def test_object_type
155155
assert_equal('commit', @lib.object_type('1cc8667014381')) # commit
156156
assert_equal('tree', @lib.object_type('1cc8667014381^{tree}')) #tree
157157
assert_equal('blob', @lib.object_type('v2.5:example.txt')) #blob
158158
assert_equal('commit', @lib.object_type('v2.5'))
159159
end
160-
160+
161161
def test_object_size
162162
assert_equal(265, @lib.object_size('1cc8667014381')) # commit
163163
assert_equal(72, @lib.object_size('1cc8667014381^{tree}')) #tree
164164
assert_equal(128, @lib.object_size('v2.5:example.txt')) #blob
165165
assert_equal(265, @lib.object_size('v2.5'))
166166
end
167-
167+
168168
def test_object_contents
169169
commit = "tree 94c827875e2cadb8bc8d4cdd900f19aa9e8634c7\n"
170170
commit << "parent 546bec6f8872efa41d5d97a369f669165ecda0de\n"
171171
commit << "author scott Chacon <schacon@agadorsparticus.corp.reactrix.com> 1194561188 -0800\n"
172172
commit << "committer scott Chacon <schacon@agadorsparticus.corp.reactrix.com> 1194561188 -0800\n"
173173
commit << "\ntest"
174174
assert_equal(commit, @lib.object_contents('1cc8667014381')) # commit
175-
175+
176176
tree = "040000 tree 6b790ddc5eab30f18cabdd0513e8f8dac0d2d3ed\tex_dir\n"
177177
tree << "100644 blob 3aac4b445017a8fc07502670ec2dbf744213dd48\texample.txt"
178178
assert_equal(tree, @lib.object_contents('1cc8667014381^{tree}')) #tree
179-
179+
180180
blob = "1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n2"
181181
assert_equal(blob, @lib.object_contents('v2.5:example.txt')) #blob
182-
182+
183183
end
184-
184+
185185
def test_object_contents_with_block
186186
commit = "tree 94c827875e2cadb8bc8d4cdd900f19aa9e8634c7\n"
187187
commit << "parent 546bec6f8872efa41d5d97a369f669165ecda0de\n"
188188
commit << "author scott Chacon <schacon@agadorsparticus.corp.reactrix.com> 1194561188 -0800\n"
189189
commit << "committer scott Chacon <schacon@agadorsparticus.corp.reactrix.com> 1194561188 -0800\n"
190190
commit << "\ntest"
191-
191+
192192
@lib.object_contents('1cc8667014381') do |f|
193193
assert_equal(commit, f.read.chomp)
194194
end
195-
195+
196196
# commit
197-
197+
198198
tree = "040000 tree 6b790ddc5eab30f18cabdd0513e8f8dac0d2d3ed\tex_dir\n"
199199
tree << "100644 blob 3aac4b445017a8fc07502670ec2dbf744213dd48\texample.txt"
200200

201201
@lib.object_contents('1cc8667014381^{tree}') do |f|
202202
assert_equal(tree, f.read.chomp) #tree
203203
end
204-
204+
205205
blob = "1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n1\n2"
206206

207207
@lib.object_contents('v2.5:example.txt') do |f|
@@ -224,8 +224,8 @@ def test_config_remote
224224
assert_equal('../working.git', config['url'])
225225
assert_equal('+refs/heads/*:refs/remotes/working/*', config['fetch'])
226226
end
227-
228-
227+
228+
229229
def test_ls_tree
230230
tree = @lib.ls_tree('94c827875e2cadb8bc8d4cdd900f19aa9e8634c7')
231231
assert_equal("3aac4b445017a8fc07502670ec2dbf744213dd48", tree['blob']['example.txt'][:sha])
@@ -247,6 +247,12 @@ def test_ls_remote
247247
assert_equal("HEAD", ls['head'][:ref])
248248
assert_equal("5e392652a881999392c2757cf9b783c5d47b67f7", ls['head'][:sha])
249249
assert_equal(nil, ls['head'][:name])
250+
251+
ls = lib.ls_remote(@wbare, :refs => true)
252+
253+
assert_equal({}, ls['head']) # head is not a ref
254+
assert_equal(%w( gitsearch1 v2.5 v2.6 v2.7 v2.8 ), ls['tags'].keys.sort)
255+
assert_equal(%w( git_grep master test test_branches test_object ), ls['branches'].keys.sort)
250256
end
251257
end
252258

@@ -261,28 +267,28 @@ def test_grep
261267
assert_equal('to search one', match['gitsearch1:scott/text.txt'].assoc(6)[1])
262268
assert_equal(2, match['gitsearch1:scott/text.txt'].size)
263269
assert_equal(2, match.size)
264-
270+
265271
match = @lib.grep('search', :object => 'gitsearch1', :path_limiter => 'scott/new*')
266272
assert_equal("you can't search me!", match["gitsearch1:scott/newfile"].first[1])
267273
assert_equal(1, match.size)
268274

269275
match = @lib.grep('SEARCH', :object => 'gitsearch1')
270276
assert_equal(0, match.size)
271-
277+
272278
match = @lib.grep('SEARCH', :object => 'gitsearch1', :ignore_case => true)
273279
assert_equal("you can't search me!", match["gitsearch1:scott/newfile"].first[1])
274280
assert_equal(2, match.size)
275-
281+
276282
match = @lib.grep('search', :object => 'gitsearch1', :invert_match => true)
277283
assert_equal(6, match['gitsearch1:scott/text.txt'].size)
278284
assert_equal(2, match.size)
279285
end
280-
286+
281287
def test_show
282288
assert(/^commit 5e53019b3238362144c2766f02a2c00d91fcc023.+\+replace with new text - diff test$/m.match(@lib.show))
283289
assert(/^commit 935badc874edd62a8629aaf103418092c73f0a56.+\+nothing!$/m.match(@lib.show('gitsearch1')))
284290
assert(/^hello.+nothing!$/m.match(@lib.show('gitsearch1', 'scott/text.txt')))
285291
assert(@lib.show('gitsearch1', 'scott/text.txt') == "hello\nthis is\na file\nthat is\nput here\nto search one\nto search two\nnothing!\n")
286292
end
287-
293+
288294
end

0 commit comments

Comments
 (0)