BigW Consortium Gitlab

diff_helper.rb 4.79 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 expand_all_diffs?
12
    params[:expand_all_diffs].present?
13 14
  end

15
  def diff_view
16 17 18 19 20
    @diff_view ||= begin
      diff_views = %w(inline parallel)
      diff_view = cookies[:diff_view]
      diff_view = diff_views.first unless diff_views.include?(diff_view)
      diff_view.to_sym
21
    end
22 23
  end

24
  def diff_options
25
    options = { ignore_whitespace_change: hide_whitespace?, no_collapse: expand_all_diffs? }
26 27

    if action_name == 'diff_for_path'
28 29
      options[:no_collapse] = true
      options[:paths] = params.values_at(:old_path, :new_path)
30
    end
31

32
    options
Douwe Maan committed
33 34
  end

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
  def diff_match_line(old_pos, new_pos, text: '', view: :inline, bottom: false)
    content = content_tag :td, text, class: "line_content match #{view == :inline ? '' : view}"
    cls = ['diff-line-num', 'unfold', 'js-unfold']
    cls << 'js-unfold-bottom' if bottom

    html = ''
    if old_pos
      html << content_tag(:td, '...', class: cls + ['old_line'], data: { linenumber: old_pos })
      html << content unless view == :inline
    end

    if new_pos
      html << content_tag(:td, '...', class: cls + ['new_line'], data: { linenumber: new_pos })
      html << content
    end
50

51
    html.html_safe
52 53
  end

54
  def diff_line_content(line)
55
    if line.blank?
56
      "&nbsp;".html_safe
57
    else
58
      line.sub(/^[\-+ ]/, '').html_safe
59 60
    end
  end
61

62 63
  def parallel_diff_discussions(left, right, diff_file)
    discussion_left = discussion_right = nil
64

65 66 67
    if left && (left.unchanged? || left.removed?)
      line_code = diff_file.line_code(left)
      discussion_left = @grouped_diff_discussions[line_code]
68 69
    end

70 71 72
    if right && right.added?
      line_code = diff_file.line_code(right)
      discussion_right = @grouped_diff_discussions[line_code]
73 74
    end

75
    [discussion_left, discussion_right]
76
  end
77 78

  def inline_diff_btn
79
    diff_btn('Inline', 'inline', diff_view == :inline)
80 81 82
  end

  def parallel_diff_btn
83
    diff_btn('Side-by-side', 'parallel', diff_view == :parallel)
84
  end
Headless committed
85

86 87
  def submodule_link(blob, ref, repository = @repository)
    tree, commit = submodule_links(blob, ref, repository)
Headless committed
88
    commit_id = if commit.nil?
89
                  Commit.truncate_sha(blob.id)
Headless committed
90
                else
91
                  link_to Commit.truncate_sha(blob.id), commit
Headless committed
92 93 94 95 96 97 98 99
                end

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

101
  def commit_for_diff(diff_file)
102
    return diff_file.content_commit if diff_file.content_commit
103

104
    if diff_file.deleted_file
105
      @base_commit || @commit.parent || @commit
106 107 108 109 110
    else
      @commit
    end
  end

111
  def diff_file_html_data(project, diff_file_path, diff_commit_id)
112 113
    {
      blob_diff_path: namespace_project_blob_diff_path(project.namespace, project,
114
                                                       tree_join(diff_commit_id, diff_file_path)),
115
      view: diff_view
116 117 118 119 120 121
    }
  end

  def editable_diff?(diff)
    !diff.deleted_file && @merge_request && @merge_request.source_project
  end
122 123 124 125 126 127 128 129 130 131

  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 committed
132
    link_to url_for(params_copy), id: "#{name}-diff-btn", class: (selected ? 'btn active' : 'btn'), data: { view_type: name } do
133 134 135
      title
    end
  end
136

137
  def commit_diff_whitespace_link(project, commit, options)
138
    url = namespace_project_commit_path(project.namespace, project, commit.id, params_with_whitespace)
139
    toggle_whitespace_link(url, options)
140 141
  end

142
  def diff_merge_request_whitespace_link(project, merge_request, options)
143
    url = diffs_namespace_project_merge_request_path(project.namespace, project, merge_request, params_with_whitespace)
144
    toggle_whitespace_link(url, options)
145 146
  end

147 148 149 150 151
  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

Alfredo Sumaran committed
152
  def hide_whitespace?
153 154 155 156
    params[:w] == '1'
  end

  def params_with_whitespace
Alfredo Sumaran committed
157
    hide_whitespace? ? request.query_parameters.except(:w) : request.query_parameters.merge(w: 1)
158 159
  end

160 161 162 163 164
  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]
165
  end
166
end