merge_request_diff.rb 5.68 KB
Newer Older
1
class MergeRequestDiff < ActiveRecord::Base
2
  include Sortable
3
  include Importable
4

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

  belongs_to :merge_request

10
  delegate :head_source_sha, :target_branch, :source_branch, to: :merge_request, prefix: nil
11 12 13

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

  serialize :st_commits
  serialize :st_diffs

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

  def reload_content
    reload_commits
    reload_diffs
  end

34 35
  def size
    real_size.presence || diffs.size
36 37
  end

38 39 40 41 42
  def diffs(options={})
    if options[:ignore_whitespace_change]
      @diffs_no_whitespace ||= begin
        compare = Gitlab::Git::Compare.new(
          self.repository.raw_repository,
43 44
          self.base,
          self.head,
45 46 47 48 49 50
        )
        compare.diffs(options)
      end
    else
      @diffs ||= load_diffs(st_diffs, options)
    end
51 52
  end

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

  def last_commit
    commits.first
  end

61 62 63 64
  def first_commit
    commits.last
  end

65 66 67 68 69 70
  def base_commit
    return nil unless self.base_commit_sha

    merge_request.target_project.commit(self.base_commit_sha)
  end

71
  def last_commit_short_sha
72
    @last_commit_short_sha ||= last_commit.short_id
73 74 75 76 77 78 79
  end

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

  def load_commits(array)
80
    array.map { |hash| Commit.new(Gitlab::Git::Commit.new(hash), merge_request.source_project) }
81 82 83 84 85 86 87 88
  end

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

89 90 91 92 93
  def load_diffs(raw, options)
    if raw.respond_to?(:each)
      Gitlab::Git::DiffCollection.new(raw, options)
    else
      Gitlab::Git::DiffCollection.new([])
94 95 96 97 98 99
    end
  end

  # Collect array of Git::Commit objects
  # between target and source branches
  def unmerged_commits
100
    commits = compare.commits
101 102

    if commits.present?
Sean McGivern's avatar
Sean McGivern committed
103
      commits = Commit.decorate(commits, merge_request.source_project).reverse
104 105 106 107 108 109 110 111
    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
112 113
    new_attributes = {}

114 115 116
    commit_objects = unmerged_commits

    if commit_objects.present?
117
      new_attributes[:st_commits] = dump_commits(commit_objects)
118 119
    end

120
    update_columns_serialized(new_attributes)
121 122 123 124 125
  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
126
    new_attributes = {}
127 128 129
    new_diffs = []

    if commits.size.zero?
130
      new_attributes[:state] = :empty
131
    else
132
      diff_collection = unmerged_diffs
133

134 135 136
      if diff_collection.overflow?
        # Set our state to 'overflow' to make the #empty? and #collected?
        # methods (generated by StateMachine) return false.
137
        new_attributes[:state] = :overflow
138 139
      end

140
      new_attributes[:real_size] = diff_collection.real_size
141

142 143
      if diff_collection.any?
        new_diffs = dump_diffs(diff_collection)
144
        new_attributes[:state] = :collected
145
      end
146
    end
147

148
    new_attributes[:st_diffs] = new_diffs
149

150 151 152
    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
153

154
    update_columns_serialized(new_attributes)
155 156

    keep_around_commits
157 158 159 160 161
  end

  # Collect array of Git::Diff objects
  # between target and source branches
  def unmerged_diffs
162
    compare.diffs(Commit.max_diff_options)
163 164 165 166 167
  end

  def repository
    merge_request.target_project.repository
  end
168

169
  def source_sha
170 171
    return head_source_sha if head_source_sha.present?

172 173 174
    source_commit = merge_request.source_project.commit(source_branch)
    source_commit.try(:sha)
  end
175

176 177 178 179 180 181 182 183 184 185 186 187
  def target_sha
    merge_request.target_sha
  end

  def base
    self.target_sha || self.target_branch
  end

  def head
    self.source_sha
  end

188 189
  def compare
    @compare ||=
190 191 192 193
      begin
        # Update ref for merge request
        merge_request.fetch_ref

194 195
        Gitlab::Git::Compare.new(
          self.repository.raw_repository,
196 197
          self.base,
          self.head
198 199
        )
      end
200
  end
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225

  private

  #
  # #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
226

227 228 229 230
  def keep_around_commits
    self.repository.keep_around(target_branch_sha)
    self.repository.keep_around(source_branch_sha)
    self.repository.keep_around(branch_base_sha)
231
  end
232
end