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
|
|
|
|
return '' if diff.blank?
|
|
|
|
|
|
|
|
encode_utf8(diff) if diff.respond_to?(:encoding)
|
|
|
|
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
|
2017-06-09 07:48:25 -04:00
|
|
|
end
|