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

      def old_ref
16
        diff_refs.try(:base_sha)
17 18 19
      end

      def new_ref
20
        diff_refs.try(:head_sha)
D
Dmitriy Zaporozhets 已提交
21 22
      end

23
      # Array of Gitlab::Diff::Line objects
D
Dmitriy Zaporozhets 已提交
24
      def diff_lines
25
        @lines ||= Gitlab::Diff::Parser.new.parse(raw_diff.each_line).to_a
26 27
      end

28
      def highlighted_diff_lines
29
        @highlighted_diff_lines ||= Gitlab::Diff::Highlight.new(self, repository: self.repository).highlight
30 31
      end

32
      def parallel_diff_lines
33
        @parallel_diff_lines ||= Gitlab::Diff::ParallelDiff.new(self).parallelize
34 35
      end

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

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

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

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

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

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

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

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