debian-mirror-gitlab/lib/gitlab/diff/file_collection/base.rb

79 lines
2.3 KiB
Ruby
Raw Normal View History

2016-09-13 17:45:13 +05:30
module Gitlab
module Diff
module FileCollection
class Base
2018-12-05 23:21:45 +05:30
include Gitlab::Utils::StrongMemoize
2018-11-20 20:47:30 +05:30
attr_reader :project, :diff_options, :diff_refs, :fallback_diff_refs, :diffable
2016-09-13 17:45:13 +05:30
delegate :count, :size, :real_size, to: :diff_files
def self.default_options
2018-12-05 23:21:45 +05:30
::Commit.max_diff_options.merge(ignore_whitespace_change: false, expanded: false, include_stats: true)
2016-09-13 17:45:13 +05:30
end
2017-09-10 17:25:29 +05:30
def initialize(diffable, project:, diff_options: nil, diff_refs: nil, fallback_diff_refs: nil)
2016-09-13 17:45:13 +05:30
diff_options = self.class.default_options.merge(diff_options || {})
2017-09-10 17:25:29 +05:30
@diffable = diffable
2018-12-05 23:21:45 +05:30
@include_stats = diff_options.delete(:include_stats)
2017-09-10 17:25:29 +05:30
@project = project
2016-09-13 17:45:13 +05:30
@diff_options = diff_options
2017-09-10 17:25:29 +05:30
@diff_refs = diff_refs
@fallback_diff_refs = fallback_diff_refs
2018-12-05 23:21:45 +05:30
@repository = project.repository
2016-09-13 17:45:13 +05:30
end
2018-12-13 13:39:08 +05:30
def diffs
@diffs ||= diffable.raw_diffs(diff_options)
end
2016-09-13 17:45:13 +05:30
def diff_files
2018-12-13 13:39:08 +05:30
@diff_files ||= diffs.decorate! { |diff| decorate_diff!(diff) }
2016-09-13 17:45:13 +05:30
end
2017-09-10 17:25:29 +05:30
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
2018-11-20 20:47:30 +05:30
def clear_cache
# No-op
end
def write_cache
# No-op
end
2016-09-13 17:45:13 +05:30
private
2018-12-05 23:21:45 +05:30
def diff_stats_collection
strong_memoize(:diff_stats) do
# There are scenarios where we don't need to request Diff Stats,
# when caching for instance.
next unless @include_stats
next unless diff_refs
@repository.diff_stats(diff_refs.base_sha, diff_refs.head_sha)
end
end
2016-09-13 17:45:13 +05:30
def decorate_diff!(diff)
2018-10-15 14:42:47 +05:30
return diff if diff.is_a?(File)
2018-12-05 23:21:45 +05:30
stats = diff_stats_collection&.find_by_path(diff.new_path)
Gitlab::Diff::File.new(diff,
repository: project.repository,
diff_refs: diff_refs,
fallback_diff_refs: fallback_diff_refs,
stats: stats)
2016-09-13 17:45:13 +05:30
end
end
end
end
end