Class: Git::Object::Commit

Inherits:
AbstractObject show all
Defined in:
lib/git/object.rb

Overview

A Git commit object

Instance Attribute Summary

Attributes inherited from AbstractObject

#mode, #objectish, #size, #type

Instance Method Summary collapse

Methods inherited from AbstractObject

#archive, #blob?, #contents, #contents_array, #diff, #grep, #log, #sha, #tag?, #to_s, #tree?

Constructor Details

#initialize(base, sha, init = nil) ⇒ Commit

Creates a commit object wrapper

Parameters:

  • base (Git::Repository)

    the repository used to query object data

  • sha (String)

    the commit SHA or object expression

  • init (Hash, nil) (defaults to: nil)

    parsed commit data used to initialize eagerly



388
389
390
391
392
393
394
395
396
397
398
# File 'lib/git/object.rb', line 388

def initialize(base, sha, init = nil)
  super(base, sha)
  @tree = nil
  @parents = nil
  @author = nil
  @committer = nil
  @message = nil
  return unless init

  from_data(init)
end

Instance Method Details

#author

git author



442
443
444
445
# File 'lib/git/object.rb', line 442

def author
  check_commit
  @author
end

#author_dateTime

Returns the author date

Returns:

  • (Time)

    the author timestamp



451
452
453
# File 'lib/git/object.rb', line 451

def author_date
  author.date
end

#commit?Boolean

Returns whether this object is a commit

Returns:

  • (Boolean)

    true



513
514
515
# File 'lib/git/object.rb', line 513

def commit?
  true
end

#committer

git author



456
457
458
459
# File 'lib/git/object.rb', line 456

def committer
  check_commit
  @committer
end

#committer_dateTime Also known as: date

Returns the committer date

Returns:

  • (Time)

    the committer timestamp



465
466
467
# File 'lib/git/object.rb', line 465

def committer_date
  committer.date
end

#diff_parentGit::Diff

Returns the diff between this commit and its first parent

Returns:

  • (Git::Diff)

    the diff from the first parent to this commit



474
475
476
# File 'lib/git/object.rb', line 474

def diff_parent
  diff(parent)
end

#from_data(data)

This method returns an undefined value.

Loads parsed commit data into this commit object

Parameters:

  • data (Hash)

    parsed commit data from git cat-file commit



500
501
502
503
504
505
506
507
# File 'lib/git/object.rb', line 500

def from_data(data)
  @sha ||= data['sha']
  @committer = Git::Author.new(data['committer'])
  @author = Git::Author.new(data['author'])
  @tree = Git::Object::Tree.new(@base, data['tree'])
  @parents = data['parent'].map { |sha| Git::Object::Commit.new(@base, sha) }
  @message = data['message'].chomp
end

#gtreeGit::Object::Tree

Returns the tree for this commit

Returns:



421
422
423
424
# File 'lib/git/object.rb', line 421

def gtree
  check_commit
  Tree.new(@base, @tree)
end

#messageString

Returns the commit message

Returns:

  • (String)

    the commit message without the trailing newline



404
405
406
407
# File 'lib/git/object.rb', line 404

def message
  check_commit
  @message
end

#nameString

Returns the symbolic name for this commit

Returns:

  • (String)

    the name produced by git name-rev



413
414
415
# File 'lib/git/object.rb', line 413

def name
  object_repository.name_rev(sha)
end

#parentGit::Object::Commit?

Returns the first parent commit

Returns:



431
432
433
# File 'lib/git/object.rb', line 431

def parent
  parents.first
end

#parents

array of all parent commits



436
437
438
439
# File 'lib/git/object.rb', line 436

def parents
  check_commit
  @parents
end

#set_commit(data)

Deprecated.

use #from_data instead

This method returns an undefined value.

Sets parsed commit data on this commit object

Parameters:

  • data (Hash)

    parsed commit data



486
487
488
489
490
491
492
# File 'lib/git/object.rb', line 486

def set_commit(data) # rubocop:disable Naming/AccessorMethodName
  Git::Deprecation.warn(
    'Git::Object::Commit#set_commit is deprecated and will be removed in a future version. ' \
    'Use #from_data instead.'
  )
  from_data(data)
end