GitLab steht wegen Wartungsarbeiten am Montag, den 10. Mai, zwischen 17:00 und 19:00 Uhr nicht zur Verfügung.

unified_diff.rb 7.53 KB
Newer Older
1
# Redmine - project management software
jplang's avatar
jplang committed
2
# Copyright (C) 2006-2013  Jean-Philippe Lang
3 4 5 6 7
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
8
#
9 10 11 12
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
13
#
14 15 16 17 18 19
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

module Redmine
  # Class used to parse unified diffs
20
  class UnifiedDiff < Array
21
    attr_reader :diff_type, :diff_style
22

jplang's avatar
jplang committed
23
    def initialize(diff, options={})
24
      options.assert_valid_keys(:type, :style, :max_lines)
jplang's avatar
jplang committed
25
      diff = diff.split("\n") if diff.is_a?(String)
26
      @diff_type = options[:type] || 'inline'
27
      @diff_style = options[:style]
28 29
      lines = 0
      @truncated = false
30
      diff_table = DiffTable.new(diff_type, diff_style)
tmaruyama's avatar
tmaruyama committed
31 32
      diff.each do |line_raw|
        line = Redmine::CodesetUtil.to_utf8_by_setting(line_raw)
33
        unless diff_table.add_line(line)
34
          self << diff_table if diff_table.length > 0
35
          diff_table = DiffTable.new(diff_type, diff_style)
36
        end
37 38 39 40 41
        lines += 1
        if options[:max_lines] && lines > options[:max_lines]
          @truncated = true
          break
        end
42 43 44 45
      end
      self << diff_table unless diff_table.empty?
      self
    end
46

47
    def truncated?; @truncated; end
48 49 50
  end

  # Class that represents a file diff
51
  class DiffTable < Array
52
    attr_reader :file_name
53 54 55

    # Initialize with a Diff file and the type of Diff View
    # The type view must be inline or sbs (side_by_side)
56
    def initialize(type="inline", style=nil)
57
      @parsing = false
58 59
      @added = 0
      @removed = 0
60
      @type = type
61 62
      @style = style
      @file_name = nil
63
      @git_diff = false
64 65 66
    end

    # Function for add a line of this Diff
67
    # Returns false when the diff ends
68 69 70
    def add_line(line)
      unless @parsing
        if line =~ /^(---|\+\+\+) (.*)$/
71
          self.file_name = $2
72
        elsif line =~ /^@@ (\+|\-)(\d+)(,\d+)? (\+|\-)(\d+)(,\d+)? @@/
73 74
          @line_num_l = $2.to_i
          @line_num_r = $5.to_i
75 76 77
          @parsing = true
        end
      else
78
        if line =~ %r{^[^\+\-\s@\\]}
79 80 81
          @parsing = false
          return false
        elsif line =~ /^@@ (\+|\-)(\d+)(,\d+)? (\+|\-)(\d+)(,\d+)? @@/
82 83
          @line_num_l = $2.to_i
          @line_num_r = $5.to_i
84
        else
85
          parse_line(line, @type)
86 87 88 89
        end
      end
      return true
    end
90

91 92 93 94 95 96 97 98 99
    def each_line
      prev_line_left, prev_line_right = nil, nil
      each do |line|
        spacing = prev_line_left && prev_line_right && (line.nb_line_left != prev_line_left+1) && (line.nb_line_right != prev_line_right+1)
        yield spacing, line
        prev_line_left = line.nb_line_left.to_i if line.nb_line_left.to_i > 0
        prev_line_right = line.nb_line_right.to_i if line.nb_line_right.to_i > 0
      end
    end
100 101 102 103 104 105 106 107 108

    def inspect
      puts '### DIFF TABLE ###'
      puts "file : #{file_name}"
      self.each do |d|
        d.inspect
      end
    end

109
    private
110

111
    def file_name=(arg)
112 113 114 115 116 117 118
      both_git_diff = false
      if file_name.nil?
        @git_diff = true if arg =~ %r{^(a/|/dev/null)}
      else
        both_git_diff = (@git_diff && arg =~ %r{^(b/|/dev/null)})
      end
      if both_git_diff
119 120
        if file_name && arg == "/dev/null"
          # keep the original file name
121
          @file_name = file_name.sub(%r{^a/}, '')
122
        else
123 124
          # remove leading b/
          @file_name = arg.sub(%r{^b/}, '')
