2014-01-22 08:19:02 -05:00
|
|
|
class MergeRequestDiff < ActiveRecord::Base
|
2015-02-05 17:20:55 -05:00
|
|
|
include Sortable
|
2016-06-01 12:03:51 -04:00
|
|
|
include Importable
|
2017-01-04 13:43:06 -05:00
|
|
|
include Gitlab::Git::EncodingHelper
|
2015-02-05 17:20:55 -05:00
|
|
|
|
2015-08-11 08:33:31 -04:00
|
|
|
# Prevent store of diff if commits amount more then 500
|
2016-03-04 09:17:12 -05:00
|
|
|
COMMITS_SAFE_SIZE = 100
|
2014-01-22 08:19:02 -05:00
|
|
|
|
2016-10-08 01:48:23 -04:00
|
|
|
# Valid types of serialized diffs allowed by Gitlab::Git::Diff
|
|
|
|
VALID_CLASSES = [Hash, Rugged::Patch, Rugged::Diff::Delta]
|
|
|
|
|
2014-01-22 08:19:02 -05:00
|
|
|
belongs_to :merge_request
|
|
|
|
|
2016-11-18 08:33:42 -05:00
|
|
|
serialize :st_commits
|
|
|
|
serialize :st_diffs
|
|
|
|
|
2014-01-22 08:19:02 -05:00
|
|
|
state_machine :state, initial: :empty do
|
|
|
|
state :collected
|
2016-03-03 12:38:44 -05:00
|
|
|
state :overflow
|
|
|
|
# Deprecated states: these are no longer used but these values may still occur
|
|
|
|
# in the database.
|
2014-01-22 08:19:02 -05:00
|
|
|
state :timeout
|
|
|
|
state :overflow_commits_safe_size
|
|
|
|
state :overflow_diff_files_limit
|
|
|
|
state :overflow_diff_lines_limit
|
|
|
|
end
|
|
|
|
|
2016-11-18 08:33:42 -05:00
|
|
|
scope :viewable, -> { without_state(:empty) }
|
2014-01-22 08:19:02 -05:00
|
|
|
|
2016-08-02 08:38:03 -04: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 15:36:18 -04:00
|
|
|
after_create :save_git_content, unless: :importing?
|
2016-07-27 07:41:19 -04:00
|
|
|
|
2016-08-09 08:16:50 -04:00
|
|
|
def self.select_without_diff
|
|
|
|
select(column_names - ['st_diffs'])
|
|
|
|
end
|
|
|
|
|
2016-09-22 05:28:36 -04:00
|
|
|
def st_commits
|
|
|
|
super || []
|
|
|
|
end
|
|
|
|
|
2016-07-28 06:46:27 -04:00
|
|
|
# Collect information about commits and diff from repository
|
|
|
|
# and save it to the database as serialized data
|
|
|
|
def save_git_content
|
2016-08-02 08:38:03 -04:00
|
|
|
ensure_commits_sha
|
2016-07-28 06:46:27 -04:00
|
|
|
save_commits
|
2016-08-02 08:38:03 -04:00
|
|
|
reload_commits
|
2016-07-28 06:46:27 -04:00
|
|
|
save_diffs
|
2016-08-02 08:38:03 -04:00
|
|
|
keep_around_commits
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_commits_sha
|
2016-08-19 05:35:16 -04:00
|
|
|
merge_request.fetch_ref
|
2016-08-02 08:38:03 -04:00
|
|
|
self.start_commit_sha ||= merge_request.target_branch_sha
|
2016-08-15 09:25:29 -04:00
|
|
|
self.head_commit_sha ||= merge_request.source_branch_sha
|
2016-08-02 08:38:03 -04:00
|
|
|
self.base_commit_sha ||= find_base_sha
|
2016-08-15 09:25:29 -04: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?
|
|
|
|
last_commit.try(:sha)
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
2014-01-22 08:19:02 -05:00
|
|
|
end
|
|
|
|
|
2016-08-15 08:20:36 -04: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 12:38:44 -05:00
|
|
|
def size
|
2016-08-03 17:32:12 -04:00
|
|
|
real_size.presence || raw_diffs.size
|
2014-01-22 08:19:02 -05:00
|
|
|
end
|
|
|
|
|
2016-08-05 22:03:01 -04:00
|
|
|
def raw_diffs(options = {})
|
2016-03-03 12:38:44 -05:00
|
|
|
if options[:ignore_whitespace_change]
|
2016-08-02 08:38:03 -04:00
|
|
|
@diffs_no_whitespace ||=
|
2016-08-25 03:59:30 -04:00
|
|
|
Gitlab::Git::Compare.new(
|
|
|
|
repository.raw_repository,
|
|
|
|
safe_start_commit_sha,
|
2016-08-25 04:58:17 -04:00
|
|
|
head_commit_sha).diffs(options)
|
2016-03-03 12:38:44 -05:00
|
|
|
else
|
2016-08-03 17:32:12 -04:00
|
|
|
@raw_diffs ||= {}
|
|
|
|
@raw_diffs[options] ||= load_diffs(st_diffs, options)
|
2016-03-03 12:38:44 -05:00
|
|
|
end
|
2015-10-21 20:55:35 -04:00
|
|
|
end
|
|
|
|
|
2014-01-22 08:19:02 -05:00
|
|
|
def commits
|
2016-09-22 05:28:36 -04:00
|
|
|
@commits ||= load_commits(st_commits)
|
2014-01-22 08:19:02 -05:00
|
|
|
end
|
|
|
|
|
2016-08-02 08:38:03 -04:00
|
|
|
def reload_commits
|
|
|
|
@commits = nil
|
|
|
|
commits
|
|
|
|
end
|
|
|
|
|
2014-01-22 08:19:02 -05:00
|
|
|
def last_commit
|
|
|
|
commits.first
|
|
|
|
end
|
|
|
|
|
2015-10-20 08:23:56 -04:00
|
|
|
def first_commit
|
|
|
|
commits.last
|
|
|
|
end
|
|
|
|
|
2016-01-20 12:44:27 -05:00
|
|
|
def base_commit
|
2016-07-27 07:41:19 -04:00
|
|
|
return unless base_commit_sha
|
2016-01-20 12:44:27 -05:00
|
|
|
|
2016-07-27 07:41:19 -04:00
|
|
|
project.commit(base_commit_sha)
|
2016-01-20 12:44:27 -05:00
|
|
|
end
|
|
|
|
|
2016-06-20 12:48:04 -04:00
|
|
|
def start_commit
|
2016-07-27 07:41:19 -04:00
|
|
|
return unless start_commit_sha
|
2016-06-20 12:48:04 -04:00
|
|
|
|
2016-07-27 07:41:19 -04:00
|
|
|
project.commit(start_commit_sha)
|
2016-06-20 12:48:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def head_commit
|
2016-08-15 09:25:29 -04:00
|
|
|
return unless head_commit_sha
|
2016-06-20 12:48:04 -04:00
|
|
|
|
2016-07-27 07:41:19 -04:00
|
|
|
project.commit(head_commit_sha)
|
2014-01-22 08:19:02 -05:00
|
|
|
end
|
|
|
|
|
2016-09-19 11:51:27 -04:00
|
|
|
def commits_sha
|
2016-12-01 06:17:30 -05:00
|
|
|
st_commits.map { |commit| commit[:id] }
|
2016-09-19 11:51:27 -04:00
|
|
|
end
|
|
|
|
|
2016-07-26 11:24:25 -04:00
|
|
|
def diff_refs
|
2016-08-15 11:11:18 -04:00
|
|
|
return unless start_commit_sha || base_commit_sha
|
2016-07-26 11:24:25 -04:00
|
|
|
|
|
|
|
Gitlab::Diff::DiffRefs.new(
|
|
|
|
base_sha: base_commit_sha,
|
|
|
|
start_sha: start_commit_sha,
|
|
|
|
head_sha: head_commit_sha
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2016-08-01 10:55:51 -04:00
|
|
|
def diff_refs_by_sha?
|
|
|
|
base_commit_sha? && head_commit_sha? && start_commit_sha?
|
|
|
|
end
|
|
|
|
|
2016-08-15 10:57:19 -04: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 08:19:02 -05:00
|
|
|
|
2016-07-27 07:41:19 -04:00
|
|
|
def compare
|
|
|
|
@compare ||=
|
2016-08-25 04:58:17 -04:00
|
|
|
Gitlab::Git::Compare.new(
|
|
|
|
repository.raw_repository,
|
|
|
|
safe_start_commit_sha,
|
|
|
|
head_commit_sha
|
|
|
|
)
|
2014-01-22 08:19:02 -05:00
|
|
|
end
|
|
|
|
|
2016-08-25 03:59:30 -04:00
|
|
|
def latest?
|
|
|
|
self == merge_request.merge_request_diff
|
|
|
|
end
|
|
|
|
|
2016-10-12 06:27:59 -04:00
|
|
|
def compare_with(sha, straight: true)
|
2016-09-29 07:04:50 -04:00
|
|
|
# When compare merge request versions we want diff A..B instead of A...B
|
2016-09-29 10:46:47 -04:00
|
|
|
# so we handle cases when user does squash and rebase of the commits between versions.
|
2016-09-29 07:04:50 -04:00
|
|
|
# For this reason we set straight to true by default.
|
2016-10-12 06:27:59 -04:00
|
|
|
CompareService.new.execute(project, head_commit_sha, project, sha, straight: straight)
|
2016-09-07 03:16:21 -04:00
|
|
|
end
|
|
|
|
|
2016-12-01 06:17:30 -05:00
|
|
|
def commits_count
|
|
|
|
st_commits.count
|
|
|
|
end
|
|
|
|
|
2016-08-15 10:57:19 -04:00
|
|
|
private
|
|
|
|
|
2016-10-08 01:48:23 -04: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
|
|
|
|
|
2016-07-06 19:29:14 -04:00
|
|
|
def dump_commits(commits)
|
|
|
|
commits.map(&:to_hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_commits(array)
|
|
|
|
array.map { |hash| Commit.new(Gitlab::Git::Commit.new(hash), merge_request.source_project) }
|
|
|
|
end
|
|
|
|
|
2016-07-28 06:46:27 -04:00
|
|
|
# Load all commits related to current merge request diff from repo
|
2014-01-22 08:19:02 -05:00
|
|
|
# and save it as array of hashes in st_commits db field
|
2016-07-28 06:46:27 -04:00
|
|
|
def save_commits
|
2016-06-24 12:30:33 -04:00
|
|
|
new_attributes = {}
|
|
|
|
|
2016-07-27 07:41:19 -04:00
|
|
|
commits = compare.commits
|
2014-01-22 08:19:02 -05:00
|
|
|
|
2016-07-27 07:41:19 -04:00
|
|
|
if commits.present?
|
|
|
|
commits = Commit.decorate(commits, merge_request.source_project).reverse
|
|
|
|
new_attributes[:st_commits] = dump_commits(commits)
|
2014-01-22 08:19:02 -05:00
|
|
|
end
|
|
|
|
|
2016-06-24 12:30:33 -04:00
|
|
|
update_columns_serialized(new_attributes)
|
2014-01-22 08:19:02 -05:00
|
|
|
end
|
|
|
|
|
2016-07-06 19:29:14 -04:00
|
|
|
def dump_diffs(diffs)
|
|
|
|
if diffs.respond_to?(:map)
|
|
|
|
diffs.map(&:to_hash)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_diffs(raw, options)
|
2016-10-08 01:48:23 -04:00
|
|
|
if valid_raw_diff?(raw)
|
2016-07-11 13:17:00 -04:00
|
|
|
if paths = options[:paths]
|
2016-06-28 12:25:32 -04:00
|
|
|
raw = raw.select do |diff|
|
2016-07-11 13:17:00 -04:00
|
|
|
paths.include?(diff[:old_path]) || paths.include?(diff[:new_path])
|
2016-06-28 12:25:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-06 19:29:14 -04:00
|
|
|
Gitlab::Git::DiffCollection.new(raw, options)
|
|
|
|
else
|
|
|
|
Gitlab::Git::DiffCollection.new([])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-28 06:46:27 -04:00
|
|
|
# Load diffs between branches related to current merge request diff from repo
|
2014-01-22 08:19:02 -05:00
|
|
|
# and save it as array of hashes in st_diffs db field
|
2016-07-28 06:46:27 -04:00
|
|
|
def save_diffs
|
2016-06-24 12:30:33 -04:00
|
|
|
new_attributes = {}
|
2014-01-22 08:19:02 -05:00
|
|
|
|
|
|
|
if commits.size.zero?
|
2016-06-24 12:30:33 -04:00
|
|
|
new_attributes[:state] = :empty
|
2014-01-22 08:19:02 -05:00
|
|
|
else
|
2016-07-27 07:41:19 -04:00
|
|
|
diff_collection = compare.diffs(Commit.max_diff_options)
|
2016-12-30 14:16:25 -05:00
|
|
|
new_attributes[:real_size] = compare.diffs.real_size
|
2014-01-22 08:19:02 -05:00
|
|
|
|
2016-03-03 12:38:44 -05:00
|
|
|
if diff_collection.any?
|
|
|
|
new_diffs = dump_diffs(diff_collection)
|
2016-06-24 12:30:33 -04:00
|
|
|
new_attributes[:state] = :collected
|
2016-03-03 12:38:44 -05:00
|
|
|
end
|
2016-12-30 14:16:25 -05:00
|
|
|
|
|
|
|
new_attributes[:st_diffs] = new_diffs || []
|
|
|
|
|
|
|
|
# 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 08:30:14 -05:00
|
|
|
end
|
2014-01-22 08:19:02 -05:00
|
|
|
|
2016-06-24 12:30:33 -04:00
|
|
|
update_columns_serialized(new_attributes)
|
2014-01-22 08:19:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def repository
|
2016-07-06 19:29:14 -04:00
|
|
|
project.repository
|
2014-01-22 08:19:02 -05:00
|
|
|
end
|
2014-05-07 09:14:24 -04:00
|
|
|
|
2016-07-28 04:36:30 -04:00
|
|
|
def find_base_sha
|
|
|
|
return unless head_commit_sha && start_commit_sha
|
2016-04-26 18:57:37 -04:00
|
|
|
|
2016-07-28 04:36:30 -04:00
|
|
|
project.merge_base_commit(head_commit_sha, start_commit_sha).try(:sha)
|
2016-04-26 18:57:37 -04:00
|
|
|
end
|
|
|
|
|
2016-07-01 07:03:37 -04:00
|
|
|
def utf8_st_diffs
|
|
|
|
st_diffs.map do |diff|
|
|
|
|
diff.each do |k, v|
|
|
|
|
diff[k] = encode_utf8(v) if v.respond_to?(:encoding)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-24 12:30:33 -04:00
|
|
|
#
|
|
|
|
# #save or #update_attributes providing changes on serialized attributes do a lot of
|
|
|
|
# serialization and deserialization calls resulting in bad performance.
|
|
|
|
# Using #update_columns solves the problem with just one YAML.dump per serialized attribute that we provide.
|
|
|
|
# As a tradeoff we need to reload the current instance to properly manage time objects on those serialized
|
|
|
|
# attributes. So to keep the same behaviour as the attribute assignment we reload the instance.
|
|
|
|
# The difference is in the usage of
|
|
|
|
# #write_attribute= (#update_attributes) and #raw_write_attribute= (#update_columns)
|
|
|
|
#
|
|
|
|
# Ex:
|
|
|
|
#
|
|
|
|
# new_attributes[:st_commits].first.slice(:committed_date)
|
|
|
|
# => {:committed_date=>2014-02-27 11:01:38 +0200}
|
|
|
|
# YAML.load(YAML.dump(new_attributes[:st_commits].first.slice(:committed_date)))
|
|
|
|
# => {:committed_date=>2014-02-27 10:01:38 +0100}
|
|
|
|
#
|
|
|
|
def update_columns_serialized(new_attributes)
|
|
|
|
return unless new_attributes.any?
|
|
|
|
|
|
|
|
update_columns(new_attributes.merge(updated_at: current_time_from_proper_timezone))
|
|
|
|
reload
|
|
|
|
end
|
2016-07-03 19:58:58 -04:00
|
|
|
|
2016-06-20 12:43:55 -04:00
|
|
|
def keep_around_commits
|
2016-10-24 12:08:30 -04: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 19:58:58 -04:00
|
|
|
end
|
2014-01-22 08:19:02 -05:00
|
|
|
end
|