Class: RJGit::Plumbing::TreeBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/rjgit.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository) ⇒ TreeBuilder

Returns a new instance of TreeBuilder.



186
187
188
189
190
# File 'lib/rjgit.rb', line 186

def initialize(repository)
  @jrepo = RJGit.repository_type(repository)
  @treemap = {}
  init_log
end

Instance Attribute Details

#logObject (readonly)

Returns the value of attribute log.



184
185
186
# File 'lib/rjgit.rb', line 184

def log
  @log
end

#treemapObject

Returns the value of attribute treemap.



183
184
185
# File 'lib/rjgit.rb', line 183

def treemap
  @treemap
end

Instance Method Details

#build_tree(start_tree, treemap = nil, flush = false) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/rjgit.rb', line 211

def build_tree(start_tree, treemap = nil, flush = false)
  existing_trees = {}
  untouched_objects = {}
  formatter = TreeFormatter.new
  treemap ||= self.treemap
  if start_tree
    treewalk = TreeWalk.new(@jrepo)
    treewalk.add_tree(start_tree)
    while treewalk.next
      filename = treewalk.get_name_string
      if treemap.keys.include?(filename)
        kind = treewalk.isSubtree ? :tree : :blob
          if treemap[filename] == false
            @log[:deleted] << [kind, filename, treewalk.get_object_id(0)]
          else
            existing_trees[filename] = treewalk.get_object_id(0) if kind == :tree
          end
      else
        mode = treewalk.get_file_mode(0)
        filename = "#{filename}/" if mode == FileMode::TREE
        untouched_objects[filename] = [mode, treewalk.get_object_id(0)]
      end
    end
  end
  
  sorted_treemap = treemap.inject({}) {|h, (k,v)| v.is_a?(Hash) ? h["#{k}/"] = v : h[k] = v; h }.merge(untouched_objects).sort
  sorted_treemap.each do |object_name, data|
    case data
      when Array
        object_name = object_name[0...-1] if data[0] == FileMode::TREE
        formatter.append(object_name.to_java_string, data[0], data[1])
      when Hash
        object_name = object_name[0...-1]
        next_tree = build_tree(existing_trees[object_name], data)
        formatter.append(object_name.to_java_string, FileMode::TREE, next_tree)
        @log[:added] << [:tree, object_name, next_tree] unless only_contains_deletions(data)
      when String
        blobid = write_blob(data)
        formatter.append(object_name.to_java_string, FileMode::REGULAR_FILE, blobid)
        @log[:added] << [:blob, object_name, blobid]
      end
  end
  object_inserter.insert(formatter)
end

#init_logObject



196
197
198
# File 'lib/rjgit.rb', line 196

def init_log
  @log = {:deleted => [], :added => [] }
end

#object_inserterObject



192
193
194
# File 'lib/rjgit.rb', line 192

def object_inserter
  @object_inserter ||= @jrepo.newObjectInserter
end

#only_contains_deletions(hashmap) ⇒ Object



200
201
202
203
204
205
206
207
208
209
# File 'lib/rjgit.rb', line 200

def only_contains_deletions(hashmap)
  hashmap.each do |key, value|
    if value.is_a?(Hash)
      return false unless only_contains_deletions(value)
    elsif value.is_a?(String)
      return false
    end
  end
  true
end

#write_blob(contents, flush = false) ⇒ Object



256
257
258
259
260
# File 'lib/rjgit.rb', line 256

def write_blob(contents, flush = false)
  blobid = object_inserter.insert(Constants::OBJ_BLOB, contents.to_java_bytes)
  object_inserter.flush if flush
  blobid
end