compare_controller.rb 1.9 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.decorate(@compare, @project).diff_file_collection(diff_options: 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 40 41 42 43 44 45 46 47 48
  def define_diff_vars
    @compare = CompareService.new.execute(@project, @head_ref, @project, @start_ref)

    if @compare
      @commits = Commit.decorate(@compare.commits, @project)

      @start_commit = @project.commit(@start_ref)
      @commit = @project.commit(@head_ref)
      @base_commit = @project.merge_base_commit(@start_ref, @head_ref)

49
      diff_refs = Gitlab::Diff::DiffRefs.new(
S
Sean McGivern 已提交
50 51 52 53
        base_sha: @base_commit.try(:sha),
        start_sha: @start_commit.try(:sha),
        head_sha: @commit.try(:sha)
      )
54
      @diffs = Compare.decorate(@compare, @project).diff_file_collection(diff_options: diff_options, diff_refs: diff_refs)
S
Sean McGivern 已提交
55 56

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

61 62
  def merge_request
    @merge_request ||= @project.merge_requests.opened.
63
      find_by(source_project: @project, source_branch: @head_ref, target_branch: @start_ref)
64
  end
65
end