Skip to content

Adding in the 'ls_remote' command #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
ls_remote: cleaned up code, added support for commands other than tag…
…s, added test
  • Loading branch information
iphands committed Feb 7, 2014
commit d46e206a64e6b7643cc2b05366bd1cd9de0036bf
16 changes: 9 additions & 7 deletions lib/git/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -507,16 +507,18 @@ def update_ref(branch, commit)

def ls_remote(location = nil, opts = {})
tmp = self.lib.ls_remote(location, opts)
if (opts[:tags])
ret = []
tmp.each { |line|
split = line.split
ret = []
tmp.each { |line|
split = line.split
if (opts[:tags])
short_name = split[1].sub(/refs\/tags\//, '')
obj = {:sha => split[0], :tag_ref => split[1], :name => short_name}
ret.push obj
}
else
obj = {:sha => split[0], :ref => split[1]}
end
ret.push obj
return ret
end
}
end

def ls_files(location=nil)
Expand Down
4 changes: 3 additions & 1 deletion lib/git/lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,9 @@ def diff_index(treeish)

def ls_remote(location = nil, opts = {})
args = []
args << (opts[:tags] ? '--tags' : '')
if (opts != {})
args << (opts[:tags] ? '--tags' : '')
end
args << location
command_lines('ls-remote', args)
end
Expand Down
2 changes: 1 addition & 1 deletion tests/files/working.git/refs/heads/master
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5e392652a881999392c2757cf9b783c5d47b67f7
9f75a00e575356b1f85247ca33a1a751cd1325a5
58 changes: 53 additions & 5 deletions tests/units/test_ls_remote.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,64 @@ def setup
set_file_paths
end

def test_remote_ls
file_name = "test_remote_ls_tags.file"
in_temp_dir do |path|
r1 = Git.clone(@wbare, 'repo1')
r2 = Git.init('repo2', :bare => true)

# setup
r1.add_remote('r2', r2)
create_file('repo1/' + file_name, 'content tets_file_1')
r1.add(file_name)
r1.commit_all('test_add/test_remote_ls_tags')
r1.push('origin', 'master')
r1.push('r2', 'master')

# add a second file
file_name += '2'
create_file('repo1/' + file_name, 'content tets_file_1')
r1.add(file_name)
r1.commit_all('test_add/test_remote_ls_tags')
r1.push('origin', 'master')

# this is the sha for the second commit
test_file_sha = r1.log(1)[0].sha

refs = r1.ls_remote('r2')
refs.each { |r|
assert_false r.has_value? test_file_sha
}

r1.push('r2', 'master')
refs = r1.ls_remote('r2')

found = false
refs.each { |r|
# the r2 HEAD must point at the local sha captured
if r[:ref] == "HEAD"
assert_equal test_file_sha, r[:sha]
found = true
end
}
assert (found === true), "Expected sha was not found in remote!"
end
end

def test_remote_ls_tags
in_temp_dir do |path|
r1 = Git.clone(@wbare, 'repo1')
r2 = Git.clone(@wbare, 'repo2')
r2 = Git.init('repo2', :bare => true)

# setup
tag_name = 'foo_bar_bazz'
r1.add_remote('r2', r2)
r1.add_tag('foo_bar_baz')
r1.add_tag(tag_name)
test_sha = r1.log(1)[0].sha

tags = r1.ls_remote('r2', {:tags => true})
tags.each { |t|
assert_false t.has_value? 'foo_bar_baz'
assert_false t.has_value? tag_name
}

r1.push('origin', 'master', {:tags => true})
Expand All @@ -27,11 +73,13 @@ def test_remote_ls_tags

found = false
tags.each { |t|
if t.has_value? 'foo_bar_baz'
if t[:sha] == test_sha
assert_equal t[:name], tag_name
assert_equal t[:tag_ref], "refs/tags/#{tag_name}"
found = true
end
}
assert found === true
assert (found === true), "Tag was not found in remote!"
end
end
end