gitlab-org--gitlab-foss/app/models/merge_request_diff.rb

249 lines
6.0 KiB
Ruby
Raw Normal View History

class MergeRequestDiff < ActiveRecord::Base
include Sortable
include Importable
include EncodingHelper
# Prevent store of diff if commits amount more then 500
COMMITS_SAFE_SIZE = 100
belongs_to :merge_request
delegate :source_branch_sha, :target_branch_sha,
:target_branch, :source_branch, to: :merge_request, prefix: nil
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.
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_initialize :set_diff_range
after_create :reload_content, unless: :importing?
after_save :keep_around_commits, unless: :importing?
def set_diff_range
self.start_commit_sha ||= target_branch_sha
self.head_commit_sha ||= source_branch_sha
self.base_commit_sha ||= branch_base_sha
end
def reload_content
reload_commits
reload_diffs
end
2016-03-03 17:38:44 +00:00
def size
real_size.presence || diffs.size
end
2016-03-03 17:38:44 +00:00
def diffs(options={})
if options[:ignore_whitespace_change]
@diffs_no_whitespace ||= begin
compare = Gitlab::Git::Compare.new(
repository.raw_repository,
start_commit_sha,
head_commit_sha
2016-03-03 17:38:44 +00:00
)
compare.diffs(options)
end
else
@diffs ||= {}
@diffs[options] ||= load_diffs(st_diffs, options)
2016-03-03 17:38:44 +00:00
end
end
def commits
@commits ||= load_commits(st_commits || [])
end
def last_commit
commits.first
end
def first_commit
commits.last
end
def base_commit
return unless base_commit_sha
project.commit(base_commit_sha)
end
def start_commit
return unless start_commit_sha
project.commit(start_commit_sha)
end
def head_commit
return last_commit unless head_commit_sha
project.commit(head_commit_sha)
end
def diff_refs
return unless start_commit || base_commit
Gitlab::Diff::DiffRefs.new(
base_sha: base_commit_sha,
start_sha: start_commit_sha,
head_sha: head_commit_sha
)
end
private
def compare
@compare ||=
begin
# Update ref for merge request
merge_request.fetch_ref
Gitlab::Git::Compare.new(
repository.raw_repository,
start_commit_sha,
head_commit_sha
)
end
end
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
# 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
new_attributes = {}
commits = compare.commits
if commits.present?
commits = Commit.decorate(commits, merge_request.source_project).reverse
new_attributes[:st_commits] = dump_commits(commits)
end
update_columns_serialized(new_attributes)
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)
if paths = options[:paths]
raw = raw.select do |diff|
paths.include?(diff[:old_path]) || paths.include?(diff[:new_path])
end
end
Gitlab::Git::DiffCollection.new(raw, options)
else
Gitlab::Git::DiffCollection.new([])
end
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_attributes = {}
new_diffs = []
if commits.size.zero?
new_attributes[:state] = :empty
else
diff_collection = compare.diffs(Commit.max_diff_options)
2016-03-03 17:38:44 +00:00
if diff_collection.overflow?
# Set our state to 'overflow' to make the #empty? and #collected?
# methods (generated by StateMachine) return false.
new_attributes[:state] = :overflow
end
new_attributes[:real_size] = diff_collection.real_size
2016-03-03 17:38:44 +00:00
if diff_collection.any?
new_diffs = dump_diffs(diff_collection)
new_attributes[:state] = :collected
2016-03-03 17:38:44 +00:00
end
end
new_attributes[:st_diffs] = new_diffs
update_columns_serialized(new_attributes)
keep_around_commits
end
def project
merge_request.target_project
end
def repository
project.repository
end
def branch_base_commit
return unless source_branch_sha && target_branch_sha
project.merge_base_commit(source_branch_sha, target_branch_sha)
end
def branch_base_sha
branch_base_commit.try(:sha)
end
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
#
# #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
def keep_around_commits
repository.keep_around(start_commit_sha)
repository.keep_around(head_commit_sha)
repository.keep_around(base_commit_sha)
end
end