file.rb 1.2 KB
Newer Older
D
Dmitriy Zaporozhets 已提交
1 2 3
module Gitlab
  module Diff
    class File
4
      attr_reader :diff, :new_ref, :old_ref
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, diff_refs)
D
Dmitriy Zaporozhets 已提交
10
        @diff = diff
11
        @old_ref, @new_ref = diff_refs
D
Dmitriy Zaporozhets 已提交
12 13 14 15
      end

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

19
      def highlighted_diff_lines
20
        Gitlab::Diff::Highlight.new(self).highlight
21 22
      end

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

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

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

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

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

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

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

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