2018-07-19 14:43:13 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-12-04 17:17:10 -05:00
|
|
|
class DiffFileEntity < DiffFileBaseEntity
|
2018-06-21 08:22:40 -04:00
|
|
|
include CommitsHelper
|
2018-02-27 19:10:43 -05:00
|
|
|
include IconsHelper
|
|
|
|
|
2018-06-21 08:22:40 -04:00
|
|
|
expose :added_lines
|
|
|
|
expose :removed_lines
|
|
|
|
|
2019-02-28 09:14:15 -05:00
|
|
|
expose :load_collapsed_diff_url, if: -> (diff_file, options) { options[:merge_request] } do |diff_file|
|
2018-06-21 08:22:40 -04:00
|
|
|
merge_request = options[:merge_request]
|
|
|
|
project = merge_request.target_project
|
|
|
|
|
|
|
|
next unless project
|
|
|
|
|
|
|
|
diff_for_path_namespace_project_merge_request_path(
|
|
|
|
namespace_id: project.namespace.to_param,
|
|
|
|
project_id: project.to_param,
|
|
|
|
id: merge_request.iid,
|
|
|
|
old_path: diff_file.old_path,
|
|
|
|
new_path: diff_file.new_path,
|
|
|
|
file_identifier: diff_file.file_identifier
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
expose :view_path, if: -> (_, options) { options[:merge_request] } do |diff_file|
|
|
|
|
merge_request = options[:merge_request]
|
|
|
|
|
|
|
|
project = merge_request.target_project
|
|
|
|
|
|
|
|
next unless project
|
2018-07-04 11:34:41 -04:00
|
|
|
next unless diff_file.content_sha
|
2018-06-21 08:22:40 -04:00
|
|
|
|
|
|
|
project_blob_path(project, tree_join(diff_file.content_sha, diff_file.new_path))
|
|
|
|
end
|
|
|
|
|
|
|
|
expose :replaced_view_path, if: -> (_, options) { options[:merge_request] } do |diff_file|
|
|
|
|
image_diff = diff_file.rich_viewer && diff_file.rich_viewer.partial_name == 'image'
|
|
|
|
image_replaced = diff_file.old_content_sha && diff_file.old_content_sha != diff_file.content_sha
|
|
|
|
|
|
|
|
merge_request = options[:merge_request]
|
|
|
|
project = merge_request.target_project
|
|
|
|
|
|
|
|
next unless project
|
|
|
|
|
|
|
|
project_blob_path(project, tree_join(diff_file.old_content_sha, diff_file.old_path)) if image_diff && image_replaced
|
|
|
|
end
|
|
|
|
|
|
|
|
expose :context_lines_path, if: -> (diff_file, _) { diff_file.text? } do |diff_file|
|
2018-07-04 11:34:41 -04:00
|
|
|
next unless diff_file.content_sha
|
|
|
|
|
2018-06-21 08:22:40 -04:00
|
|
|
project_blob_diff_path(diff_file.repository.project, tree_join(diff_file.content_sha, diff_file.file_path))
|
|
|
|
end
|
|
|
|
|
|
|
|
# Used for inline diffs
|
2020-03-02 19:08:11 -05:00
|
|
|
expose :highlighted_diff_lines, using: DiffLineEntity, if: -> (diff_file, options) { inline_diff_view?(options, diff_file) && diff_file.text? } do |diff_file|
|
2018-06-21 08:22:40 -04:00
|
|
|
diff_file.diff_lines_for_serializer
|
|
|
|
end
|
|
|
|
|
2019-04-01 13:29:10 -04:00
|
|
|
expose :is_fully_expanded do |diff_file|
|
2019-02-28 09:14:15 -05:00
|
|
|
diff_file.fully_expanded?
|
|
|
|
end
|
|
|
|
|
2018-06-21 08:22:40 -04:00
|
|
|
# Used for parallel diffs
|
2020-03-02 19:08:11 -05:00
|
|
|
expose :parallel_diff_lines, using: DiffLineParallelEntity, if: -> (diff_file, options) { parallel_diff_view?(options, diff_file) && diff_file.text? }
|
2019-11-10 19:06:26 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-03-02 19:08:11 -05:00
|
|
|
def parallel_diff_view?(options, diff_file)
|
|
|
|
return true unless Feature.enabled?(:single_mr_diff_view, diff_file.repository.project)
|
2019-11-10 19:06:26 -05:00
|
|
|
|
|
|
|
# If we're not rendering inline, we must be rendering parallel
|
2020-03-02 19:08:11 -05:00
|
|
|
!inline_diff_view?(options, diff_file)
|
2019-11-10 19:06:26 -05:00
|
|
|
end
|
|
|
|
|
2020-03-02 19:08:11 -05:00
|
|
|
def inline_diff_view?(options, diff_file)
|
|
|
|
return true unless Feature.enabled?(:single_mr_diff_view, diff_file.repository.project)
|
2019-11-10 19:06:26 -05:00
|
|
|
|
|
|
|
# If nothing is present, inline will be the default.
|
|
|
|
options.fetch(:diff_view, :inline).to_sym == :inline
|
|
|
|
end
|
2018-02-27 19:10:43 -05:00
|
|
|
end
|