Skip to content

Commit 0777848

Browse files
committed
Added git ls-remote command to Git::Lib
1 parent b79de3a commit 0777848

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

lib/git/lib.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,18 @@ def ls_files(location=nil)
304304
hsh
305305
end
306306

307+
def ls_remote(location=nil)
308+
Hash.new{ |h,k| h[k] = {} }.tap do |hsh|
309+
command_lines('ls-remote', [location], false).each do |line|
310+
(sha, info) = line.split("\t")
311+
(ref, type, name) = info.split('/', 3)
312+
type ||= 'head'
313+
type = 'branches' if type == 'heads'
314+
value = {:ref => ref, :sha => sha}
315+
hsh[type].update( name.nil? ? value : { name => value })
316+
end
317+
end
318+
end
307319

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

tests/units/test_lib.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,23 @@ def test_ls_tree
137137
assert(tree['tree'])
138138
end
139139

140+
def test_ls_remote
141+
in_temp_dir do |path|
142+
lib = Git::Lib.new
143+
ls = lib.ls_remote(@wbare)
144+
145+
assert_equal(%w( gitsearch1 v2.5 v2.6 v2.7 v2.8 ), ls['tags'].keys)
146+
assert_equal("935badc874edd62a8629aaf103418092c73f0a56", ls['tags']['gitsearch1'][:sha])
147+
148+
assert_equal(%w( git_grep master test test_branches test_object ), ls['branches'].keys)
149+
assert_equal("5e392652a881999392c2757cf9b783c5d47b67f7", ls['branches']['master'][:sha])
150+
151+
assert_equal("HEAD", ls['head'][:ref])
152+
assert_equal("5e392652a881999392c2757cf9b783c5d47b67f7", ls['head'][:sha])
153+
assert_equal(nil, ls['head'][:name])
154+
end
155+
end
156+
140157

141158
# options this will accept
142159
# :treeish
@@ -165,4 +182,4 @@ def test_grep
165182
assert_equal(2, match.size)
166183
end
167184

168-
end
185+
end

0 commit comments

Comments
 (0)