Class Grit::Tree
In: lib/grit/tree.rb
Parent: Object

Methods

Attributes

id  [R] 
mode  [R] 
name  [R] 

Public Class methods

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)

[Source]

    # File lib/grit/tree.rb, line 15
15:     def self.construct(repo, treeish, paths = [])
16:       output = repo.git.ls_tree({}, treeish, *paths)
17:       self.allocate.construct_initialize(repo, treeish, output)
18:     end

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)

[Source]

    # File lib/grit/tree.rb, line 42
42:     def self.create(repo, atts)
43:       self.allocate.create_initialize(repo, atts)
44:     end

Public Instance methods

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

[Source]

    # File lib/grit/tree.rb, line 90
90:     def /(file)
91:       if file =~ /\//
92:         file.split("/").inject(self) { |acc, x| acc/x } rescue nil
93:       else
94:         self.contents.find { |c| c.name == file }
95:       end
96:     end

Compares trees by name

[Source]

     # File lib/grit/tree.rb, line 118
118:     def <=>(other)
119:       name <=> other.name
120:     end

[Source]

     # File lib/grit/tree.rb, line 98
 98:     def basename
 99:       File.basename(name)
100:     end

Find only Blob objects from contents

[Source]

     # File lib/grit/tree.rb, line 113
113:     def blobs
114:       contents.select {|v| v.kind_of? Blob}
115:     end

[Source]

    # File lib/grit/tree.rb, line 20
20:     def construct_initialize(repo, id, text)
21:       @repo = repo
22:       @id = id
23:       @contents = []
24:       
25:       text.split("\n").each do |line|
26:         @contents << content_from_string(repo, line)
27:       end
28:       @contents.compact!
29: 
30:       self
31:     end

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

[Source]

    # File lib/grit/tree.rb, line 65
65:     def content_from_string(repo, text)
66:       mode, type, id, name = text.split(" ", 4)
67:       case type
68:         when "tree"
69:           Tree.create(repo, :id => id, :mode => mode, :name => name)
70:         when "blob"
71:           Blob.create(repo, :id => id, :mode => mode, :name => name)
72:         when "link"
73:           Blob.create(repo, :id => id, :mode => mode, :name => name)
74:         when "commit"
75:           Submodule.create(repo, :id => id, :mode => mode, :name => name)
76:         else
77:           raise "Invalid type: #{type}"
78:       end
79:     end

Initializer for Tree.create

  +repo+ is the Repo
  +atts+ is a Hash of instance variable data

Returns Grit::Tree (unbaked)

[Source]

    # File lib/grit/tree.rb, line 51
51:     def create_initialize(repo, atts)
52:       @repo = repo
53:       
54:       atts.each do |k, v|
55:         instance_variable_set("@#{k}", v)
56:       end
57:       self
58:     end

Pretty object inspection

[Source]

     # File lib/grit/tree.rb, line 103
103:     def inspect
104:       %Q{#<Grit::Tree "#{@id}">}
105:     end

[Source]

    # File lib/grit/tree.rb, line 33
33:     def lazy_source
34:       Tree.construct(@repo, @id, [])
35:     end

Find only Tree objects from contents

[Source]

     # File lib/grit/tree.rb, line 108
108:     def trees
109:       contents.select {|v| v.kind_of? Tree}
110:     end

[Validate]