compare_controller.rb 1.7 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 29 30 31 32
    if params[:from].blank? || params[:to].blank?
      flash[:alert] = "You must select from & to branches"
      redirect_to namespace_project_compare_index_path
    else
      redirect_to namespace_project_compare_path(@project.namespace, @project,
V
Vinnie Okada 已提交
33
                                               params[:from], params[:to])
34
    end
35
  end
36 37 38

  private

S
Sean McGivern 已提交
39
  def define_ref_vars
40
    @start_ref = Addressable::URI.unescape(params[:from])
41 42 43
    @ref = @head_ref = Addressable::URI.unescape(params[:to])
  end

S
Sean McGivern 已提交
44 45 46 47
  def define_diff_vars
    @compare = CompareService.new.execute(@project, @head_ref, @project, @start_ref)

    if @compare
48 49 50 51
      @commits = @compare.commits
      @start_commit = @compare.start_commit
      @commit = @compare.commit
      @base_commit = @compare.base_commit
S
Sean McGivern 已提交
52

53
      @diffs = @compare.diffs(diff_options)
S
Sean McGivern 已提交
54 55

      @diff_notes_disabled = true
56
      @grouped_diff_discussions = {}
S
Sean McGivern 已提交
57 58 59
    end
  end

60
  def merge_request
61
    @merge_request ||= MergeRequestsFinder.new(current_user, project_id: @project.id).execute.opened.
62
      find_by(source_project: @project, source_branch: @head_ref, target_branch: @start_ref)
63
  end
64
end