Commit 23706716 authored by Marin Jankovski's avatar Marin Jankovski

Now refactor all to work properly.

parent 13cfa49a
......@@ -23,13 +23,55 @@ module CommitsHelper
end
end
def side_diff_line(diff, index)
def parallel_diff_line(diff, index)
Gitlab::DiffParser.new(diff.diff.lines.to_a, diff.new_path)
.each do |full_line, type, line_code, line_new, line_old, raw_line, next_type, next_line|
yield(full_line, type, line_code, line_new, line_old, raw_line, next_type, next_line)
end
end
def parallel_diff(diff, index)
lines = []
skip_next = false
# Building array of lines
#
# [left_type, left_line_number, left_line_content, right_line_type, right_line_number, right_line_content]
#
parallel_diff_line(diff, index) do |full_line, type, line_code, line_new, line_old, raw_line, next_type, next_line|
line = [type, line_old, full_line, next_type, line_new]
if type == 'match' || type.nil?
# line in the right panel is the same as in the left one
line = [type, line_old, full_line, type, line_new, full_line]
lines.push(line)
elsif type == 'old'
if next_type == 'new'
# Left side has text removed, right side has text added
line.push(next_line)
lines.push(line)
skip_next = true
elsif next_type == 'old' || next_type.nil?
# Left side has text removed, right side doesn't have any change
line.pop # remove the newline
line.push(nil) # no line number on the right panel
line.push(" ") # empty line on the right panel
lines.push(line)
end
elsif type == 'new'
if skip_next
# Change has been already included in previous line so no need to do it again
skip_next = false
next
else
# Change is only on the right side, left side has no change
line = [nil, nil, " ", type, line_new, full_line]
lines.push(line)
end
end
end
lines
end
def each_diff_line_near(diff, index, expected_line_code)
max_number_of_lines = 16
......@@ -112,82 +154,6 @@ module CommitsHelper
branches.sort.map { |branch| link_to(branch, project_tree_path(project, branch)) }.join(", ").html_safe
end
def parallel_diff_lines(project, commit, diff, file)
old_file = project.repository.blob_at(commit.parent_id, diff.old_path) if commit.parent_id
deleted_lines = {}
added_lines = {}
each_diff_line(diff, 0) do |line, type, line_code, line_new, line_old|
if type == "old"
deleted_lines[line_old] = { line_code: line_code, type: type, line: line }
elsif type == "new"
added_lines[line_new] = { line_code: line_code, type: type, line: line }
end
end
max_length = old_file ? [old_file.loc, file.loc].max : file.loc
offset1 = 0
offset2 = 0
old_lines = []
new_lines = []
max_length.times do |line_index|
line_index1 = line_index - offset1
line_index2 = line_index - offset2
deleted_line = deleted_lines[line_index1 + 1]
added_line = added_lines[line_index2 + 1]
old_line = old_file.lines[line_index1] if old_file
new_line = file.lines[line_index2]
if deleted_line && added_line
elsif deleted_line
new_line = nil
offset2 += 1
elsif added_line
old_line = nil
offset1 += 1
end
old_lines[line_index] = DiffLine.new
new_lines[line_index] = DiffLine.new
# old
if line_index == 0 && diff.new_file
old_lines[line_index].type = :file_created
old_lines[line_index].content = 'File was created'
elsif deleted_line
old_lines[line_index].type = :deleted
old_lines[line_index].content = old_line
old_lines[line_index].num = line_index1 + 1
old_lines[line_index].code = deleted_line[:line_code]
elsif old_line
old_lines[line_index].type = :no_change
old_lines[line_index].content = old_line
old_lines[line_index].num = line_index1 + 1
else
old_lines[line_index].type = :added
end
# new
if line_index == 0 && diff.deleted_file
new_lines[line_index].type = :file_deleted
new_lines[line_index].content = "File was deleted"
elsif added_line
new_lines[line_index].type = :added
new_lines[line_index].num = line_index2 + 1
new_lines[line_index].content = new_line
new_lines[line_index].code = added_line[:line_code]
elsif new_line
new_lines[line_index].type = :no_change
new_lines[line_index].num = line_index2 + 1
new_lines[line_index].content = new_line
else
new_lines[line_index].type = :deleted
end
end
return old_lines, new_lines
end
def link_to_browse_code(project, commit)
if current_controller?(:projects, :commits)
if @repo.blob_at(commit.id, @path)
......
/ Side-by-side diff view
%div.text-file
%table
- side_diff_line(diff, index) do |line, type, line_code, line_new, line_old, raw_line, next_type, next_line|
%tr.line_holder.parallel{ id: line_code }
- if type == "match"
= render "projects/commits/diffs/match_line_parallel", {line: line,
line_old: line_old, line_new: line_new, bottom: false}
- else
- if diff.new_file
- parallel_diff(diff, index).each do |line|
- type_left = line[0]
- line_number_left = line[1]
- line_content_left = line[2]
- type_right = line[3]
- line_number_right = line[4]
- line_content_right = line[5]
%tr.line_holder.parallel
- if type_left == 'match'
= render "projects/commits/diffs/match_line_parallel", {line: line_content_left,
line_old: line_number_left, line_new: line_number_right, bottom: false}
- elsif type_left == 'old'
%td.old_line{ class: "old" }
= link_to raw(line_number_left)
%td.line_content{class: "parallel noteable_line old"}= raw line_content_left
%td.new_line{class: "#{type_right == 'new' ? 'new' : nil}", data: {linenumber: line_number_right}}
= link_to raw(line_number_right)
%td.line_content.parallel{class: "noteable_line #{type_right == 'new' ? 'new' : nil}"}= raw line_content_right
- elsif type_left.nil?
%td.old_line
%td.line_content.parallel= raw " "
- else
- next if type == 'new'
%td.old_line{ class: "#{type}" }
= link_to raw(line_old), "##{line_code}", id: line_code
- if type == 'old'
%td.line_content{class: "parallel noteable_line old #{line_code}", "line_code" => line_code}= raw line
- else
%td.line_content.parallel= raw line
- if diff.deleted_file
%td.new_line{ data: {linenumber: line_new}}
%td.line_content.parallel= raw " "
- else
- if type == 'old'
%td.new_line{class: "#{next_type == 'new' ? 'new' : nil}", data: {linenumber: line_new}}
- if next_type == 'new'
- content = next_line
= link_to raw(line_new) , "##{line_code}", id: line_code
%td.line_content.parallel{class: "noteable_line new #{line_code}", "line_code" => line_code}= raw content
- else
- content = " "
%td.line_content.parallel{class: "noteable_line #{line_code}", "line_code" => line_code}= raw content
- else
%td.new_line{class: "#{type}", data: {linenumber: line_new}}
= link_to raw(line_new) , "##{line_code}", id: line_code
%td.line_content.parallel{class: "#{type}"}= raw line
= link_to raw(line_number_left)
%td.line_content{class: "parallel noteable_line"}= raw line_content_left
%td.new_line{class: "#{type_right == 'new' ? 'new' : nil}", data: {linenumber: line_number_right}}
= link_to raw(line_number_right)
%td.line_content.parallel{class: "noteable_line #{type_right == 'new' ? 'new' : nil}"}= raw line_content_right
- if diff.diff.blank? && diff_file_mode_changed?(diff)
.file-mode-changed
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment