Commit 7018edbe authored by Jan Provaznik's avatar Jan Provaznik

Merge branch '199843-fix-viewing-nil-blobs' into 'master'

Fix display of some overflowing merge request diffs

Closes #199843

See merge request gitlab-org/gitlab!29267
parents fafe1608 901d20bc
---
title: Fix display of some overflowing merge request diffs
merge_request: 29267
author:
type: fixed
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
# This has been extracted from https://github.com/github/linguist/blob/master/lib/linguist/blob_helper.rb # This has been extracted from https://github.com/github/linguist/blob/master/lib/linguist/blob_helper.rb
module Gitlab module Gitlab
module BlobHelper module BlobHelper
include Gitlab::Utils::StrongMemoize
def extname def extname
File.extname(name.to_s) File.extname(name.to_s)
end end
...@@ -120,8 +122,18 @@ module Gitlab ...@@ -120,8 +122,18 @@ module Gitlab
end end
def encoded_newlines_re def encoded_newlines_re
@encoded_newlines_re ||= strong_memoize(:encoded_newlines_re) do
Regexp.union(["\r\n", "\r", "\n"].map { |nl| nl.encode(ruby_encoding, "ASCII-8BIT").force_encoding(data.encoding) }) 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 end
def ruby_encoding def ruby_encoding
......
...@@ -652,4 +652,16 @@ describe Gitlab::Git::Blob, :seed_helper do ...@@ -652,4 +652,16 @@ describe Gitlab::Git::Blob, :seed_helper do
expect(described_class).to respond_to(:gitlab_blob_size) expect(described_class).to respond_to(:gitlab_blob_size)
end end
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 end
...@@ -22,7 +22,11 @@ module FakeBlobHelpers ...@@ -22,7 +22,11 @@ module FakeBlobHelpers
alias_method :name, :path alias_method :name, :path
def id def id
0 "00000000"
end
def commit_id
"11111111"
end end
def binary_in_repo? 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