Class: RightGit::Git::Commit

Inherits:
Object
  • Object
show all
Includes:
BelongsToRepository
Defined in:
lib/right_git/git/commit.rb

Overview

A commit within a Git repository.

Defined Under Namespace

Classes: CommitError

Constant Summary collapse

LOG_FORMAT_LONG =
"%H %at %aE %s"
LOG_FORMAT =
"%h %at %aE %s"
COMMIT_INFO =
/^([0-9A-Fa-f]+) ([0-9]+) (\S+) (.*)$/
COMMIT_SHA1_REGEX =
/^[0-9a-fA-F]{40}$/

Instance Attribute Summary

Attributes included from BelongsToRepository

#repo

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BelongsToRepository

#logger

Constructor Details

#initialize(repo, line) ⇒ Commit

Returns a new instance of Commit.

Parameters:

  • repo (Repository)

    hosting commit

  • line (String)

    of git output describing commit



40
41
42
43
44
45
46
# File 'lib/right_git/git/commit.rb', line 40

def initialize(repo, line)
  @repo = repo
  unless match = COMMIT_INFO.match(line)
    raise CommitError, "Unrecognized commit summary: #{line.inspect}"
  end
  @info = [ match[1], Integer(match[2]), match[3], match[4] ]
end

Class Method Details

.sha?(revision) ⇒ TrueClass|FalseClass

Returns true if the given revision is a (fully qualified, not abbreviated) commit SHA.

Returns:

  • (TrueClass|FalseClass)

    true if the given revision is a (fully qualified, not abbreviated) commit SHA



83
84
85
# File 'lib/right_git/git/commit.rb', line 83

def self.sha?(revision)
  !!COMMIT_SHA1_REGEX.match(revision)
end

Instance Method Details

#authorString

Returns author of commit.

Returns:

  • (String)

    author of commit



71
72
73
# File 'lib/right_git/git/commit.rb', line 71

def author
  @info[2]
end

#hashString

The commit hash. This overrides String#hash on purpose

Returns:

  • (String)

    hash of commit (may be abbreviated)



61
62
63
# File 'lib/right_git/git/commit.rb', line 61

def hash
  @info[0]
end

#inspectObject

Provide a programmer-friendly representation of this branch.



54
55
56
# File 'lib/right_git/git/commit.rb', line 54

def inspect
  '#<%s:%s>' % [self.class.name, hash]
end

#subjectString Also known as: comment

Returns:

  • (String)


76
77
78
# File 'lib/right_git/git/commit.rb', line 76

def subject
  @info[3]
end

#timestampTime

Returns time of commit.

Returns:

  • (Time)

    time of commit



66
67
68
# File 'lib/right_git/git/commit.rb', line 66

def timestamp
  ::Time.at(@info[1])
end

#to_sObject

Provide a String representation of this commit (specifically, its commit hash).



49
50
51
# File 'lib/right_git/git/commit.rb', line 49

def to_s
  hash
end