Class: Grit::Tree

Inherits:
Object
  • Object
show all
Extended by:
Lazy
Defined in:
lib/grit/tree.rb,
lib/grit_ext/tree.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Lazy

extended, lazy_reader

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/grit/tree.rb', line 7

def id
  @id
end

#modeObject (readonly)

Returns the value of attribute mode.



8
9
10
# File 'lib/grit/tree.rb', line 8

def mode
  @mode
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/grit/tree.rb', line 9

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)



17
18
19
20
# File 'lib/grit/tree.rb', line 17

def self.construct(repo, treeish, paths = [])
  output = repo.git.ls_tree({:raise => true}, 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)



44
45
46
# File 'lib/grit/tree.rb', line 44

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



92
93
94
95
96
97
98
# File 'lib/grit/tree.rb', line 92

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



120
121
122
# File 'lib/grit/tree.rb', line 120

def <=>(other)
  name <=> other.name
end

#basenameObject



100
101
102
# File 'lib/grit/tree.rb', line 100

def basename
  File.basename(name)
end

#blobsObject

Find only Blob objects from contents



115
116
117
# File 'lib/grit/tree.rb', line 115

def blobs
  contents.select {|v| v.kind_of? Blob}
end

#construct_initialize(repo, id, text) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/grit/tree.rb', line 22

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/grit/tree.rb', line 67

def content_from_string(repo, text)
  mode, type, id, name = text.split(/ |\t/, 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 Grit::InvalidObjectType, 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)



53
54
55
56
57
58
59
60
# File 'lib/grit/tree.rb', line 53

def create_initialize(repo, atts)
  @repo = repo

  atts.each do |k, v|
    instance_variable_set("@#{k}", v)
  end
  self
end

#inspectObject

Pretty object inspection



105
106
107
# File 'lib/grit/tree.rb', line 105

def inspect
  %Q{#<Grit::Tree "#{@id}">}
end

#lazy_sourceObject



35
36
37
# File 'lib/grit/tree.rb', line 35

def lazy_source
  Tree.construct(@repo, @id, [])
end

#old_nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/grit_ext/tree.rb', line 4

def name
  @name
end

#treesObject

Find only Tree objects from contents



110
111
112
# File 'lib/grit/tree.rb', line 110

def trees
  contents.select {|v| v.kind_of? Tree}
end