debian-mirror-gitlab/app/models/merge_request_diff_file.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

93 lines
2.3 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2019-07-07 11:18:12 +05:30
class MergeRequestDiffFile < ApplicationRecord
2021-03-11 19:13:27 +05:30
extend SuppressCompositePrimaryKeyWarning
2020-03-13 15:44:24 +05:30
include BulkInsertSafe
2017-09-10 17:25:29 +05:30
include Gitlab::EncodingHelper
2018-11-08 19:23:39 +05:30
include DiffFile
2017-09-10 17:25:29 +05:30
2019-03-02 22:35:43 +05:30
belongs_to :merge_request_diff, inverse_of: :merge_request_diff_files
2019-12-21 20:55:43 +05:30
alias_attribute :index, :relative_order
2017-09-10 17:25:29 +05:30
2021-01-29 00:20:46 +05:30
scope :by_paths, ->(paths) do
where("new_path in (?) OR old_path in (?)", paths, paths)
end
2017-09-10 17:25:29 +05:30
def utf8_diff
2023-01-13 00:05:48 +05:30
fetched_diff = merge_request_diff&.stored_externally? ? diff_export : diff
2017-09-10 17:25:29 +05:30
2022-07-23 23:45:48 +05:30
return '' if fetched_diff.blank?
encode_utf8(fetched_diff) if fetched_diff.respond_to?(:encoding)
2022-11-25 23:54:43 +05:30
rescue StandardError => e
log_exception('Failed fetching merge request diff', e)
''
2017-09-10 17:25:29 +05:30
end
def diff
2019-03-02 22:35:43 +05:30
content =
if merge_request_diff&.stored_externally?
merge_request_diff.opening_external_diff do |file|
file.seek(external_diff_offset)
2019-12-04 20:38:33 +05:30
force_encode_utf8(file.read(external_diff_size))
2019-03-02 22:35:43 +05:30
end
else
super
end
2020-11-24 15:15:51 +05:30
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-09-10 17:25:29 +05:30
end
2022-08-13 15:12:31 +05:30
private
# This method is meant to be used during Project Export.
# It is identical to the behaviour in #diff with the only
# 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
content
rescue StandardError => e
2022-11-25 23:54:43 +05:30
log_exception('Cached external diff export failed', e)
diff
end
def log_exception(message, exception)
2022-08-13 15:12:31 +05:30
log_payload = {
2022-11-25 23:54:43 +05:30
message: message,
2022-08-13 15:12:31 +05:30
merge_request_diff_file_id: id,
merge_request_diff_id: merge_request_diff&.id
}
2022-11-25 23:54:43 +05:30
Gitlab::ExceptionLogFormatter.format!(exception, log_payload)
2022-08-13 15:12:31 +05:30
Gitlab::AppLogger.warn(log_payload)
end
2017-09-10 17:25:29 +05:30
end