BigW Consortium Gitlab

file.rb 1.47 KB
Newer Older
1 2 3
module Gitlab
  module Diff
    class File
4
      attr_reader :diff, :diff_refs
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)
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
20 21 22 23
      end

      # Array of Gitlab::DIff::Line objects
      def diff_lines
24
        @lines ||= parser.parse(raw_diff.each_line).to_a
25 26
      end

27 28 29 30
      def too_large?
        diff.too_large?
      end

31
      def highlighted_diff_lines
32
        Gitlab::Diff::Highlight.new(self).highlight
33 34
      end

35 36 37 38
      def parallel_diff_lines
        Gitlab::Diff::ParallelDiff.new(self).parallelize
      end

39
      def mode_changed?
40
        !!(diff.a_mode && diff.b_mode && diff.a_mode != diff.b_mode)
41 42 43 44 45 46
      end

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

47
      def raw_diff
48
        diff.diff.to_s
49 50
      end

51 52 53 54 55 56 57 58 59
      def next_line(index)
        diff_lines[index + 1]
      end

      def prev_line(index)
        if index > 0
          diff_lines[index - 1]
        end
      end
60 61 62 63 64 65 66 67

      def file_path
        if diff.new_path.present?
          diff.new_path
        elsif diff.old_path.present?
          diff.old_path
        end
      end
68 69

      def added_lines
70
        diff_lines.count(&:added?)
71 72 73
      end

      def removed_lines
74
        diff_lines.count(&:removed?)
75
      end
76 77 78
    end
  end
end