diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-20 21:38:24 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-04-20 21:38:24 +0300 |
commit | 983a0bba5d2a042c4a3bbb22432ec192c7501d82 (patch) | |
tree | b153cd387c14ba23bd5a07514c7c01fddf6a78a0 /lib/gitlab/diff | |
parent | a2bddee2cdb38673df0e004d5b32d9f77797de64 (diff) |
Add latest changes from gitlab-org/gitlab@12-10-stable-ee
Diffstat (limited to 'lib/gitlab/diff')
-rw-r--r-- | lib/gitlab/diff/formatters/text_formatter.rb | 4 | ||||
-rw-r--r-- | lib/gitlab/diff/highlight_cache.rb | 41 |
2 files changed, 42 insertions, 3 deletions
diff --git a/lib/gitlab/diff/formatters/text_formatter.rb b/lib/gitlab/diff/formatters/text_formatter.rb index 5b670b1f83b..728457b3139 100644 --- a/lib/gitlab/diff/formatters/text_formatter.rb +++ b/lib/gitlab/diff/formatters/text_formatter.rb @@ -6,10 +6,12 @@ module Gitlab class TextFormatter < BaseFormatter attr_reader :old_line attr_reader :new_line + attr_reader :line_range def initialize(attrs) @old_line = attrs[:old_line] @new_line = attrs[:new_line] + @line_range = attrs[:line_range] super(attrs) end @@ -23,7 +25,7 @@ module Gitlab end def to_h - super.merge(old_line: old_line, new_line: new_line) + super.merge(old_line: old_line, new_line: new_line, line_range: line_range) end def line_age diff --git a/lib/gitlab/diff/highlight_cache.rb b/lib/gitlab/diff/highlight_cache.rb index 055eae2c0fd..01ec9798fe4 100644 --- a/lib/gitlab/diff/highlight_cache.rb +++ b/lib/gitlab/diff/highlight_cache.rb @@ -94,7 +94,11 @@ module Gitlab Gitlab::Redis::Cache.with do |redis| redis.pipelined do hash.each do |diff_file_id, highlighted_diff_lines_hash| - redis.hset(key, diff_file_id, highlighted_diff_lines_hash.to_json) + redis.hset( + key, + diff_file_id, + compose_data(highlighted_diff_lines_hash.to_json) + ) end # HSETs have to have their expiration date manually updated @@ -152,12 +156,45 @@ module Gitlab end results.map! do |result| - JSON.parse(result, symbolize_names: true) unless result.nil? + JSON.parse(extract_data(result), symbolize_names: true) unless result.nil? end file_paths.zip(results).to_h end + def compose_data(json_data) + if ::Feature.enabled?(:gzip_diff_cache, default_enabled: true) + # #compress returns ASCII-8BIT, so we need to force the encoding to + # UTF-8 before caching it in redis, else we risk encoding mismatch + # errors. + # + ActiveSupport::Gzip.compress(json_data).force_encoding("UTF-8") + else + json_data + end + rescue Zlib::GzipFile::Error + json_data + end + + def extract_data(data) + # Since when we deploy this code, we'll be dealing with an already + # populated cache full of data that isn't gzipped, we want to also + # check to see if the data is gzipped before we attempt to #decompress + # it, thus we check the first 2 bytes for "\x1F\x8B" to confirm it is + # a gzipped string. While a non-gzipped string will raise a + # Zlib::GzipFile::Error, which we're rescuing, we don't want to count + # on rescue for control flow. This check can be removed in the release + # after this change is released. + # + if ::Feature.enabled?(:gzip_diff_cache, default_enabled: true) && data[0..1] == "\x1F\x8B" + ActiveSupport::Gzip.decompress(data) + else + data + end + rescue Zlib::GzipFile::Error + data + end + def cacheable?(diff_file) diffable.present? && diff_file.text? && diff_file.diffable? end |