2014-01-22 13:19:02 +00:00
|
|
|
class MergeRequestDiff < ActiveRecord::Base
|
2015-02-05 22:20:55 +00:00
|
|
|
include Sortable
|
2016-06-01 16:03:51 +00:00
|
|
|
include Importable
|
2017-06-01 21:21:14 +00:00
|
|
|
include Gitlab::EncodingHelper
|
2015-02-05 22:20:55 +00:00
|
|
|
|
2015-08-11 12:33:31 +00:00
|
|
|
# Prevent store of diff if commits amount more then 500
|
2016-03-04 14:17:12 +00:00
|
|
|
COMMITS_SAFE_SIZE = 100
|
2014-01-22 13:19:02 +00:00
|
|
|
|
2016-10-08 05:48:23 +00:00
|
|
|
# Valid types of serialized diffs allowed by Gitlab::Git::Diff
|
2017-02-21 23:32:18 +00:00
|
|
|
VALID_CLASSES = [Hash, Rugged::Patch, Rugged::Diff::Delta].freeze
|
2016-10-08 05:48:23 +00:00
|
|
|
|
2014-01-22 13:19:02 +00:00
|
|
|
belongs_to :merge_request
|
2017-06-09 11:48:25 +00:00
|
|
|
has_many :merge_request_diff_files, -> { order(:merge_request_diff_id, :relative_order) }
|
2017-06-16 14:00:58 +00:00
|
|
|
has_many :merge_request_diff_commits, -> { order(:merge_request_diff_id, :relative_order) }
|
2014-01-22 13:19:02 +00:00
|
|
|
|
2017-07-03 14:01:41 +00:00
|
|
|
serialize :st_commits # rubocop:disable Cop/ActiveRecordSerialize
|
|
|
|
serialize :st_diffs # rubocop:disable Cop/ActiveRecordSerialize
|
2016-11-18 13:33:42 +00:00
|
|
|
|
2014-01-22 13:19:02 +00:00
|
|
|
state_machine :state, initial: :empty do
|
|
|
|
state :collected
|
2016-03-03 17:38:44 +00:00
|
|
|
state :overflow
|
|
|
|
# Deprecated states: these are no longer used but these values may still occur
|
|
|
|
# in the database.
|
2014-01-22 13:19:02 +00:00
|
|
|
state :timeout
|
|
|
|
state :overflow_commits_safe_size
|
|
|
|
state :overflow_diff_files_limit
|
|
|
|
state :overflow_diff_lines_limit
|
|
|
|
end
|
|
|
|
|
2016-11-18 13:33:42 +00:00
|
|
|
scope :viewable, -> { without_state(:empty) }
|
2014-01-22 13:19:02 +00:00
|
|
|
|
2016-08-02 12:38:03 +00:00
|
|
|
# All diff information is collected from repository after object is created.
|
|
|
|
# It allows you to override variables like head_commit_sha before getting diff.
|
2016-07-28 19:36:18 +00:00
|
|
|
after_create :save_git_content, unless: :importing?
|
2016-07-27 11:41:19 +00:00
|
|
|
|
2017-04-06 22:13:28 +00:00
|
|
|
def self.find_by_diff_refs(diff_refs)
|
2017-04-06 22:24:51 +00:00
|
|
|
find_by(start_commit_sha: diff_refs.start_sha, head_commit_sha: diff_refs.head_sha, base_commit_sha: diff_refs.base_sha)
|
2017-04-06 22:13:28 +00:00
|
|
|
end
|
|
|
|
|
2016-08-09 12:16:50 +00:00
|
|
|
def self.select_without_diff
|
|
|
|
select(column_names - ['st_diffs'])
|
|
|
|
end
|
|
|
|
|
2016-09-22 09:28:36 +00:00
|
|
|
def st_commits
|
|
|
|
super || []
|
|
|
|
end
|
|
|
|
|
2016-07-28 10:46:27 +00:00
|
|
|
# Collect information about commits and diff from repository
|
|
|
|
# and save it to the database as serialized data
|
|
|
|
def save_git_content
|
2017-06-16 14:00:58 +00:00
|
|
|
ensure_commit_shas
|
2016-07-28 10:46:27 +00:00
|
|
|
save_commits
|
|
|
|
save_diffs
|
2016-08-02 12:38:03 +00:00
|
|
|
keep_around_commits
|
|
|
|
end
|
|
|
|
|
2017-06-16 14:00:58 +00:00
|
|
|
def ensure_commit_shas
|
2016-08-19 09:35:16 +00:00
|
|
|
merge_request.fetch_ref
|
2016-08-02 12:38:03 +00:00
|
|
|
self.start_commit_sha ||= merge_request.target_branch_sha
|
2016-08-15 13:25:29 +00:00
|
|
|
self.head_commit_sha ||= merge_request.source_branch_sha
|
2016-08-02 12:38:03 +00:00
|
|
|
self.base_commit_sha ||= find_base_sha
|
2016-08-15 13:25:29 +00:00
|
|
|
save
|
|
|
|
end
|
|
|
|
|
|
|
|
# Override head_commit_sha to keep compatibility with merge request diff
|
|
|
|
# created before version 8.4 that does not store head_commit_sha in separate db field.
|
|
|
|
def head_commit_sha
|
|
|
|
if persisted? && super.nil?
|
2017-06-16 14:00:58 +00:00
|
|
|
last_commit_sha
|
2016-08-15 13:25:29 +00:00
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
2014-01-22 13:19:02 +00:00
|
|
|
end
|
|
|
|
|
2016-08-15 12:20:36 +00:00
|
|
|
# This method will rely on repository branch sha
|
|
|
|
# in case start_commit_sha is nil. Its necesarry for old merge request diff
|
|
|
|
# created before version 8.4 to work
|
|
|
|
def safe_start_commit_sha
|
|
|
|
start_commit_sha || merge_request.target_branch_sha
|
|
|
|
end
|
|
|
|
|
2016-03-03 17:38:44 +00:00
|
|
|
def size
|
2016-08-03 21:32:12 +00:00
|
|
|
real_size.presence || raw_diffs.size
|
2014-01-22 13:19:02 +00:00
|
|
|
end
|
|
|
|
|
2016-08-06 02:03:01 +00:00
|
|
|
def raw_diffs(options = {})
|
2016-03-03 17:38:44 +00:00
|
|
|
if options[:ignore_whitespace_change]
|
2016-08-02 12:38:03 +00:00
|
|
|
@diffs_no_whitespace ||=
|
2016-08-25 07:59:30 +00:00
|
|
|
Gitlab::Git::Compare.new(
|
|
|
|
repository.raw_repository,
|
|
|
|
safe_start_commit_sha,
|
2016-08-25 08:58:17 +00:00
|
|
|
head_commit_sha).diffs(options)
|
2016-03-03 17:38:44 +00:00
|
|
|
else
|
2016-08-03 21:32:12 +00:00
|
|
|
@raw_diffs ||= {}
|
2017-06-09 11:48:25 +00:00
|
|
|
@raw_diffs[options] ||= load_diffs(options)
|
2016-03-03 17:38:44 +00:00
|
|
|
end
|
2015-10-22 00:55:35 +00:00
|
|
|
end
|
|
|
|
|
2014-01-22 13:19:02 +00:00
|
|
|
def commits
|
2017-06-16 14:00:58 +00:00
|
|
|
@commits ||= load_commits
|
2014-01-22 13:19:02 +00:00
|
|
|
end
|
|
|
|
|
2017-06-16 14:00:58 +00:00
|
|
|
def last_commit_sha
|
|
|
|
commit_shas.first
|
2014-01-22 13:19:02 +00:00
|
|
|
end
|
|
|
|
|
2015-10-20 12:23:56 +00:00
|
|
|
def first_commit
|
|
|
|
commits.last
|
|
|
|
end
|
|
|
|
|
2016-01-20 17:44:27 +00:00
|
|
|
def base_commit
|
2016-07-27 11:41:19 +00:00
|
|
|
return unless base_commit_sha
|
2016-01-20 17:44:27 +00:00
|
|
|
|
2016-07-27 11:41:19 +00:00
|
|
|
project.commit(base_commit_sha)
|
2016-01-20 17:44:27 +00:00
|
|
|
end
|
|
|
|
|
2016-06-20 16:48:04 +00:00
|
|
|
def start_commit
|
2016-07-27 11:41:19 +00:00
|
|
|
return unless start_commit_sha
|
2016-06-20 16:48:04 +00:00
|
|
|
|
2016-07-27 11:41:19 +00:00
|
|
|
project.commit(start_commit_sha)
|
2016-06-20 16:48:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def head_commit
|
2016-08-15 13:25:29 +00:00
|
|
|
return unless head_commit_sha
|
2016-06-20 16:48:04 +00:00
|
|
|
|
2016-07-27 11:41:19 +00:00
|
|
|
project.commit(head_commit_sha)
|
2014-01-22 13:19:02 +00:00
|
|
|
end
|
|
|
|
|
2017-06-16 14:00:58 +00:00
|
|
|
def commit_shas
|
|
|
|
if st_commits.present?
|
|
|
|
st_commits.map { |commit| commit[:id] }
|
|
|
|
else
|
|
|
|
merge_request_diff_commits.map(&:sha)
|
|
|
|
end
|
2016-09-19 15:51:27 +00:00
|
|
|
end
|
|
|
|
|
2017-04-10 20:17:47 +00:00
|
|
|
def diff_refs=(new_diff_refs)
|
2017-04-11 15:51:48 +00:00
|
|
|
self.base_commit_sha = new_diff_refs&.base_sha
|
|
|
|
self.start_commit_sha = new_diff_refs&.start_sha
|
|
|
|
self.head_commit_sha = new_diff_refs&.head_sha
|
2017-04-10 20:17:47 +00:00
|
|
|
end
|
|
|
|
|
2016-07-26 15:24:25 +00:00
|
|
|
def diff_refs
|
2016-08-15 15:11:18 +00:00
|
|
|
return unless start_commit_sha || base_commit_sha
|
2016-07-26 15:24:25 +00:00
|
|
|
|
|
|
|
Gitlab::Diff::DiffRefs.new(
|
|
|
|
base_sha: base_commit_sha,
|
|
|
|
start_sha: start_commit_sha,
|
|
|
|
head_sha: head_commit_sha
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2017-05-15 18:19:49 +00:00
|
|
|
# MRs created before 8.4 don't store their true diff refs (start and base),
|
|
|
|
# but we need to get a commit SHA for the "View file @ ..." link by a file,
|
2017-05-16 23:58:23 +00:00
|
|
|
# so we use an approximation of the diff refs if we can't get the actual one.
|
|
|
|
#
|
2017-05-15 18:19:49 +00:00
|
|
|
# These will not be the actual diff refs if the target branch was merged into
|
|
|
|
# the source branch after the merge request was created, but it is good enough
|
|
|
|
# for the specific purpose of linking to a commit.
|
2017-05-16 23:58:23 +00:00
|
|
|
#
|
2017-05-15 18:19:49 +00:00
|
|
|
# It is not good enough for highlighting diffs, so we can't simply pass
|
|
|
|
# these as `diff_refs.`
|
|
|
|
def fallback_diff_refs
|
2017-05-15 18:44:15 +00:00
|
|
|
real_refs = diff_refs
|
|
|
|
return real_refs if real_refs
|
|
|
|
|
2017-05-15 18:19:49 +00:00
|
|
|
likely_base_commit_sha = (first_commit&.parent || first_commit)&.sha
|
|
|
|
|
|
|
|
Gitlab::Diff::DiffRefs.new(
|
|
|
|
base_sha: likely_base_commit_sha,
|
|
|
|
start_sha: safe_start_commit_sha,
|
|
|
|
head_sha: head_commit_sha
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2016-08-01 14:55:51 +00:00
|
|
|
def diff_refs_by_sha?
|
|
|
|
base_commit_sha? && head_commit_sha? && start_commit_sha?
|
|
|
|
end
|
|
|
|
|
2016-08-15 14:57:19 +00:00
|
|
|
def diffs(diff_options = nil)
|
|
|
|
Gitlab::Diff::FileCollection::MergeRequestDiff.new(self, diff_options: diff_options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def project
|
|
|
|
merge_request.target_project
|
|
|
|
end
|
2014-01-22 13:19:02 +00:00
|
|
|
|
2016-07-27 11:41:19 +00:00
|
|
|
def compare
|
|
|
|
@compare ||=
|
2016-08-25 08:58:17 +00:00
|
|
|
Gitlab::Git::Compare.new(
|
|
|
|
repository.raw_repository,
|
|
|
|
safe_start_commit_sha,
|
|
|
|
head_commit_sha
|
|
|
|
)
|
2014-01-22 13:19:02 +00:00
|
|
|
end
|
|
|
|
|
2016-08-25 07:59:30 +00:00
|
|
|
def latest?
|
|
|
|
self == merge_request.merge_request_diff
|
|
|
|
end
|
|
|
|
|
2017-05-21 20:38:33 +00:00
|
|
|
def compare_with(sha)
|
2016-09-29 11:04:50 +00:00
|
|
|
# When compare merge request versions we want diff A..B instead of A...B
|
2016-09-29 14:46:47 +00:00
|
|
|
# so we handle cases when user does squash and rebase of the commits between versions.
|
2016-09-29 11:04:50 +00:00
|
|
|
# For this reason we set straight to true by default.
|
2017-05-21 20:38:33 +00:00
|
|
|
CompareService.new(project, head_commit_sha).execute(project, sha, straight: true)
|
2016-09-07 07:16:21 +00:00
|
|
|
end
|
|
|
|
|
2016-12-01 11:17:30 +00:00
|
|
|
def commits_count
|
2017-06-16 14:00:58 +00:00
|
|
|
if st_commits.present?
|
|
|
|
st_commits.size
|
|
|
|
else
|
|
|
|
merge_request_diff_commits.size
|
|
|
|
end
|
2016-12-01 11:17:30 +00:00
|
|
|
end
|
|
|
|
|
2017-04-04 16:34:19 +00:00
|
|
|
def utf8_st_diffs
|
|
|
|
return [] if st_diffs.blank?
|
|
|
|
|
|
|
|
st_diffs.map do |diff|
|
|
|
|
diff.each do |k, v|
|
|
|
|
diff[k] = encode_utf8(v) if v.respond_to?(:encoding)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-15 14:57:19 +00:00
|
|
|
private
|
|
|
|
|
2016-10-08 05:48:23 +00:00
|
|
|
# Old GitLab implementations may have generated diffs as ["--broken-diff"].
|
|
|
|
# Avoid an error 500 by ignoring bad elements. See:
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab-ce/issues/20776
|
|
|
|
def valid_raw_diff?(raw)
|
|
|
|
return false unless raw.respond_to?(:each)
|
|
|
|
|
|
|
|
raw.any? { |element| VALID_CLASSES.include?(element.class) }
|
|
|
|
end
|
|
|
|
|
2017-06-09 11:48:25 +00:00
|
|
|
def create_merge_request_diff_files(diffs)
|
|
|
|
rows = diffs.map.with_index do |diff, index|
|
|
|
|
diff.to_hash.merge(
|
|
|
|
merge_request_diff_id: self.id,
|
|
|
|
relative_order: index
|
|
|
|
)
|
2016-07-06 23:29:14 +00:00
|
|
|
end
|
2017-06-09 11:48:25 +00:00
|
|
|
|
|
|
|
Gitlab::Database.bulk_insert('merge_request_diff_files', rows)
|
2016-07-06 23:29:14 +00:00
|
|
|
end
|
|
|
|
|
2017-06-09 11:48:25 +00:00
|
|
|
def load_diffs(options)
|
|
|
|
return Gitlab::Git::DiffCollection.new([]) unless diffs_from_database
|
2016-06-28 16:25:32 +00:00
|
|
|
|
2017-06-09 11:48:25 +00:00
|
|
|
raw = diffs_from_database
|
|
|
|
|
|
|
|
if paths = options[:paths]
|
|
|
|
raw = raw.select do |diff|
|
|
|
|
paths.include?(diff[:old_path]) || paths.include?(diff[:new_path])
|
|
|
|
end
|
2016-07-06 23:29:14 +00:00
|
|
|
end
|
2017-06-09 11:48:25 +00:00
|
|
|
|
|
|
|
Gitlab::Git::DiffCollection.new(raw, options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def diffs_from_database
|
|
|
|
return @diffs_from_database if defined?(@diffs_from_database)
|
|
|
|
|
|
|
|
@diffs_from_database =
|
|
|
|
if st_diffs.present?
|
|
|
|
if valid_raw_diff?(st_diffs)
|
|
|
|
st_diffs
|
|
|
|
end
|
|
|
|
elsif merge_request_diff_files.present?
|
|
|
|
merge_request_diff_files
|
|
|
|
.as_json(only: Gitlab::Git::Diff::SERIALIZE_KEYS)
|
|
|
|
.map(&:with_indifferent_access)
|
|
|
|
end
|
2016-07-06 23:29:14 +00:00
|
|
|
end
|
|
|
|
|
2017-06-16 14:00:58 +00:00
|
|
|
def load_commits
|
|
|
|
commits = st_commits.presence || merge_request_diff_commits
|
|
|
|
|
|
|
|
commits.map do |commit|
|
|
|
|
Commit.new(Gitlab::Git::Commit.new(commit.to_hash), merge_request.source_project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-28 10:46:27 +00:00
|
|
|
def save_diffs
|
2016-06-24 16:30:33 +00:00
|
|
|
new_attributes = {}
|
2014-01-22 13:19:02 +00:00
|
|
|
|
2017-06-16 14:00:58 +00:00
|
|
|
if compare.commits.size.zero?
|
2016-06-24 16:30:33 +00:00
|
|
|
new_attributes[:state] = :empty
|
2014-01-22 13:19:02 +00:00
|
|
|
else
|
2016-07-27 11:41:19 +00:00
|
|
|
diff_collection = compare.diffs(Commit.max_diff_options)
|
2017-04-20 14:47:32 +00:00
|
|
|
new_attributes[:real_size] = diff_collection.real_size
|
2014-01-22 13:19:02 +00:00
|
|
|
|
2016-03-03 17:38:44 +00:00
|
|
|
if diff_collection.any?
|
2016-06-24 16:30:33 +00:00
|
|
|
new_attributes[:state] = :collected
|
2016-12-30 19:16:25 +00:00
|
|
|
|
2017-06-09 11:48:25 +00:00
|
|
|
create_merge_request_diff_files(diff_collection)
|
|
|
|
end
|
2016-12-30 19:16:25 +00:00
|
|
|
|
|
|
|
# Set our state to 'overflow' to make the #empty? and #collected?
|
|
|
|
# methods (generated by StateMachine) return false.
|
|
|
|
#
|
|
|
|
# This attribution has to come at the end of the method so 'overflow'
|
|
|
|
# state does not get overridden by 'collected'.
|
|
|
|
new_attributes[:state] = :overflow if diff_collection.overflow?
|
2014-01-22 13:30:14 +00:00
|
|
|
end
|
2014-01-22 13:19:02 +00:00
|
|
|
|
2017-06-16 14:00:58 +00:00
|
|
|
update(new_attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
def save_commits
|
|
|
|
MergeRequestDiffCommit.create_bulk(self.id, compare.commits.reverse)
|
|
|
|
|
|
|
|
merge_request_diff_commits.reload
|
2014-01-22 13:19:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def repository
|
2016-07-06 23:29:14 +00:00
|
|
|
project.repository
|
2014-01-22 13:19:02 +00:00
|
|
|
end
|
2014-05-07 13:14:24 +00:00
|
|
|
|
2016-07-28 08:36:30 +00:00
|
|
|
def find_base_sha
|
|
|
|
return unless head_commit_sha && start_commit_sha
|
2016-04-26 22:57:37 +00:00
|
|
|
|
2016-07-28 08:36:30 +00:00
|
|
|
project.merge_base_commit(head_commit_sha, start_commit_sha).try(:sha)
|
2016-04-26 22:57:37 +00:00
|
|
|
end
|
|
|
|
|
2016-06-20 16:43:55 +00:00
|
|
|
def keep_around_commits
|
2016-10-24 16:08:30 +00:00
|
|
|
[repository, merge_request.source_project.repository].each do |repo|
|
|
|
|
repo.keep_around(start_commit_sha)
|
|
|
|
repo.keep_around(head_commit_sha)
|
|
|
|
repo.keep_around(base_commit_sha)
|
|
|
|
end
|
2016-07-03 23:58:58 +00:00
|
|
|
end
|
2014-01-22 13:19:02 +00:00
|
|
|
end
|