compare_controller.rb 1.6 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
V
Vinnie Okada 已提交
28 29
    redirect_to namespace_project_compare_path(@project.namespace, @project,
                                               params[:from], params[:to])
30
  end
31 32 33

  private

S
Sean McGivern 已提交
34
  def define_ref_vars
35
    @start_ref = Addressable::URI.unescape(params[:from])
36 37 38
    @ref = @head_ref = Addressable::URI.unescape(params[:to])
  end

S
Sean McGivern 已提交
39
  def define_diff_vars
40 41
    @compare = CompareService.new(@project, @head_ref).
      execute(@project, @start_ref)
S
Sean McGivern 已提交
42 43

    if @compare
44 45 46 47
      @commits = @compare.commits
      @start_commit = @compare.start_commit
      @commit = @compare.commit
      @base_commit = @compare.base_commit
S
Sean McGivern 已提交
48

49
      @diffs = @compare.diffs(diff_options)
S
Sean McGivern 已提交
50 51

      @diff_notes_disabled = true
52
      @grouped_diff_discussions = {}
S
Sean McGivern 已提交
53 54 55
    end
  end

56
  def merge_request
57
    @merge_request ||= MergeRequestsFinder.new(current_user, project_id: @project.id).execute.opened.
58
      find_by(source_project: @project, source_branch: @head_ref, target_branch: @start_ref)
59
  end
60
end