Class: RJGit::Plumbing::Index

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository) ⇒ Index



260
261
262
263
264
# File 'lib/rjgit.rb', line 260

def initialize(repository)
  @treemap = {}
  @jrepo = RJGit.repository_type(repository)
  @treebuilder = TreeBuilder.new(@jrepo)
end

Instance Attribute Details

#current_treeObject

Returns the value of attribute current_tree.



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

def current_tree
  @current_tree
end

#jrepoObject (readonly)

Returns the value of attribute jrepo.



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

def jrepo
  @jrepo
end

#treemapObject

Returns the value of attribute treemap.



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

def treemap
  @treemap
end

Class Method Details

.successful?(result) ⇒ Boolean



344
345
346
# File 'lib/rjgit.rb', line 344

def self.successful?(result)
  ["NEW", "FAST_FORWARD", "FORCED"].include?(result)
end

Instance Method Details

#add(path, data) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/rjgit.rb', line 266

def add(path, data)
  path = path[1..-1] if path[0] == '/'
  path = path.split('/')
  filename = path.pop

  current = self.treemap

  path.each do |dir|
    current[dir] ||= {}
    node = current[dir]
    current = node
  end

  current[filename] = data
  @treemap
end

#commit(message, author, parents = nil, ref = nil, force = false) ⇒ Object



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/rjgit.rb', line 317

def commit(message, author, parents = nil, ref = nil, force = false)
  ref = ref ? ref : "refs/heads/#{Constants::MASTER}"
  @current_tree = @current_tree ? RJGit.tree_type(@current_tree) : @jrepo.resolve("refs/heads/#{Constants::MASTER}^{tree}")
  @treebuilder.treemap = @treemap
  new_tree = @treebuilder.build_tree(@current_tree)
  return false if @current_tree && new_tree.name == @current_tree.name
  
  parents = parents ? parents : @jrepo.resolve(ref+"^{commit}")
  new_head = do_commit(message, author, parents, new_tree)

  # Point ref to the newest commit
  ru = @jrepo.updateRef(ref)
  ru.setNewObjectId(new_head)
  ru.setForceUpdate(force)
  ru.setRefLogIdent(author.person_ident)
  ru.setRefLogMessage("commit: #{message}", false)
  res = ru.update.to_string
  
  @treebuilder.object_inserter.release
  @current_tree = new_tree
  @treemap = {}
  log = @treebuilder.log
  @treebuilder.init_log
  sha =  ObjectId.to_string(new_head)
  return res, log, sha
end

#delete(path) ⇒ Object



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/rjgit.rb', line 283

def delete(path)
  path = path[1..-1] if path[0] == '/'
  path = path.split('/')
  last = path.pop
    
  current = self.treemap
    
  path.each do |dir|
    current[dir] ||= {}
    node = current[dir]
    current = node
  end
    
  current[last] = false
  @treemap
end

#do_commit(message, author, parents, new_tree) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/rjgit.rb', line 300

def do_commit(message, author, parents, new_tree)
  commit_builder = CommitBuilder.new
  person = author.person_ident
  commit_builder.setCommitter(person)
  commit_builder.setAuthor(person)
  commit_builder.setMessage(message)
  commit_builder.setTreeId(RJGit.tree_type(new_tree))
    if parents.is_a?(Array) then
      parents.each {|parent| commit_builder.addParentId(RJGit.commit_type(parent)) }
    elsif parents
      commit_builder.addParentId(RJGit.commit_type(parents))
    end
  result = @treebuilder.object_inserter.insert(commit_builder)
  @treebuilder.object_inserter.flush
  result
end