merge_request_diff.rb 6.15 KB
Newer Older
1
class MergeRequestDiff < ActiveRecord::Base
2
  include Sortable
3
  include Importable
James Lopez's avatar
James Lopez committed
4
  include EncodingHelper
5

6
  # Prevent store of diff if commits amount more then 500
7
  COMMITS_SAFE_SIZE = 100
8 9 10 11 12

  belongs_to :merge_request

  state_machine :state, initial: :empty do
    state :collected
13 14 15
    state :overflow
    # Deprecated states: these are no longer used but these values may still occur
    # in the database.
16 17 18 19 20 21 22 23 24
    state :timeout
    state :overflow_commits_safe_size
    state :overflow_diff_files_limit
    state :overflow_diff_lines_limit
  end

  serialize :st_commits
  serialize :st_diffs

25 26 27 28 29 30 31
  validates :head_commit_sha,  presence: true
  validates :start_commit_sha, presence: true
  validates :base_commit_sha,  presence: true

  after_initialize :ensure_head_commit_sha, if: :persisted?
  before_create :set_diff_range,   unless: :importing?
  after_create :reload_content,    unless: :importing?
32
  after_save :keep_around_commits, unless: :importing?
33

34 35 36 37
  def ensure_head_commit_sha
    self.head_commit_sha ||= last_commit.sha
  end

38
  def set_diff_range
39 40 41
    self.start_commit_sha ||= merge_request.target_branch_sha
    self.head_commit_sha  ||= merge_request.source_branch_sha
    self.base_commit_sha  ||= find_base_sha
42 43
  end

44 45 46 47 48
  def reload_content
    reload_commits
    reload_diffs
  end

49 50
  def size
    real_size.presence || diffs.size
51 52
  end

53 54 55 56
  def diffs(options={})
    if options[:ignore_whitespace_change]
      @diffs_no_whitespace ||= begin
        compare = Gitlab::Git::Compare.new(
57
          repository.raw_repository,
58 59
          start_commit_sha,
          head_commit_sha
60 61 62 63
        )
        compare.diffs(options)
      end
    else
64
      @diffs ||= {}
65
      @diffs[options] ||= load_diffs(st_diffs, options)
66
    end
67 68
  end

69 70 71 72 73 74 75 76
  def commits
    @commits ||= load_commits(st_commits || [])
  end

  def last_commit
    commits.first
  end

77 78 79 80
  def first_commit
    commits.last
  end

81
  def base_commit
82
    return unless base_commit_sha
83

84
    project.commit(base_commit_sha)
85 86
  end

87
  def start_commit
88
    return unless start_commit_sha
89

90
    project.commit(start_commit_sha)
91 92 93
  end

  def head_commit
94
    return last_commit unless head_commit_sha
95

96
    project.commit(head_commit_sha)
97 98
  end

99 100 101 102 103 104 105 106 107 108
  def diff_refs
    return unless start_commit || base_commit

    Gitlab::Diff::DiffRefs.new(
      base_sha:  base_commit_sha,
      start_sha: start_commit_sha,
      head_sha:  head_commit_sha
    )
  end

109
  private
110

111 112 113 114 115
  def compare
    @compare ||=
      begin
        # Update ref for merge request
        merge_request.fetch_ref
116

117 118 119 120 121 122
        Gitlab::Git::Compare.new(
          repository.raw_repository,
          start_commit_sha,
          head_commit_sha
        )
      end
123 124
  end

125 126 127 128 129 130 131 132
  def dump_commits(commits)
    commits.map(&:to_hash)
  end

  def load_commits(array)
    array.map { |hash| Commit.new(Gitlab::Git::Commit.new(hash), merge_request.source_project) }
  end

133 134 135
  # 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
136 137
    new_attributes = {}

138
    commits = compare.commits
139

140 141 142
    if commits.present?
      commits = Commit.decorate(commits, merge_request.source_project).reverse
      new_attributes[:st_commits] = dump_commits(commits)
143 144
    end

145
    update_columns_serialized(new_attributes)
146 147
  end

148 149 150 151 152 153 154 155
  def dump_diffs(diffs)
    if diffs.respond_to?(:map)
      diffs.map(&:to_hash)
    end
  end

  def load_diffs(raw, options)
    if raw.respond_to?(:each)
156
      if paths = options[:paths]
157
        raw = raw.select do |diff|
158
          paths.include?(diff[:old_path]) || paths.include?(diff[:new_path])
159 160 161
        end
      end

162 163 164 165 166 167
      Gitlab::Git::DiffCollection.new(raw, options)
    else
      Gitlab::Git::DiffCollection.new([])
    end
  end

168 169 170
  # 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
171
    new_attributes = {}
172 173 174
    new_diffs = []

    if commits.size.zero?
175
      new_attributes[:state] = :empty
176
    else
177
      diff_collection = compare.diffs(Commit.max_diff_options)
178

179 180 181
      if diff_collection.overflow?
        # Set our state to 'overflow' to make the #empty? and #collected?
        # methods (generated by StateMachine) return false.
182
        new_attributes[:state] = :overflow
183 184
      end

185
      new_attributes[:real_size] = diff_collection.real_size
186

187 188
      if diff_collection.any?
        new_diffs = dump_diffs(diff_collection)
189
        new_attributes[:state] = :collected
190
      end
191
    end
192

193
    new_attributes[:st_diffs] = new_diffs
194

195
    update_columns_serialized(new_attributes)
196 197

    keep_around_commits
198 199
  end

200 201
  def project
    merge_request.target_project
202 203 204
  end

  def repository
205
    project.repository
206
  end
207

208 209
  def find_base_sha
    return unless head_commit_sha && start_commit_sha
210

211
    project.merge_base_commit(head_commit_sha, start_commit_sha).try(:sha)
212 213
  end

James Lopez's avatar
James Lopez committed
214 215 216 217 218 219 220 221
  def utf8_st_diffs
    st_diffs.map do |diff|
      diff.each do |k, v|
        diff[k] = encode_utf8(v) if v.respond_to?(:encoding)
      end
    end
  end

222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
  #
  # #save or #update_attributes providing changes on serialized attributes do a lot of
  # serialization and deserialization calls resulting in bad performance.
  # Using #update_columns solves the problem with just one YAML.dump per serialized attribute that we provide.
  # As a tradeoff we need to reload the current instance to properly manage time objects on those serialized
  # attributes. So to keep the same behaviour as the attribute assignment we reload the instance.
  # The difference is in the usage of
  # #write_attribute= (#update_attributes) and #raw_write_attribute= (#update_columns)
  #
  # Ex:
  #
  #   new_attributes[:st_commits].first.slice(:committed_date)
  #   => {:committed_date=>2014-02-27 11:01:38 +0200}
  #   YAML.load(YAML.dump(new_attributes[:st_commits].first.slice(:committed_date)))
  #   => {:committed_date=>2014-02-27 10:01:38 +0100}
  #
  def update_columns_serialized(new_attributes)
    return unless new_attributes.any?

    update_columns(new_attributes.merge(updated_at: current_time_from_proper_timezone))
    reload
  end
244

245
  def keep_around_commits
246 247 248
    repository.keep_around(start_commit_sha)
    repository.keep_around(head_commit_sha)
    repository.keep_around(base_commit_sha)
249
  end
250
end