summaryrefslogtreecommitdiff
path: root/app/models/merge_request_diff.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/merge_request_diff.rb')
-rw-r--r--app/models/merge_request_diff.rb34
1 files changed, 33 insertions, 1 deletions
diff --git a/app/models/merge_request_diff.rb b/app/models/merge_request_diff.rb
index 24809141570..d23e66b9697 100644
--- a/app/models/merge_request_diff.rb
+++ b/app/models/merge_request_diff.rb
@@ -358,6 +358,7 @@ class MergeRequestDiff < ApplicationRecord
if comparison
comparison.diffs_in_batch(batch_page, batch_size, diff_options: diff_options)
else
+ reorder_diff_files!
diffs_in_batch_collection(batch_page, batch_size, diff_options: diff_options)
end
end
@@ -371,6 +372,7 @@ class MergeRequestDiff < ApplicationRecord
if comparison
comparison.diffs(diff_options)
else
+ reorder_diff_files!
diffs_collection(diff_options)
end
end
@@ -565,7 +567,7 @@ class MergeRequestDiff < ApplicationRecord
end
def build_merge_request_diff_files(diffs)
- diffs.map.with_index do |diff, index|
+ sort_diffs(diffs).map.with_index do |diff, index|
diff_hash = diff.to_hash.merge(
binary: false,
merge_request_diff_id: self.id,
@@ -678,6 +680,7 @@ class MergeRequestDiff < ApplicationRecord
rows = build_merge_request_diff_files(diff_collection)
create_merge_request_diff_files(rows)
+ new_attributes[:sorted] = true
self.class.uncached { merge_request_diff_files.reset }
end
@@ -719,6 +722,35 @@ class MergeRequestDiff < ApplicationRecord
repo.keep_around(start_commit_sha, head_commit_sha, base_commit_sha)
end
end
+
+ def reorder_diff_files!
+ return unless sort_diffs?
+ return if sorted? || merge_request_diff_files.empty?
+
+ diff_files = sort_diffs(merge_request_diff_files)
+
+ diff_files.each_with_index do |diff_file, index|
+ diff_file.relative_order = index
+ end
+
+ transaction do
+ # The `merge_request_diff_files` table doesn't have an `id` column so
+ # we cannot use `Gitlab::Database::BulkUpdate`.
+ MergeRequestDiffFile.where(merge_request_diff_id: id).delete_all
+ MergeRequestDiffFile.bulk_insert!(diff_files)
+ update_column(:sorted, true)
+ end
+ end
+
+ def sort_diffs(diffs)
+ return diffs unless sort_diffs?
+
+ Gitlab::Diff::FileCollectionSorter.new(diffs).sort
+ end
+
+ def sort_diffs?
+ Feature.enabled?(:sort_diffs, project, default_enabled: false)
+ end
end
MergeRequestDiff.prepend_if_ee('EE::MergeRequestDiff')