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, 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(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.



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

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.



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.

Raises:

  • (ArgumentError)


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

#dumpObject



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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)


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

#namesObject



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

def names
  @children.keys.sort
end

#saveObject

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

#typeObject



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

def type
  :tree
end

#valuesObject Also known as: children



119
120
121
# File 'lib/gitrb/tree.rb', line 119

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