Class: RJGit::Plumbing::Index

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

Direct Known Subclasses

ApplyPatchToIndex

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository) ⇒ Index

Returns a new instance of Index.



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

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.



340
341
342
# File 'lib/rjgit.rb', line 340

def current_tree
  @current_tree
end

#jrepoObject (readonly)

Returns the value of attribute jrepo.



341
342
343
# File 'lib/rjgit.rb', line 341

def jrepo
  @jrepo
end

#treemapObject

Returns the value of attribute treemap.



340
341
342
# File 'lib/rjgit.rb', line 340

def treemap
  @treemap
end

Class Method Details

.successful?(result) ⇒ Boolean

Returns:

  • (Boolean)


422
423
424
# File 'lib/rjgit.rb', line 422

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

Instance Method Details

#add(path, data) ⇒ Object



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/rjgit.rb', line 349

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 = "refs/heads/#{Constants::MASTER}", force = false) ⇒ Object



400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/rjgit.rb', line 400

def commit(message, author, parents = nil, ref = "refs/heads/#{Constants::MASTER}", force = false)
  new_tree = build_new_tree(@treemap, "#{ref}^{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

  @current_tree = new_tree
  log = @treebuilder.log
  @treebuilder.init_log
  sha =  ObjectId.to_string(new_head)
  return res, log, sha
end

#delete(path) ⇒ Object



366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/rjgit.rb', line 366

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



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/rjgit.rb', line 383

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)
    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