2011-11-20 21:32:12 +01:00
|
|
|
class Commit
|
2012-01-28 15:47:55 +01:00
|
|
|
include ActiveModel::Conversion
|
2012-09-02 05:39:28 +02:00
|
|
|
include StaticModel
|
2012-01-28 15:47:55 +01:00
|
|
|
extend ActiveModel::Naming
|
2011-11-28 10:46:41 +01:00
|
|
|
|
2012-10-25 11:16:14 +02:00
|
|
|
# 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 08:20:36 +02:00
|
|
|
attr_accessor :commit, :head, :refs
|
|
|
|
|
|
|
|
delegate :message, :authored_date, :committed_date, :parents, :sha,
|
2013-01-04 21:13:32 +01:00
|
|
|
:date, :committer, :author, :diffs, :tree, :id, :stats,
|
2012-09-27 08:20:36 +02:00
|
|
|
:to_patch, to: :commit
|
2011-11-27 16:35:49 +01:00
|
|
|
|
2012-09-02 05:39:28 +02:00
|
|
|
class << self
|
2012-05-16 15:34:10 +02:00
|
|
|
def find_or_first(repo, commit_id = nil, root_ref)
|
2012-03-09 20:43:46 +01:00
|
|
|
commit = if commit_id
|
|
|
|
repo.commit(commit_id)
|
|
|
|
else
|
2012-05-16 15:34:10 +02:00
|
|
|
repo.commits(root_ref).first
|
2012-03-09 20:43:46 +01:00
|
|
|
end
|
2012-09-27 08:20:36 +02:00
|
|
|
|
2012-03-09 20:43:46 +01: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|
|
2012-08-11 00:07:50 +02:00
|
|
|
repo.log(h.name, nil, since: date).each { |c| Commit.new(c, h) }
|
2012-03-09 20:43:46 +01: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
|
2012-08-11 00:07:50 +02:00
|
|
|
repo.log(ref, path, max_count: limit, skip: offset)
|
2012-03-09 20:43:46 +01:00
|
|
|
elsif limit && offset
|
|
|
|
repo.commits(ref, limit, offset)
|
|
|
|
else
|
|
|
|
repo.commits(ref)
|
2012-05-21 22:17:41 +02:00
|
|
|
end.map{ |c| Commit.new(c) }
|
2012-03-09 20:43:46 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def commits_between(repo, from, to)
|
|
|
|
repo.commits_between(from, to).map { |c| Commit.new(c) }
|
|
|
|
end
|
2012-07-20 07:39:34 +02:00
|
|
|
|
|
|
|
def compare(project, from, to)
|
2012-09-02 05:39:28 +02:00
|
|
|
result = {
|
2012-08-11 00:07:50 +02:00
|
|
|
commits: [],
|
|
|
|
diffs: [],
|
2012-09-21 10:22:53 +02:00
|
|
|
commit: nil,
|
|
|
|
same: false
|
2012-07-20 07:39:34 +02:00
|
|
|
}
|
|
|
|
|
2012-09-21 10:22:53 +02:00
|
|
|
return result unless from && to
|
|
|
|
|
2013-01-03 20:09:18 +01:00
|
|
|
first = project.repository.commit(to.try(:strip))
|
|
|
|
last = project.repository.commit(from.try(:strip))
|
2012-09-21 10:22:53 +02:00
|
|
|
|
2012-07-20 07:39:34 +02:00
|
|
|
if first && last
|
2012-12-20 13:50:43 +01:00
|
|
|
result[:same] = (first.id == last.id)
|
|
|
|
result[:commits] = project.repo.commits_between(last.id, first.id).map {|c| Commit.new(c)}
|
|
|
|
result[:diffs] = project.repo.diff(last.id, first.id) rescue []
|
|
|
|
result[:commit] = Commit.new(first)
|
2012-07-20 07:39:34 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
2012-03-09 20:43:46 +01:00
|
|
|
end
|
|
|
|
|
2011-11-27 16:35:49 +01:00
|
|
|
def initialize(raw_commit, head = nil)
|
2013-01-04 23:35:38 +01:00
|
|
|
raise "Nil as raw commit passed" unless raw_commit
|
|
|
|
|
2011-11-27 16:35:49 +01:00
|
|
|
@commit = raw_commit
|
|
|
|
@head = head
|
|
|
|
end
|
|
|
|
|
2012-07-27 10:22:05 +02:00
|
|
|
def short_id(length = 10)
|
|
|
|
id.to_s[0..length]
|
|
|
|
end
|
|
|
|
|
2011-11-27 16:35:49 +01:00
|
|
|
def safe_message
|
2012-11-08 18:41:07 +01:00
|
|
|
@safe_message ||= message
|
2011-11-27 16:35:49 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def created_at
|
|
|
|
committed_date
|
|
|
|
end
|
|
|
|
|
|
|
|
def author_email
|
2011-12-30 14:41:39 +01:00
|
|
|
author.email
|
2011-11-27 16:35:49 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def author_name
|
2012-11-08 18:41:07 +01:00
|
|
|
author.name
|
2011-11-27 16:35:49 +01:00
|
|
|
end
|
2011-11-29 19:06:37 +01:00
|
|
|
|
2012-04-14 00:46:11 +02: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
|
|
|
|
|
2012-01-12 00:12:49 +01:00
|
|
|
def committer_name
|
2012-11-08 18:41:07 +01:00
|
|
|
committer.name
|
2012-01-12 00:12:49 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def committer_email
|
|
|
|
committer.email
|
|
|
|
end
|
|
|
|
|
2011-11-29 19:06:37 +01:00
|
|
|
def prev_commit
|
2013-01-04 23:35:38 +01:00
|
|
|
@prev_commit ||= if parents.present?
|
|
|
|
Commit.new(parents.first)
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
2011-11-29 19:06:37 +01:00
|
|
|
end
|
2012-02-29 22:34:06 +01:00
|
|
|
|
|
|
|
def prev_commit_id
|
2012-04-19 22:35:35 +02:00
|
|
|
prev_commit.try :id
|
2012-02-29 22:34:06 +01:00
|
|
|
end
|
2012-07-27 10:22:05 +02:00
|
|
|
|
2012-11-22 20:49:01 +01: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
|
2012-12-22 20:36:33 +01:00
|
|
|
lines.pop if lines.last =~ /^[\d.]+$/ # Git version
|
|
|
|
lines.pop if lines.last == "-- " # end of diff
|
2012-11-22 20:49:01 +01:00
|
|
|
lines.join("\n")
|
|
|
|
end
|
2011-11-20 21:32:12 +01:00
|
|
|
end
|