125
        end
126
      elsif @style == "Subversion"
127 128
        # removing trailing "(revision nn)"
        @file_name = arg.sub(%r{\t+\(.*\)$}, '')
129 130 131 132 133
      else
        @file_name = arg
      end
    end

134 135 136 137 138 139 140 141 142
    def diff_for_added_line
      if @type == 'sbs' && @removed > 0 && @added < @removed
        self[-(@removed - @added)]
      else
        diff = Diff.new
        self << diff
        diff
      end
    end
143 144 145

    def parse_line(line, type="inline")
      if line[0, 1] == "+"
146
        diff = diff_for_added_line
147
        diff.line_right = line[1..-1]
148 149 150
        diff.nb_line_right = @line_num_r
        diff.type_diff_right = 'diff_in'
        @line_num_r += 1
151
        @added += 1
152 153 154
        true
      elsif line[0, 1] == "-"
        diff = Diff.new
155
        diff.line_left = line[1..-1]
156
        diff.nb_line_left = @line_num_l
157 158
        diff.type_diff_left = 'diff_out'
        self << diff
159
        @line_num_l += 1
160
        @removed += 1
161
        true
162 163 164 165
      else
        write_offsets
        if line[0, 1] =~ /\s/
          diff = Diff.new
166
          diff.line_right = line[1..-1]
167
          diff.nb_line_right = @line_num_r
168
          diff.line_left = line[1..-1]
169 170 171 172 173 174
          diff.nb_line_left = @line_num_l
          self << diff
          @line_num_l += 1
          @line_num_r += 1
          true
        elsif line[0, 1] = "\\"
175 176 177 178 179 180
          true
        else
          false
        end
      end
    end
181

182 183 184 185 186 187 188 189 190 191 192 193
    def write_offsets
      if @added > 0 && @added == @removed
        @added.times do |i|
          line = self[-(1 + i)]
          removed = (@type == 'sbs') ? line : self[-(1 + @added + i)]
          offsets = offsets(removed.line_left, line.line_right)
          removed.offsets = line.offsets = offsets
        end
      end
      @added = 0
      @removed = 0
    end
194

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    def offsets(line_left, line_right)
      if line_left.present? && line_right.present? && line_left != line_right
        max = [line_left.size, line_right.size].min
        starting = 0
        while starting < max && line_left[starting] == line_right[starting]
          starting += 1
        end
        ending = -1
        while ending >= -(max - starting) && line_left[ending] == line_right[ending]
          ending -= 1
        end
        unless starting == 0 && ending == -1
          [starting, ending]
        end
      end
    end
  end
212 213

  # A line of diff
214
  class Diff
215 216 217 218 219 220
    attr_accessor :nb_line_left
    attr_accessor :line_left
    attr_accessor :nb_line_right
    attr_accessor :line_right
    attr_accessor :type_diff_right
    attr_accessor :type_diff_left
221
    attr_accessor :offsets
222

223 224 225 226 227 228 229 230
    def initialize()
      self.nb_line_left = ''
      self.nb_line_right = ''
      self.line_left = ''
      self.line_right = ''
      self.type_diff_right = ''
      self.type_diff_left = ''
    end
231

232 233 234
    def type_diff
      type_diff_right == 'diff_in' ? type_diff_right : type_diff_left
    end
235

236 237 238
    def line
      type_diff_right == 'diff_in' ? line_right : line_left
    end
239

240
    def html_line_left
241
      line_to_html(line_left, offsets)
242
    end
243

244
    def html_line_right
245
      line_to_html(line_right, offsets)
246
    end
247

248
    def html_line
249
      line_to_html(line, offsets)
250
    end
251 252 253 254 255 256 257 258

    def inspect
      puts '### Start Line Diff ###'
      puts self.nb_line_left
      puts self.line_left
      puts self.nb_line_right
      puts self.line_right
    end
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276

    private

    def line_to_html(line, offsets)
      if offsets
        s = ''
        unless offsets.first == 0
          s << CGI.escapeHTML(line[0..offsets.first-1])
        end
        s << '<span>' + CGI.escapeHTML(line[offsets.first..offsets.last]) + '</span>'
        unless offsets.last == -1
          s << CGI.escapeHTML(line[offsets.last+1..-1])
        end
        s
      else
        CGI.escapeHTML(line)
      end
    end
277 278
  end
end