2014-04-09 08:05:03 -04:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: merge_request_diffs
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
2014-08-25 05:25:02 -04:00
|
|
|
# state :string(255)
|
2014-04-09 08:05:03 -04:00
|
|
|
# st_commits :text
|
|
|
|
# st_diffs :text
|
|
|
|
# merge_request_id :integer not null
|
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
|
|
|
#
|
|
|
|
|
2014-01-22 08:19:02 -05:00
|
|
|
require Rails.root.join("app/models/commit")
|
|
|
|
|
|
|
|
class MergeRequestDiff < ActiveRecord::Base
|
2015-02-05 17:20:55 -05:00
|
|
|
include Sortable
|
|
|
|
|
2015-08-11 08:33:31 -04:00
|
|
|
# Prevent store of diff if commits amount more then 500
|
|
|
|
COMMITS_SAFE_SIZE = 500
|
2014-01-22 08:19:02 -05:00
|
|
|
|
|
|
|
attr_reader :commits, :diffs
|
|
|
|
|
|
|
|
belongs_to :merge_request
|
|
|
|
|
|
|
|
delegate :target_branch, :source_branch, to: :merge_request, prefix: nil
|
|
|
|
|
|
|
|
state_machine :state, initial: :empty do
|
|
|
|
state :collected
|
|
|
|
state :timeout
|
|
|
|
state :overflow_commits_safe_size
|
|
|
|
state :overflow_diff_files_limit
|
|
|
|
state :overflow_diff_lines_limit
|
|
|
|
end
|
|
|
|
|
|
|
|
serialize :st_commits
|
|
|
|
serialize :st_diffs
|
|
|
|
|
|
|
|
after_create :reload_content
|
|
|
|
|
|
|
|
def reload_content
|
|
|
|
reload_commits
|
|
|
|
reload_diffs
|
|
|
|
end
|
|
|
|
|
|
|
|
def diffs
|
|
|
|
@diffs ||= (load_diffs(st_diffs) || [])
|
|
|
|
end
|
|
|
|
|
|
|
|
def commits
|
|
|
|
@commits ||= load_commits(st_commits || [])
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_commit
|
|
|
|
commits.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_commit_short_sha
|
2014-10-10 08:39:48 -04:00
|
|
|
@last_commit_short_sha ||= last_commit.short_id
|
2014-01-22 08:19:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def dump_commits(commits)
|
|
|
|
commits.map(&:to_hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_commits(array)
|
2015-04-21 09:09:15 -04:00
|
|
|
array.map { |hash| Commit.new(Gitlab::Git::Commit.new(hash), merge_request.source_project) }
|
2014-01-22 08:19:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def dump_diffs(diffs)
|
|
|
|
if diffs.respond_to?(:map)
|
|
|
|
diffs.map(&:to_hash)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_diffs(raw)
|
|
|
|
if raw.respond_to?(:map)
|
|
|
|
raw.map { |hash| Gitlab::Git::Diff.new(hash) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Collect array of Git::Commit objects
|
|
|
|
# between target and source branches
|
|
|
|
def unmerged_commits
|
2014-07-29 05:21:03 -04:00
|
|
|
commits = compare_result.commits
|
2014-01-22 08:19:02 -05:00
|
|
|
|
|
|
|
if commits.present?
|
2015-04-21 09:09:15 -04:00
|
|
|
commits = Commit.decorate(commits, merge_request.source_project).
|
2014-01-22 08:19:02 -05:00
|
|
|
sort_by(&:created_at).
|
|
|
|
reverse
|
|
|
|
end
|
|
|
|
|
|
|
|
commits
|
|
|
|
end
|
|
|
|
|
|
|
|
# Reload all commits related to current merge request from repo
|
|
|
|
# and save it as array of hashes in st_commits db field
|
|
|
|
def reload_commits
|
|
|
|
commit_objects = unmerged_commits
|
|
|
|
|
|
|
|
if commit_objects.present?
|
|
|
|
self.st_commits = dump_commits(commit_objects)
|
|
|
|
end
|
|
|
|
|
|
|
|
save
|
|
|
|
end
|
|
|
|
|
|
|
|
# Reload diffs between branches related to current merge request from repo
|
|
|
|
# and save it as array of hashes in st_diffs db field
|
|
|
|
def reload_diffs
|
|
|
|
new_diffs = []
|
|
|
|
|
|
|
|
if commits.size.zero?
|
|
|
|
self.state = :empty
|
|
|
|
elsif commits.size > COMMITS_SAFE_SIZE
|
|
|
|
self.state = :overflow_commits_safe_size
|
|
|
|
else
|
|
|
|
new_diffs = unmerged_diffs
|
|
|
|
end
|
|
|
|
|
|
|
|
if new_diffs.any?
|
|
|
|
if new_diffs.size > Commit::DIFF_HARD_LIMIT_FILES
|
|
|
|
self.state = :overflow_diff_files_limit
|
2015-08-11 08:33:31 -04:00
|
|
|
new_diffs = new_diffs.first[Commit::DIFF_HARD_LIMIT_LINES]
|
2014-01-22 08:19:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
if new_diffs.sum { |diff| diff.diff.lines.count } > Commit::DIFF_HARD_LIMIT_LINES
|
|
|
|
self.state = :overflow_diff_lines_limit
|
2015-08-11 08:33:31 -04:00
|
|
|
new_diffs = new_diffs.first[Commit::DIFF_HARD_LIMIT_LINES]
|
2014-01-22 08:19:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-22 08:30:14 -05:00
|
|
|
if new_diffs.present?
|
|
|
|
new_diffs = dump_commits(new_diffs)
|
|
|
|
self.state = :collected
|
|
|
|
end
|
2014-01-22 08:19:02 -05:00
|
|
|
|
|
|
|
self.st_diffs = new_diffs
|
|
|
|
self.save
|
|
|
|
end
|
|
|
|
|
|
|
|
# Collect array of Git::Diff objects
|
|
|
|
# between target and source branches
|
|
|
|
def unmerged_diffs
|
2014-07-29 05:21:03 -04:00
|
|
|
diffs = compare_result.diffs
|
2014-01-22 08:19:02 -05:00
|
|
|
diffs ||= []
|
|
|
|
diffs
|
2014-02-12 05:54:49 -05:00
|
|
|
rescue Gitlab::Git::Diff::TimeoutError => ex
|
|
|
|
self.state = :timeout
|
|
|
|
diffs = []
|
2014-01-22 08:19:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def repository
|
|
|
|
merge_request.target_project.repository
|
|
|
|
end
|
2014-05-07 09:14:24 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2014-07-29 05:21:03 -04:00
|
|
|
def compare_result
|
2015-08-11 08:33:31 -04:00
|
|
|
@compare_result ||=
|
|
|
|
begin
|
|
|
|
# Update ref for merge request
|
|
|
|
merge_request.fetch_ref
|
|
|
|
|
|
|
|
# Get latest sha of branch from source project
|
|
|
|
source_sha = merge_request.source_project.commit(source_branch).sha
|
|
|
|
|
|
|
|
Gitlab::CompareResult.new(
|
|
|
|
Gitlab::Git::Compare.new(
|
|
|
|
merge_request.target_project.repository.raw_repository,
|
|
|
|
merge_request.target_branch,
|
|
|
|
source_sha,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
2014-05-07 09:14:24 -04:00
|
|
|
end
|
2014-01-22 08:19:02 -05:00
|
|
|
end
|