BigW Consortium Gitlab

file.rb 3.09 KB
Newer Older
1 2 3
module Gitlab
  module Diff
    class File
4
      attr_reader :diff, :repository, :diff_refs
5 6

      delegate :new_file, :deleted_file, :renamed_file,
7
        :old_path, :new_path, :a_mode, :b_mode,
8
        :submodule?, :too_large?, :collapsed?, to: :diff, prefix: false
9

10
      def initialize(diff, repository:, diff_refs: nil)
11
        @diff = diff
12
        @repository = repository
13 14 15
        @diff_refs = diff_refs
      end

Douwe Maan committed
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
Douwe Maan committed
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

Douwe Maan committed
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

52 53
      def content_commit
        return unless diff_refs
54

55 56 57
        repository.commit(deleted_file ? old_ref : new_ref)
      end

58 59 60 61 62 63
      def old_content_commit
        return unless diff_refs

        repository.commit(old_ref)
      end

64
      def old_ref
65
        diff_refs.try(:base_sha)
66 67 68
      end

      def new_ref
69
        diff_refs.try(:head_sha)
70 71
      end

72
      attr_writer :highlighted_diff_lines
73

74
      # Array of Gitlab::Diff::Line objects
75
      def diff_lines
76
        @diff_lines ||= Gitlab::Diff::Parser.new.parse(raw_diff.each_line).to_a
77 78
      end

79
      def highlighted_diff_lines
80
        @highlighted_diff_lines ||= Gitlab::Diff::Highlight.new(self, repository: self.repository).highlight
81 82
      end

83
      # Array[<Hash>] with right/left keys that contains Gitlab::Diff::Line objects which text is hightlighted
84
      def parallel_diff_lines
85
        @parallel_diff_lines ||= Gitlab::Diff::ParallelDiff.new(self).parallelize
86 87
      end

88
      def mode_changed?
89
        a_mode && b_mode && a_mode != b_mode
90 91
      end

92
      def raw_diff
93
        diff.diff.to_s
94 95
      end

96 97 98 99 100
      def next_line(index)
        diff_lines[index + 1]
      end

      def prev_line(index)
101
        diff_lines[index - 1] if index > 0
102
      end
103

Douwe Maan committed
104 105 106 107
      def paths
        [old_path, new_path].compact
      end

108
      def file_path
109
        new_path.presence || old_path
110
      end
111 112

      def added_lines
113
        diff_lines.count(&:added?)
114 115 116
      end

      def removed_lines
117
        diff_lines.count(&:removed?)
118
      end
119

120
      def old_blob(commit = old_content_commit)
121 122
        return unless commit

123
        repository.blob_at(commit.id, old_path)
124 125 126 127
      end

      def blob(commit = content_commit)
        return unless commit
128

129 130
        repository.blob_at(commit.id, file_path)
      end
131

132
      def file_identifier
133 134
        "#{file_path}-#{new_file}-#{deleted_file}-#{renamed_file}"
      end
135 136 137
    end
  end
end