gitlab-org--gitlab-foss/app/models/commit.rb

169 lines
3.7 KiB
Ruby
Raw Normal View History

2011-11-20 15:32:12 -05:00
class Commit
2012-01-28 09:47:55 -05:00
include ActiveModel::Conversion
include StaticModel
2012-01-28 09:47:55 -05:00
extend ActiveModel::Naming
# Safe amount of files with diffs in one commit to render
# Used to prevent 500 error on huge commits by suppressing diff
#
DIFF_SAFE_SIZE = 100
2012-09-27 02:20:36 -04:00
attr_accessor :commit, :head, :refs
delegate :message, :authored_date, :committed_date, :parents, :sha,
:date, :committer, :author, :message, :diffs, :tree, :id,
:to_patch, to: :commit
2011-11-27 10:35:49 -05:00
class << self
def find_or_first(repo, commit_id = nil, root_ref)
2012-03-09 14:43:46 -05:00
commit = if commit_id
repo.commit(commit_id)
else
repo.commits(root_ref).first
2012-03-09 14:43:46 -05:00
end
2012-09-27 02:20:36 -04:00
2012-03-09 14:43:46 -05:00
Commit.new(commit) if commit
end
def fresh_commits(repo, n = 10)
commits = repo.heads.map do |h|
repo.commits(h.name, n).map { |c| Commit.new(c, h) }
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)
commits = repo.branches.map { |ref| Commit.new(ref.commit, ref) }
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|
repo.log(h.name, nil, since: date).each { |c| Commit.new(c, h) }
2012-03-09 14:43:46 -05: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)
2012-03-09 14:43:46 -05:00
elsif limit && offset
repo.commits(ref, limit, offset)
else
repo.commits(ref)
end.map{ |c| Commit.new(c) }
2012-03-09 14:43:46 -05:00
end
def commits_between(repo, from, to)
repo.commits_between(from, to).map { |c| Commit.new(c) }
end
def compare(project, from, to)
result = {
commits: [],
diffs: [],
commit: nil,
same: false
}
return result unless from && to
first = project.commit(to.try(:strip))
last = project.commit(from.try(:strip))
if first && last
commits = [first, last].sort_by(&:created_at)
younger = commits.first
older = commits.last
result[:same] = (younger.id == older.id)
result[:commits] = project.repo.commits_between(younger.id, older.id).map {|c| Commit.new(c)}
result[:diffs] = project.repo.diff(younger.id, older.id) rescue []
result[:commit] = Commit.new(older)
end
result
end
2012-03-09 14:43:46 -05:00
end
2011-11-27 10:35:49 -05:00
def initialize(raw_commit, head = nil)
@commit = raw_commit
@head = head
end
def short_id(length = 10)
id.to_s[0..length]
end
2011-11-27 10:35:49 -05:00
def safe_message
@safe_message ||= message
2011-11-27 10:35:49 -05:00
end
def created_at
committed_date
end
def author_email
2011-12-30 08:41:39 -05:00
author.email
2011-11-27 10:35:49 -05:00
end
def author_name
author.name
2011-11-27 10:35:49 -05:00
end
2011-11-29 13:06:37 -05:00
2012-04-13 18:46:11 -04:00
# 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
2011-11-29 13:06:37 -05:00
def prev_commit
parents.try :first
2011-11-29 13:06:37 -05:00
end
2012-02-29 16:34:06 -05:00
def prev_commit_id
prev_commit.try :id
2012-02-29 16:34:06 -05:00
end
def parents_count
parents && parents.count || 0
end
2012-11-22 14:49:01 -05:00
# 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
# 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.join("\n")
end
2011-11-20 15:32:12 -05:00
end