From c86c1905b5574cac234315598d8d715fcaee3ea7 Mon Sep 17 00:00:00 2001 From: Paco Guzman Date: Wed, 27 Jul 2016 19:00:34 +0200 Subject: switch from diff_file_collection to diffs So we have raw_diffs too --- lib/gitlab/diff/file_collection.rb | 9 --------- lib/gitlab/diff/file_collection/base.rb | 17 +++++++++++------ lib/gitlab/diff/file_collection/commit.rb | 5 +---- lib/gitlab/diff/file_collection/compare.rb | 5 +---- lib/gitlab/diff/file_collection/merge_request.rb | 23 ++++++----------------- 5 files changed, 19 insertions(+), 40 deletions(-) delete mode 100644 lib/gitlab/diff/file_collection.rb (limited to 'lib/gitlab/diff') diff --git a/lib/gitlab/diff/file_collection.rb b/lib/gitlab/diff/file_collection.rb deleted file mode 100644 index ce6717c7205..00000000000 --- a/lib/gitlab/diff/file_collection.rb +++ /dev/null @@ -1,9 +0,0 @@ -module Gitlab - module Diff - module FileCollection - def self.default_options - ::Commit.max_diff_options.merge(ignore_whitespace_change: false, no_collapse: false) - end - end - end -end diff --git a/lib/gitlab/diff/file_collection/base.rb b/lib/gitlab/diff/file_collection/base.rb index a0c88265c45..2b9fc65b985 100644 --- a/lib/gitlab/diff/file_collection/base.rb +++ b/lib/gitlab/diff/file_collection/base.rb @@ -1,28 +1,33 @@ module Gitlab module Diff module FileCollection - class Base attr_reader :project, :diff_options, :diff_view, :diff_refs delegate :count, :size, :real_size, to: :diff_files - def initialize(diffs, project:, diff_options: nil, diff_refs: nil) - @diffs = diffs + def self.default_options + ::Commit.max_diff_options.merge(ignore_whitespace_change: false, no_collapse: false) + end + + def initialize(diffable, project:, diff_options: nil, diff_refs: nil) + diff_options = self.class.default_options.merge(diff_options || {}) + + @diffable = diffable + @diffs = diffable.raw_diffs(diff_options) @project = project @diff_options = diff_options @diff_refs = diff_refs end def diff_files - @diffs.decorate! { |diff| decorate_diff!(diff) } + @diff_files ||= @diffs.decorate! { |diff| decorate_diff!(diff) } end private def decorate_diff!(diff) - return diff if diff.is_a?(Gitlab::Diff::File) - Gitlab::Diff::File.new(diff, diff_refs: @diff_refs, repository: @project.repository) + Gitlab::Diff::File.new(diff, repository: project.repository, diff_refs: diff_refs) end end end diff --git a/lib/gitlab/diff/file_collection/commit.rb b/lib/gitlab/diff/file_collection/commit.rb index 19def300b74..4dc297ec036 100644 --- a/lib/gitlab/diff/file_collection/commit.rb +++ b/lib/gitlab/diff/file_collection/commit.rb @@ -3,10 +3,7 @@ module Gitlab module FileCollection class Commit < Base def initialize(commit, diff_options:) - # Not merge just set defaults - diff_options = diff_options || Gitlab::Diff::FileCollection.default_options - - super(commit.diffs(diff_options), + super(commit, project: commit.project, diff_options: diff_options, diff_refs: commit.diff_refs) diff --git a/lib/gitlab/diff/file_collection/compare.rb b/lib/gitlab/diff/file_collection/compare.rb index aba5a28b51f..20d8f891cc3 100644 --- a/lib/gitlab/diff/file_collection/compare.rb +++ b/lib/gitlab/diff/file_collection/compare.rb @@ -3,10 +3,7 @@ module Gitlab module FileCollection class Compare < Base def initialize(compare, project:, diff_options:, diff_refs: nil) - # Not merge just set defaults - diff_options = diff_options || Gitlab::Diff::FileCollection.default_options - - super(compare.diffs(diff_options), + super(compare, project: project, diff_options: diff_options, diff_refs: diff_refs) diff --git a/lib/gitlab/diff/file_collection/merge_request.rb b/lib/gitlab/diff/file_collection/merge_request.rb index 9fde0bba183..4f946908e2f 100644 --- a/lib/gitlab/diff/file_collection/merge_request.rb +++ b/lib/gitlab/diff/file_collection/merge_request.rb @@ -4,10 +4,8 @@ module Gitlab class MergeRequest < Base def initialize(merge_request, diff_options:) @merge_request = merge_request - # Not merge just set defaults - diff_options = diff_options || Gitlab::Diff::FileCollection.default_options - super(merge_request.diffs(diff_options), + super(merge_request, project: merge_request.project, diff_options: diff_options, diff_refs: merge_request.diff_refs) @@ -19,18 +17,11 @@ module Gitlab private - # Extracted method to highlight in the same iteration to the diff_collection. Iteration in the DiffCollections - # seems particularly slow on big diffs (event when already populated). + # Extracted method to highlight in the same iteration to the diff_collection. def decorate_diff!(diff) - highlight! super - end - - def highlight!(diff_file) - if cacheable? - cache_highlight!(diff_file) - else - diff_file # Don't need to eager load highlighted diff lines - end + diff_file = super + cache_highlight!(diff_file) if cacheable? + diff_file end def highlight_diff_file_from_cache!(diff_file, cache_diff_lines) @@ -55,15 +46,13 @@ module Gitlab else highlight_cache[file_path] = diff_file.highlighted_diff_lines.map(&:to_hash) end - - diff_file end def highlight_cache return @highlight_cache if defined?(@highlight_cache) @highlight_cache = Rails.cache.read(cache_key) || {} - @highlight_cache_was_empty = highlight_cache.empty? + @highlight_cache_was_empty = @highlight_cache.empty? @highlight_cache end -- cgit v1.2.3