Welcome to mirror list, hosted at ThFree Co, Russian Federation.

diff_file_entity.rb « serializers « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b2a544e1125c9f1618fa5e16572a5f82e867acf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# frozen_string_literal: true

class DiffFileEntity < DiffFileBaseEntity
  include CommitsHelper
  include IconsHelper
  include Gitlab::Utils::StrongMemoize

  expose :added_lines
  expose :removed_lines

  expose :load_collapsed_diff_url, if: -> (diff_file, options) { options[:merge_request] } do |diff_file|
    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
    next unless diff_file.content_sha

    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|
    next unless diff_file.content_sha

    project_blob_diff_path(diff_file.repository.project, tree_join(diff_file.content_sha, diff_file.file_path))
  end

  # Used for inline diffs
  expose :highlighted_diff_lines, using: DiffLineEntity, if: -> (diff_file, options) { inline_diff_view?(options) && diff_file.text? } do |diff_file|
    file = conflict_file(options, diff_file) || diff_file
    file.diff_lines_for_serializer
  end

  expose :is_fully_expanded do |diff_file|
    if conflict_file(options, diff_file)
      false
    else
      diff_file.fully_expanded?
    end
  end

  # Used for parallel diffs
  expose :parallel_diff_lines, using: DiffLineParallelEntity, if: -> (diff_file, options) { parallel_diff_view?(options) && diff_file.text? }

  expose :code_navigation_path, if: -> (diff_file) { options[:code_navigation_path] } do |diff_file|
    options[:code_navigation_path].full_json_path_for(diff_file.new_path)
  end

  private

  def parallel_diff_view?(options)
    diff_view(options) == :parallel
  end

  def inline_diff_view?(options)
    diff_view(options) == :inline
  end

  def diff_view(options)
    # If nothing is present, inline will be the default.
    options.fetch(:diff_view, :inline).to_sym
  end

  def conflict_file(options, diff_file)
    strong_memoize(:conflict_file) do
      options[:conflicts] && options[:conflicts][diff_file.new_path]
    end
  end
end