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

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

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

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

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

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

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

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

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

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

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