Class: Grit::Tree
- Inherits:
-
Object
- Object
- Grit::Tree
- Defined in:
- lib/grit/lib/grit/tree.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.construct(repo, treeish, paths = []) ⇒ Object
Construct the contents of the tree
repo
is the Repotreeish
is the referencepaths
is an optional Array of directory paths to restrict the tree. -
.create(repo, atts) ⇒ Object
Create an unbaked Tree containing just the specified attributes
repo
is the Repoatts
is a Hash of instance variable data.
Instance Method Summary collapse
-
#/(file) ⇒ Object
Find the named object in this tree’s contents.
-
#<=>(other) ⇒ Object
Compares trees by name.
- #basename ⇒ Object
-
#blobs ⇒ Object
Find only Blob objects from contents.
- #construct_initialize(repo, id, text) ⇒ Object
-
#content_from_string(repo, text) ⇒ Object
Parse a content item and create the appropriate object
repo
is the Repotext
is the single line containing the items data in ‘git ls-tree` format. -
#create_initialize(repo, atts) ⇒ Object
Initializer for Tree.create
repo
is the Repoatts
is a Hash of instance variable data. -
#inspect ⇒ Object
Pretty object inspection.
- #lazy_source ⇒ Object
-
#trees ⇒ Object
Find only Tree objects from contents.
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
5 6 7 |
# File 'lib/grit/lib/grit/tree.rb', line 5 def id @id end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
6 7 8 |
# File 'lib/grit/lib/grit/tree.rb', line 6 def mode @mode end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/grit/lib/grit/tree.rb', line 7 def name @name end |
Class Method Details
.construct(repo, treeish, paths = []) ⇒ Object
Construct the contents of the tree
+repo+ is the Repo
+treeish+ is the reference
+paths+ is an optional Array of directory paths to restrict the tree
Returns Grit::Tree (baked)
15 16 17 18 |
# File 'lib/grit/lib/grit/tree.rb', line 15 def self.construct(repo, treeish, paths = []) output = repo.git.ls_tree({}, treeish, *paths) self.allocate.construct_initialize(repo, treeish, output) end |
.create(repo, atts) ⇒ Object
Create an unbaked Tree containing just the specified attributes
+repo+ is the Repo
+atts+ is a Hash of instance variable data
Returns Grit::Tree (unbaked)
42 43 44 |
# File 'lib/grit/lib/grit/tree.rb', line 42 def self.create(repo, atts) self.allocate.create_initialize(repo, atts) end |
Instance Method Details
#/(file) ⇒ Object
Find the named object in this tree’s contents
Examples
Repo.new('/path/to/grit').tree/'lib'
# => #<Grit::Tree "6cc23ee138be09ff8c28b07162720018b244e95e">
Repo.new('/path/to/grit').tree/'README.txt'
# => #<Grit::Blob "8b1e02c0fb554eed2ce2ef737a68bb369d7527df">
Returns Grit::Blob or Grit::Tree or nil if not found
90 91 92 93 94 95 96 |
# File 'lib/grit/lib/grit/tree.rb', line 90 def /(file) if file =~ /\// file.split("/").inject(self) { |acc, x| acc/x } rescue nil else self.contents.find { |c| c.name == file } end end |
#<=>(other) ⇒ Object
Compares trees by name
118 119 120 |
# File 'lib/grit/lib/grit/tree.rb', line 118 def <=>(other) name <=> other.name end |
#basename ⇒ Object
98 99 100 |
# File 'lib/grit/lib/grit/tree.rb', line 98 def basename File.basename(name) end |
#blobs ⇒ Object
Find only Blob objects from contents
113 114 115 |
# File 'lib/grit/lib/grit/tree.rb', line 113 def blobs contents.select {|v| v.kind_of? Blob} end |
#construct_initialize(repo, id, text) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/grit/lib/grit/tree.rb', line 20 def construct_initialize(repo, id, text) @repo = repo @id = id @contents = [] text.split("\n").each do |line| @contents << content_from_string(repo, line) end @contents.compact! self end |
#content_from_string(repo, text) ⇒ Object
Parse a content item and create the appropriate object
+repo+ is the Repo
+text+ is the single line containing the items data in `git ls-tree` format
Returns Grit::Blob or Grit::Tree
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/grit/lib/grit/tree.rb', line 65 def content_from_string(repo, text) mode, type, id, name = text.split(" ", 4) case type when "tree" Tree.create(repo, :id => id, :mode => mode, :name => name) when "blob" Blob.create(repo, :id => id, :mode => mode, :name => name) when "link" Blob.create(repo, :id => id, :mode => mode, :name => name) when "commit" Submodule.create(repo, :id => id, :mode => mode, :name => name) else raise "Invalid type: #{type}" end end |
#create_initialize(repo, atts) ⇒ Object
Initializer for Tree.create
+repo+ is the Repo
+atts+ is a Hash of instance variable data
Returns Grit::Tree (unbaked)
51 52 53 54 55 56 57 58 |
# File 'lib/grit/lib/grit/tree.rb', line 51 def create_initialize(repo, atts) @repo = repo atts.each do |k, v| instance_variable_set("@#{k}", v) end self end |
#inspect ⇒ Object
Pretty object inspection
103 104 105 |
# File 'lib/grit/lib/grit/tree.rb', line 103 def inspect %Q{#<Grit::Tree "#{@id}">} end |
#lazy_source ⇒ Object
33 34 35 |
# File 'lib/grit/lib/grit/tree.rb', line 33 def lazy_source Tree.construct(@repo, @id, []) end |
#trees ⇒ Object
Find only Tree objects from contents
108 109 110 |
# File 'lib/grit/lib/grit/tree.rb', line 108 def trees contents.select {|v| v.kind_of? Tree} end |