file.rb 1.05 KB
Newer Older
1 2 3
module Gitlab
  module Diff
    class File
4
      attr_reader :diff
5 6 7 8

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

9
      def initialize(diff)
10 11 12 13 14
        @diff = diff
      end

      # Array of Gitlab::DIff::Line objects
      def diff_lines
15
        @lines ||= parser.parse(raw_diff.lines)
16 17 18
      end

      def mode_changed?
19
        !!(diff.a_mode && diff.b_mode && diff.a_mode != diff.b_mode)
20 21 22 23 24 25
      end

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

26
      def raw_diff
27
        diff.diff.to_s
28 29
      end

30 31 32 33 34 35 36 37 38
      def next_line(index)
        diff_lines[index + 1]
      end

      def prev_line(index)
        if index > 0
          diff_lines[index - 1]
        end
      end
39 40 41 42 43 44 45 46

      def file_path
        if diff.new_path.present?
          diff.new_path
        elsif diff.old_path.present?
          diff.old_path
        end
      end
47 48

      def added_lines
Gabriel Mazetto's avatar
Gabriel Mazetto committed
49
        diff_lines.count(&:added?)
50 51 52
      end

      def removed_lines
Gabriel Mazetto's avatar
Gabriel Mazetto committed
53
        diff_lines.count(&:removed?)
54
      end
55 56 57
    end
  end
end