2016-07-25 14:47:09 -04:00
|
|
|
module Gitlab
|
|
|
|
module Conflict
|
|
|
|
class File
|
2016-08-03 07:02:20 -04:00
|
|
|
include Gitlab::Routing.url_helpers
|
2016-08-03 07:10:42 -04:00
|
|
|
include IconsHelper
|
2016-08-03 07:02:20 -04:00
|
|
|
|
2017-03-01 06:00:37 -05:00
|
|
|
MissingResolution = Class.new(ResolutionError)
|
2016-07-27 12:54:04 -04:00
|
|
|
|
2016-07-25 14:47:09 -04:00
|
|
|
CONTEXT_LINES = 3
|
|
|
|
|
2016-09-07 07:28:13 -04:00
|
|
|
attr_reader :merge_file_result, :their_path, :our_path, :our_mode, :merge_request, :repository
|
2016-07-25 14:47:09 -04:00
|
|
|
|
2016-08-03 07:02:20 -04:00
|
|
|
def initialize(merge_file_result, conflict, merge_request:)
|
2016-07-27 07:42:18 -04:00
|
|
|
@merge_file_result = merge_file_result
|
2016-07-25 14:47:09 -04:00
|
|
|
@their_path = conflict[:theirs][:path]
|
|
|
|
@our_path = conflict[:ours][:path]
|
2016-08-03 07:10:42 -04:00
|
|
|
@our_mode = conflict[:ours][:mode]
|
2016-08-03 07:02:20 -04:00
|
|
|
@merge_request = merge_request
|
|
|
|
@repository = merge_request.project.repository
|
2016-08-05 08:24:15 -04:00
|
|
|
@match_line_headers = {}
|
2016-07-25 14:47:09 -04:00
|
|
|
end
|
|
|
|
|
2016-09-01 08:59:10 -04:00
|
|
|
def content
|
|
|
|
merge_file_result[:data]
|
|
|
|
end
|
|
|
|
|
2016-10-13 05:34:15 -04:00
|
|
|
def our_blob
|
|
|
|
@our_blob ||= repository.blob_at(merge_request.diff_refs.head_sha, our_path)
|
|
|
|
end
|
|
|
|
|
2016-09-07 07:28:13 -04:00
|
|
|
def type
|
|
|
|
lines unless @type
|
|
|
|
|
|
|
|
@type.inquiry
|
|
|
|
end
|
|
|
|
|
2016-07-25 14:47:09 -04:00
|
|
|
# Array of Gitlab::Diff::Line objects
|
|
|
|
def lines
|
2016-09-01 08:59:10 -04:00
|
|
|
return @lines if defined?(@lines)
|
|
|
|
|
|
|
|
begin
|
|
|
|
@type = 'text'
|
|
|
|
@lines = Gitlab::Conflict::Parser.new.parse(content,
|
2016-07-27 07:42:18 -04:00
|
|
|
our_path: our_path,
|
2016-07-29 08:29:14 -04:00
|
|
|
their_path: their_path,
|
2016-08-02 04:20:22 -04:00
|
|
|
parent_file: self)
|
2016-09-01 08:59:10 -04:00
|
|
|
rescue Gitlab::Conflict::Parser::ParserError
|
|
|
|
@type = 'text-editor'
|
|
|
|
@lines = nil
|
|
|
|
end
|
2016-07-25 14:47:09 -04:00
|
|
|
end
|
|
|
|
|
2016-07-27 12:54:04 -04:00
|
|
|
def resolve_lines(resolution)
|
2016-07-29 09:51:11 -04:00
|
|
|
section_id = nil
|
2016-07-27 12:54:04 -04:00
|
|
|
|
|
|
|
lines.map do |line|
|
|
|
|
unless line.type
|
2016-07-29 09:51:11 -04:00
|
|
|
section_id = nil
|
2016-07-27 12:54:04 -04:00
|
|
|
next line
|
|
|
|
end
|
|
|
|
|
2016-07-29 09:51:11 -04:00
|
|
|
section_id ||= line_code(line)
|
2016-07-27 12:54:04 -04:00
|
|
|
|
2016-07-29 09:51:11 -04:00
|
|
|
case resolution[section_id]
|
2016-08-02 08:56:50 -04:00
|
|
|
when 'head'
|
2016-07-27 12:54:04 -04:00
|
|
|
next unless line.type == 'new'
|
2016-08-02 08:56:50 -04:00
|
|
|
when 'origin'
|
2016-07-27 12:54:04 -04:00
|
|
|
next unless line.type == 'old'
|
|
|
|
else
|
2016-07-29 09:51:11 -04:00
|
|
|
raise MissingResolution, "Missing resolution for section ID: #{section_id}"
|
2016-07-27 12:54:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
line
|
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
|
2016-09-01 08:59:10 -04:00
|
|
|
def resolve_content(resolution)
|
|
|
|
if resolution == content
|
|
|
|
raise MissingResolution, "Resolved content has no changes for file #{our_path}"
|
|
|
|
end
|
|
|
|
|
|
|
|
resolution
|
|
|
|
end
|
|
|
|
|
2016-07-29 08:29:14 -04:00
|
|
|
def highlight_lines!
|
2016-08-01 09:50:08 -04:00
|
|
|
their_file = lines.reject { |line| line.type == 'new' }.map(&:text).join("\n")
|
|
|
|
our_file = lines.reject { |line| line.type == 'old' }.map(&:text).join("\n")
|
|
|
|
|
2016-08-02 04:20:22 -04:00
|
|
|
their_highlight = Gitlab::Highlight.highlight(their_path, their_file, repository: repository).lines
|
|
|
|
our_highlight = Gitlab::Highlight.highlight(our_path, our_file, repository: repository).lines
|
2016-07-25 14:47:09 -04:00
|
|
|
|
2016-07-29 08:29:14 -04:00
|
|
|
lines.each do |line|
|
2017-02-22 10:10:32 -05:00
|
|
|
line.rich_text =
|
|
|
|
if line.type == 'old'
|
|
|
|
their_highlight[line.old_line - 1].try(:html_safe)
|
|
|
|
else
|
|
|
|
our_highlight[line.new_line - 1].try(:html_safe)
|
|
|
|
end
|
2016-07-25 14:47:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def sections
|
|
|
|
return @sections if @sections
|
|
|
|
|
2016-08-10 23:42:35 -04:00
|
|
|
chunked_lines = lines.chunk { |line| line.type.nil? }.to_a
|
2016-07-25 14:47:09 -04:00
|
|
|
match_line = nil
|
|
|
|
|
2016-08-10 23:42:35 -04:00
|
|
|
sections_count = chunked_lines.size
|
|
|
|
|
2016-07-25 14:47:09 -04:00
|
|
|
@sections = chunked_lines.flat_map.with_index do |(no_conflict, lines), i|
|
|
|
|
section = nil
|
|
|
|
|
2016-08-04 09:12:08 -04:00
|
|
|
# We need to reduce context sections to CONTEXT_LINES. Conflict sections are
|
|
|
|
# always shown in full.
|
2016-07-25 14:47:09 -04:00
|
|
|
if no_conflict
|
|
|
|
conflict_before = i > 0
|
2016-08-10 23:42:35 -04:00
|
|
|
conflict_after = (sections_count - i) > 1
|
2016-07-25 14:47:09 -04:00
|
|
|
|
|
|
|
if conflict_before && conflict_after
|
2016-08-04 09:12:08 -04:00
|
|
|
# Create a gap in a long context section.
|
2016-07-25 14:47:09 -04:00
|
|
|
if lines.length > CONTEXT_LINES * 2
|
2016-08-01 10:41:49 -04:00
|
|
|
head_lines = lines.first(CONTEXT_LINES)
|
2016-07-25 14:47:09 -04:00
|
|
|
tail_lines = lines.last(CONTEXT_LINES)
|
2016-08-01 10:41:49 -04:00
|
|
|
|
2016-08-04 09:12:08 -04:00
|
|
|
# Ensure any existing match line has text for all lines up to the last
|
|
|
|
# line of its context.
|
2016-08-05 08:24:15 -04:00
|
|
|
update_match_line_text(match_line, head_lines.last)
|
2016-08-01 10:41:49 -04:00
|
|
|
|
2016-08-04 09:12:08 -04:00
|
|
|
# Insert a new match line after the created gap.
|
2016-08-01 10:41:49 -04:00
|
|
|
match_line = create_match_line(tail_lines.first)
|
2016-07-25 14:47:09 -04:00
|
|
|
|
|
|
|
section = [
|
2016-08-01 10:41:49 -04:00
|
|
|
{ conflict: false, lines: head_lines },
|
2016-07-25 14:47:09 -04:00
|
|
|
{ conflict: false, lines: tail_lines.unshift(match_line) }
|
|
|
|
]
|
|
|
|
end
|
|
|
|
elsif conflict_after
|
2016-08-01 10:41:49 -04:00
|
|
|
tail_lines = lines.last(CONTEXT_LINES)
|
|
|
|
|
2016-08-04 09:12:08 -04:00
|
|
|
# Create a gap and insert a match line at the start.
|
|
|
|
if lines.length > tail_lines.length
|
2016-08-01 10:41:49 -04:00
|
|
|
match_line = create_match_line(tail_lines.first)
|
|
|
|
|
|
|
|
tail_lines.unshift(match_line)
|
|
|
|
end
|
|
|
|
|
|
|
|
lines = tail_lines
|
2016-07-25 14:47:09 -04:00
|
|
|
elsif conflict_before
|
2016-08-04 09:12:08 -04:00
|
|
|
# We're at the end of the file (no conflicts after), so just remove extra
|
|
|
|
# trailing lines.
|
2016-07-25 14:47:09 -04:00
|
|
|
lines = lines.first(CONTEXT_LINES)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-04 09:12:08 -04:00
|
|
|
# We want to update the match line's text every time unless we've already
|
|
|
|
# created a gap and its corresponding match line.
|
2016-08-05 08:24:15 -04:00
|
|
|
update_match_line_text(match_line, lines.last) unless section
|
2016-07-25 14:47:09 -04:00
|
|
|
|
2016-07-27 12:54:04 -04:00
|
|
|
section ||= { conflict: !no_conflict, lines: lines }
|
|
|
|
section[:id] = line_code(lines.first) unless no_conflict
|
|
|
|
section
|
2016-07-25 14:47:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-27 12:54:04 -04:00
|
|
|
def line_code(line)
|
|
|
|
Gitlab::Diff::LineCode.generate(our_path, line.new_pos, line.old_pos)
|
|
|
|
end
|
|
|
|
|
2016-08-01 10:41:49 -04:00
|
|
|
def create_match_line(line)
|
|
|
|
Gitlab::Diff::Line.new('', 'match', line.index, line.old_pos, line.new_pos)
|
|
|
|
end
|
|
|
|
|
2016-08-05 08:24:15 -04:00
|
|
|
# Any line beginning with a letter, an underscore, or a dollar can be used in a
|
|
|
|
# match line header. Only context sections can contain match lines, as match lines
|
|
|
|
# have to exist in both versions of the file.
|
|
|
|
def find_match_line_header(index)
|
|
|
|
return @match_line_headers[index] if @match_line_headers.key?(index)
|
|
|
|
|
|
|
|
@match_line_headers[index] = begin
|
|
|
|
if index >= 0
|
|
|
|
line = lines[index]
|
|
|
|
|
|
|
|
if line.type.nil? && line.text.match(/\A[A-Za-z$_]/)
|
|
|
|
" #{line.text}"
|
|
|
|
else
|
|
|
|
find_match_line_header(index - 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-04 09:12:08 -04:00
|
|
|
# Set the match line's text for the current line. A match line takes its start
|
|
|
|
# position and context header (where present) from itself, and its end position from
|
|
|
|
# the line passed in.
|
2016-08-05 08:24:15 -04:00
|
|
|
def update_match_line_text(match_line, line)
|
2016-08-01 10:41:49 -04:00
|
|
|
return unless match_line
|
|
|
|
|
2016-08-05 08:24:15 -04:00
|
|
|
header = find_match_line_header(match_line.index - 1)
|
2016-08-01 10:41:49 -04:00
|
|
|
|
|
|
|
match_line.text = "@@ -#{match_line.old_pos},#{line.old_pos} +#{match_line.new_pos},#{line.new_pos} @@#{header}"
|
|
|
|
end
|
|
|
|
|
2016-09-01 08:59:10 -04:00
|
|
|
def as_json(opts = {})
|
|
|
|
json_hash = {
|
2016-07-25 14:47:09 -04:00
|
|
|
old_path: their_path,
|
|
|
|
new_path: our_path,
|
2016-08-03 07:10:42 -04:00
|
|
|
blob_icon: file_type_icon_class('file', our_mode, our_path),
|
2016-08-03 07:02:20 -04:00
|
|
|
blob_path: namespace_project_blob_path(merge_request.project.namespace,
|
|
|
|
merge_request.project,
|
2016-09-01 08:59:10 -04:00
|
|
|
::File.join(merge_request.diff_refs.head_sha, our_path))
|
2016-07-25 14:47:09 -04:00
|
|
|
}
|
2016-09-01 08:59:10 -04:00
|
|
|
|
2016-09-07 07:28:13 -04:00
|
|
|
json_hash.tap do |json_hash|
|
|
|
|
if opts[:full_content]
|
|
|
|
json_hash[:content] = content
|
2016-10-13 05:34:15 -04:00
|
|
|
json_hash[:blob_ace_mode] = our_blob && our_blob.language.try(:ace_mode)
|
2016-09-07 07:28:13 -04:00
|
|
|
else
|
|
|
|
json_hash[:sections] = sections if type.text?
|
|
|
|
json_hash[:type] = type
|
|
|
|
json_hash[:content_path] = content_path
|
|
|
|
end
|
2016-09-01 08:59:10 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def content_path
|
|
|
|
conflict_for_path_namespace_project_merge_request_path(merge_request.project.namespace,
|
|
|
|
merge_request.project,
|
|
|
|
merge_request,
|
|
|
|
old_path: their_path,
|
|
|
|
new_path: our_path)
|
2016-07-25 14:47:09 -04:00
|
|
|
end
|
2016-08-24 10:20:06 -04:00
|
|
|
|
|
|
|
# Don't try to print merge_request or repository.
|
|
|
|
def inspect
|
2016-09-01 08:59:10 -04:00
|
|
|
instance_variables = [:merge_file_result, :their_path, :our_path, :our_mode, :type].map do |instance_variable|
|
2016-08-24 10:20:06 -04:00
|
|
|
value = instance_variable_get("@#{instance_variable}")
|
|
|
|
|
|
|
|
"#{instance_variable}=\"#{value}\""
|
|
|
|
end
|
|
|
|
|
|
|
|
"#<#{self.class} #{instance_variables.join(' ')}>"
|
|
|
|
end
|
2016-07-25 14:47:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|