From ab454510d68142c178dd04d4cfe4706c954cd11e Mon Sep 17 00:00:00 2001 From: Ian Page Hands Date: Thu, 6 Feb 2014 17:56:27 -0500 Subject: [PATCH 1/2] Adding in the 'ls_remote' command --- lib/git/base.rb | 17 ++++++++++++++-- lib/git/lib.rb | 9 ++++++++- tests/units/test_ls_remote.rb | 37 +++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 tests/units/test_ls_remote.rb diff --git a/lib/git/base.rb b/lib/git/base.rb index bcec10e6..8af679da 100644 --- a/lib/git/base.rb +++ b/lib/git/base.rb @@ -504,8 +504,21 @@ def write_and_commit_tree(opts = {}) def update_ref(branch, commit) branch(branch).update_ref(commit) end - - + + def ls_remote(location = nil, opts = {}) + tmp = self.lib.ls_remote(location, opts) + if (opts[:tags]) + ret = [] + tmp.each { |line| + split = line.split + short_name = split[1].sub(/refs\/tags\//, '') + obj = {:sha => split[0], :tag_ref => split[1], :name => short_name} + ret.push obj + } + return ret + end + end + def ls_files(location=nil) self.lib.ls_files(location) end diff --git a/lib/git/lib.rb b/lib/git/lib.rb index 975bc148..d092e0c5 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -305,7 +305,14 @@ def diff_files def diff_index(treeish) diff_as_hash('diff-index', treeish) end - + + def ls_remote(location = nil, opts = {}) + args = [] + args << (opts[:tags] ? '--tags' : '') + args << location + command_lines('ls-remote', args) + end + def ls_files(location=nil) hsh = {} command_lines('ls-files', ['--stage', location]).each do |line| diff --git a/tests/units/test_ls_remote.rb b/tests/units/test_ls_remote.rb new file mode 100644 index 00000000..20ff9767 --- /dev/null +++ b/tests/units/test_ls_remote.rb @@ -0,0 +1,37 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../test_helper' + +class TestRemoteLs < Test::Unit::TestCase + def setup + set_file_paths + end + + def test_remote_ls_tags + in_temp_dir do |path| + r1 = Git.clone(@wbare, 'repo1') + r2 = Git.clone(@wbare, 'repo2') + + # setup + r1.add_remote('r2', r2) + r1.add_tag('foo_bar_baz') + + tags = r1.ls_remote('r2', {:tags => true}) + tags.each { |t| + assert_false t.has_value? 'foo_bar_baz' + } + + r1.push('origin', 'master', {:tags => true}) + r1.push('r2', 'master', {:tags => true}) + tags = r1.ls_remote('r2', {:tags => true}) + + found = false + tags.each { |t| + if t.has_value? 'foo_bar_baz' + found = true + end + } + assert found === true + end + end +end From d46e206a64e6b7643cc2b05366bd1cd9de0036bf Mon Sep 17 00:00:00 2001 From: Ian Page Hands Date: Fri, 7 Feb 2014 10:37:15 -0500 Subject: [PATCH 2/2] ls_remote: cleaned up code, added support for commands other than tags, added test --- lib/git/base.rb | 16 ++++--- lib/git/lib.rb | 4 +- tests/files/working.git/refs/heads/master | 2 +- tests/units/test_ls_remote.rb | 58 +++++++++++++++++++++-- 4 files changed, 66 insertions(+), 14 deletions(-) diff --git a/lib/git/base.rb b/lib/git/base.rb index 8af679da..4f961441 100644 --- a/lib/git/base.rb +++ b/lib/git/base.rb @@ -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) diff --git a/lib/git/lib.rb b/lib/git/lib.rb index d092e0c5..1f200fae 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -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 diff --git a/tests/files/working.git/refs/heads/master b/tests/files/working.git/refs/heads/master index 6f2e7bdb..a92a141b 100644 --- a/tests/files/working.git/refs/heads/master +++ b/tests/files/working.git/refs/heads/master @@ -1 +1 @@ -5e392652a881999392c2757cf9b783c5d47b67f7 +9f75a00e575356b1f85247ca33a1a751cd1325a5 diff --git a/tests/units/test_ls_remote.rb b/tests/units/test_ls_remote.rb index 20ff9767..1d1e6dc6 100644 --- a/tests/units/test_ls_remote.rb +++ b/tests/units/test_ls_remote.rb @@ -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}) @@ -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