Commit 901d20bc authored by Nick Thomas's avatar Nick Thomas

Fix display of some overflowing merge request diffs

MR diffs that overflow may create blobs where `ruby_encoding: nil`.
parent cbb0a680
---
title: Fix display of some overflowing merge request diffs
merge_request: 29267
author:
type: fixed
......@@ -3,6 +3,8 @@
# This has been extracted from https://github.com/github/linguist/blob/master/lib/linguist/blob_helper.rb
module Gitlab
module BlobHelper
include Gitlab::Utils::StrongMemoize
def extname
File.extname(name.to_s)
end
......@@ -120,8 +122,18 @@ module Gitlab
end
def encoded_newlines_re
@encoded_newlines_re ||=
Regexp.union(["\r\n", "\r", "\n"].map { |nl| nl.encode(ruby_encoding, "ASCII-8BIT").force_encoding(data.encoding) })
strong_memoize(:encoded_newlines_re) do
newlines = ["\r\n", "\r", "\n"]
data_encoding = data&.encoding
if ruby_encoding && data_encoding
newlines.map! do |nl|
nl.encode(ruby_encoding, "ASCII-8BIT").force_encoding(data_encoding)
end
end
Regexp.union(newlines)
end
end
def ruby_encoding
......
......@@ -652,4 +652,16 @@ describe Gitlab::Git::Blob, :seed_helper do
expect(described_class).to respond_to(:gitlab_blob_size)
end
end
describe '#lines' do
context 'when the encoding cannot be detected' do
it 'successfully splits the data' do
data = "test\nblob"
blob = Gitlab::Git::Blob.new(name: 'test', size: data.bytesize, data: data)
expect(blob).to receive(:ruby_encoding) { nil }
expect(blob.lines).to eq(data.split("\n"))
end
end
end
end
......@@ -22,7 +22,11 @@ module FakeBlobHelpers
alias_method :name, :path
def id
0
"00000000"
end
def commit_id
"11111111"
end
def binary_in_repo?
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment