merge_request_diff.rb 4.73 KB
Newer Older
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1 2 3 4 5
# == Schema Information
#
# Table name: merge_request_diffs
#
#  id               :integer          not null, primary key
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
6
#  state            :string(255)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
7 8 9 10 11 12 13
#  st_commits       :text
#  st_diffs         :text
#  merge_request_id :integer          not null
#  created_at       :datetime
#  updated_at       :datetime
#

14 15 16
require Rails.root.join("app/models/commit")

class MergeRequestDiff < ActiveRecord::Base
17 18
  include Sortable

19 20
  # Prevent store of diff if commits amount more then 500
  COMMITS_SAFE_SIZE = 500
21

22
  attr_reader :commits, :diffs, :diffs_no_whitespace
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

  belongs_to :merge_request

  delegate :target_branch, :source_branch, to: :merge_request, prefix: nil

  state_machine :state, initial: :empty do
    state :collected
    state :timeout
    state :overflow_commits_safe_size
    state :overflow_diff_files_limit
    state :overflow_diff_lines_limit
  end

  serialize :st_commits
  serialize :st_diffs

  after_create :reload_content

  def reload_content
    reload_commits
    reload_diffs
  end

  def diffs
    @diffs ||= (load_diffs(st_diffs) || [])
  end

50 51 52 53 54 55 56 57 58 59 60 61 62 63
  def diffs_no_whitespace
    # Get latest sha of branch from source project
    source_sha = merge_request.source_project.commit(source_branch).sha

    compare_result = Gitlab::CompareResult.new(
      Gitlab::Git::Compare.new(
        merge_request.target_project.repository.raw_repository,
        merge_request.target_branch,
        source_sha,
      ), { ignore_whitespace_change: true }
    )
    @diffs_no_whitespace ||= load_diffs(dump_commits(compare_result.diffs))
  end

64 65 66 67 68 69 70 71
  def commits
    @commits ||= load_commits(st_commits || [])
  end

  def last_commit
    commits.first
  end

72 73 74 75
  def first_commit
    commits.last
  end

76 77 78 79 80 81
  def base_commit
    return nil unless self.base_commit_sha

    merge_request.target_project.commit(self.base_commit_sha)
  end

82
  def last_commit_short_sha
83
    @last_commit_short_sha ||= last_commit.short_id
84 85 86 87 88 89 90 91 92
  end

  private

  def dump_commits(commits)
    commits.map(&:to_hash)
  end

  def load_commits(array)
93
    array.map { |hash| Commit.new(Gitlab::Git::Commit.new(hash), merge_request.source_project) }
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
  end

  def dump_diffs(diffs)
    if diffs.respond_to?(:map)
      diffs.map(&:to_hash)
    end
  end

  def load_diffs(raw)
    if raw.respond_to?(:map)
      raw.map { |hash| Gitlab::Git::Diff.new(hash) }
    end
  end

  # Collect array of Git::Commit objects
  # between target and source branches
  def unmerged_commits
111
    commits = compare_result.commits
112 113

    if commits.present?
114
      commits = Commit.decorate(commits, merge_request.source_project).
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
        sort_by(&:created_at).
        reverse
    end

    commits
  end

  # Reload all commits related to current merge request from repo
  # and save it as array of hashes in st_commits db field
  def reload_commits
    commit_objects = unmerged_commits

    if commit_objects.present?
      self.st_commits = dump_commits(commit_objects)
    end

    save
  end

  # Reload diffs between branches related to current merge request from repo
  # and save it as array of hashes in st_diffs db field
  def reload_diffs
    new_diffs = []

    if commits.size.zero?
      self.state = :empty
    elsif commits.size > COMMITS_SAFE_SIZE
      self.state = :overflow_commits_safe_size
    else
      new_diffs = unmerged_diffs
    end

    if new_diffs.any?
      if new_diffs.size > Commit::DIFF_HARD_LIMIT_FILES
        self.state = :overflow_diff_files_limit
150
        new_diffs = new_diffs.first(Commit::DIFF_HARD_LIMIT_LINES)
151 152 153 154
      end

      if new_diffs.sum { |diff| diff.diff.lines.count } > Commit::DIFF_HARD_LIMIT_LINES
        self.state = :overflow_diff_lines_limit
155
        new_diffs = new_diffs.first(Commit::DIFF_HARD_LIMIT_LINES)
156 157 158
      end
    end

159 160 161 162
    if new_diffs.present?
      new_diffs = dump_commits(new_diffs)
      self.state = :collected
    end
163 164

    self.st_diffs = new_diffs
165 166 167

    self.base_commit_sha = merge_request.target_project.commit(target_branch).try(:sha)

168 169 170 171 172 173
    self.save
  end

  # Collect array of Git::Diff objects
  # between target and source branches
  def unmerged_diffs
174 175
    compare_result.diffs || []
  rescue Gitlab::Git::Diff::TimeoutError
176
    self.state = :timeout
177
    []
178 179 180 181 182
  end

  def repository
    merge_request.target_project.repository
  end
183 184 185

  private

186
  def compare_result
187 188 189 190 191 192
    @compare_result ||=
      begin
        # Update ref for merge request
        merge_request.fetch_ref

        # Get latest sha of branch from source project
193 194
        source_commit = merge_request.source_project.commit(source_branch)
        source_sha = source_commit.try(:sha)
195 196 197 198 199 200 201 202 203

        Gitlab::CompareResult.new(
          Gitlab::Git::Compare.new(
            merge_request.target_project.repository.raw_repository,
            merge_request.target_branch,
            source_sha,
          )
        )
      end
204
  end
205
end