2015-09-11 14:41:01 +05:30
|
|
|
require 'addressable/uri'
|
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
class Projects::CompareController < Projects::ApplicationController
|
2016-08-24 12:49:21 +05:30
|
|
|
include DiffForPath
|
2016-06-02 11:05:42 +05:30
|
|
|
include DiffHelper
|
2018-03-17 18:26:18 +05:30
|
|
|
include RendersCommits
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2014-09-02 18:07:02 +05:30
|
|
|
# Authorize
|
2015-09-11 14:41:01 +05:30
|
|
|
before_action :require_non_empty_project
|
|
|
|
before_action :authorize_download_code!
|
2016-08-24 12:49:21 +05:30
|
|
|
before_action :define_ref_vars, only: [:index, :show, :diff_for_path]
|
|
|
|
before_action :define_diff_vars, only: [:show, :diff_for_path]
|
2016-04-02 18:10:28 +05:30
|
|
|
before_action :merge_request, only: [:index, :show]
|
2014-09-02 18:07:02 +05:30
|
|
|
|
|
|
|
def index
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2016-09-13 17:45:13 +05:30
|
|
|
apply_diff_view_cookie!
|
2018-03-17 18:26:18 +05:30
|
|
|
# n+1: https://gitlab.com/gitlab-org/gitlab-ce/issues/37430
|
|
|
|
Gitlab::GitalyClient.allow_n_plus_1_calls do
|
|
|
|
render
|
|
|
|
end
|
2016-08-24 12:49:21 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def diff_for_path
|
|
|
|
return render_404 unless @compare
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
render_diff_for_path(@compare.diffs(diff_options))
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2017-08-17 22:00:37 +05:30
|
|
|
if params[:from].blank? || params[:to].blank?
|
2018-03-17 18:26:18 +05:30
|
|
|
flash[:alert] = "You must select a Source and a Target revision"
|
2017-08-17 22:00:37 +05:30
|
|
|
from_to_vars = {
|
|
|
|
from: params[:from].presence,
|
|
|
|
to: params[:to].presence
|
|
|
|
}
|
2017-09-10 17:25:29 +05:30
|
|
|
redirect_to project_compare_index_path(@project, from_to_vars)
|
2017-08-17 22:00:37 +05:30
|
|
|
else
|
2017-09-10 17:25:29 +05:30
|
|
|
redirect_to project_compare_path(@project,
|
2015-04-26 12:48:37 +05:30
|
|
|
params[:from], params[:to])
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2016-04-02 18:10:28 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def define_ref_vars
|
|
|
|
@start_ref = Addressable::URI.unescape(params[:from])
|
2016-04-02 18:10:28 +05:30
|
|
|
@ref = @head_ref = Addressable::URI.unescape(params[:to])
|
|
|
|
end
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
def define_diff_vars
|
2017-08-17 22:00:37 +05:30
|
|
|
@compare = CompareService.new(@project, @head_ref)
|
|
|
|
.execute(@project, @start_ref)
|
2016-08-24 12:49:21 +05:30
|
|
|
|
|
|
|
if @compare
|
2018-03-17 18:26:18 +05:30
|
|
|
@commits = prepare_commits_for_rendering(@compare.commits)
|
2016-08-24 12:49:21 +05:30
|
|
|
@diffs = @compare.diffs(diff_options)
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
environment_params = @repository.branch_exists?(@head_ref) ? { ref: @head_ref } : { commit: @compare.commit }
|
2017-08-17 22:00:37 +05:30
|
|
|
@environment = EnvironmentsFinder.new(@project, current_user, environment_params).execute.last
|
|
|
|
|
2016-08-24 12:49:21 +05:30
|
|
|
@diff_notes_disabled = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-02 18:10:28 +05:30
|
|
|
def merge_request
|
2017-09-10 17:25:29 +05:30
|
|
|
@merge_request ||= MergeRequestsFinder.new(current_user, project_id: @project.id).execute.opened
|
|
|
|
.find_by(source_project: @project, source_branch: @head_ref, target_branch: @start_ref)
|
2016-04-02 18:10:28 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|