class Gitlab::Git::Commit

Constants

SERIALIZE_KEYS

Attributes

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

Public Class Methods

between(repo, base, head) click to toggle source

Get commits between two revspecs See also repository.commits_between

Ex.

Commit.between(repo, '29eda46b', 'master')
# File lib/gitlab_git/commit.rb, line 102
def between(repo, base, head)
  repo.commits_between(base, head).map do |commit|
    decorate(commit)
  end
rescue Rugged::ReferenceError
  []
end
decorate(commit, ref = nil) click to toggle source
# File lib/gitlab_git/commit.rb, line 115
def decorate(commit, ref = nil)
  Gitlab::Git::Commit.new(commit, ref)
end
diff_from_parent(rugged_commit, options = {}) click to toggle source

Returns a diff object for the changes introduced by rugged_commit. If rugged_commit doesn't have a parent, then the diff is between this commit and an empty repo. See Repository#diff for the keys allowed in the options hash.

# File lib/gitlab_git/commit.rb, line 123
def diff_from_parent(rugged_commit, options = {})
  options ||= {}
  break_rewrites = options[:break_rewrites]
  actual_options = Diff.filter_diff_options(options)

  diff = if rugged_commit.parents.empty?
           rugged_commit.diff(actual_options.merge(reverse: true))
         else
           rugged_commit.parents[0].diff(rugged_commit, actual_options)
         end

  diff.find_similar!(break_rewrites: break_rewrites)
  diff
end
find(repo, commit_id = "HEAD") click to toggle source

Get single commit

Ex.

Commit.find(repo, '29eda46b')

Commit.find(repo, 'master')
# File lib/gitlab_git/commit.rb, line 55
def find(repo, commit_id = "HEAD")
  return decorate(commit_id) if commit_id.is_a?(Rugged::Commit)

  obj = if commit_id.is_a?(String)
          repo.rev_parse_target(commit_id)
        else
          Ref.dereference_object(commit_id)
        end

  return nil unless obj.is_a?(Rugged::Commit)

  decorate(obj)
rescue Rugged::ReferenceError, Rugged::InvalidError, Rugged::ObjectError, Gitlab::Git::Repository::NoRepository
  nil
end
find_all(repo, options = {}) click to toggle source

Delegate Repository#find_commits

# File lib/gitlab_git/commit.rb, line 111
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 76
def last(repo)
  find(repo)
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 87
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 139
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)
  elsif raw_commit.is_a?(Rugged::Commit)
    init_from_rugged(raw_commit)
  else
    raise "Invalid raw commit type: #{raw_commit.class}"
  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 41
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

==(other) click to toggle source
# File lib/gitlab_git/commit.rb, line 17
def ==(other)
  return false unless other.is_a?(Gitlab::Git::Commit)

  methods = [:message, :parent_ids, :authored_date, :author_name,
             :author_email, :committed_date, :committer_name,
             :committer_email]

  methods.all? do |method|
    send(method) == other.send(method)
  end
end
author_email() click to toggle source
# File lib/gitlab_git/commit.rb, line 267
def author_email
  encode! @author_email
end
author_name() click to toggle source
# File lib/gitlab_git/commit.rb, line 263
def author_name
  encode! @author_name
end
committer_email() click to toggle source
# File lib/gitlab_git/commit.rb, line 275
def committer_email
  encode! @committer_email
end
committer_name() click to toggle source
# File lib/gitlab_git/commit.rb, line 271
def committer_name
  encode! @committer_name
end
created_at() click to toggle source
# File lib/gitlab_git/commit.rb, line 165
def created_at
  committed_date
end
date() click to toggle source
# File lib/gitlab_git/commit.rb, line 209
def date
  committed_date
end
diff_from_parent(options = {}) click to toggle source

Returns a diff object for the changes from this commit's first parent. If there is no parent, then the diff is between this commit and an empty repo. See Repository#diff for keys allowed in the options hash.

# File lib/gitlab_git/commit.rb, line 189
def diff_from_parent(options = {})
  Commit.diff_from_parent(raw_commit, options)
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 170
def different_committer?
  author_name != committer_name || author_email != committer_email
end
diffs(options = {}) click to toggle source
# File lib/gitlab_git/commit.rb, line 213
def diffs(options = {})
  DiffCollection.new(diff_from_parent(options), options)
end
has_zero_stats?() click to toggle source
# File lib/gitlab_git/commit.rb, line 193
def has_zero_stats?
  stats.total.zero?
rescue
  true
end
message() click to toggle source
# File lib/gitlab_git/commit.rb, line 259
def message
  encode! @message
end
no_commit_message() click to toggle source
# File lib/gitlab_git/commit.rb, line 199
def no_commit_message
  "--no commit message"
end
parent_id() click to toggle source
# File lib/gitlab_git/commit.rb, line 174
def parent_id
  parent_ids.first
end
parents() click to toggle source
# File lib/gitlab_git/commit.rb, line 217
def parents
  raw_commit.parents.map { |c| Gitlab::Git::Commit.new(c) }
end
ref_names(repo) click to toggle source

Get ref names collection

Ex.

commit.ref_names(repo)
# File lib/gitlab_git/commit.rb, line 253
def ref_names(repo)
  refs(repo).map do |ref|
    ref.name.sub(%r{^refs/(heads|remotes|tags)/}, "")
  end
end
safe_message() click to toggle source
# File lib/gitlab_git/commit.rb, line 161
def safe_message
  @safe_message ||= message
end
sha() click to toggle source
# File lib/gitlab_git/commit.rb, line 153
def sha
  id
end
short_id(length = 10) click to toggle source
# File lib/gitlab_git/commit.rb, line 157
def short_id(length = 10)
  id.to_s[0..length]
end
stats() click to toggle source
# File lib/gitlab_git/commit.rb, line 225
def stats
  Gitlab::Git::CommitStats.new(self)
end
to_diff(options = {}) 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 181
def to_diff(options = {})
  diff_from_parent(options).patch
end
to_hash() click to toggle source
# File lib/gitlab_git/commit.rb, line 203
def to_hash
  serialize_keys.map.with_object({}) do |key, hash|
    hash[key] = send(key)
  end
end
to_patch(options = {}) click to toggle source
# File lib/gitlab_git/commit.rb, line 229
def to_patch(options = {})
  begin
    raw_commit.to_mbox(options)
  rescue Rugged::InvalidError => ex
    if ex.message =~ /Commit \w+ is a merge commit/
      'Patch format is not currently supported for merge commits.'
    end
  end
end
tree() click to toggle source
# File lib/gitlab_git/commit.rb, line 221
def tree
  raw_commit.tree
end

Private Instance Methods

init_from_hash(hash) click to toggle source
# File lib/gitlab_git/commit.rb, line 281
def init_from_hash(hash)
  raw_commit = hash.symbolize_keys

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

  @raw_commit = commit
  @id = commit.oid
  @message = commit.message
  @authored_date = author[:time]
  @committed_date = committer[:time]
  @author_name = author[:name]
  @author_email = author[:email]
  @committer_name = committer[:name]
  @committer_email = committer[:email]
  @parent_ids = commit.parents.map(&:oid)
end
serialize_keys() click to toggle source
# File lib/gitlab_git/commit.rb, line 305
def serialize_keys
  SERIALIZE_KEYS
end