Skip to content

Commit

Permalink
Allow users to provide '--refs' to 'ls-remote' (#494)
Browse files Browse the repository at this point in the history
Signed-off-by: Borislav Stanimirov <b.stanimirov@abv.bg>
  • Loading branch information
iboB committed Nov 27, 2020
1 parent 55424e5 commit f9abb18
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 27 deletions.
7 changes: 5 additions & 2 deletions lib/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,13 @@ def self.init(working_dir = '.', options = {})
# returns a Hash containing information about the references
# of the target repository
#
# options
# :refs
#
# @param [String|NilClass] location the target repository location or nil for '.'
# @return [{String=>Hash}] the available references of the target repo.
def self.ls_remote(location=nil)
Git::Lib.new.ls_remote(location)
def self.ls_remote(location = nil, options = {})
Git::Lib.new.ls_remote(location, options)
end

# open an existing git working directory
Expand Down
9 changes: 6 additions & 3 deletions lib/git/lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -448,10 +448,13 @@ def ls_files(location=nil)
hsh
end

def ls_remote(location=nil)
location ||= '.'
def ls_remote(location=nil, opts={})
arr_opts = []
arr_opts << ['--refs'] if opts[:refs]
arr_opts << (location || '.')

Hash.new{ |h,k| h[k] = {} }.tap do |hsh|
command_lines('ls-remote', location).each do |line|
command_lines('ls-remote', arr_opts).each do |line|
(sha, info) = line.split("\t")
(ref, type, name) = info.split('/', 3)
type ||= 'head'
Expand Down
50 changes: 28 additions & 22 deletions tests/units/test_lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,17 @@ def test_log_commits
a = @lib.log_commits :count => 10
assert(a.first.is_a?(String))
assert_equal(10, a.size)

a = @lib.log_commits :count => 20, :since => "#{Date.today.year - 2006} years ago"
assert(a.first.is_a?(String))
assert_equal(20, a.size)

a = @lib.log_commits :count => 20, :since => '1 second ago'
assert_equal(0, a.size)

a = @lib.log_commits :count => 20, :between => ['v2.5', 'v2.6']
assert_equal(2, a.size)

a = @lib.log_commits :count => 20, :path_limiter => 'ex_dir/'
assert_equal(1, a.size)

Expand Down Expand Up @@ -150,58 +150,58 @@ def test_revparse
assert_equal('94c827875e2cadb8bc8d4cdd900f19aa9e8634c7', @lib.revparse('1cc8667014381^{tree}')) #tree
assert_equal('ba492c62b6227d7f3507b4dcc6e6d5f13790eabf', @lib.revparse('v2.5:example.txt')) #blob
end

def test_object_type
assert_equal('commit', @lib.object_type('1cc8667014381')) # commit
assert_equal('tree', @lib.object_type('1cc8667014381^{tree}')) #tree
assert_equal('blob', @lib.object_type('v2.5:example.txt')) #blob
assert_equal('commit', @lib.object_type('v2.5'))
end

def test_object_size
assert_equal(265, @lib.object_size('1cc8667014381')) # commit
assert_equal(72, @lib.object_size('1cc8667014381^{tree}')) #tree
assert_equal(128, @lib.object_size('v2.5:example.txt')) #blob
assert_equal(265, @lib.object_size('v2.5'))
end

def test_object_contents
commit = "tree 94c827875e2cadb8bc8d4cdd900f19aa9e8634c7\n"
commit << "parent 546bec6f8872efa41d5d97a369f669165ecda0de\n"
commit << "author scott Chacon <schacon@agadorsparticus.corp.reactrix.com> 1194561188 -0800\n"
commit << "committer scott Chacon <schacon@agadorsparticus.corp.reactrix.com> 1194561188 -0800\n"
commit << "\ntest"
assert_equal(commit, @lib.object_contents('1cc8667014381')) # commit

tree = "040000 tree 6b790ddc5eab30f18cabdd0513e8f8dac0d2d3ed\tex_dir\n"
tree << "100644 blob 3aac4b445017a8fc07502670ec2dbf744213dd48\texample.txt"
assert_equal(tree, @lib.object_contents('1cc8667014381^{tree}')) #tree

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"
assert_equal(blob, @lib.object_contents('v2.5:example.txt')) #blob

end

def test_object_contents_with_block
commit = "tree 94c827875e2cadb8bc8d4cdd900f19aa9e8634c7\n"
commit << "parent 546bec6f8872efa41d5d97a369f669165ecda0de\n"
commit << "author scott Chacon <schacon@agadorsparticus.corp.reactrix.com> 1194561188 -0800\n"
commit << "committer scott Chacon <schacon@agadorsparticus.corp.reactrix.com> 1194561188 -0800\n"
commit << "\ntest"

@lib.object_contents('1cc8667014381') do |f|
assert_equal(commit, f.read.chomp)
end

# commit

tree = "040000 tree 6b790ddc5eab30f18cabdd0513e8f8dac0d2d3ed\tex_dir\n"
tree << "100644 blob 3aac4b445017a8fc07502670ec2dbf744213dd48\texample.txt"

@lib.object_contents('1cc8667014381^{tree}') do |f|
assert_equal(tree, f.read.chomp) #tree
end

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"

@lib.object_contents('v2.5:example.txt') do |f|
Expand All @@ -224,8 +224,8 @@ def test_config_remote
assert_equal('../working.git', config['url'])
assert_equal('+refs/heads/*:refs/remotes/working/*', config['fetch'])
end


def test_ls_tree
tree = @lib.ls_tree('94c827875e2cadb8bc8d4cdd900f19aa9e8634c7')
assert_equal("3aac4b445017a8fc07502670ec2dbf744213dd48", tree['blob']['example.txt'][:sha])
Expand All @@ -247,6 +247,12 @@ def test_ls_remote
assert_equal("HEAD", ls['head'][:ref])
assert_equal("5e392652a881999392c2757cf9b783c5d47b67f7", ls['head'][:sha])
assert_equal(nil, ls['head'][:name])

ls = lib.ls_remote(@wbare, :refs => true)

assert_equal({}, ls['head']) # head is not a ref
assert_equal(%w( gitsearch1 v2.5 v2.6 v2.7 v2.8 ), ls['tags'].keys.sort)
assert_equal(%w( git_grep master test test_branches test_object ), ls['branches'].keys.sort)
end
end

Expand All @@ -261,28 +267,28 @@ def test_grep
assert_equal('to search one', match['gitsearch1:scott/text.txt'].assoc(6)[1])
assert_equal(2, match['gitsearch1:scott/text.txt'].size)
assert_equal(2, match.size)

match = @lib.grep('search', :object => 'gitsearch1', :path_limiter => 'scott/new*')
assert_equal("you can't search me!", match["gitsearch1:scott/newfile"].first[1])
assert_equal(1, match.size)

match = @lib.grep('SEARCH', :object => 'gitsearch1')
assert_equal(0, match.size)

match = @lib.grep('SEARCH', :object => 'gitsearch1', :ignore_case => true)
assert_equal("you can't search me!", match["gitsearch1:scott/newfile"].first[1])
assert_equal(2, match.size)

match = @lib.grep('search', :object => 'gitsearch1', :invert_match => true)
assert_equal(6, match['gitsearch1:scott/text.txt'].size)
assert_equal(2, match.size)
end

def test_show
assert(/^commit 5e53019b3238362144c2766f02a2c00d91fcc023.+\+replace with new text - diff test$/m.match(@lib.show))
assert(/^commit 935badc874edd62a8629aaf103418092c73f0a56.+\+nothing!$/m.match(@lib.show('gitsearch1')))
assert(/^hello.+nothing!$/m.match(@lib.show('gitsearch1', 'scott/text.txt')))
assert(@lib.show('gitsearch1', 'scott/text.txt') == "hello\nthis is\na file\nthat is\nput here\nto search one\nto search two\nnothing!\n")
end

end

0 comments on commit f9abb18

Please sign in to comment.