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

#==(other) ⇒ Object



21
22
23
# File 'lib/gitrb/tree.rb', line 21

def ==(other)
  Tree === other && id == other.id
end

#[](path) ⇒ Object

Read an entry on specified path.



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/gitrb/tree.rb', line 57

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.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/gitrb/tree.rb', line 70

def []=(path, 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.



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/gitrb/tree.rb', line 91

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

#dumpObject



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

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



111
112
113
114
115
# File 'lib/gitrb/tree.rb', line 111

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)


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

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

#id=(id) ⇒ Object

Set new repository (modified flag is reset)



26
27
28
29
# File 'lib/gitrb/tree.rb', line 26

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

#modified?Boolean

Has this tree been modified?

Returns:

  • (Boolean)


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

def modified?
  @modified || @children.values.any? { |entry| entry.type == :tree && entry.modified? }
end

#move(path, dest) ⇒ Object

Move a entry



106
107
108
# File 'lib/gitrb/tree.rb', line 106

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

#namesObject



117
118
119
# File 'lib/gitrb/tree.rb', line 117

def names
  @children.keys.sort
end

#saveObject

Save this tree back to the git repository.

Returns the object id of the tree.



46
47
48
49
# File 'lib/gitrb/tree.rb', line 46

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



121
122
123
# File 'lib/gitrb/tree.rb', line 121

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