Commit 4e0c2f00d3edcabd24488161933b00d497656bfd

Authored by Dmitriy Zaporozhets
2 parents 264ee0c5 e3fed8aa

Merge pull request #1026 from gitlabhq/inline_diff

Inline diff
app/assets/stylesheets/sections/commits.scss
... ... @@ -82,6 +82,17 @@
82 82 color:#333;
83 83 font-size: 12px;
84 84 font-family: 'Menlo', 'Liberation Mono', 'Consolas', 'Courier New', 'andale mono','lucida console',monospace;
  85 + .old{
  86 + span.idiff{
  87 + background-color:#FAA;
  88 + }
  89 + }
  90 + .new{
  91 + span.idiff{
  92 + background-color:#AFA;
  93 + }
  94 + }
  95 +
85 96 }
86 97 .diff_file_content_image {
87 98 background:#eee;
... ...
app/helpers/commits_helper.rb
... ... @@ -35,8 +35,8 @@ module CommitsHelper
35 35 line_old = 1
36 36 line_new = 1
37 37 type = nil
38   -
39   - lines_arr = diff_arr
  38 +
  39 + lines_arr = ::Gitlab::InlineDiff.processing diff_arr
40 40 lines_arr.each do |line|
41 41 next if line.match(/^\-\-\- \/dev\/null/)
42 42 next if line.match(/^\+\+\+ \/dev\/null/)
... ... @@ -44,6 +44,7 @@ module CommitsHelper
44 44 next if line.match(/^\+\+\+ b/)
45 45  
46 46 full_line = html_escape(line.gsub(/\n/, ''))
  47 + full_line = ::Gitlab::InlineDiff.replace_markers full_line
47 48  
48 49 if line.match(/^@@ -/)
49 50 type = "match"
... ... @@ -81,4 +82,5 @@ module CommitsHelper
81 82 nil
82 83 end
83 84 end
  85 +
84 86 end
... ...
lib/gitlab/inline_diff.rb 0 → 100644
... ... @@ -0,0 +1,68 @@
  1 +module Gitlab
  2 + class InlineDiff
  3 + class << self
  4 +
  5 + START = ""
  6 + FINISH = ""
  7 +
  8 + def processing diff_arr
  9 + indexes = _indexes_of_changed_lines diff_arr
  10 +
  11 + indexes.each do |index|
  12 + first_line = diff_arr[index+1]
  13 + second_line = diff_arr[index+2]
  14 + max_length = [first_line.size, second_line.size].max
  15 +
  16 + first_the_same_symbols = 0
  17 + (0..max_length + 1).each do |i|
  18 + first_the_same_symbols = i - 1
  19 + if first_line[i] != second_line[i] && i > 0
  20 + break
  21 + end
  22 + end
  23 + first_token = first_line[0..first_the_same_symbols][1..-1]
  24 +
  25 + diff_arr[index+1].sub!(first_token, first_token + START)
  26 + diff_arr[index+2].sub!(first_token, first_token + START)
  27 +
  28 + last_the_same_symbols = 0
  29 + (1..max_length + 1).each do |i|
  30 + last_the_same_symbols = -i
  31 + if first_line[-i] != second_line[-i]
  32 + break
  33 + end
  34 + end
  35 + last_the_same_symbols += 1
  36 + last_token = first_line[last_the_same_symbols..-1]
  37 + diff_arr[index+1].sub!(/#{Regexp.escape(last_token)}$/, FINISH + last_token)
  38 + diff_arr[index+2].sub!(/#{Regexp.escape(last_token)}$/, FINISH + last_token)
  39 + end
  40 + diff_arr
  41 + end
  42 +
  43 + def _indexes_of_changed_lines diff_arr
  44 + chain_of_first_symbols = ""
  45 + diff_arr.each_with_index do |line, i|
  46 + chain_of_first_symbols += line[0]
  47 + end
  48 + chain_of_first_symbols.gsub!(/[^\-\+]/, "#")
  49 +
  50 + offset = 0
  51 + indexes = []
  52 + while index = chain_of_first_symbols.index("#-+#", offset)
  53 + indexes << index
  54 + offset = index + 1
  55 + end
  56 + indexes
  57 + end
  58 +
  59 + def replace_markers line
  60 + line.gsub!(START, "<span class='idiff'>")
  61 + line.gsub!(FINISH, "</span>")
  62 + line
  63 + end
  64 +
  65 + end
  66 +
  67 + end
  68 +end
... ...