commit_controller.rb 3.6 KB
Newer Older
Robert Speicher's avatar
Robert Speicher committed
1 2 3
# Controller for a specific Commit
#
# Not to be confused with CommitsController, plural.
4
class Projects::CommitController < Projects::ApplicationController
5
  include CreatesCommit
Sean McGivern's avatar
Sean McGivern committed
6
  include DiffForPath
7
  include DiffHelper
8

Robert Speicher's avatar
Robert Speicher committed
9
  # Authorize
10
  before_action :require_non_empty_project
Filipa Lacerda's avatar
Filipa Lacerda committed
11
  before_action :authorize_download_code!
Kamil Trzcinski's avatar
Kamil Trzcinski committed
12
  before_action :authorize_read_pipeline!, only: [:pipelines]
13
  before_action :commit
14 15
  before_action :define_commit_vars, only: [:show, :diff_for_path, :pipelines]
  before_action :define_status_vars, only: [:show, :pipelines]
Sean McGivern's avatar
Sean McGivern committed
16
  before_action :define_note_vars, only: [:show, :diff_for_path]
17
  before_action :authorize_edit_tree!, only: [:revert, :cherry_pick]
Robert Speicher's avatar
Robert Speicher committed
18 19

  def show
20 21
    apply_diff_view_cookie!

22
    respond_to do |format|
23
      format.html
24
      format.diff  { render text: @commit.to_diff }
25
      format.patch { render text: @commit.to_patch }
Robert Speicher's avatar
Robert Speicher committed
26 27
    end
  end
28

29
  def diff_for_path
30
    render_diff_for_path(@commit.diffs(diff_options))
31 32
  end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
33 34 35
  def pipelines
  end

36 37 38 39 40 41
  def branches
    @branches = @project.repository.branch_names_contains(commit.id)
    @tags = @project.repository.tag_names_contains(commit.id)
    render layout: false
  end

42
  def revert
43
    assign_change_commit_vars(@commit.revert_branch_name)
44

45
    return render_404 if @target_branch.blank?
46

47
    create_commit(Commits::RevertService, success_notice: "The #{@commit.change_type_title(current_user)} has been successfully reverted.",
48
                                          success_path: successful_change_path, failure_path: failed_change_path)
49
  end
50

51 52
  def cherry_pick
    assign_change_commit_vars(@commit.cherry_pick_branch_name)
53

54
    return render_404 if @target_branch.blank?
55

56
    create_commit(Commits::CherryPickService, success_notice: "The #{@commit.change_type_title(current_user)} has been successfully cherry-picked.",
57
                                              success_path: successful_change_path, failure_path: failed_change_path)
58 59
  end

60 61 62
  private

  def successful_change_path
63
    referenced_merge_request_url || namespace_project_commits_url(@project.namespace, @project, @target_branch)
64 65
  end

66
  def failed_change_path
67
    referenced_merge_request_url || namespace_project_commit_url(@project.namespace, @project, params[:id])
68 69 70
  end

  def referenced_merge_request_url
71 72 73
    if merge_request = @commit.merged_merge_request(current_user)
      namespace_project_merge_request_url(@project.namespace, @project, merge_request)
    end
74 75
  end

76
  def commit
77
    @noteable = @commit ||= @project.commit(params[:id])
78
  end
79

Sean McGivern's avatar
Sean McGivern committed
80
  def define_commit_vars
81 82
    return git_not_found! unless commit

83 84
    opts = diff_options
    opts[:ignore_whitespace_change] = true if params[:format] == 'diff'
85

86
    @diffs = commit.diffs(opts)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
87
    @notes_count = commit.notes.count
Sean McGivern's avatar
Sean McGivern committed
88 89 90
  end

  def define_note_vars
91
    @grouped_diff_discussions = commit.notes.grouped_diff_discussions
Sean McGivern's avatar
Sean McGivern committed
92 93 94
    @notes = commit.notes.non_diff_notes.fresh

    Banzai::NoteRenderer.render(
95
      @grouped_diff_discussions.values.flat_map(&:notes) + @notes,
Sean McGivern's avatar
Sean McGivern committed
96 97 98 99 100 101 102 103 104 105 106 107
      @project,
      current_user,
    )

    @note = @project.build_commit_note(commit)

    @noteable = @commit
    @comments_target = {
      noteable_type: 'Commit',
      commit_id: @commit.id
    }
  end
Douwe Maan's avatar
Douwe Maan committed
108

Sean McGivern's avatar
Sean McGivern committed
109
  def define_status_vars
Kamil Trzcinski's avatar
Kamil Trzcinski committed
110
    @ci_pipelines = project.pipelines.where(sha: commit.sha)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
111
  end
112

113
  def assign_change_commit_vars(mr_source_branch)
114
    @commit = project.commit(params[:id])
115
    @target_branch = params[:target_branch]
116
    @mr_source_branch = mr_source_branch
117
    @mr_target_branch = @target_branch
118
    @commit_params = {
119 120
      commit: @commit,
      create_merge_request: params[:create_merge_request].present? || different_project?
121 122
    }
  end
Robert Speicher's avatar
Robert Speicher committed
123
end