file.rb 2.8 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

      delegate :new_file, :deleted_file, :renamed_file,
7 8
        :old_path, :new_path, :a_mode, :b_mode,
        :submodule?, :too_large?, to: :diff, prefix: false
D
Dmitriy Zaporozhets 已提交
9

10
      def initialize(diff, repository:, diff_refs: nil)
D
Dmitriy Zaporozhets 已提交
11
        @diff = diff
12
        @repository = repository
13 14 15
        @diff_refs = diff_refs
      end

D
Douwe Maan 已提交
16 17 18 19 20 21 22 23
      def position(line)
        return unless diff_refs

        Position.new(
          old_path: old_path,
          new_path: new_path,
          old_line: line.old_line,
          new_line: line.new_line,
24
          diff_refs: diff_refs
D
Douwe Maan 已提交
25 26 27
        )
      end

28 29 30 31 32 33 34 35 36 37
      def line_code(line)
        return if line.meta?

        Gitlab::Diff::LineCode.generate(file_path, line.new_pos, line.old_pos)
      end

      def line_for_line_code(code)
        diff_lines.find { |line| line_code(line) == code }
      end

D
Douwe Maan 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51
      def line_for_position(pos)
        diff_lines.find { |line| position(line) == pos }
      end

      def position_for_line_code(code)
        line = line_for_line_code(code)
        position(line) if line
      end

      def line_code_for_position(pos)
        line = line_for_position(pos)
        line_code(line) if line
      end

D
Douwe Maan 已提交
52 53
      def content_commit
        return unless diff_refs
54

D
Douwe Maan 已提交
55 56 57
        repository.commit(deleted_file ? old_ref : new_ref)
      end

58
      def old_ref
59
        diff_refs.try(:base_sha)
60 61 62
      end

      def new_ref
63
        diff_refs.try(:head_sha)
D
Dmitriy Zaporozhets 已提交
64 65
      end

66
      # Array of Gitlab::Diff::Line objects
D
Dmitriy Zaporozhets 已提交
67
      def diff_lines
68
        @lines ||= Gitlab::Diff::Parser.new.parse(raw_diff.each_line).to_a
69 70
      end

71
      def highlighted_diff_lines
72
        @highlighted_diff_lines ||= Gitlab::Diff::Highlight.new(self, repository: self.repository).highlight
73 74
      end

75
      def parallel_diff_lines
76
        @parallel_diff_lines ||= Gitlab::Diff::ParallelDiff.new(self).parallelize
77 78
      end

D
Dmitriy Zaporozhets 已提交
79
      def mode_changed?
80
        a_mode && b_mode && a_mode != b_mode
D
Dmitriy Zaporozhets 已提交
81 82
      end

83
      def raw_diff
84
        diff.diff.to_s
85 86
      end

D
Dmitriy Zaporozhets 已提交
87 88 89 90 91
      def next_line(index)
        diff_lines[index + 1]
      end

      def prev_line(index)
92
        diff_lines[index - 1] if index > 0
D
Dmitriy Zaporozhets 已提交
93
      end
94

D
Douwe Maan 已提交
95 96 97 98
      def paths
        [old_path, new_path].compact
      end

99
      def file_path
100
        new_path.presence || old_path
101
      end
102 103

      def added_lines
G
Gabriel Mazetto 已提交
104
        diff_lines.count(&:added?)
105 106 107
      end

      def removed_lines
G
Gabriel Mazetto 已提交
108
        diff_lines.count(&:removed?)
109
      end
D
Douwe Maan 已提交
110 111 112 113 114 115 116 117 118 119 120 121

      def old_blob(commit = content_commit)
        return unless commit

        parent_id = commit.parent_id
        return unless parent_id

        repository.blob_at(parent_id, old_path)
      end

      def blob(commit = content_commit)
        return unless commit
122

D
Douwe Maan 已提交
123 124
        repository.blob_at(commit.id, file_path)
      end
D
Dmitriy Zaporozhets 已提交
125 126 127
    end
  end
end