2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class MergeRequestDiffFile < ApplicationRecord
|
2021-01-27 07:09:01 -05:00
|
|
|
extend SuppressCompositePrimaryKeyWarning
|
|
|
|
|
2020-02-12 16:08:48 -05:00
|
|
|
include BulkInsertSafe
|
2017-06-09 07:48:25 -04:00
|
|
|
include Gitlab::EncodingHelper
|
2018-05-16 11:46:18 -04:00
|
|
|
include DiffFile
|
2017-06-09 07:48:25 -04:00
|
|
|
|
2019-01-09 12:01:28 -05:00
|
|
|
belongs_to :merge_request_diff, inverse_of: :merge_request_diff_files
|
2019-10-09 17:06:24 -04:00
|
|
|
alias_attribute :index, :relative_order
|
2017-06-09 07:48:25 -04:00
|
|
|
|
2020-10-27 11:08:39 -04:00
|
|
|
scope :by_paths, ->(paths) do
|
|
|
|
where("new_path in (?) OR old_path in (?)", paths, paths)
|
|
|
|
end
|
|
|
|
|
2017-06-09 07:48:25 -04:00
|
|
|
def utf8_diff
|
2022-07-06 14:08:29 -04:00
|
|
|
fetched_diff = if Feature.enabled?(:externally_stored_diffs_caching_export) &&
|
|
|
|
merge_request_diff&.stored_externally?
|
|
|
|
diff_export
|
|
|
|
else
|
|
|
|
diff
|
|
|
|
end
|
2017-06-09 07:48:25 -04:00
|
|
|
|
2022-06-16 20:09:23 -04:00
|
|
|
return '' if fetched_diff.blank?
|
|
|
|
|
|
|
|
encode_utf8(fetched_diff) if fetched_diff.respond_to?(:encoding)
|
2022-09-23 14:12:59 -04:00
|
|
|
rescue StandardError => e
|
|
|
|
log_exception('Failed fetching merge request diff', e)
|
|
|
|
|
|
|
|
''
|
2017-06-09 07:48:25 -04:00
|
|
|
end
|
2017-07-25 12:57:02 -04:00
|
|
|
|
|
|
|
def diff
|
2019-01-09 12:01:28 -05:00
|
|
|
content =
|
|
|
|
if merge_request_diff&.stored_externally?
|
|
|
|
merge_request_diff.opening_external_diff do |file|
|
|
|
|
file.seek(external_diff_offset)
|
2019-09-10 03:33:03 -04:00
|
|
|
force_encode_utf8(file.read(external_diff_size))
|
2019-01-09 12:01:28 -05:00
|
|
|
end
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2020-08-28 11:10:21 -04:00
|
|
|
return content unless binary?
|
|
|
|
|
|
|
|
# If the data isn't valid base64, return it as-is, since it's almost certain
|
|
|
|
# to be a valid diff. Parsing it as a diff will fail if it's something else.
|
|
|
|
#
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab/-/issues/240921
|
|
|
|
begin
|
|
|
|
content.unpack1('m0')
|
|
|
|
rescue ArgumentError
|
|
|
|
content
|
|
|
|
end
|
2017-07-25 12:57:02 -04:00
|
|
|
end
|
2022-07-05 14:08:43 -04:00
|
|
|
|
2022-07-06 14:08:29 -04:00
|
|
|
private
|
|
|
|
|
2022-07-05 14:08:43 -04:00
|
|
|
# This method is meant to be used during Project Export.
|
2022-07-06 14:08:29 -04:00
|
|
|
# It is identical to the behaviour in #diff with the only
|
2022-07-05 14:08:43 -04:00
|
|
|
# difference of caching externally stored diffs on local disk in
|
|
|
|
# temp storage location in order to improve diff export performance.
|
|
|
|
def diff_export
|
|
|
|
content = merge_request_diff.cached_external_diff do |file|
|
|
|
|
file.seek(external_diff_offset)
|
|
|
|
|
|
|
|
force_encode_utf8(file.read(external_diff_size))
|
|
|
|
end
|
|
|
|
|
|
|
|
# See #diff
|
|
|
|
if binary?
|
|
|
|
content = begin
|
|
|
|
content.unpack1('m0')
|
|
|
|
rescue ArgumentError
|
|
|
|
content
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-06 14:08:29 -04:00
|
|
|
content
|
|
|
|
rescue StandardError => e
|
2022-09-23 14:12:59 -04:00
|
|
|
log_exception('Cached external diff export failed', e)
|
|
|
|
|
|
|
|
diff
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_exception(message, exception)
|
2022-07-06 14:08:29 -04:00
|
|
|
log_payload = {
|
2022-09-23 14:12:59 -04:00
|
|
|
message: message,
|
2022-07-06 14:08:29 -04:00
|
|
|
merge_request_diff_file_id: id,
|
|
|
|
merge_request_diff_id: merge_request_diff&.id
|
|
|
|
}
|
2022-07-05 14:08:43 -04:00
|
|
|
|
2022-09-23 14:12:59 -04:00
|
|
|
Gitlab::ExceptionLogFormatter.format!(exception, log_payload)
|
2022-07-06 14:08:29 -04:00
|
|
|
Gitlab::AppLogger.warn(log_payload)
|
2022-07-05 14:08:43 -04:00
|
|
|
end
|
2017-06-09 07:48:25 -04:00
|
|
|
end
|