Class: Gitrb::Tree
- Includes:
- Enumerable
- Defined in:
- lib/gitrb/tree.rb
Instance Attribute Summary collapse
-
#mode ⇒ Object
Returns the value of attribute mode.
-
#repository ⇒ Object
Returns the value of attribute repository.
Attributes inherited from GitObject
Instance Method Summary collapse
-
#[](path) ⇒ Object
Read an entry on specified path.
-
#[]=(path, entry) ⇒ Object
Write an entry on specified path.
-
#delete(path) ⇒ Object
Delete an entry on specified path.
- #dump ⇒ Object
-
#each(&block) ⇒ Object
Iterate over all children.
-
#empty? ⇒ Boolean
Are there no children?.
-
#exists?(name) ⇒ Boolean
Does this key exist in the children?.
-
#id=(id) ⇒ Object
Set new repository (modified flag is reset).
-
#initialize(options = {}) ⇒ Tree
constructor
Initialize a tree.
-
#modified? ⇒ Boolean
Has this tree been modified?.
-
#move(path, dest) ⇒ Object
Move a entry.
- #names ⇒ Object
-
#save ⇒ Object
Save this tree back to the git repository.
-
#size ⇒ Object
Number of children.
- #type ⇒ Object
- #values ⇒ Object (also: #children)
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( = {}) super() @children = {} @mode = [:mode] || 040000 parse([:data]) if [:data] @modified = true if !id end |
Instance Attribute Details
#mode ⇒ Object
Returns the value of attribute mode.
7 8 9 |
# File 'lib/gitrb/tree.rb', line 7 def mode @mode end |
#repository ⇒ Object
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.
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 |
#dump ⇒ Object
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 |
#names ⇒ Object
134 135 136 |
# File 'lib/gitrb/tree.rb', line 134 def names map {|name, child| name } end |
#save ⇒ Object
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 |
#size ⇒ Object
Number of children
62 63 64 |
# File 'lib/gitrb/tree.rb', line 62 def size @children.size end |
#type ⇒ Object
18 19 20 |
# File 'lib/gitrb/tree.rb', line 18 def type :tree end |
#values ⇒ Object Also known as: children
138 139 140 |
# File 'lib/gitrb/tree.rb', line 138 def values map {|name, child| child } end |