compare_controller.rb 2.1 KB
Newer Older
1 2
require 'addressable/uri'

3
class Projects::CompareController < Projects::ApplicationController
S
Sean McGivern 已提交
4
  include DiffForPath
J
Jacob Vosmaer 已提交
5 6
  include DiffHelper

7
  # Authorize
8 9
  before_action :require_non_empty_project
  before_action :authorize_download_code!
S
Sean McGivern 已提交
10 11
  before_action :define_ref_vars, only: [:index, :show, :diff_for_path]
  before_action :define_diff_vars, only: [:show, :diff_for_path]
12
  before_action :merge_request, only: [:index, :show]
13

14 15 16
  def index
  end

17
  def show
P
Phil Hughes 已提交
18
    apply_diff_view_cookie!
19
  end
20

S
Sean McGivern 已提交
21
  def diff_for_path
S
Sean McGivern 已提交
22
    return render_404 unless @compare
S
Sean McGivern 已提交
23

24
    render_diff_for_path(@compare.diffs(diff_options))
S
Sean McGivern 已提交
25 26
  end

27
  def create
28
    if params[:from].blank? || params[:to].blank?
29
      flash[:alert] = "You must select from and to branches"
30 31 32 33 34
      from_to_vars = {
        from: params[:from].presence,
        to: params[:to].presence
      }
      redirect_to namespace_project_compare_index_path(@project.namespace, @project, from_to_vars)
35 36
    else
      redirect_to namespace_project_compare_path(@project.namespace, @project,
V
Vinnie Okada 已提交
37
                                               params[:from], params[:to])
38
    end
39
  end
40 41 42

  private

S
Sean McGivern 已提交
43
  def define_ref_vars
44
    @start_ref = Addressable::URI.unescape(params[:from])
45 46 47
    @ref = @head_ref = Addressable::URI.unescape(params[:to])
  end

S
Sean McGivern 已提交
48
  def define_diff_vars
49 50
    @compare = CompareService.new(@project, @head_ref)
      .execute(@project, @start_ref)
S
Sean McGivern 已提交
51 52

    if @compare
53 54 55 56
      @commits = @compare.commits
      @start_commit = @compare.start_commit
      @commit = @compare.commit
      @base_commit = @compare.base_commit
S
Sean McGivern 已提交
57

58
      @diffs = @compare.diffs(diff_options)
S
Sean McGivern 已提交
59

D
Douwe Maan 已提交
60 61
      environment_params = @repository.branch_exists?(@head_ref) ? { ref: @head_ref } : { commit: @commit }
      @environment = EnvironmentsFinder.new(@project, current_user, environment_params).execute.last
62

S
Sean McGivern 已提交
63
      @diff_notes_disabled = true
64
      @grouped_diff_discussions = {}
S
Sean McGivern 已提交
65 66 67
    end
  end

68
  def merge_request
69 70
    @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)
71
  end
72
end