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



320
321
322
323
324
# File 'lib/rjgit.rb', line 320

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.



317
318
319
# File 'lib/rjgit.rb', line 317

def current_tree
  @current_tree
end

#jrepoObject (readonly)

Returns the value of attribute jrepo.



318
319
320
# File 'lib/rjgit.rb', line 318

def jrepo
  @jrepo
end

#treemapObject

Returns the value of attribute treemap.



317
318
319
# File 'lib/rjgit.rb', line 317

def treemap
  @treemap
end

Class Method Details

.successful?(result) ⇒ Boolean



399
400
401
# File 'lib/rjgit.rb', line 399

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

Instance Method Details

#add(path, data) ⇒ Object



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/rjgit.rb', line 326

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



377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/rjgit.rb', line 377

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



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/rjgit.rb', line 343

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



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/rjgit.rb', line 360

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