diff --git a/lib/git/lib.rb b/lib/git/lib.rb index 52fb2e6c..c93ac329 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -304,6 +304,18 @@ def ls_files(location=nil) hsh end + def ls_remote(location=nil) + Hash.new{ |h,k| h[k] = {} }.tap do |hsh| + command_lines('ls-remote', [location], false).each do |line| + (sha, info) = line.split("\t") + (ref, type, name) = info.split('/', 3) + type ||= 'head' + type = 'branches' if type == 'heads' + value = {:ref => ref, :sha => sha} + hsh[type].update( name.nil? ? value : { name => value }) + end + end + end def ignored_files command_lines('ls-files', ['--others', '-i', '--exclude-standard']) diff --git a/tests/units/test_lib.rb b/tests/units/test_lib.rb index b2b4b499..57b36152 100644 --- a/tests/units/test_lib.rb +++ b/tests/units/test_lib.rb @@ -137,6 +137,23 @@ def test_ls_tree assert(tree['tree']) end + def test_ls_remote + in_temp_dir do |path| + lib = Git::Lib.new + ls = lib.ls_remote(@wbare) + + assert_equal(%w( gitsearch1 v2.5 v2.6 v2.7 v2.8 ), ls['tags'].keys) + assert_equal("935badc874edd62a8629aaf103418092c73f0a56", ls['tags']['gitsearch1'][:sha]) + + assert_equal(%w( git_grep master test test_branches test_object ), ls['branches'].keys) + assert_equal("5e392652a881999392c2757cf9b783c5d47b67f7", ls['branches']['master'][:sha]) + + assert_equal("HEAD", ls['head'][:ref]) + assert_equal("5e392652a881999392c2757cf9b783c5d47b67f7", ls['head'][:sha]) + assert_equal(nil, ls['head'][:name]) + end + end + # options this will accept # :treeish @@ -165,4 +182,4 @@ def test_grep assert_equal(2, match.size) end -end \ No newline at end of file +end