2013-04-01 15:04:35 +02:00
|
|
|
# Gitlab::Git::Commit is a wrapper around native Grit::Commit object
|
2013-03-31 17:49:06 +02:00
|
|
|
# We dont want to use grit objects inside app/
|
|
|
|
# It helps us easily migrate to rugged in future
|
|
|
|
module Gitlab
|
|
|
|
module Git
|
2013-04-01 15:04:35 +02:00
|
|
|
class Commit
|
2013-03-31 17:49:06 +02:00
|
|
|
attr_accessor :raw_commit, :head, :refs
|
|
|
|
|
|
|
|
delegate :message, :authored_date, :committed_date, :parents, :sha,
|
2013-03-31 18:00:45 +02:00
|
|
|
:date, :committer, :author, :diffs, :tree, :id, :stats, :to_patch,
|
|
|
|
to: :raw_commit
|
2013-03-31 17:49:06 +02:00
|
|
|
|
|
|
|
class << self
|
|
|
|
def find_or_first(repo, commit_id = nil, root_ref)
|
|
|
|
commit = if commit_id
|
|
|
|
repo.commit(commit_id)
|
|
|
|
else
|
|
|
|
repo.commits(root_ref).first
|
|
|
|
end
|
|
|
|
|
2013-04-01 15:04:35 +02:00
|
|
|
Commit.new(commit) if commit
|
2013-03-31 17:49:06 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def fresh_commits(repo, n = 10)
|
|
|
|
commits = repo.heads.map do |h|
|
2013-04-01 15:04:35 +02:00
|
|
|
repo.commits(h.name, n).map { |c| Commit.new(c, h) }
|
2013-03-31 17:49:06 +02:00
|
|
|
end.flatten.uniq { |c| c.id }
|
|
|
|
|
|
|
|
commits.sort! do |x, y|
|
|
|
|
y.committed_date <=> x.committed_date
|
|
|
|
end
|
|
|
|
|
|
|
|
commits[0...n]
|
|
|
|
end
|
|
|
|
|
|
|
|
def commits_with_refs(repo, n = 20)
|
2013-04-01 15:04:35 +02:00
|
|
|
commits = repo.branches.map { |ref| Commit.new(ref.commit, ref) }
|
2013-03-31 17:49:06 +02:00
|
|
|
|
|
|
|
commits.sort! do |x, y|
|
|
|
|
y.committed_date <=> x.committed_date
|
|
|
|
end
|
|
|
|
|
|
|
|
commits[0..n]
|
|
|
|
end
|
|
|
|
|
|
|
|
def commits_since(repo, date)
|
|
|
|
commits = repo.heads.map do |h|
|
2013-04-01 15:04:35 +02:00
|
|
|
repo.log(h.name, nil, since: date).each { |c| Commit.new(c, h) }
|
2013-03-31 17:49:06 +02:00
|
|
|
end.flatten.uniq { |c| c.id }
|
|
|
|
|
|
|
|
commits.sort! do |x, y|
|
|
|
|
y.committed_date <=> x.committed_date
|
|
|
|
end
|
|
|
|
|
|
|
|
commits
|
|
|
|
end
|
|
|
|
|
|
|
|
def commits(repo, ref, path = nil, limit = nil, offset = nil)
|
|
|
|
if path
|
|
|
|
repo.log(ref, path, max_count: limit, skip: offset)
|
|
|
|
elsif limit && offset
|
|
|
|
repo.commits(ref, limit, offset)
|
|
|
|
else
|
|
|
|
repo.commits(ref)
|
2013-04-01 15:04:35 +02:00
|
|
|
end.map{ |c| Commit.new(c) }
|
2013-03-31 17:49:06 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def commits_between(repo, from, to)
|
2013-04-01 15:04:35 +02:00
|
|
|
repo.commits_between(from, to).map { |c| Commit.new(c) }
|
2013-03-31 17:49:06 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(raw_commit, head = nil)
|
|
|
|
raise "Nil as raw commit passed" unless raw_commit
|
|
|
|
|
|
|
|
@raw_commit = raw_commit
|
|
|
|
@head = head
|
|
|
|
end
|
|
|
|
|
|
|
|
def short_id(length = 10)
|
|
|
|
id.to_s[0..length]
|
|
|
|
end
|
|
|
|
|
|
|
|
def safe_message
|
|
|
|
@safe_message ||= message
|
|
|
|
end
|
|
|
|
|
|
|
|
def created_at
|
|
|
|
committed_date
|
|
|
|
end
|
|
|
|
|
|
|
|
def author_email
|
|
|
|
author.email
|
|
|
|
end
|
|
|
|
|
|
|
|
def author_name
|
|
|
|
author.name
|
|
|
|
end
|
|
|
|
|
|
|
|
# Was this commit committed by a different person than the original author?
|
|
|
|
def different_committer?
|
|
|
|
author_name != committer_name || author_email != committer_email
|
|
|
|
end
|
|
|
|
|
|
|
|
def committer_name
|
|
|
|
committer.name
|
|
|
|
end
|
|
|
|
|
|
|
|
def committer_email
|
|
|
|
committer.email
|
|
|
|
end
|
|
|
|
|
|
|
|
def prev_commit
|
|
|
|
@prev_commit ||= if parents.present?
|
2013-04-01 15:04:35 +02:00
|
|
|
Commit.new(parents.first)
|
2013-03-31 17:49:06 +02:00
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def prev_commit_id
|
|
|
|
prev_commit.try :id
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
def to_diff
|
2013-04-01 15:04:35 +02:00
|
|
|
# see Grit::Commit#show
|
2013-03-31 17:49:06 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
def has_zero_stats?
|
|
|
|
stats.total.zero?
|
|
|
|
rescue
|
|
|
|
true
|
|
|
|
end
|
2013-04-01 16:27:44 +02:00
|
|
|
|
|
|
|
def no_commit_message
|
|
|
|
"--no commit message"
|
|
|
|
end
|
2013-03-31 17:49:06 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|