2014-01-22 08:19:02 -05:00
|
|
|
class MergeRequestDiff < ActiveRecord::Base
|
2015-02-05 17:20:55 -05:00
|
|
|
include Sortable
|
2016-06-01 12:03:51 -04:00
|
|
|
include Importable
|
Use latest_merge_request_diff association
Compared to the merge_request_diff association:
1. It's simpler to query. The query uses a foreign key to the
merge_request_diffs table, so no ordering is necessary.
2. It's faster for preloading. The merge_request_diff association has to load
every diff for the MRs in the set, then discard all but the most recent for
each. This association means that Rails can just query for N diffs from N
MRs.
3. It's more complicated to update. This is a bidirectional foreign key, so we
need to update two tables when adding a diff record. This also means we need
to handle this as a special case when importing a GitLab project.
There is some juggling with this association in the merge request model:
* `MergeRequest#latest_merge_request_diff` is _always_ the latest diff.
* `MergeRequest#merge_request_diff` reuses
`MergeRequest#latest_merge_request_diff` unless:
* Arguments are passed. These are typically to force-reload the association.
* It doesn't exist. That means we might be trying to implicitly create a
diff. This only seems to happen in specs.
* The association is already loaded. This is important for the reasons
explained in the comment, which I'll reiterate here: if we a) load a
non-latest diff, then b) get its `merge_request`, then c) get that MR's
`merge_request_diff`, we should get the diff we loaded in c), even though
that's not the latest diff.
Basically, `MergeRequest#merge_request_diff` is the latest diff in most cases,
but not quite all.
2017-11-15 12:22:18 -05:00
|
|
|
include ManualInverseAssociation
|
2017-11-21 11:58:08 -05:00
|
|
|
include IgnorableColumn
|
2015-02-05 17:20:55 -05:00
|
|
|
|
2017-11-21 11:58:08 -05:00
|
|
|
# Don't display more than 100 commits at once
|
2016-03-04 09:17:12 -05:00
|
|
|
COMMITS_SAFE_SIZE = 100
|
2014-01-22 08:19:02 -05:00
|
|
|
|
2017-11-21 11:58:08 -05:00
|
|
|
ignore_column :st_commits,
|
|
|
|
:st_diffs
|
2016-10-08 01:48:23 -04:00
|
|
|
|
2014-01-22 08:19:02 -05:00
|
|
|
belongs_to :merge_request
|
Use latest_merge_request_diff association
Compared to the merge_request_diff association:
1. It's simpler to query. The query uses a foreign key to the
merge_request_diffs table, so no ordering is necessary.
2. It's faster for preloading. The merge_request_diff association has to load
every diff for the MRs in the set, then discard all but the most recent for
each. This association means that Rails can just query for N diffs from N
MRs.
3. It's more complicated to update. This is a bidirectional foreign key, so we
need to update two tables when adding a diff record. This also means we need
to handle this as a special case when importing a GitLab project.
There is some juggling with this association in the merge request model:
* `MergeRequest#latest_merge_request_diff` is _always_ the latest diff.
* `MergeRequest#merge_request_diff` reuses
`MergeRequest#latest_merge_request_diff` unless:
* Arguments are passed. These are typically to force-reload the association.
* It doesn't exist. That means we might be trying to implicitly create a
diff. This only seems to happen in specs.
* The association is already loaded. This is important for the reasons
explained in the comment, which I'll reiterate here: if we a) load a
non-latest diff, then b) get its `merge_request`, then c) get that MR's
`merge_request_diff`, we should get the diff we loaded in c), even though
that's not the latest diff.
Basically, `MergeRequest#merge_request_diff` is the latest diff in most cases,
but not quite all.
2017-11-15 12:22:18 -05:00
|
|
|
manual_inverse_association :merge_request, :merge_request_diff
|
|
|
|
|
2017-06-09 07:48:25 -04:00
|
|
|
has_many :merge_request_diff_files, -> { order(:merge_request_diff_id, :relative_order) }
|
2017-06-16 10:00:58 -04:00
|
|
|
has_many :merge_request_diff_commits, -> { order(:merge_request_diff_id, :relative_order) }
|
2014-01-22 08:19:02 -05:00
|
|
|
|
|
|
|
state_machine :state, initial: :empty do
|
|
|
|
state :collected
|
2016-03-03 12:38:44 -05:00
|
|
|
state :overflow
|
|
|
|
# Deprecated states: these are no longer used but these values may still occur
|
|
|
|
# in the database.
|
2014-01-22 08:19:02 -05:00
|
|
|
state :timeout
|
|
|
|
state :overflow_commits_safe_size
|
|
|
|
state :overflow_diff_files_limit
|
|
|
|
state :overflow_diff_lines_limit
|
|
|
|
end
|
|
|
|
|
2016-11-18 08:33:42 -05:00
|
|
|
scope :viewable, -> { without_state(:empty) }
|
2014-01-22 08:19:02 -05:00
|
|
|
|
2017-11-21 11:58:08 -05:00
|
|
|
scope :recent, -> { order(id: :desc).limit(100) }
|
|
|
|
|
2016-08-02 08:38:03 -04:00
|
|
|
# All diff information is collected from repository after object is created.
|
|
|
|
# It allows you to override variables like head_commit_sha before getting diff.
|
2016-07-28 15:36:18 -04:00
|
|
|
after_create :save_git_content, unless: :importing?
|
2016-07-27 07:41:19 -04:00
|
|
|
|
2017-04-06 18:13:28 -04:00
|
|
|
def self.find_by_diff_refs(diff_refs)
|
2017-04-06 18:24:51 -04:00
|
|
|
find_by(start_commit_sha: diff_refs.start_sha, head_commit_sha: diff_refs.head_sha, base_commit_sha: diff_refs.base_sha)
|
2017-04-06 18:13:28 -04:00
|
|
|
end
|
|
|
|
|
2016-07-28 06:46:27 -04:00
|
|
|
# Collect information about commits and diff from repository
|
|
|
|
# and save it to the database as serialized data
|
|
|
|
def save_git_content
|
2017-10-26 06:33:54 -04:00
|
|
|
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-06-16 10:00:58 -04:00
|
|
|
ensure_commit_shas
|
2016-07-28 06:46:27 -04:00
|
|
|
save_commits
|
|
|
|
save_diffs
|
2016-08-02 08:38:03 -04:00
|
|
|
keep_around_commits
|
|
|
|
end
|
|
|
|
|
2017-06-16 10:00:58 -04:00
|
|
|
def ensure_commit_shas
|
2016-08-02 08:38:03 -04:00
|
|
|
self.start_commit_sha ||= merge_request.target_branch_sha
|
2016-08-15 09:25:29 -04:00
|
|
|
self.head_commit_sha ||= merge_request.source_branch_sha
|
2016-08-02 08:38:03 -04:00
|
|
|
self.base_commit_sha ||= find_base_sha
|
2016-08-15 09:25:29 -04:00
|
|
|
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?
|
2017-06-16 10:00:58 -04:00
|
|
|
last_commit_sha
|
2016-08-15 09:25:29 -04:00
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
2014-01-22 08:19:02 -05:00
|
|
|
end
|
|
|
|
|
2016-08-15 08:20:36 -04:00
|
|
|
# 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
|
|
|
|
|
2016-03-03 12:38:44 -05:00
|
|
|
def size
|
2016-08-03 17:32:12 -04:00
|
|
|
real_size.presence || raw_diffs.size
|
2014-01-22 08:19:02 -05:00
|
|
|
end
|
|
|
|
|
2016-08-05 22:03:01 -04:00
|
|
|
def raw_diffs(options = {})
|
2016-03-03 12:38:44 -05:00
|
|
|
if options[:ignore_whitespace_change]
|
2017-07-03 10:48:59 -04:00
|
|
|
@diffs_no_whitespace ||= compare.diffs(options)
|
2016-03-03 12:38:44 -05:00
|
|
|
else
|
2016-08-03 17:32:12 -04:00
|
|
|
@raw_diffs ||= {}
|
2017-06-09 07:48:25 -04:00
|
|
|
@raw_diffs[options] ||= load_diffs(options)
|
2016-03-03 12:38:44 -05:00
|
|
|
end
|
2015-10-21 20:55:35 -04:00
|
|
|
end
|
|
|
|
|
2014-01-22 08:19:02 -05:00
|
|
|
def commits
|
2017-06-16 10:00:58 -04:00
|
|
|
@commits ||= load_commits
|
2014-01-22 08:19:02 -05:00
|
|
|
end
|
|
|
|
|
2017-06-16 10:00:58 -04:00
|
|
|
def last_commit_sha
|
|
|
|
commit_shas.first
|
2014-01-22 08:19:02 -05:00
|
|
|
end
|
|
|
|
|
2015-10-20 08:23:56 -04:00
|
|
|
def first_commit
|
|
|
|
commits.last
|
|
|
|
end
|
|
|
|
|
2016-01-20 12:44:27 -05:00
|
|
|
def base_commit
|
2016-07-27 07:41:19 -04:00
|
|
|
return unless base_commit_sha
|
2016-01-20 12:44:27 -05:00
|
|
|
|
2016-07-27 07:41:19 -04:00
|
|
|
project.commit(base_commit_sha)
|
2016-01-20 12:44:27 -05:00
|
|
|
end
|
|
|
|
|
2016-06-20 12:48:04 -04:00
|
|
|
def start_commit
|
2016-07-27 07:41:19 -04:00
|
|
|
return unless start_commit_sha
|
2016-06-20 12:48:04 -04:00
|
|
|
|
2016-07-27 07:41:19 -04:00
|
|
|
project.commit(start_commit_sha)
|
2016-06-20 12:48:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def head_commit
|
2016-08-15 09:25:29 -04:00
|
|
|
return unless head_commit_sha
|
2016-06-20 12:48:04 -04:00
|
|
|
|
2016-07-27 07:41:19 -04:00
|
|
|
project.commit(head_commit_sha)
|
2014-01-22 08:19:02 -05:00
|
|
|
end
|
|
|
|
|
2017-06-16 10:00:58 -04:00
|
|
|
def commit_shas
|
2017-11-21 11:58:08 -05:00
|
|
|
merge_request_diff_commits.map(&:sha)
|
2016-09-19 11:51:27 -04:00
|
|
|
end
|
|
|
|
|
2017-04-10 16:17:47 -04:00
|
|
|
def diff_refs=(new_diff_refs)
|
2017-04-11 11:51:48 -04:00
|
|
|
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
|
2017-04-10 16:17:47 -04:00
|
|
|
end
|
|
|
|
|
2016-07-26 11:24:25 -04:00
|
|
|
def diff_refs
|
2016-08-15 11:11:18 -04:00
|
|
|
return unless start_commit_sha || base_commit_sha
|
2016-07-26 11:24:25 -04:00
|
|
|
|
|
|
|
Gitlab::Diff::DiffRefs.new(
|
|
|
|
base_sha: base_commit_sha,
|
|
|
|
start_sha: start_commit_sha,
|
|
|
|
head_sha: head_commit_sha
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2017-05-15 14:19:49 -04:00
|
|
|
# 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,
|
2017-05-16 19:58:23 -04:00
|
|
|
# so we use an approximation of the diff refs if we can't get the actual one.
|
|
|
|
#
|
2017-05-15 14:19:49 -04:00
|
|
|
# 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.
|
2017-05-16 19:58:23 -04:00
|
|
|
#
|
2017-05-15 14:19:49 -04:00
|
|
|
# It is not good enough for highlighting diffs, so we can't simply pass
|
|
|
|
# these as `diff_refs.`
|
|
|
|
def fallback_diff_refs
|
2017-05-15 14:44:15 -04:00
|
|
|
real_refs = diff_refs
|
|
|
|
return real_refs if real_refs
|
|
|
|
|
2017-05-15 14:19:49 -04:00
|
|
|
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-08-01 10:55:51 -04:00
|
|
|
def diff_refs_by_sha?
|
|
|
|
base_commit_sha? && head_commit_sha? && start_commit_sha?
|
|
|
|
end
|
|
|
|
|
2016-08-15 10:57:19 -04:00
|
|
|
def diffs(diff_options = nil)
|
|
|
|
Gitlab::Diff::FileCollection::MergeRequestDiff.new(self, diff_options: diff_options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def project
|
|
|
|
merge_request.target_project
|
|
|
|
end
|
2014-01-22 08:19:02 -05:00
|
|
|
|
2016-07-27 07:41:19 -04:00
|
|
|
def compare
|
|
|
|
@compare ||=
|
2016-08-25 04:58:17 -04:00
|
|
|
Gitlab::Git::Compare.new(
|
|
|
|
repository.raw_repository,
|
|
|
|
safe_start_commit_sha,
|
|
|
|
head_commit_sha
|
|
|
|
)
|
2014-01-22 08:19:02 -05:00
|
|
|
end
|
|
|
|
|
2016-08-25 03:59:30 -04:00
|
|
|
def latest?
|
Use latest_merge_request_diff association
Compared to the merge_request_diff association:
1. It's simpler to query. The query uses a foreign key to the
merge_request_diffs table, so no ordering is necessary.
2. It's faster for preloading. The merge_request_diff association has to load
every diff for the MRs in the set, then discard all but the most recent for
each. This association means that Rails can just query for N diffs from N
MRs.
3. It's more complicated to update. This is a bidirectional foreign key, so we
need to update two tables when adding a diff record. This also means we need
to handle this as a special case when importing a GitLab project.
There is some juggling with this association in the merge request model:
* `MergeRequest#latest_merge_request_diff` is _always_ the latest diff.
* `MergeRequest#merge_request_diff` reuses
`MergeRequest#latest_merge_request_diff` unless:
* Arguments are passed. These are typically to force-reload the association.
* It doesn't exist. That means we might be trying to implicitly create a
diff. This only seems to happen in specs.
* The association is already loaded. This is important for the reasons
explained in the comment, which I'll reiterate here: if we a) load a
non-latest diff, then b) get its `merge_request`, then c) get that MR's
`merge_request_diff`, we should get the diff we loaded in c), even though
that's not the latest diff.
Basically, `MergeRequest#merge_request_diff` is the latest diff in most cases,
but not quite all.
2017-11-15 12:22:18 -05:00
|
|
|
self.id == merge_request.latest_merge_request_diff_id
|
2016-08-25 03:59:30 -04:00
|
|
|
end
|
|
|
|
|
2017-05-21 16:38:33 -04:00
|
|
|
def compare_with(sha)
|
2016-09-29 07:04:50 -04:00
|
|
|
# When compare merge request versions we want diff A..B instead of A...B
|
2016-09-29 10:46:47 -04:00
|
|
|
# so we handle cases when user does squash and rebase of the commits between versions.
|
2016-09-29 07:04:50 -04:00
|
|
|
# For this reason we set straight to true by default.
|
2017-05-21 16:38:33 -04:00
|
|
|
CompareService.new(project, head_commit_sha).execute(project, sha, straight: true)
|
2016-09-07 03:16:21 -04:00
|
|
|
end
|
|
|
|
|
2016-12-01 06:17:30 -05:00
|
|
|
def commits_count
|
2017-11-21 11:58:08 -05:00
|
|
|
merge_request_diff_commits.size
|
2017-04-04 12:34:19 -04:00
|
|
|
end
|
|
|
|
|
2016-08-15 10:57:19 -04:00
|
|
|
private
|
|
|
|
|
2017-06-09 07:48:25 -04:00
|
|
|
def create_merge_request_diff_files(diffs)
|
|
|
|
rows = diffs.map.with_index do |diff, index|
|
2017-07-25 12:57:02 -04:00
|
|
|
diff_hash = diff.to_hash.merge(
|
|
|
|
binary: false,
|
2017-06-09 07:48:25 -04:00
|
|
|
merge_request_diff_id: self.id,
|
|
|
|
relative_order: index
|
|
|
|
)
|
2017-07-25 12:57:02 -04:00
|
|
|
|
|
|
|
# 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
|
2016-07-06 19:29:14 -04:00
|
|
|
end
|
2017-06-09 07:48:25 -04:00
|
|
|
|
|
|
|
Gitlab::Database.bulk_insert('merge_request_diff_files', rows)
|
2016-07-06 19:29:14 -04:00
|
|
|
end
|
|
|
|
|
2017-06-09 07:48:25 -04:00
|
|
|
def load_diffs(options)
|
2017-11-21 11:58:08 -05:00
|
|
|
raw = merge_request_diff_files.map(&:to_hash)
|
2017-06-09 07:48:25 -04:00
|
|
|
|
|
|
|
if paths = options[:paths]
|
|
|
|
raw = raw.select do |diff|
|
|
|
|
paths.include?(diff[:old_path]) || paths.include?(diff[:new_path])
|
|
|
|
end
|
2016-07-06 19:29:14 -04:00
|
|
|
end
|
2017-06-09 07:48:25 -04:00
|
|
|
|
|
|
|
Gitlab::Git::DiffCollection.new(raw, options)
|
|
|
|
end
|
|
|
|
|
2017-06-16 10:00:58 -04:00
|
|
|
def load_commits
|
2017-11-21 11:58:08 -05:00
|
|
|
commits = merge_request_diff_commits.map { |commit| Commit.from_hash(commit.to_hash, project) }
|
2017-06-16 10:00:58 -04:00
|
|
|
|
2017-11-10 14:57:11 -05:00
|
|
|
CommitCollection
|
|
|
|
.new(merge_request.source_project, commits, merge_request.source_branch)
|
2017-06-16 10:00:58 -04:00
|
|
|
end
|
|
|
|
|
2016-07-28 06:46:27 -04:00
|
|
|
def save_diffs
|
2016-06-24 12:30:33 -04:00
|
|
|
new_attributes = {}
|
2014-01-22 08:19:02 -05:00
|
|
|
|
2017-06-16 10:00:58 -04:00
|
|
|
if compare.commits.size.zero?
|
2016-06-24 12:30:33 -04:00
|
|
|
new_attributes[:state] = :empty
|
2014-01-22 08:19:02 -05:00
|
|
|
else
|
2016-07-27 07:41:19 -04:00
|
|
|
diff_collection = compare.diffs(Commit.max_diff_options)
|
2017-04-20 10:47:32 -04:00
|
|
|
new_attributes[:real_size] = diff_collection.real_size
|
2014-01-22 08:19:02 -05:00
|
|
|
|
2016-03-03 12:38:44 -05:00
|
|
|
if diff_collection.any?
|
2016-06-24 12:30:33 -04:00
|
|
|
new_attributes[:state] = :collected
|
2016-12-30 14:16:25 -05:00
|
|
|
|
2017-06-09 07:48:25 -04:00
|
|
|
create_merge_request_diff_files(diff_collection)
|
|
|
|
end
|
2016-12-30 14:16:25 -05:00
|
|
|
|
|
|
|
# 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-01-22 08:30:14 -05:00
|
|
|
end
|
2014-01-22 08:19:02 -05:00
|
|
|
|
2017-06-16 10:00:58 -04:00
|
|
|
update(new_attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
def save_commits
|
|
|
|
MergeRequestDiffCommit.create_bulk(self.id, compare.commits.reverse)
|
|
|
|
|
|
|
|
merge_request_diff_commits.reload
|
2014-01-22 08:19:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def repository
|
2016-07-06 19:29:14 -04:00
|
|
|
project.repository
|
2014-01-22 08:19:02 -05:00
|
|
|
end
|
2014-05-07 09:14:24 -04:00
|
|
|
|
2016-07-28 04:36:30 -04:00
|
|
|
def find_base_sha
|
|
|
|
return unless head_commit_sha && start_commit_sha
|
2016-04-26 18:57:37 -04:00
|
|
|
|
2016-07-28 04:36:30 -04:00
|
|
|
project.merge_base_commit(head_commit_sha, start_commit_sha).try(:sha)
|
2016-04-26 18:57:37 -04:00
|
|
|
end
|
|
|
|
|
2016-06-20 12:43:55 -04:00
|
|
|
def keep_around_commits
|
2016-10-24 12:08:30 -04:00
|
|
|
[repository, merge_request.source_project.repository].each do |repo|
|
|
|
|
repo.keep_around(start_commit_sha)
|
|
|
|
repo.keep_around(head_commit_sha)
|
|
|
|
repo.keep_around(base_commit_sha)
|
|
|
|
end
|
2016-07-03 19:58:58 -04:00
|
|
|
end
|
2014-01-22 08:19:02 -05:00
|
|
|
end
|