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.
-
#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.
- #type ⇒ Object
- #values ⇒ Object (also: #children)
Methods inherited from GitObject
#==, factory, inherited, #object
Constructor Details
#initialize(options = {}) ⇒ Tree
Initialize a tree
9 10 11 12 13 14 15 |
# File 'lib/gitrb/tree.rb', line 9 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.
6 7 8 |
# File 'lib/gitrb/tree.rb', line 6 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.
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/gitrb/tree.rb', line 53 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.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/gitrb/tree.rb', line 66 def []=(path, entry) raise ArgumentError if !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.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/gitrb/tree.rb', line 88 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
32 33 34 35 36 37 |
# File 'lib/gitrb/tree.rb', line 32 def dump @children.to_a.sort {|a,b| a.first <=> b.first }.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
109 110 111 112 113 |
# File 'lib/gitrb/tree.rb', line 109 def each(&block) @children.sort.each do |name, child| yield(name, child) end end |
#exists?(name) ⇒ Boolean
Does this key exist in the children?
48 49 50 |
# File 'lib/gitrb/tree.rb', line 48 def exists?(name) self[name] != nil end |
#id=(id) ⇒ Object
Set new repository (modified flag is reset)
22 23 24 25 |
# File 'lib/gitrb/tree.rb', line 22 def id=(id) @modified = false super end |
#modified? ⇒ Boolean
Has this tree been modified?
28 29 30 |
# File 'lib/gitrb/tree.rb', line 28 def modified? @modified || @children.values.any? { |entry| entry.type == :tree && entry.modified? } end |
#move(path, dest) ⇒ Object
Move a entry
104 105 106 |
# File 'lib/gitrb/tree.rb', line 104 def move(path, dest) self[dest] = delete(path) end |
#names ⇒ Object
115 116 117 |
# File 'lib/gitrb/tree.rb', line 115 def names @children.keys.sort end |
#save ⇒ Object
Save this tree back to the git repository.
Returns the object id of the tree.
42 43 44 45 |
# File 'lib/gitrb/tree.rb', line 42 def save repository.put(self) if modified? id end |
#type ⇒ Object
17 18 19 |
# File 'lib/gitrb/tree.rb', line 17 def type :tree end |
#values ⇒ Object Also known as: children
119 120 121 |
# File 'lib/gitrb/tree.rb', line 119 def values map { |name, child| child } end |