Skip to content
Snippets Groups Projects
file.rb 2.14 KiB
Newer Older
  • Learn to ignore specific revisions
  • module Gitlab
      module Diff
        class File
    
          attr_reader :diff, :repository, :diff_refs
    
    
          delegate :new_file, :deleted_file, :renamed_file,
    
            :old_path, :new_path, :a_mode, :b_mode,
            :submodule?, :too_large?, to: :diff, prefix: false
    
          def initialize(diff, repository:, diff_refs: nil)
    
            @diff = diff
    
            @repository = repository
    
          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
    
    
          def content_commit
            return unless diff_refs
            
            repository.commit(deleted_file ? old_ref : new_ref)
          end
    
    
            diff_refs.try(:base_sha)
    
            diff_refs.try(:head_sha)
    
          # Array of Gitlab::Diff::Line objects
    
          def diff_lines
    
            @lines ||= Gitlab::Diff::Parser.new.parse(raw_diff.each_line).to_a
    
          def highlighted_diff_lines
    
            @highlighted_diff_lines ||= Gitlab::Diff::Highlight.new(self, repository: self.repository).highlight
    
          def parallel_diff_lines
    
            @parallel_diff_lines ||= Gitlab::Diff::ParallelDiff.new(self).parallelize
    
          def mode_changed?
    
            a_mode && b_mode && a_mode != b_mode
    
          def next_line(index)
            diff_lines[index + 1]
          end
    
          def prev_line(index)
    
            diff_lines[index - 1] if index > 0
    
            new_path.presence || old_path
    
    Gabriel Mazetto's avatar
    Gabriel Mazetto committed
            diff_lines.count(&:added?)
    
    Gabriel Mazetto's avatar
    Gabriel Mazetto committed
            diff_lines.count(&:removed?)
    
    
          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
            repository.blob_at(commit.id, file_path)
          end