Skip to content

Commit e36a41b

Browse files
Merge pull request ruby-git#37 from JangoSteve/ls_remote
Added git ls-remote command to Git::Lib
2 parents bf61510 + 0777848 commit e36a41b

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lib/git/lib.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,18 @@ def ls_files(location=nil)
367367
hsh
368368
end
369369

370+
def ls_remote(location=nil)
371+
Hash.new{ |h,k| h[k] = {} }.tap do |hsh|
372+
command_lines('ls-remote', [location], false).each do |line|
373+
(sha, info) = line.split("\t")
374+
(ref, type, name) = info.split('/', 3)
375+
type ||= 'head'
376+
type = 'branches' if type == 'heads'
377+
value = {:ref => ref, :sha => sha}
378+
hsh[type].update( name.nil? ? value : { name => value })
379+
end
380+
end
381+
end
370382

371383
def ignored_files
372384
command_lines('ls-files', ['--others', '-i', '--exclude-standard'])

tests/units/test_lib.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,23 @@ def test_ls_tree
149149
assert(tree['tree'])
150150
end
151151

152+
def test_ls_remote
153+
in_temp_dir do |path|
154+
lib = Git::Lib.new
155+
ls = lib.ls_remote(@wbare)
156+
157+
assert_equal(%w( gitsearch1 v2.5 v2.6 v2.7 v2.8 ), ls['tags'].keys)
158+
assert_equal("935badc874edd62a8629aaf103418092c73f0a56", ls['tags']['gitsearch1'][:sha])
159+
160+
assert_equal(%w( git_grep master test test_branches test_object ), ls['branches'].keys)
161+
assert_equal("5e392652a881999392c2757cf9b783c5d47b67f7", ls['branches']['master'][:sha])
162+
163+
assert_equal("HEAD", ls['head'][:ref])
164+
assert_equal("5e392652a881999392c2757cf9b783c5d47b67f7", ls['head'][:sha])
165+
assert_equal(nil, ls['head'][:name])
166+
end
167+
end
168+
152169

153170
# options this will accept
154171
# :treeish

0 commit comments

Comments
 (0)