diff_helper.rb 4.32 KB
Newer Older
1
module DiffHelper
2 3 4
  def mark_inline_diffs(old_line, new_line)
    old_diffs, new_diffs = Gitlab::Diff::InlineDiff.new(old_line, new_line).inline_diffs

5 6
    marked_old_line = Gitlab::Diff::InlineDiffMarker.new(old_line).mark(old_diffs, mode: :deletion)
    marked_new_line = Gitlab::Diff::InlineDiffMarker.new(new_line).mark(new_diffs, mode: :addition)
7 8 9 10

    [marked_old_line, marked_new_line]
  end

11
  def diff_view
12 13 14 15 16 17 18
    diff_views = %w(inline parallel)

    if diff_views.include?(cookies[:diff_view])
      cookies[:diff_view]
    else
      diff_views.first
    end
19 20
  end

21 22
  def diff_hard_limit_enabled?
    params[:force_show_diff].present?
23 24
  end

25
  def diff_options
Alfredo Sumaran's avatar
Alfredo Sumaran committed
26
    options = { ignore_whitespace_change: hide_whitespace? }
27
    if diff_hard_limit_enabled?
28
      options.merge!(Commit.max_diff_options)
29
    end
30
    options
31 32
  end

33 34
  def safe_diff_files(diffs, diff_refs: nil, repository: nil)
    diffs.decorate! { |diff| Gitlab::Diff::File.new(diff, diff_refs: diff_refs, repository: repository) }
35
  end
36

37
  def unfold_bottom_class(bottom)
38
    bottom ? 'js-unfold-bottom' : ''
39 40
  end

41
  def unfold_class(unfold)
42
    unfold ? 'unfold js-unfold' : ''
43 44
  end

45
  def diff_line_content(line, line_type = nil)
46
    if line.blank?
47
      "  ".html_safe
48
    else
Annabel Dunstone's avatar
Annabel Dunstone committed
49
      line[0] = ' ' if %w[new old].include?(line_type)
50
      line
51 52
    end
  end
53

54 55
  def organize_comments(left, right)
    notes_left = notes_right = nil
56

57 58
    unless left[:type].nil? && right[:type] == 'new'
      notes_left = @grouped_diff_notes[left[:line_code]]
59 60
    end

61 62
    unless left[:type].nil? && right[:type].nil?
      notes_right = @grouped_diff_notes[right[:line_code]]
63 64
    end

65
    [notes_left, notes_right]
66
  end
67 68

  def inline_diff_btn
69
    diff_btn('Inline', 'inline', diff_view == 'inline')
70 71 72
  end

  def parallel_diff_btn
73
    diff_btn('Side-by-side', 'parallel', diff_view == 'parallel')
74
  end
Headless's avatar
Headless committed
75

76 77
  def submodule_link(blob, ref, repository = @repository)
    tree, commit = submodule_links(blob, ref, repository)
Headless's avatar
Headless committed
78
    commit_id = if commit.nil?
79
                  Commit.truncate_sha(blob.id)
Headless's avatar
Headless committed
80
                else
81
                  link_to Commit.truncate_sha(blob.id), commit
Headless's avatar
Headless committed
82 83 84 85 86 87 88 89
                end

    [
      content_tag(:span, link_to(truncate(blob.name, length: 40), tree)),
      '@',
      content_tag(:span, commit_id, class: 'monospace'),
    ].join(' ').html_safe
  end
90

91
  def commit_for_diff(diff_file)
92 93
    return diff_file.content_commit if diff_file.content_commit
    
94
    if diff_file.deleted_file
95
      @base_commit || @commit.parent || @commit
96 97 98 99 100
    else
      @commit
    end
  end

101
  def diff_file_html_data(project, diff_file)
102
    commit = commit_for_diff(diff_file)
103 104
    {
      blob_diff_path: namespace_project_blob_diff_path(project.namespace, project,
105
                                                       tree_join(commit.id, diff_file.file_path))
106 107 108 109 110 111
    }
  end

  def editable_diff?(diff)
    !diff.deleted_file && @merge_request && @merge_request.source_project
  end
112 113 114 115 116 117 118 119 120 121

  private

  def diff_btn(title, name, selected)
    params_copy = params.dup
    params_copy[:view] = name

    # Always use HTML to handle case where JSON diff rendered this button
    params_copy.delete(:format)

Alfredo Sumaran's avatar
Alfredo Sumaran committed
122
    link_to url_for(params_copy), id: "#{name}-diff-btn", class: (selected ? 'btn active' : 'btn'), data: { view_type: name } do
123 124 125
      title
    end
  end
126

127
  def commit_diff_whitespace_link(project, commit, options)
128
    url = namespace_project_commit_path(project.namespace, project, commit.id, params_with_whitespace)
129
    toggle_whitespace_link(url, options)
130 131
  end

132
  def diff_merge_request_whitespace_link(project, merge_request, options)
133
    url = diffs_namespace_project_merge_request_path(project.namespace, project, merge_request, params_with_whitespace)
134
    toggle_whitespace_link(url, options)
135 136
  end

137 138 139 140 141
  def diff_compare_whitespace_link(project, from, to, options)
    url = namespace_project_compare_path(project.namespace, project, from, to, params_with_whitespace)
    toggle_whitespace_link(url, options)
  end

142 143
  private

Alfredo Sumaran's avatar
Alfredo Sumaran committed
144
  def hide_whitespace?
145 146 147 148
    params[:w] == '1'
  end

  def params_with_whitespace
Alfredo Sumaran's avatar
Alfredo Sumaran committed
149
    hide_whitespace? ? request.query_parameters.except(:w) : request.query_parameters.merge(w: 1)
150 151
  end

152 153 154 155 156
  def toggle_whitespace_link(url, options)
    options[:class] ||= ''
    options[:class] << ' btn btn-default'

    link_to "#{hide_whitespace? ? 'Show' : 'Hide'} whitespace changes", url, class: options[:class]
157
  end
158
end