Commit 4e0c2f00d3edcabd24488161933b00d497656bfd
Exists in
master
and in
4 other branches
Merge pull request #1026 from gitlabhq/inline_diff
Inline diff
Showing
3 changed files
with
83 additions
and
2 deletions
Show diff stats
app/assets/stylesheets/sections/commits.scss
| @@ -82,6 +82,17 @@ | @@ -82,6 +82,17 @@ | ||
| 82 | color:#333; | 82 | color:#333; |
| 83 | font-size: 12px; | 83 | font-size: 12px; |
| 84 | font-family: 'Menlo', 'Liberation Mono', 'Consolas', 'Courier New', 'andale mono','lucida console',monospace; | 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 | .diff_file_content_image { | 97 | .diff_file_content_image { |
| 87 | background:#eee; | 98 | background:#eee; |
app/helpers/commits_helper.rb
| @@ -35,8 +35,8 @@ module CommitsHelper | @@ -35,8 +35,8 @@ module CommitsHelper | ||
| 35 | line_old = 1 | 35 | line_old = 1 |
| 36 | line_new = 1 | 36 | line_new = 1 |
| 37 | type = nil | 37 | type = nil |
| 38 | - | ||
| 39 | - lines_arr = diff_arr | 38 | + |
| 39 | + lines_arr = ::Gitlab::InlineDiff.processing diff_arr | ||
| 40 | lines_arr.each do |line| | 40 | lines_arr.each do |line| |
| 41 | next if line.match(/^\-\-\- \/dev\/null/) | 41 | next if line.match(/^\-\-\- \/dev\/null/) |
| 42 | next if line.match(/^\+\+\+ \/dev\/null/) | 42 | next if line.match(/^\+\+\+ \/dev\/null/) |
| @@ -44,6 +44,7 @@ module CommitsHelper | @@ -44,6 +44,7 @@ module CommitsHelper | ||
| 44 | next if line.match(/^\+\+\+ b/) | 44 | next if line.match(/^\+\+\+ b/) |
| 45 | 45 | ||
| 46 | full_line = html_escape(line.gsub(/\n/, '')) | 46 | full_line = html_escape(line.gsub(/\n/, '')) |
| 47 | + full_line = ::Gitlab::InlineDiff.replace_markers full_line | ||
| 47 | 48 | ||
| 48 | if line.match(/^@@ -/) | 49 | if line.match(/^@@ -/) |
| 49 | type = "match" | 50 | type = "match" |
| @@ -81,4 +82,5 @@ module CommitsHelper | @@ -81,4 +82,5 @@ module CommitsHelper | ||
| 81 | nil | 82 | nil |
| 82 | end | 83 | end |
| 83 | end | 84 | end |
| 85 | + | ||
| 84 | end | 86 | end |
| @@ -0,0 +1,68 @@ | @@ -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 |