file.rb 1.2 KB
Newer Older
D
Dmitriy Zaporozhets 已提交
1 2 3
module Gitlab
  module Diff
    class File
4
      attr_reader :diff
D
Dmitriy Zaporozhets 已提交
5 6 7 8

      delegate :new_file, :deleted_file, :renamed_file,
        :old_path, :new_path, to: :diff, prefix: false

9
      def initialize(diff)
D
Dmitriy Zaporozhets 已提交
10 11 12 13 14
        @diff = diff
      end

      # Array of Gitlab::DIff::Line objects
      def diff_lines
15
        @lines ||= parser.parse(raw_diff.lines)
D
Dmitriy Zaporozhets 已提交
16 17
      end

18
      def highlighted_diff_lines
R
Rubén Dávila 已提交
19
        Gitlab::Diff::Highlight.process_diff_lines(new_path, diff_lines)
20 21
      end

D
Dmitriy Zaporozhets 已提交
22
      def mode_changed?
23
        !!(diff.a_mode && diff.b_mode && diff.a_mode != diff.b_mode)
D
Dmitriy Zaporozhets 已提交
24 25 26 27 28 29
      end

      def parser
        Gitlab::Diff::Parser.new
      end

30
      def raw_diff
31
        diff.diff.to_s
32 33
      end

D
Dmitriy Zaporozhets 已提交
34 35 36 37 38 39 40 41 42
      def next_line(index)
        diff_lines[index + 1]
      end

      def prev_line(index)
        if index > 0
          diff_lines[index - 1]
        end
      end
43 44 45 46 47 48 49 50

      def file_path
        if diff.new_path.present?
          diff.new_path
        elsif diff.old_path.present?
          diff.old_path
        end
      end
51 52

      def added_lines
G
Gabriel Mazetto 已提交
53
        diff_lines.count(&:added?)
54 55 56
      end

      def removed_lines
G
Gabriel Mazetto 已提交
57
        diff_lines.count(&:removed?)
58
      end
D
Dmitriy Zaporozhets 已提交
59 60 61
    end
  end
end