2017-06-13 18:12:31 -04:00
|
|
|
class Projects::MergeRequests::DiffsController < Projects::MergeRequests::ApplicationController
|
|
|
|
include DiffForPath
|
|
|
|
include DiffHelper
|
|
|
|
include RendersNotes
|
|
|
|
|
|
|
|
before_action :apply_diff_view_cookie!
|
2017-11-14 11:48:40 -05:00
|
|
|
before_action :commit
|
2017-06-13 18:12:31 -04:00
|
|
|
before_action :define_diff_vars
|
|
|
|
before_action :define_diff_comment_vars
|
|
|
|
|
|
|
|
def show
|
|
|
|
@environment = @merge_request.environments_for(current_user).last
|
|
|
|
|
2017-11-03 09:16:43 -04:00
|
|
|
render json: { html: view_to_html_string("projects/merge_requests/diffs/_diffs") }
|
2017-06-13 18:12:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def diff_for_path
|
|
|
|
render_diff_for_path(@diffs)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def define_diff_vars
|
2017-11-22 09:48:09 -05:00
|
|
|
@merge_request_diffs = @merge_request.merge_request_diffs.viewable.order_id_desc
|
2017-11-08 15:39:29 -05:00
|
|
|
@compare = commit || find_merge_request_diff_compare
|
2017-06-29 17:19:09 -04:00
|
|
|
return render_404 unless @compare
|
|
|
|
|
|
|
|
@diffs = @compare.diffs(diff_options)
|
|
|
|
end
|
|
|
|
|
2017-11-14 11:48:40 -05:00
|
|
|
def commit
|
|
|
|
return nil unless commit_id = params[:commit_id].presence
|
2017-11-22 09:48:09 -05:00
|
|
|
return nil unless @merge_request.all_commits.exists?(sha: commit_id)
|
2017-11-14 11:48:40 -05:00
|
|
|
|
|
|
|
@commit ||= @project.commit(commit_id)
|
|
|
|
end
|
|
|
|
|
2017-06-29 17:19:09 -04:00
|
|
|
def find_merge_request_diff_compare
|
2017-06-13 18:12:31 -04:00
|
|
|
@merge_request_diff =
|
2017-06-29 17:19:09 -04:00
|
|
|
if diff_id = params[:diff_id].presence
|
|
|
|
@merge_request.merge_request_diffs.viewable.find_by(id: diff_id)
|
2017-06-13 18:12:31 -04:00
|
|
|
else
|
|
|
|
@merge_request.merge_request_diff
|
|
|
|
end
|
|
|
|
|
2017-06-29 17:19:09 -04:00
|
|
|
return unless @merge_request_diff
|
|
|
|
|
2017-06-13 18:12:31 -04:00
|
|
|
@comparable_diffs = @merge_request_diffs.select { |diff| diff.id < @merge_request_diff.id }
|
|
|
|
|
2017-06-29 17:19:09 -04:00
|
|
|
if @start_sha = params[:start_sha].presence
|
2017-06-13 18:12:31 -04:00
|
|
|
@start_version = @comparable_diffs.find { |diff| diff.head_commit_sha == @start_sha }
|
|
|
|
|
|
|
|
unless @start_version
|
|
|
|
@start_sha = @merge_request_diff.head_commit_sha
|
|
|
|
@start_version = @merge_request_diff
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-29 17:19:09 -04:00
|
|
|
if @start_sha
|
|
|
|
@merge_request_diff.compare_with(@start_sha)
|
|
|
|
else
|
|
|
|
@merge_request_diff
|
|
|
|
end
|
2017-06-13 18:12:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def define_diff_comment_vars
|
|
|
|
@new_diff_note_attrs = {
|
|
|
|
noteable_type: 'MergeRequest',
|
2017-06-29 17:19:09 -04:00
|
|
|
noteable_id: @merge_request.id,
|
|
|
|
commit_id: @commit&.id
|
2017-06-13 18:12:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@diff_notes_disabled = false
|
|
|
|
|
|
|
|
@use_legacy_diff_notes = !@merge_request.has_complete_diff_refs?
|
|
|
|
|
|
|
|
@grouped_diff_discussions = @merge_request.grouped_diff_discussions(@compare.diff_refs)
|
2017-07-29 11:04:42 -04:00
|
|
|
@notes = prepare_notes_for_rendering(@grouped_diff_discussions.values.flatten.flat_map(&:notes), @merge_request)
|
2017-06-13 18:12:31 -04:00
|
|
|
end
|
|
|
|
end
|