file.rb 1.4 KB
Newer Older
D
Dmitriy Zaporozhets 已提交
1 2 3
module Gitlab
  module Diff
    class File
4
      attr_reader :diff, :diff_refs
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 12 13 14 15 16 17 18 19
        @diff_refs = diff_refs
      end

      def old_ref
        diff_refs[0] if diff_refs
      end

      def new_ref
        diff_refs[1] if diff_refs
D
Dmitriy Zaporozhets 已提交
20 21 22 23
      end

      # Array of Gitlab::DIff::Line objects
      def diff_lines
J
Jacob Vosmaer 已提交
24
        @lines ||= parser.parse(raw_diff.each_line).to_a
D
Dmitriy Zaporozhets 已提交
25 26
      end

27
      def highlighted_diff_lines
28
        Gitlab::Diff::Highlight.new(self).highlight
29 30
      end

31 32 33 34
      def parallel_diff_lines
        Gitlab::Diff::ParallelDiff.new(self).parallelize
      end

D
Dmitriy Zaporozhets 已提交
35
      def mode_changed?
36
        !!(diff.a_mode && diff.b_mode && diff.a_mode != diff.b_mode)
D
Dmitriy Zaporozhets 已提交
37 38 39 40 41 42
      end

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

43
      def raw_diff
44
        diff.diff.to_s
45 46
      end

D
Dmitriy Zaporozhets 已提交
47 48 49 50 51 52 53 54 55
      def next_line(index)
        diff_lines[index + 1]
      end

      def prev_line(index)
        if index > 0
          diff_lines[index - 1]
        end
      end
56 57 58 59 60 61 62 63

      def file_path
        if diff.new_path.present?
          diff.new_path
        elsif diff.old_path.present?
          diff.old_path
        end
      end
64 65

      def added_lines
G
Gabriel Mazetto 已提交
66
        diff_lines.count(&:added?)
67 68 69
      end

      def removed_lines
G
Gabriel Mazetto 已提交
70
        diff_lines.count(&:removed?)
71
      end
D
Dmitriy Zaporozhets 已提交
72 73 74
    end
  end
end