class Gitlab::Git::Commit

Constants

SERIALIZE_KEYS

Attributes

head[RW]
raw_commit[RW]
refs[RW]

Public Class Methods

between(repo, from, to) click to toggle source

Get commits between two refs

Ex.

Commit.between('29eda46b', 'master')
# File lib/gitlab_git/commit.rb, line 88
def between(repo, from, to)
  repo.commits_between(from, to).map do |commit|
    decorate(commit)
  end
end
decorate(commit, ref = nil) click to toggle source
# File lib/gitlab_git/commit.rb, line 99
def decorate(commit, ref = nil)
  Gitlab::Git::Commit.new(commit, ref)
end
find(repo, commit_id = nil) click to toggle source

Get single commit

Ex.

Commit.find(repo, '29eda46b')

Commit.find(repo, 'master')
# File lib/gitlab_git/commit.rb, line 42
def find(repo, commit_id = nil)
  # Find repo.tags first,
  # because if commit_id is "tag name",
  # repo.commit(commit_id) returns wrong commit sha
  # that is git tag object sha.
  tag = repo.tags.find {|tag| tag.name == commit_id} if commit_id

  commit = if tag
             tag.commit
           else
             repo.log(ref: commit_id).first
           end

  decorate(commit) if commit
end
find_all(repo, options = {}) click to toggle source

Delegate Gitlab::Git::Repository#find_commits

# File lib/gitlab_git/commit.rb, line 95
def find_all(repo, options = {})
  repo.find_commits(options)
end
last(repo) click to toggle source

Get last commit for HEAD

Ex.

Commit.last(repo)
# File lib/gitlab_git/commit.rb, line 63
def last(repo)
  find(repo, nil)
end
last_for_path(repo, ref, path = nil) click to toggle source

Get last commit for specified path and ref

Ex.

Commit.last_for_path(repo, '29eda46b', 'app/models')

Commit.last_for_path(repo, 'master', 'Gemfile')
# File lib/gitlab_git/commit.rb, line 74
def last_for_path(repo, ref, path = nil)
  where(
    repo: repo,
    ref: ref,
    path: path,
    limit: 1
  ).first
end
new(raw_commit, head = nil) click to toggle source
# File lib/gitlab_git/commit.rb, line 104
def initialize(raw_commit, head = nil)
  raise "Nil as raw commit passed" unless raw_commit

  if raw_commit.is_a?(Hash)
    init_from_hash(raw_commit)
  else
    init_from_grit(raw_commit)
  end

  @head = head
end
where(options) click to toggle source

Get commits collection

Ex.

Commit.where(
  repo: repo,
  ref: 'master',
  path: 'app/models',
  limit: 10,
  offset: 5,
)
# File lib/gitlab_git/commit.rb, line 28
def where(options)
  repo = options.delete(:repo)
  raise 'Gitlab::Git::Repository is required' unless repo.respond_to?(:log)

  repo.log(options).map { |c| decorate(c) }
end

Public Instance Methods

created_at() click to toggle source
# File lib/gitlab_git/commit.rb, line 128
def created_at
  committed_date
end
date() click to toggle source
# File lib/gitlab_git/commit.rb, line 174
def date
  committed_date
end
different_committer?() click to toggle source

Was this commit committed by a different person than the original author?

# File lib/gitlab_git/commit.rb, line 133
def different_committer?
  author_name != committer_name || author_email != committer_email
end
diffs() click to toggle source
# File lib/gitlab_git/commit.rb, line 178
def diffs
  raw_commit.diffs.map { |diff| Gitlab::Git::Diff.new(diff) }
end
has_zero_stats?() click to toggle source
# File lib/gitlab_git/commit.rb, line 158
def has_zero_stats?
  stats.total.zero?
rescue
  true
end
no_commit_message() click to toggle source
# File lib/gitlab_git/commit.rb, line 164
def no_commit_message
  "--no commit message"
end
parent_id() click to toggle source
# File lib/gitlab_git/commit.rb, line 137
def parent_id
  parent_ids.first
end
parents() click to toggle source
# File lib/gitlab_git/commit.rb, line 182
def parents
  raw_commit.parents
end
ref_names(repo) click to toggle source

Get ref names collection

Ex.

commit.ref_names(repo)
# File lib/gitlab_git/commit.rb, line 212
def ref_names(repo)
  refs(repo).map(&:name)
end
safe_message() click to toggle source
# File lib/gitlab_git/commit.rb, line 124
def safe_message
  @safe_message ||= message
end
sha() click to toggle source
# File lib/gitlab_git/commit.rb, line 116
def sha
  id
end
short_id(length = 10) click to toggle source
# File lib/gitlab_git/commit.rb, line 120
def short_id(length = 10)
  id.to_s[0..length]
end
stats() click to toggle source
# File lib/gitlab_git/commit.rb, line 190
def stats
  raw_commit.stats
end
to_diff() click to toggle source

Shows the diff between the commit's parent and the commit.

Cuts out the header and stats from to_patch and returns only the diff.

# File lib/gitlab_git/commit.rb, line 144
def to_diff
  # see Grit::Commit#show
  patch = to_patch

  # discard lines before the diff
  lines = patch.split("\n")
  while !lines.first.start_with?("diff --git") do
    lines.shift
  end
  lines.pop if lines.last =~ /^[\d.]+$/ # Git version
  lines.pop if lines.last == "-- "      # end of diff
  lines.join("\n")
end
to_hash() click to toggle source
# File lib/gitlab_git/commit.rb, line 168
def to_hash
  serialize_keys.map.with_object({}) do |key, hash|
    hash[key] = send(key)
  end
end
to_patch() click to toggle source
# File lib/gitlab_git/commit.rb, line 194
def to_patch
  raw_commit.to_patch
end
tree() click to toggle source
# File lib/gitlab_git/commit.rb, line 186
def tree
  raw_commit.tree
end

Private Instance Methods

init_from_grit(grit) click to toggle source
# File lib/gitlab_git/commit.rb, line 218
def init_from_grit(grit)
  @raw_commit = grit
  @id = grit.id
  @message = grit.message
  @authored_date = grit.authored_date
  @committed_date = grit.committed_date
  @author_name = grit.author.name
  @author_email = grit.author.email
  @committer_name = grit.committer.name
  @committer_email = grit.committer.email
  @parent_ids = grit.parents.map(&:id)
end
init_from_hash(hash) click to toggle source
# File lib/gitlab_git/commit.rb, line 231
def init_from_hash(hash)
  raw_commit = hash.symbolize_keys

  serialize_keys.each do |key|
    send("#{key}=", raw_commit[key])
  end
end
serialize_keys() click to toggle source
# File lib/gitlab_git/commit.rb, line 239
def serialize_keys
  SERIALIZE_KEYS
end