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

3
class Projects::CompareController < Projects::ApplicationController
4 5
  include DiffHelper

6
  # Authorize
7 8
  before_action :require_non_empty_project
  before_action :authorize_download_code!
9 10
  before_action :assign_ref_vars, only: [:index, :show]
  before_action :merge_request, only: [:index, :show]
11

12 13 14
  def index
  end

15
  def show
16
    compare = CompareService.new.
17
      execute(@project, @head_ref, @project, @base_ref, diff_options)
18

19 20
    if compare
      @commits = Commit.decorate(compare.commits, @project)
21 22
      @commit = @project.commit(@head_ref)
      @base_commit = @project.merge_base_commit(@base_ref, @head_ref)
23
      @diffs = compare.diffs(diff_options)
24
      @diff_refs = [@base_commit, @commit]
25
      @diff_notes_disabled = true
26
      @grouped_diff_notes = {}
27
    end
28
  end
29 30

  def create
Vinnie Okada's avatar
Vinnie Okada committed
31 32
    redirect_to namespace_project_compare_path(@project.namespace, @project,
                                               params[:from], params[:to])
33
  end
34 35 36 37 38 39 40 41 42 43 44 45

  private

  def assign_ref_vars
    @base_ref = Addressable::URI.unescape(params[:from])
    @ref = @head_ref = Addressable::URI.unescape(params[:to])
  end

  def merge_request
    @merge_request ||= @project.merge_requests.opened.
      find_by(source_project: @project, source_branch: @head_ref, target_branch: @base_ref)
  end
46
end