Class: Gitrb::Tree

Inherits:
GitObject show all
Includes:
Enumerable
Defined in:
lib/gitrb/tree.rb

Instance Attribute Summary collapse

Attributes inherited from GitObject

#id

Instance Method Summary collapse

Methods inherited from GitObject

#==, factory, #git_object, inherited

Constructor Details

#initialize(options = {}) ⇒ Tree

Initialize a tree


10
11
12
13
14
15
16
# File 'lib/gitrb/tree.rb', line 10

def initialize(options = {})
  super(options)
  @children = {}
  @mode = options[:mode] || 040000
  parse(options[:data]) if options[:data]
  @modified = true if !id
end

Instance Attribute Details

#modeObject

Returns the value of attribute mode


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

def mode
  @mode
end

#repositoryObject

Returns the value of attribute repository


6
7
8
# File 'lib/gitrb/tree.rb', line 6

def repository
  @repository
end

Instance Method Details

#[](path) ⇒ Object

Read an entry on specified path.


72
73
74
75
76
77
78
79
80
81
82
# File 'lib/gitrb/tree.rb', line 72

def [](path)
  path = normalize_path(path)
  return self if path.empty?
  entry = @children[path.first]
  if path.size == 1
    entry
  elsif entry
    raise 'Not a tree' if entry.type != :tree
    entry[path[1..-1]]
  end
end

#[]=(path, entry) ⇒ Object

Write an entry on specified path.

Raises:

  • (ArgumentError)

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/gitrb/tree.rb', line 85

def []=(path, entry)
  raise ArgumentError unless entry && (Reference === entry || GitObject === entry)
  path = normalize_path(path)
  if path.empty?
    raise 'Empty path'
  elsif path.size == 1
    raise 'No blob or tree' if entry.type != :tree && entry.type != :blob
    entry.repository = repository
    @modified = true
    @children[path.first] = entry
  else
    tree = @children[path.first]
    if !tree
      tree = @children[path.first] = Tree.new(:repository => repository)
      @modified = true
    end
    raise 'Not a tree' if tree.type != :tree
    tree[path[1..-1]] = entry
  end
end

#delete(path) ⇒ Object

Delete an entry on specified path.


107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/gitrb/tree.rb', line 107

def delete(path)
  path = normalize_path(path)
  if path.empty?
    raise 'Empty path'
  elsif path.size == 1
    child = @children.delete(path.first)
    @modified = true if child
    child
  else
    tree = @children[path.first]
    raise 'Not a tree' if tree.type != :tree
    tree.delete(path[1..-1])
  end
end

#dumpObject


41
42
43
44
45
46
# File 'lib/gitrb/tree.rb', line 41

def dump
  sorted_children.map do |name, child|
	child.save if !(Reference === child) || child.resolved?
    "#{child.mode.to_s(8)} #{name}\0#{repository.set_encoding [child.id].pack("H*")}"
  end.join
end

#each(&block) ⇒ Object

Iterate over all children


128
129
130
131
132
# File 'lib/gitrb/tree.rb', line 128

def each(&block)
  sorted_children.each do |name, child|
    yield(name, child)
  end
end

#empty?Boolean

Are there no children?


57
58
59
# File 'lib/gitrb/tree.rb', line 57

def empty?
  @children.empty?
end

#exists?(name) ⇒ Boolean

Does this key exist in the children?


67
68
69
# File 'lib/gitrb/tree.rb', line 67

def exists?(name)
  self[name] != nil
end

#id=(id) ⇒ Object

Set new repository (modified flag is reset)


31
32
33
34
# File 'lib/gitrb/tree.rb', line 31

def id=(id)
  @modified = false
  super
end

#modified?Boolean

Has this tree been modified?


37
38
39
# File 'lib/gitrb/tree.rb', line 37

def modified?
  @modified || @children.values.any? {|child| (!(Reference === child) || child.resolved?) && child.modified? }
end

#move(path, dest) ⇒ Object

Move a entry


123
124
125
# File 'lib/gitrb/tree.rb', line 123

def move(path, dest)
  self[dest] = delete(path)
end

#namesObject


134
135
136
# File 'lib/gitrb/tree.rb', line 134

def names
  map {|name, child| name }
end

#saveObject

Save this tree back to the git repository.

Returns the object id of the tree.


51
52
53
54
# File 'lib/gitrb/tree.rb', line 51

def save
  repository.put(self) if modified?
  id
end

#sizeObject

Number of children


62
63
64
# File 'lib/gitrb/tree.rb', line 62

def size
  @children.size
end

#typeObject


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

def type
  :tree
end

#valuesObject Also known as: children


138
139
140
# File 'lib/gitrb/tree.rb', line 138

def values
  map {|name, child| child }
end