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
|
2016-07-01 07:03:37 -04:00
|
|
|
include 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
|
|
|
|
|
|
|
belongs_to :merge_request
|
|
|
|
|
2016-06-20 12:48:04 -04:00
|
|
|
delegate :source_branch_sha, :target_branch_sha, :target_branch, :source_branch, to: :merge_request, prefix: nil
|
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
|
|
|
|
|
|
|
|
serialize :st_commits
|
|
|
|
serialize :st_diffs
|
|
|
|
|
2016-06-01 12:03:51 -04:00
|
|
|
after_create :reload_content, unless: :importing?
|
2016-07-04 12:35:56 -04:00
|
|
|
after_save :keep_around_commits, unless: :importing?
|
2014-01-22 08:19:02 -05:00
|
|
|
|
|
|
|
def reload_content
|
|
|
|
reload_commits
|
|
|
|
reload_diffs
|
|
|
|
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-03 17:32:12 -04:00
|
|
|
@raw_diffs_no_whitespace ||= begin
|
2016-03-03 12:38:44 -05:00
|
|
|
compare = Gitlab::Git::Compare.new(
|
2016-07-07 14:14:13 -04:00
|
|
|
repository.raw_repository,
|
2016-07-04 13:11:33 -04:00
|
|
|
self.start_commit_sha || self.target_branch_sha,
|
|
|
|
self.head_commit_sha || self.source_branch_sha,
|
2016-03-03 12:38:44 -05:00
|
|
|
)
|
|
|
|
compare.diffs(options)
|
|
|
|
end
|
|
|
|
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
|
|
|
|
@commits ||= load_commits(st_commits || [])
|
|
|
|
end
|
|
|
|
|
|
|
|
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-06-20 12:48:04 -04:00
|
|
|
return unless self.base_commit_sha
|
2016-01-20 12:44:27 -05:00
|
|
|
|
2016-07-06 19:29:14 -04:00
|
|
|
project.commit(self.base_commit_sha)
|
2016-01-20 12:44:27 -05:00
|
|
|
end
|
|
|
|
|
2016-06-20 12:48:04 -04:00
|
|
|
def start_commit
|
|
|
|
return unless self.start_commit_sha
|
|
|
|
|
2016-07-06 19:29:14 -04:00
|
|
|
project.commit(self.start_commit_sha)
|
2016-06-20 12:48:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def head_commit
|
|
|
|
return last_commit unless self.head_commit_sha
|
|
|
|
|
2016-07-06 19:29:14 -04:00
|
|
|
project.commit(self.head_commit_sha)
|
2014-01-22 08:19:02 -05:00
|
|
|
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-07-06 19:29:14 -04:00
|
|
|
def compare
|
|
|
|
@compare ||=
|
|
|
|
begin
|
|
|
|
# Update ref for merge request
|
|
|
|
merge_request.fetch_ref
|
2014-01-22 08:19:02 -05:00
|
|
|
|
2016-07-06 19:29:14 -04:00
|
|
|
Gitlab::Git::Compare.new(
|
2016-07-07 14:14:13 -04:00
|
|
|
repository.raw_repository,
|
2016-07-06 19:29:14 -04:00
|
|
|
self.target_branch_sha,
|
|
|
|
self.source_branch_sha
|
|
|
|
)
|
|
|
|
end
|
2014-01-22 08:19:02 -05:00
|
|
|
end
|
|
|
|
|
2016-07-06 19:29:14 -04:00
|
|
|
private
|
2014-01-22 08:19:02 -05:00
|
|
|
|
|
|
|
# Collect array of Git::Commit objects
|
|
|
|
# between target and source branches
|
|
|
|
def unmerged_commits
|
2016-03-03 12:38:44 -05:00
|
|
|
commits = compare.commits
|
2014-01-22 08:19:02 -05:00
|
|
|
|
|
|
|
if commits.present?
|
2016-05-18 12:14:55 -04:00
|
|
|
commits = Commit.decorate(commits, merge_request.source_project).reverse
|
2014-01-22 08:19:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
commits
|
|
|
|
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
|
|
|
|
|
2014-01-22 08:19:02 -05:00
|
|
|
# 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
|
2016-06-24 12:30:33 -04:00
|
|
|
new_attributes = {}
|
|
|
|
|
2014-01-22 08:19:02 -05:00
|
|
|
commit_objects = unmerged_commits
|
|
|
|
|
|
|
|
if commit_objects.present?
|
2016-06-24 12:30:33 -04:00
|
|
|
new_attributes[:st_commits] = dump_commits(commit_objects)
|
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
|
|
|
# Collect array of Git::Diff objects
|
|
|
|
# between target and source branches
|
|
|
|
def unmerged_diffs
|
|
|
|
compare.diffs(Commit.max_diff_options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def dump_diffs(diffs)
|
|
|
|
if diffs.respond_to?(:map)
|
|
|
|
diffs.map(&:to_hash)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_diffs(raw, options)
|
|
|
|
if raw.respond_to?(:each)
|
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
|
|
|
|
|
2014-01-22 08:19:02 -05:00
|
|
|
# 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
|
2016-06-24 12:30:33 -04:00
|
|
|
new_attributes = {}
|
2014-01-22 08:19:02 -05:00
|
|
|
new_diffs = []
|
|
|
|
|
|
|
|
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-03-03 12:38:44 -05:00
|
|
|
diff_collection = unmerged_diffs
|
2014-01-22 08:19:02 -05:00
|
|
|
|
2016-03-03 12:38:44 -05:00
|
|
|
if diff_collection.overflow?
|
|
|
|
# Set our state to 'overflow' to make the #empty? and #collected?
|
|
|
|
# methods (generated by StateMachine) return false.
|
2016-06-24 12:30:33 -04:00
|
|
|
new_attributes[:state] = :overflow
|
2014-01-22 08:19:02 -05:00
|
|
|
end
|
|
|
|
|
2016-06-24 12:30:33 -04:00
|
|
|
new_attributes[:real_size] = diff_collection.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
|
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
|
|
|
new_attributes[:st_diffs] = new_diffs
|
2016-07-03 19:58:58 -04:00
|
|
|
|
2016-06-20 12:43:55 -04:00
|
|
|
new_attributes[:start_commit_sha] = self.target_branch_sha
|
|
|
|
new_attributes[:head_commit_sha] = self.source_branch_sha
|
|
|
|
new_attributes[:base_commit_sha] = branch_base_sha
|
2016-01-20 12:44:27 -05:00
|
|
|
|
2016-06-24 12:30:33 -04:00
|
|
|
update_columns_serialized(new_attributes)
|
2016-06-20 12:43:55 -04:00
|
|
|
|
|
|
|
keep_around_commits
|
2014-01-22 08:19:02 -05:00
|
|
|
end
|
|
|
|
|
2016-07-06 19:29:14 -04:00
|
|
|
def project
|
|
|
|
merge_request.target_project
|
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-06-20 12:48:04 -04:00
|
|
|
def branch_base_commit
|
|
|
|
return unless self.source_branch_sha && self.target_branch_sha
|
2016-04-26 18:57:37 -04:00
|
|
|
|
2016-07-06 19:29:14 -04:00
|
|
|
project.merge_base_commit(self.source_branch_sha, self.target_branch_sha)
|
2016-04-26 18:57:37 -04:00
|
|
|
end
|
|
|
|
|
2016-06-20 12:48:04 -04:00
|
|
|
def branch_base_sha
|
|
|
|
branch_base_commit.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-07-07 14:14:13 -04:00
|
|
|
repository.keep_around(target_branch_sha)
|
|
|
|
repository.keep_around(source_branch_sha)
|
|
|
|
repository.keep_around(branch_base_sha)
|
2016-07-03 19:58:58 -04:00
|
|
|
end
|
2014-01-22 08:19:02 -05:00
|
|
|
end
|