base.rb 1.5 KB
Newer Older
1 2 3 4
module Gitlab
  module Diff
    module FileCollection
      class Base
D
Douwe Maan 已提交
5
        attr_reader :project, :diff_options, :diff_refs, :fallback_diff_refs
6 7 8

        delegate :count, :size, :real_size, to: :diff_files

9
        def self.default_options
10
          ::Commit.max_diff_options.merge(ignore_whitespace_change: false, expanded: false)
11 12
        end

13
        def initialize(diffable, project:, diff_options: nil, diff_refs: nil, fallback_diff_refs: nil)
14 15
          diff_options = self.class.default_options.merge(diff_options || {})

16 17 18
          @diffable = diffable
          @diffs = diffable.raw_diffs(diff_options)
          @project = project
19
          @diff_options = diff_options
20 21
          @diff_refs = diff_refs
          @fallback_diff_refs = fallback_diff_refs
22 23 24
        end

        def diff_files
25 26 27 28
          # n+1: https://gitlab.com/gitlab-org/gitlab-ce/issues/37445
          Gitlab::GitalyClient.allow_n_plus_1_calls do
            @diff_files ||= @diffs.decorate! { |diff| decorate_diff!(diff) }
          end
29 30
        end

31 32 33 34 35 36 37 38
        def diff_file_with_old_path(old_path)
          diff_files.find { |diff_file| diff_file.old_path == old_path }
        end

        def diff_file_with_new_path(new_path)
          diff_files.find { |diff_file| diff_file.new_path == new_path }
        end

39 40 41
        private

        def decorate_diff!(diff)
42
          Gitlab::Diff::File.new(diff, repository: project.repository, diff_refs: diff_refs, fallback_diff_refs: fallback_diff_refs)
43 44 45 46 47
        end
      end
    end
  end
end