2014-09-02 18:07:02 +05:30
|
|
|
class MergeRequestDiff < ActiveRecord::Base
|
2015-04-26 12:48:37 +05:30
|
|
|
include Sortable
|
2016-06-22 15:30:34 +05:30
|
|
|
include Importable
|
2018-03-17 18:26:18 +05:30
|
|
|
include ManualInverseAssociation
|
|
|
|
include IgnorableColumn
|
2015-04-26 12:48:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
# Don't display more than 100 commits at once
|
2016-06-02 11:05:42 +05:30
|
|
|
COMMITS_SAFE_SIZE = 100
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
ignore_column :st_commits,
|
|
|
|
:st_diffs
|
2016-11-03 12:29:30 +05:30
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
belongs_to :merge_request
|
2018-03-17 18:26:18 +05:30
|
|
|
manual_inverse_association :merge_request, :merge_request_diff
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
has_many :merge_request_diff_files, -> { order(:merge_request_diff_id, :relative_order) }
|
|
|
|
has_many :merge_request_diff_commits, -> { order(:merge_request_diff_id, :relative_order) }
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
state_machine :state, initial: :empty do
|
|
|
|
state :collected
|
2016-06-02 11:05:42 +05:30
|
|
|
state :overflow
|
|
|
|
# Deprecated states: these are no longer used but these values may still occur
|
|
|
|
# in the database.
|
2014-09-02 18:07:02 +05:30
|
|
|
state :timeout
|
|
|
|
state :overflow_commits_safe_size
|
|
|
|
state :overflow_diff_files_limit
|
|
|
|
state :overflow_diff_lines_limit
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
scope :viewable, -> { without_state(:empty) }
|
2018-03-17 18:26:18 +05:30
|
|
|
scope :by_commit_sha, ->(sha) do
|
|
|
|
joins(:merge_request_diff_commits).where(merge_request_diff_commits: { sha: sha }).reorder(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
scope :recent, -> { order(id: :desc).limit(100) }
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
# All diff information is collected from repository after object is created.
|
|
|
|
# It allows you to override variables like head_commit_sha before getting diff.
|
|
|
|
after_create :save_git_content, unless: :importing?
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def self.find_by_diff_refs(diff_refs)
|
|
|
|
find_by(start_commit_sha: diff_refs.start_sha, head_commit_sha: diff_refs.head_sha, base_commit_sha: diff_refs.base_sha)
|
|
|
|
end
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
# Collect information about commits and diff from repository
|
|
|
|
# and save it to the database as serialized data
|
|
|
|
def save_git_content
|
2018-03-17 18:26:18 +05:30
|
|
|
MergeRequest
|
|
|
|
.where('id = ? AND COALESCE(latest_merge_request_diff_id, 0) < ?', self.merge_request_id, self.id)
|
|
|
|
.update_all(latest_merge_request_diff_id: self.id)
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
ensure_commit_shas
|
2016-09-29 09:46:39 +05:30
|
|
|
save_commits
|
|
|
|
save_diffs
|
2018-03-17 18:26:18 +05:30
|
|
|
save
|
2016-09-29 09:46:39 +05:30
|
|
|
keep_around_commits
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def ensure_commit_shas
|
2016-09-29 09:46:39 +05:30
|
|
|
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
|
|
|
|
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?
|
2017-09-10 17:25:29 +05:30
|
|
|
last_commit_sha
|
2016-09-29 09:46:39 +05:30
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
def size
|
2016-09-13 17:45:13 +05:30
|
|
|
real_size.presence || raw_diffs.size
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def raw_diffs(options = {})
|
2016-06-02 11:05:42 +05:30
|
|
|
if options[:ignore_whitespace_change]
|
2017-09-10 17:25:29 +05:30
|
|
|
@diffs_no_whitespace ||= compare.diffs(options)
|
2016-06-02 11:05:42 +05:30
|
|
|
else
|
2016-09-13 17:45:13 +05:30
|
|
|
@raw_diffs ||= {}
|
2017-09-10 17:25:29 +05:30
|
|
|
@raw_diffs[options] ||= load_diffs(options)
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
2015-11-26 14:37:03 +05:30
|
|
|
end
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
def commits
|
2017-09-10 17:25:29 +05:30
|
|
|
@commits ||= load_commits
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def last_commit_sha
|
|
|
|
commit_shas.first
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2015-10-24 18:46:33 +05:30
|
|
|
def first_commit
|
|
|
|
commits.last
|
|
|
|
end
|
|
|
|
|
2016-01-29 22:53:50 +05:30
|
|
|
def base_commit
|
2016-09-29 09:46:39 +05:30
|
|
|
return unless base_commit_sha
|
2016-01-29 22:53:50 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
project.commit_by(oid: base_commit_sha)
|
2016-01-29 22:53:50 +05:30
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def start_commit
|
2016-09-29 09:46:39 +05:30
|
|
|
return unless start_commit_sha
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
project.commit_by(oid: start_commit_sha)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def head_commit
|
2016-09-29 09:46:39 +05:30
|
|
|
return unless head_commit_sha
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
project.commit_by(oid: head_commit_sha)
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def commit_shas
|
2018-03-17 18:26:18 +05:30
|
|
|
merge_request_diff_commits.map(&:sha)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def diff_refs=(new_diff_refs)
|
|
|
|
self.base_commit_sha = new_diff_refs&.base_sha
|
|
|
|
self.start_commit_sha = new_diff_refs&.start_sha
|
|
|
|
self.head_commit_sha = new_diff_refs&.head_sha
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
def diff_refs
|
|
|
|
return unless start_commit_sha || base_commit_sha
|
|
|
|
|
|
|
|
Gitlab::Diff::DiffRefs.new(
|
|
|
|
base_sha: base_commit_sha,
|
|
|
|
start_sha: start_commit_sha,
|
|
|
|
head_sha: head_commit_sha
|
|
|
|
)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
# MRs created before 8.4 don't store their true diff refs (start and base),
|
|
|
|
# but we need to get a commit SHA for the "View file @ ..." link by a file,
|
|
|
|
# so we use an approximation of the diff refs if we can't get the actual one.
|
|
|
|
#
|
|
|
|
# These will not be the actual diff refs if the target branch was merged into
|
|
|
|
# the source branch after the merge request was created, but it is good enough
|
|
|
|
# for the specific purpose of linking to a commit.
|
|
|
|
#
|
|
|
|
# It is not good enough for highlighting diffs, so we can't simply pass
|
|
|
|
# these as `diff_refs.`
|
|
|
|
def fallback_diff_refs
|
|
|
|
real_refs = diff_refs
|
|
|
|
return real_refs if real_refs
|
|
|
|
|
|
|
|
likely_base_commit_sha = (first_commit&.parent || first_commit)&.sha
|
|
|
|
|
|
|
|
Gitlab::Diff::DiffRefs.new(
|
|
|
|
base_sha: likely_base_commit_sha,
|
|
|
|
start_sha: safe_start_commit_sha,
|
|
|
|
head_sha: head_commit_sha
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def diff_refs_by_sha?
|
|
|
|
base_commit_sha? && head_commit_sha? && start_commit_sha?
|
|
|
|
end
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
def diffs(diff_options = nil)
|
|
|
|
Gitlab::Diff::FileCollection::MergeRequestDiff.new(self, diff_options: diff_options)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
def project
|
|
|
|
merge_request.target_project
|
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
def compare
|
|
|
|
@compare ||=
|
|
|
|
Gitlab::Git::Compare.new(
|
|
|
|
repository.raw_repository,
|
|
|
|
safe_start_commit_sha,
|
|
|
|
head_commit_sha
|
|
|
|
)
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
def latest?
|
2018-03-17 18:26:18 +05:30
|
|
|
self.id == merge_request.latest_merge_request_diff_id
|
2016-09-29 09:46:39 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def compare_with(sha)
|
2016-11-03 12:29:30 +05:30
|
|
|
# When compare merge request versions we want diff A..B instead of A...B
|
|
|
|
# so we handle cases when user does squash and rebase of the commits between versions.
|
|
|
|
# For this reason we set straight to true by default.
|
2017-09-10 17:25:29 +05:30
|
|
|
CompareService.new(project, head_commit_sha).execute(project, sha, straight: true)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def commits_count
|
2018-03-17 18:26:18 +05:30
|
|
|
super || merge_request_diff_commits.size
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
private
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def create_merge_request_diff_files(diffs)
|
|
|
|
rows = diffs.map.with_index do |diff, index|
|
|
|
|
diff_hash = diff.to_hash.merge(
|
|
|
|
binary: false,
|
|
|
|
merge_request_diff_id: self.id,
|
|
|
|
relative_order: index
|
|
|
|
)
|
|
|
|
|
|
|
|
# Compatibility with old diffs created with Psych.
|
|
|
|
diff_hash.tap do |hash|
|
|
|
|
diff_text = hash[:diff]
|
|
|
|
|
|
|
|
if diff_text.encoding == Encoding::BINARY && !diff_text.ascii_only?
|
|
|
|
hash[:binary] = true
|
|
|
|
hash[:diff] = [diff_text].pack('m0')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
Gitlab::Database.bulk_insert('merge_request_diff_files', rows)
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def load_diffs(options)
|
2018-03-17 18:26:18 +05:30
|
|
|
raw = merge_request_diff_files.map(&:to_hash)
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
if paths = options[:paths]
|
|
|
|
raw = raw.select do |diff|
|
|
|
|
paths.include?(diff[:old_path]) || paths.include?(diff[:new_path])
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
Gitlab::Git::DiffCollection.new(raw, options)
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def load_commits
|
2018-03-17 18:26:18 +05:30
|
|
|
commits = merge_request_diff_commits.map { |commit| Commit.from_hash(commit.to_hash, project) }
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
CommitCollection
|
|
|
|
.new(merge_request.source_project, commits, merge_request.source_branch)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
def save_diffs
|
2016-08-24 12:49:21 +05:30
|
|
|
new_attributes = {}
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
if compare.commits.size.zero?
|
2016-08-24 12:49:21 +05:30
|
|
|
new_attributes[:state] = :empty
|
2014-09-02 18:07:02 +05:30
|
|
|
else
|
2016-09-29 09:46:39 +05:30
|
|
|
diff_collection = compare.diffs(Commit.max_diff_options)
|
2016-08-24 12:49:21 +05:30
|
|
|
new_attributes[:real_size] = diff_collection.real_size
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
if diff_collection.any?
|
2016-08-24 12:49:21 +05:30
|
|
|
new_attributes[:state] = :collected
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
create_merge_request_diff_files(diff_collection)
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
# Set our state to 'overflow' to make the #empty? and #collected?
|
|
|
|
# methods (generated by StateMachine) return false.
|
|
|
|
#
|
|
|
|
# This attribution has to come at the end of the method so 'overflow'
|
|
|
|
# state does not get overridden by 'collected'.
|
|
|
|
new_attributes[:state] = :overflow if diff_collection.overflow?
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
assign_attributes(new_attributes)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def save_commits
|
|
|
|
MergeRequestDiffCommit.create_bulk(self.id, compare.commits.reverse)
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
# merge_request_diff_commits.reload is preferred way to reload associated
|
|
|
|
# objects but it returns cached result for some reason in this case
|
|
|
|
commits = merge_request_diff_commits(true)
|
|
|
|
self.commits_count = commits.size
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def repository
|
2016-08-24 12:49:21 +05:30
|
|
|
project.repository
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
def find_base_sha
|
|
|
|
return unless head_commit_sha && start_commit_sha
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-09-29 09:46:39 +05:30
|
|
|
project.merge_base_commit(head_commit_sha, start_commit_sha).try(:sha)
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def keep_around_commits
|
2018-03-17 18:26:18 +05:30
|
|
|
[repository, merge_request.source_project.repository].uniq.each do |repo|
|
2017-08-17 22:00:37 +05:30
|
|
|
repo.keep_around(start_commit_sha)
|
|
|
|
repo.keep_around(head_commit_sha)
|
|
|
|
repo.keep_around(base_commit_sha)
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|