merge_request_diff.rb 6.93 KB
Newer Older
1
class MergeRequestDiff < ActiveRecord::Base
2
  include Sortable
3
  include Importable
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
  # All diff information is collected from repository after object is created.
  # It allows you to override variables like head_commit_sha before getting diff.
27
  after_create :save_git_content, unless: :importing?
28

29 30 31 32
  def self.select_without_diff
    select(column_names - ['st_diffs'])
  end

33 34 35
  # Collect information about commits and diff from repository
  # and save it to the database as serialized data
  def save_git_content
36
    ensure_commits_sha
37
    save_commits
38
    reload_commits
39
    save_diffs
40 41 42 43
    keep_around_commits
  end

  def ensure_commits_sha
44
    merge_request.fetch_ref
45
    self.start_commit_sha ||= merge_request.target_branch_sha
46
    self.head_commit_sha  ||= merge_request.source_branch_sha
47
    self.base_commit_sha  ||= find_base_sha
48 49 50 51 52 53 54 55 56 57 58
    save
  end

  # Override head_commit_sha to keep compatibility with merge request diff
  # created before version 8.4 that does not store head_commit_sha in separate db field.
  def head_commit_sha
    if persisted? && super.nil?
      last_commit.try(:sha)
    else
      super
    end
59 60
  end

61 62 63 64 65 66 67
  # This method will rely on repository branch sha
  # in case start_commit_sha is nil. Its necesarry for old merge request diff
  # created before version 8.4 to work
  def safe_start_commit_sha
    start_commit_sha || merge_request.target_branch_sha
  end

68
  def size
69
    real_size.presence || raw_diffs.size
70 71
  end

72
  def raw_diffs(options = {})
73
    if options[:ignore_whitespace_change]
74
      @diffs_no_whitespace ||=
75 76 77
        Gitlab::Git::Compare.new(
          repository.raw_repository,
          safe_start_commit_sha,
78
          head_commit_sha).diffs(options)
79
    else
80 81
      @raw_diffs ||= {}
      @raw_diffs[options] ||= load_diffs(st_diffs, options)
82
    end
83 84
  end

85 86 87 88
  def commits
    @commits ||= load_commits(st_commits || [])
  end

89 90 91 92 93
  def reload_commits
    @commits = nil
    commits
  end

94 95 96 97
  def last_commit
    commits.first
  end

98 99 100 101
  def first_commit
    commits.last
  end

102
  def base_commit
103
    return unless base_commit_sha
104

105
    project.commit(base_commit_sha)
106 107
  end

108
  def start_commit
109
    return unless start_commit_sha
110

111
    project.commit(start_commit_sha)
112 113 114
  end

  def head_commit
115
    return unless head_commit_sha
116

117
    project.commit(head_commit_sha)
118 119
  end

120
  def diff_refs
121
    return unless start_commit_sha || base_commit_sha
122 123 124 125 126 127 128 129

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

130 131 132 133
  def diff_refs_by_sha?
    base_commit_sha? && head_commit_sha? && start_commit_sha?
  end

134 135 136 137 138 139 140
  def diffs(diff_options = nil)
    Gitlab::Diff::FileCollection::MergeRequestDiff.new(self, diff_options: diff_options)
  end

  def project
    merge_request.target_project
  end
141

142 143
  def compare
    @compare ||=
144 145 146 147 148
      Gitlab::Git::Compare.new(
        repository.raw_repository,
        safe_start_commit_sha,
        head_commit_sha
      )
149 150
  end

151 152 153 154
  def latest?
    self == merge_request.merge_request_diff
  end

155 156
  private

157 158 159 160 161 162 163 164
  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

165
  # Load all commits related to current merge request diff from repo
166
  # and save it as array of hashes in st_commits db field
167
  def save_commits
168 169
    new_attributes = {}

170
    commits = compare.commits
171

172 173 174
    if commits.present?
      commits = Commit.decorate(commits, merge_request.source_project).reverse
      new_attributes[:st_commits] = dump_commits(commits)
175 176
    end

177
    update_columns_serialized(new_attributes)
178 179
  end

180 181 182 183 184 185 186 187
  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)
188
      if paths = options[:paths]
189
        raw = raw.select do |diff|
190
          paths.include?(diff[:old_path]) || paths.include?(diff[:new_path])
191 192 193
        end
      end

194 195 196 197 198 199
      Gitlab::Git::DiffCollection.new(raw, options)
    else
      Gitlab::Git::DiffCollection.new([])
    end
  end

200
  # Load diffs between branches related to current merge request diff from repo
201
  # and save it as array of hashes in st_diffs db field
202
  def save_diffs
203
    new_attributes = {}
204 205 206
    new_diffs = []

    if commits.size.zero?
207
      new_attributes[:state] = :empty
208
    else
209
      diff_collection = compare.diffs(Commit.max_diff_options)
210

211 212 213
      if diff_collection.overflow?
        # Set our state to 'overflow' to make the #empty? and #collected?
        # methods (generated by StateMachine) return false.
214
        new_attributes[:state] = :overflow
215 216
      end

217
      new_attributes[:real_size] = diff_collection.real_size
218

219 220
      if diff_collection.any?
        new_diffs = dump_diffs(diff_collection)
221
        new_attributes[:state] = :collected
222
      end
223
    end
224

225 226
    new_attributes[:st_diffs] = new_diffs
    update_columns_serialized(new_attributes)
227 228 229
  end

  def repository
230
    project.repository
231
  end
232

233 234
  def find_base_sha
    return unless head_commit_sha && start_commit_sha
235

236
    project.merge_base_commit(head_commit_sha, start_commit_sha).try(:sha)
237 238
  end

239 240 241 242 243 244 245 246
  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

247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
  #
  # #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
269

270
  def keep_around_commits
271 272 273
    repository.keep_around(start_commit_sha)
    repository.keep_around(head_commit_sha)
    repository.keep_around(base_commit_sha)
274
  end
275
end