merge_request_diff.rb 4.23 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
class MergeRequestDiff < ActiveRecord::Base
15 16
  include Sortable

17
  # Prevent store of diff if commits amount more then 500
18
  COMMITS_SAFE_SIZE = 100
19 20 21 22 23 24 25

  belongs_to :merge_request

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

  state_machine :state, initial: :empty do
    state :collected
26 27 28
    state :overflow
    # Deprecated states: these are no longer used but these values may still occur
    # in the database.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
    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

45 46
  def size
    real_size.presence || diffs.size
47 48
  end

49 50 51 52 53 54 55 56 57 58 59 60 61
  def diffs(options={})
    if options[:ignore_whitespace_change]
      @diffs_no_whitespace ||= begin
        compare = Gitlab::Git::Compare.new(
          self.repository.raw_repository,
          self.target_branch,
          self.source_sha,
        )
        compare.diffs(options)
      end
    else
      @diffs ||= load_diffs(st_diffs, options)
    end
62 63
  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
  end

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

  def load_commits(array)
91
    array.map { |hash| Commit.new(Gitlab::Git::Commit.new(hash), merge_request.source_project) }
92 93 94 95 96 97 98 99
  end

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

100 101 102 103 104
  def load_diffs(raw, options)
    if raw.respond_to?(:each)
      Gitlab::Git::DiffCollection.new(raw, options)
    else
      Gitlab::Git::DiffCollection.new([])
105 106 107 108 109 110
    end
  end

  # Collect array of Git::Commit objects
  # between target and source branches
  def unmerged_commits
111
    commits = compare.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
        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
    else
142
      diff_collection = unmerged_diffs
143

144 145 146 147
      if diff_collection.overflow?
        # Set our state to 'overflow' to make the #empty? and #collected?
        # methods (generated by StateMachine) return false.
        self.state = :overflow
148 149
      end

150
      self.real_size = diff_collection.real_size
151

152 153 154 155
      if diff_collection.any?
        new_diffs = dump_diffs(diff_collection)
        self.state = :collected
      end
156
    end
157 158

    self.st_diffs = new_diffs
159

160
    self.base_commit_sha = self.repository.merge_base(self.source_sha, self.target_branch)
161

162 163 164 165 166 167
    self.save
  end

  # Collect array of Git::Diff objects
  # between target and source branches
  def unmerged_diffs
168
    compare.diffs(Commit.max_diff_options)
169 170 171 172 173
  end

  def repository
    merge_request.target_project.repository
  end
174

175 176 177 178
  def source_sha
    source_commit = merge_request.source_project.commit(source_branch)
    source_commit.try(:sha)
  end
179

180 181
  def compare
    @compare ||=
182 183 184 185
      begin
        # Update ref for merge request
        merge_request.fetch_ref

186 187 188 189
        Gitlab::Git::Compare.new(
          self.repository.raw_repository,
          self.target_branch,
          self.source_sha
190 191
        )
      end
192
  end
193
end