summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorscott Chacon <schacon@agadorsparticus.corp.reactrix.com>2007-11-12 17:58:02 -0800
committerscott Chacon <schacon@agadorsparticus.corp.reactrix.com>2007-11-12 17:58:02 -0800
commitf590869eaa6a4c7f25a1df8eca171a119fcee7ed (patch)
treea946f572fc3c9f6cbaef60331a2c6ef7db2b86f1
parent852a0e63d294de874c3311f5e7edf40e2f2ecd60 (diff)
downloadthird_party-ruby-git-f590869eaa6a4c7f25a1df8eca171a119fcee7ed.zip
third_party-ruby-git-f590869eaa6a4c7f25a1df8eca171a119fcee7ed.tar.gz
third_party-ruby-git-f590869eaa6a4c7f25a1df8eca171a119fcee7ed.tar.xz
added the tree functions and tests
-rw-r--r--README4
-rw-r--r--lib/git/lib.rb10
-rw-r--r--lib/git/object.rb60
-rw-r--r--tests/test_helper.rb17
-rw-r--r--tests/units/test_object.rb7
5 files changed, 84 insertions, 14 deletions
diff --git a/README b/README
index 36fcba9..cc37f4c 100644
--- a/README
+++ b/README
@@ -67,6 +67,10 @@ Here are the operations that need read permission only.
commit.date.strftime("%m-%d-%y")
commit.message
+ tree = g.gtree("HEAD^{tree}")
+ tree.blobs
+ tree.subtrees
+ tree.children # blobs and subtrees
g.revparse('v2.5:Makefile')
diff --git a/lib/git/lib.rb b/lib/git/lib.rb
index 13a3b4c..051025c 100644
--- a/lib/git/lib.rb
+++ b/lib/git/lib.rb
@@ -109,6 +109,16 @@ module Git
command('cat-file', ['-p', sha])
end
+ def ls_tree(sha)
+ data = {'blob' => {}, 'tree' => {}}
+ command_lines('ls-tree', sha.to_s).each do |line|
+ (info, filenm) = line.split("\t")
+ (mode, type, sha) = info.split
+ data[type][filenm] = {:mode => mode, :sha => sha}
+ end
+ data
+ end
+
def branches_all
arr = []
command_lines('branch', '-a').each do |b|
diff --git a/lib/git/object.rb b/lib/git/object.rb
index 58ea7ab..4886f09 100644
--- a/lib/git/object.rb
+++ b/lib/git/object.rb
@@ -51,15 +51,61 @@ module Git
class Blob < AbstractObject
+
+ def initialize(base, sha, mode = nil)
+ super(base, sha)
+ @mode = mode
+ end
+
def setup
@type = 'blob'
end
end
class Tree < AbstractObject
+
+ @trees = nil
+ @blobs = nil
+
+ def initialize(base, sha, mode = nil)
+ super(base, sha)
+ @mode = mode
+ end
+
def setup
@type = 'tree'
end
+
+ def children
+ blobs.merge(subtrees)
+ end
+
+ def blobs
+ check_tree
+ @blobs
+ end
+ alias_method :files, :blobs
+
+ def trees
+ check_tree
+ @trees
+ end
+ alias_method :subtrees, :trees
+ alias_method :subdirectories, :trees
+
+ private
+
+ # actually run the git command
+ def check_tree
+ if !@trees
+ @trees = {}
+ @blobs = {}
+ data = @base.lib.ls_tree(@sha)
+ data['tree'].each { |k, d| @trees[k] = Tree.new(@base, d[:sha], d[:mode]) }
+ data['blob'].each { |k, d| @blobs[k] = Blob.new(@base, d[:sha], d[:mode]) }
+ end
+ end
+
end
class Commit < AbstractObject
@@ -123,12 +169,14 @@ module Git
# see if this object has been initialized and do so if not
def check_commit
- data = @base.lib.commit_data(@sha)
- @committer = Git::Author.new(data['committer'])
- @author = Git::Author.new(data['author'])
- @tree = Tree.new(@base, data['tree'])
- @parents = data['parent'].map{ |sha| Commit.new(@base, sha) }
- @message = data['message'].chomp
+ if !@tree
+ data = @base.lib.commit_data(@sha)
+ @committer = Git::Author.new(data['committer'])
+ @author = Git::Author.new(data['author'])
+ @tree = Tree.new(@base, data['tree'])
+ @parents = data['parent'].map{ |sha| Commit.new(@base, sha) }
+ @message = data['message'].chomp
+ end
end
end
diff --git a/tests/test_helper.rb b/tests/test_helper.rb
index 78328c9..9907ffe 100644
--- a/tests/test_helper.rb
+++ b/tests/test_helper.rb
@@ -22,17 +22,18 @@ class Test::Unit::TestCase
end
def teardown
- if @wdir
- FileUtils.rm_r(@wdir)
+ if @tmp_path
+ #puts "teardown #{@tmp_path}"
+ FileUtils.rm_r(@tmp_path)
end
end
def create_temp_repo(clone_path)
- filename = 'git_test' + Time.now.to_i.to_s + rand(300).to_s
- tmp_path = File.join("/tmp/", filename)
- FileUtils.mkdir_p(tmp_path)
- FileUtils.cp_r(clone_path, tmp_path)
- tmp_path = File.join(tmp_path, 'working')
+ filename = 'git_test' + Time.now.to_i.to_s + rand(300).to_s.rjust(3, '0')
+ @tmp_path = File.join("/tmp/", filename)
+ FileUtils.mkdir_p(@tmp_path)
+ FileUtils.cp_r(clone_path, @tmp_path)
+ tmp_path = File.join(@tmp_path, 'working')
Dir.chdir(tmp_path) do
FileUtils.mv('dot_git', '.git')
end
@@ -40,7 +41,7 @@ class Test::Unit::TestCase
end
def in_temp_dir(remove_after = true)
- filename = 'git_test' + Time.now.to_i.to_s + rand(300).to_s
+ filename = 'git_test' + Time.now.to_i.to_s + rand(300).to_s.rjust(3, '0')
tmp_path = File.join("/tmp/", filename)
FileUtils.mkdir(tmp_path)
Dir.chdir tmp_path do
diff --git a/tests/units/test_object.rb b/tests/units/test_object.rb
index c4a2de7..8b98e98 100644
--- a/tests/units/test_object.rb
+++ b/tests/units/test_object.rb
@@ -59,6 +59,13 @@ class TestObject < Test::Unit::TestCase
o = @git.object('1cc8667014381^{tree}')
assert(o.is_a?(Git::Object::Tree))
+ o = @git.object('v2.7^{tree}')
+
+ assert_equal(2, o.children.size)
+ assert_equal(1, o.blobs.size)
+ assert_equal(1, o.subtrees.size)
+ assert_equal(1, o.trees['ex_dir'].blobs.size)
+
o = @git.object('94c827875e2cadb8bc8d4cdd900f19aa9e8634c7')
assert(o.is_a?(Git::Object::Tree))
assert_equal('tree', o.type)