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

47 lines
1.1 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
return '' if diff.blank?
encode_utf8(diff) if diff.respond_to?(:encoding)
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
end