| 1 |
module Grit |
| 2 |
|
| 3 |
class Tree |
| 4 |
lazy_reader :contents |
| 5 |
attr_reader :id |
| 6 |
attr_reader :mode |
| 7 |
attr_reader :name |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
def self.construct(repo, treeish, paths = []) |
| 16 |
output = repo.git.ls_tree({}, treeish, *paths) |
| 17 |
|
| 18 |
self.allocate.construct_initialize(repo, treeish, output) |
| 19 |
end |
| 20 |
|
| 21 |
def construct_initialize(repo, id, text) |
| 22 |
@repo = repo |
| 23 |
@id = id |
| 24 |
@contents = [] |
| 25 |
|
| 26 |
text.split("\n").each do |line| |
| 27 |
@contents << content_from_string(repo, line) |
| 28 |
end |
| 29 |
@contents.compact! |
| 30 |
|
| 31 |
self |
| 32 |
end |
| 33 |
|
| 34 |
def lazy_source |
| 35 |
Tree.construct(@repo, @id, []) |
| 36 |
end |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
def self.create(repo, atts) |
| 44 |
self.allocate.create_initialize(repo, atts) |
| 45 |
end |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
def create_initialize(repo, atts) |
| 53 |
@repo = repo |
| 54 |
|
| 55 |
atts.each do |k, v| |
| 56 |
instance_variable_set("@#{k}", v) |
| 57 |
end |
| 58 |
self |
| 59 |
end |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
def content_from_string(repo, text) |
| 67 |
mode, type, id, name = text.split(" ", 4) |
| 68 |
case type |
| 69 |
when "tree" |
| 70 |
Tree.create(repo, :id => id, :mode => mode, :name => name) |
| 71 |
when "blob" |
| 72 |
Blob.create(repo, :id => id, :mode => mode, :name => name) |
| 73 |
when "commit" |
| 74 |
nil |
| 75 |
else |
| 76 |
raise "Invalid type: #{type}" |
| 77 |
end |
| 78 |
end |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
def /(file) |
| 90 |
self.contents.select { |c| c.name == file }.first |
| 91 |
end |
| 92 |
|
| 93 |
def basename |
| 94 |
File.basename(@name) |
| 95 |
end |
| 96 |
|
| 97 |
|
| 98 |
def inspect |
| 99 |
%Q{#<Grit::Tree "#{@id}">} |
| 100 |
end |
| 101 |
end |
| 102 |
|
| 103 |
end |