merge_request_diff.rb 6.34 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

  belongs_to :merge_request

11
  delegate :source_branch_sha, :target_branch_sha, :target_branch, :source_branch, to: :merge_request, prefix: nil
12 13 14

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

  serialize :st_commits
  serialize :st_diffs

27
  after_create :reload_content, unless: :importing?
28
  after_save :keep_around_commits, unless: :importing?
29 30 31 32 33 34

  def reload_content
    reload_commits
    reload_diffs
  end

35
  def size
36
    real_size.presence || raw_diffs.size
37 38
  end

39
  def raw_diffs(options={})
40
    if options[:ignore_whitespace_change]
41
      @raw_diffs_no_whitespace ||= begin
42
        compare = Gitlab::Git::Compare.new(
43
          repository.raw_repository,
44 45
          self.start_commit_sha || self.target_branch_sha,
          self.head_commit_sha || self.source_branch_sha,
46 47 48 49
        )
        compare.diffs(options)
      end
    else
50 51
      @raw_diffs ||= {}
      @raw_diffs[options] ||= load_diffs(st_diffs, options)
52
    end
53 54
  end

55 56 57 58 59 60 61 62
  def commits
    @commits ||= load_commits(st_commits || [])
  end

  def last_commit
    commits.first
  end

63 64 65 66
  def first_commit
    commits.last
  end

67
  def base_commit
68
    return unless self.base_commit_sha
69

70
    project.commit(self.base_commit_sha)
71 72
  end

73 74 75
  def start_commit
    return unless self.start_commit_sha

76
    project.commit(self.start_commit_sha)
77 78 79 80 81
  end

  def head_commit
    return last_commit unless self.head_commit_sha

82
    project.commit(self.head_commit_sha)
83 84
  end

85 86 87 88
  def diff_refs_by_sha?
    base_commit_sha? && head_commit_sha? && start_commit_sha?
  end

89 90 91 92 93
  def compare
    @compare ||=
      begin
        # Update ref for merge request
        merge_request.fetch_ref
94

95
        Gitlab::Git::Compare.new(
96
          repository.raw_repository,
97 98 99 100
          self.target_branch_sha,
          self.source_branch_sha
        )
      end
101 102
  end

103
  private
104 105 106 107

  # Collect array of Git::Commit objects
  # between target and source branches
  def unmerged_commits
108
    commits = compare.commits
109 110

    if commits.present?
Sean McGivern's avatar
Sean McGivern committed
111
      commits = Commit.decorate(commits, merge_request.source_project).reverse
112 113 114 115 116
    end

    commits
  end

117 118 119 120 121 122 123 124
  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

125 126 127
  # 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
128 129
    new_attributes = {}

130 131 132
    commit_objects = unmerged_commits

    if commit_objects.present?
133
      new_attributes[:st_commits] = dump_commits(commit_objects)
134 135
    end

136
    update_columns_serialized(new_attributes)
137 138
  end

139 140 141 142 143 144 145 146 147 148 149 150 151 152
  # Collect array of Git::Diff objects
  # between target and source branches
  def unmerged_diffs
    compare.diffs(Commit.max_diff_options)
  end

  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)
153
      if paths = options[:paths]
154
        raw = raw.select do |diff|
155
          paths.include?(diff[:old_path]) || paths.include?(diff[:new_path])
156 157 158
        end
      end

159 160 161 162 163 164
      Gitlab::Git::DiffCollection.new(raw, options)
    else
      Gitlab::Git::DiffCollection.new([])
    end
  end

165 166 167
  # 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
168
    new_attributes = {}
169 170 171
    new_diffs = []

    if commits.size.zero?
172
      new_attributes[:state] = :empty
173
    else
174
      diff_collection = unmerged_diffs
175

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

182
      new_attributes[:real_size] = diff_collection.real_size
183

184 185
      if diff_collection.any?
        new_diffs = dump_diffs(diff_collection)
186
        new_attributes[:state] = :collected
187
      end
188
    end
189

190
    new_attributes[:st_diffs] = new_diffs
191

192 193 194
    new_attributes[:start_commit_sha] = self.target_branch_sha
    new_attributes[:head_commit_sha] = self.source_branch_sha
    new_attributes[:base_commit_sha] = branch_base_sha
195

196
    update_columns_serialized(new_attributes)
197 198

    keep_around_commits
199 200
  end

201 202
  def project
    merge_request.target_project
203 204 205
  end

  def repository
206
    project.repository
207
  end
208

209 210
  def branch_base_commit
    return unless self.source_branch_sha && self.target_branch_sha
211

212
    project.merge_base_commit(self.source_branch_sha, self.target_branch_sha)
213 214
  end

215 216
  def branch_base_sha
    branch_base_commit.try(:sha)
217 218
  end

James Lopez's avatar
James Lopez committed
219 220 221 222 223 224 225 226
  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

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
  #
  # #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
249

250
  def keep_around_commits
251 252 253
    repository.keep_around(target_branch_sha)
    repository.keep_around(source_branch_sha)
    repository.keep_around(branch_base_sha)
254
  end
255
end