BigW Consortium Gitlab

file.rb 6.73 KB
Newer Older
1 2 3
module Gitlab
  module Conflict
    class File
4
      include Gitlab::Routing.url_helpers
5
      include IconsHelper
6

7 8 9
      class MissingResolution < StandardError
      end

10 11
      CONTEXT_LINES = 3

12
      attr_reader :merge_file_result, :their_path, :our_path, :our_mode, :merge_request, :repository
13

14
      def initialize(merge_file_result, conflict, merge_request:)
15
        @merge_file_result = merge_file_result
16 17
        @their_path = conflict[:theirs][:path]
        @our_path = conflict[:ours][:path]
18
        @our_mode = conflict[:ours][:mode]
19 20
        @merge_request = merge_request
        @repository = merge_request.project.repository
21
        @match_line_headers = {}
22 23 24 25
      end

      # Array of Gitlab::Diff::Line objects
      def lines
26 27
        @lines ||= Gitlab::Conflict::Parser.new.parse(merge_file_result[:data],
                                                      our_path: our_path,
28
                                                      their_path: their_path,
Sean McGivern committed
29
                                                      parent_file: self)
30 31
      end

32
      def resolve_lines(resolution)
33
        section_id = nil
34 35 36

        lines.map do |line|
          unless line.type
37
            section_id = nil
38 39 40
            next line
          end

41
          section_id ||= line_code(line)
42

43
          case resolution[section_id]
44
          when 'head'
45
            next unless line.type == 'new'
46
          when 'origin'
47 48
            next unless line.type == 'old'
          else
49
            raise MissingResolution, "Missing resolution for section ID: #{section_id}"
50 51 52 53 54 55
          end

          line
        end.compact
      end

56
      def highlight_lines!
57 58 59
        their_file = lines.reject { |line| line.type == 'new' }.map(&:text).join("\n")
        our_file = lines.reject { |line| line.type == 'old' }.map(&:text).join("\n")

Sean McGivern committed
60 61
        their_highlight = Gitlab::Highlight.highlight(their_path, their_file, repository: repository).lines
        our_highlight = Gitlab::Highlight.highlight(our_path, our_file, repository: repository).lines
62

63
        lines.each do |line|
64
          if line.type == 'old'
65
            line.rich_text = their_highlight[line.old_line - 1].try(:html_safe)
66
          else
67
            line.rich_text = our_highlight[line.new_line - 1].try(:html_safe)
68 69 70 71 72 73 74
          end
        end
      end

      def sections
        return @sections if @sections

75
        chunked_lines = lines.chunk { |line| line.type.nil? }.to_a
76 77
        match_line = nil

78 79
        sections_count = chunked_lines.size

80 81 82
        @sections = chunked_lines.flat_map.with_index do |(no_conflict, lines), i|
          section = nil

83 84
          # We need to reduce context sections to CONTEXT_LINES. Conflict sections are
          # always shown in full.
85 86
          if no_conflict
            conflict_before = i > 0
87
            conflict_after = (sections_count - i) > 1
88 89

            if conflict_before && conflict_after
90
              # Create a gap in a long context section.
91
              if lines.length > CONTEXT_LINES * 2
92
                head_lines = lines.first(CONTEXT_LINES)
93
                tail_lines = lines.last(CONTEXT_LINES)
94

95 96
                # Ensure any existing match line has text for all lines up to the last
                # line of its context.
97
                update_match_line_text(match_line, head_lines.last)
98

99
                # Insert a new match line after the created gap.
100
                match_line = create_match_line(tail_lines.first)
101 102

                section = [
103
                  { conflict: false, lines: head_lines },
104 105 106 107
                  { conflict: false, lines: tail_lines.unshift(match_line) }
                ]
              end
            elsif conflict_after
108 109
              tail_lines = lines.last(CONTEXT_LINES)

110 111
              # Create a gap and insert a match line at the start.
              if lines.length > tail_lines.length
112 113 114 115 116 117
                match_line = create_match_line(tail_lines.first)

                tail_lines.unshift(match_line)
              end

              lines = tail_lines
118
            elsif conflict_before
119 120
              # We're at the end of the file (no conflicts after), so just remove extra
              # trailing lines.
121 122 123 124
              lines = lines.first(CONTEXT_LINES)
            end
          end

125 126
          # We want to update the match line's text every time unless we've already
          # created a gap and its corresponding match line.
127
          update_match_line_text(match_line, lines.last) unless section
128

129 130 131
          section ||= { conflict: !no_conflict, lines: lines }
          section[:id] = line_code(lines.first) unless no_conflict
          section
132 133 134
        end
      end

135 136 137 138
      def line_code(line)
        Gitlab::Diff::LineCode.generate(our_path, line.new_pos, line.old_pos)
      end

139 140 141 142
      def create_match_line(line)
        Gitlab::Diff::Line.new('', 'match', line.index, line.old_pos, line.new_pos)
      end

143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
      # 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

162 163 164
      # 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.
165
      def update_match_line_text(match_line, line)
166 167
        return unless match_line

168
        header = find_match_line_header(match_line.index - 1)
169 170 171 172

        match_line.text = "@@ -#{match_line.old_pos},#{line.old_pos} +#{match_line.new_pos},#{line.new_pos} @@#{header}"
      end

173 174 175 176
      def as_json(opts = nil)
        {
          old_path: their_path,
          new_path: our_path,
177
          blob_icon: file_type_icon_class('file', our_mode, our_path),
178 179 180
          blob_path: namespace_project_blob_path(merge_request.project.namespace,
                                                 merge_request.project,
                                                 ::File.join(merge_request.diff_refs.head_sha, our_path)),
181 182 183
          sections: sections
        }
      end
184 185 186 187 188 189 190 191 192 193 194

      # Don't try to print merge_request or repository.
      def inspect
        instance_variables = [:merge_file_result, :their_path, :our_path, :our_mode].map do |instance_variable|
          value = instance_variable_get("@#{instance_variable}")

          "#{instance_variable}=\"#{value}\""
        end

        "#<#{self.class} #{instance_variables.join(' ')}>"
      end
195 196 197
    end
  end
end