diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-01-27 21:09:08 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-01-27 21:09:08 +0300 |
commit | 874d5e8019150bfb2428cac3089c1dac1d6f9ed9 (patch) | |
tree | 3a1d9b1828222fe16a5ca9071da34598c7280d50 /app/models/merge_request_diff.rb | |
parent | 0cbb4a75699e1ab6a0cb704b551e672e09192377 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/models/merge_request_diff.rb')
-rw-r--r-- | app/models/merge_request_diff.rb | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/app/models/merge_request_diff.rb b/app/models/merge_request_diff.rb index d23e66b9697..5500ee7f74a 100644 --- a/app/models/merge_request_diff.rb +++ b/app/models/merge_request_diff.rb @@ -32,6 +32,7 @@ class MergeRequestDiff < ApplicationRecord has_many :merge_request_diff_commits, -> { order(:merge_request_diff_id, :relative_order) } validates :base_commit_sha, :head_commit_sha, :start_commit_sha, sha: true + validates :merge_request_id, uniqueness: { scope: :diff_type }, if: :merge_head? state_machine :state, initial: :empty do event :clean do @@ -50,6 +51,11 @@ class MergeRequestDiff < ApplicationRecord state :overflow_diff_lines_limit end + enum diff_type: { + regular: 1, + merge_head: 2 + } + scope :with_files, -> { without_states(:without_files, :empty) } scope :viewable, -> { without_state(:empty) } scope :by_commit_sha, ->(sha) do @@ -72,6 +78,7 @@ class MergeRequestDiff < ApplicationRecord join_condition = merge_requests[:id].eq(mr_diffs[:merge_request_id]) .and(mr_diffs[:id].not_eq(merge_requests[:latest_merge_request_diff_id])) + .and(mr_diffs[:diff_type].eq(diff_types[:regular])) arel_join = mr_diffs.join(merge_requests).on(join_condition) joins(arel_join.join_sources) @@ -196,6 +203,10 @@ class MergeRequestDiff < ApplicationRecord end def set_as_latest_diff + # Don't set merge_head diff as latest so it won't get considered as the + # MergeRequest#merge_request_diff. + return if merge_head? + 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) @@ -203,8 +214,16 @@ class MergeRequestDiff < ApplicationRecord def ensure_commit_shas 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 + + if merge_head? && merge_request.merge_ref_head.present? + diff_refs = merge_request.merge_ref_head.diff_refs + + self.head_commit_sha ||= diff_refs.head_sha + self.base_commit_sha ||= diff_refs.base_sha + else + self.head_commit_sha ||= merge_request.source_branch_sha + self.base_commit_sha ||= find_base_sha + end end # Override head_commit_sha to keep compatibility with merge request diff |