commits_helper.rb 1.82 KB
Newer Older
gitlabhq's avatar
gitlabhq committed
1
module CommitsHelper
Cedric Gatay's avatar
Cedric Gatay committed
2 3 4 5
  def commit_msg_with_link_to_issues(project, message)
    return '' unless message
    out = ''
    message.split(/(#[0-9]+)/m).each do |m|
6 7
      if m =~ /(#([0-9]+))/m
        begin
Valery Sizov's avatar
Valery Sizov committed
8
          issue = project.issues.find($2)
Cedric Gatay's avatar
Cedric Gatay committed
9
          out += link_to($1, project_issue_path(project, $2))
10
        rescue
Cedric Gatay's avatar
Cedric Gatay committed
11
          out += $1
12 13
        end
      else
Cedric Gatay's avatar
Cedric Gatay committed
14
        out += m
15 16
      end
    end
Cedric Gatay's avatar
Cedric Gatay committed
17
    preserve out
18 19
  end

Valeriy Sizov's avatar
Valeriy Sizov committed
20
  def identification_type(line)
21 22 23 24
    if line[0] == "+"
      "new"
    elsif line[0] == "-"
      "old"
25
    else
26 27 28 29
      nil
    end
  end

Valeriy Sizov's avatar
Valeriy Sizov committed
30
  def build_line_anchor(index, line_new, line_old)
31 32 33 34 35 36 37 38 39 40
    "#{index}_#{line_old}_#{line_new}"
  end

  def each_diff_line(diff_arr, index)
    line_old = 1
    line_new = 1
    type = nil

    lines_arr = diff_arr
    lines_arr.each do |line|
41 42 43 44 45
      next if line.match(/^\-\-\- \/dev\/null/)
      next if line.match(/^\+\+\+ \/dev\/null/)
      next if line.match(/^\-\-\- a/)
      next if line.match(/^\+\+\+ b/)

46
      full_line = html_escape(line.gsub(/\n/, ''))
47 48 49 50 51 52

      if line.match(/^@@ -/)
        type = "match"

        line_old = line.match(/\-[0-9]*/)[0].to_i.abs rescue 0
        line_new = line.match(/\+[0-9]*/)[0].to_i.abs rescue 0
53

Valeriy Sizov's avatar
Valeriy Sizov committed
54
        next if line_old == 1 && line_new == 1 #top of file
55
        yield(full_line, type, nil, nil, nil)
56 57
        next
      else
Valeriy Sizov's avatar
Valeriy Sizov committed
58 59
        type = identification_type(line)
        line_code = build_line_anchor(index, line_new, line_old)
60 61 62 63 64 65 66 67 68 69 70 71
        yield(full_line, type, line_code, line_new, line_old)
      end


      if line[0] == "+"
        line_new += 1
      elsif line[0] == "-"
        line_old += 1
      else
        line_new += 1
        line_old += 1
      end
72 73
    end
  end
74 75 76 77 78 79 80 81 82 83

  def image_diff_class(diff)
    if diff.deleted_file
      "diff_image_removed"
    elsif diff.new_file
      "diff_image_added"
    else
      nil
    end
  end
gitlabhq's avatar
gitlabhq committed
84
end