Class: Projects::MergeRequests::DiffsController

Inherits:
ApplicationController
  • Object
show all
Includes:
DiffHelper, Gitlab::Cache::Helpers, RendersNotes
Defined in:
app/controllers/projects/merge_requests/diffs_controller.rb

Constant Summary

Constants included from Gitlab::Cache::Helpers

Gitlab::Cache::Helpers::DEFAULT_EXPIRY

Instance Method Summary collapse

Methods included from Gitlab::Cache::Helpers

#cache, #render_cached

Methods included from RendersNotes

#prepare_notes_for_rendering

Methods included from DiffHelper

#apply_diff_view_cookie!, #collapsed_diff_url, #diff_file_blob_raw_path, #diff_file_blob_raw_url, #diff_file_html_data, #diff_file_old_blob_raw_path, #diff_file_old_blob_raw_url, #diff_file_stats_data, #diff_line_content, #diff_link_number, #diff_match_line, #diff_nomappinginraw_line, #diff_options, #diff_view, #diffs_expanded?, #editable_diff?, #inline_diff_btn, #mark_inline_diffs, #parallel_diff_btn, #parallel_diff_discussions, #render_fork_suggestion, #render_overflow_warning?, #show_only_context_commits?, #submodule_diff_compare_link, #submodule_link

Instance Method Details

#diff_for_pathObject


28
29
30
# File 'app/controllers/projects/merge_requests/diffs_controller.rb', line 28

def diff_for_path
  render_diffs
end

#diffs_batchObject


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
# File 'app/controllers/projects/merge_requests/diffs_controller.rb', line 32

def diffs_batch
  diff_options_hash = diff_options
  diff_options_hash[:paths] = params[:paths] if params[:paths]

  diffs = @compare.diffs_in_batch(params[:page], params[:per_page], diff_options: diff_options_hash)
  unfoldable_positions = @merge_request.note_positions_for_paths(diffs.diff_file_paths, current_user).unfoldable

  diffs.unfold_diff_files(unfoldable_positions)
  diffs.write_cache

  options = {
    merge_request: @merge_request,
    commit: commit,
    diff_view: diff_view,
    merge_ref_head_diff: render_merge_ref_head_diff?,
    pagination_data: diffs.pagination_data,
    allow_tree_conflicts: display_merge_conflicts_in_diff?
  }

  if diff_options_hash[:paths].blank?
    # NOTE: Any variables that would affect the resulting json needs to be added to the cache_context to avoid stale cache issues.
    cache_context = [
      current_user&.cache_key,
      unfoldable_positions.map(&:to_h),
      diff_view,
      params[:w],
      params[:expanded],
      params[:page],
      params[:per_page],
      options[:merge_ref_head_diff],
      options[:allow_tree_conflicts]
    ]

    render_cached(
      diffs,
      with: PaginatedDiffSerializer.new(current_user: current_user),
      cache_context: -> (_) { [Digest::SHA256.hexdigest(cache_context.to_s)] },
      **options
    )
  else
    render json: PaginatedDiffSerializer.new(current_user: current_user).represent(diffs, options)
  end
end

#diffs_metadataObject


76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/controllers/projects/merge_requests/diffs_controller.rb', line 76

def 
  diffs = @compare.diffs(diff_options)

  options = additional_attributes.merge(
    only_context_commits: show_only_context_commits?,
    merge_ref_head_diff: render_merge_ref_head_diff?,
    allow_tree_conflicts: display_merge_conflicts_in_diff?
  )

  render json: DiffsMetadataSerializer.new(project: @merge_request.project, current_user: current_user)
                 .represent(diffs, options)
end

#showObject


24
25
26
# File 'app/controllers/projects/merge_requests/diffs_controller.rb', line 24

def show
  render_diffs
end