commit_controller.rb 4.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
Douwe Maan's avatar
Douwe Maan committed
5
  include RendersNotes
6
  include CreatesCommit
Sean McGivern's avatar
Sean McGivern committed
7
  include DiffForPath
8
  include DiffHelper
9

Robert Speicher's avatar
Robert Speicher committed
10
  # Authorize
11
  before_action :require_non_empty_project
Filipa Lacerda's avatar
Filipa Lacerda committed
12
  before_action :authorize_download_code!
Kamil Trzcinski's avatar
Kamil Trzcinski committed
13
  before_action :authorize_read_pipeline!, only: [:pipelines]
14
  before_action :commit
Filipa Lacerda's avatar
Filipa Lacerda committed
15
  before_action :define_commit_vars, only: [:show, :diff_for_path, :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 20
  BRANCH_SEARCH_LIMIT = 1000

Robert Speicher's avatar
Robert Speicher committed
21
  def show
22 23
    apply_diff_view_cookie!

24
    respond_to do |format|
25 26 27 28 29 30
      format.html do
        # n+1: https://gitlab.com/gitlab-org/gitlab-ce/issues/37599
        Gitlab::GitalyClient.allow_n_plus_1_calls do
          render
        end
      end
31
      format.diff  { render text: @commit.to_diff }
32
      format.patch { render text: @commit.to_patch }
Robert Speicher's avatar
Robert Speicher committed
33 34
    end
  end
35

36
  def diff_for_path
37
    render_diff_for_path(@commit.diffs(diff_options))
38 39
  end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
40
  def pipelines
41 42 43 44 45
    @pipelines = @commit.pipelines.order(id: :desc)

    respond_to do |format|
      format.html
      format.json do
46 47
        Gitlab::PollingInterval.set_header(response, interval: 10_000)

48 49 50 51 52 53 54 55
        render json: {
          pipelines: PipelineSerializer
            .new(project: @project, current_user: @current_user)
            .represent(@pipelines),
          count: {
            all: @pipelines.count
          }
        }
56 57
      end
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
58 59
  end

60
  def branches
61 62 63
    # branch_names_contains/tag_names_contains can take a long time when there are thousands of
    # branches/tags - each `git branch --contains xxx` request can consume a cpu core.
    # so only do the query when there are a manageable number of branches/tags
64 65 66 67 68
    @branches_limit_exceeded = @project.repository.branch_count > BRANCH_SEARCH_LIMIT
    @branches = @branches_limit_exceeded ? [] : @project.repository.branch_names_contains(commit.id)

    @tags_limit_exceeded = @project.repository.tag_count > BRANCH_SEARCH_LIMIT
    @tags = @tags_limit_exceeded ? [] : @project.repository.tag_names_contains(commit.id)
69 70 71
    render layout: false
  end

72
  def revert
73
    assign_change_commit_vars
74

75 76
    return render_404 if @start_branch.blank?

Douwe Maan's avatar
Douwe Maan committed
77
    @branch_name = create_new_branch? ? @commit.revert_branch_name : @start_branch
78

79
    create_commit(Commits::RevertService, success_notice: "The #{@commit.change_type_title(current_user)} has been successfully reverted.",
80
                                          success_path: -> { successful_change_path }, failure_path: failed_change_path)
81
  end
82

83
  def cherry_pick
84
    assign_change_commit_vars
85

86 87
    return render_404 if @start_branch.blank?

Douwe Maan's avatar
Douwe Maan committed
88
    @branch_name = create_new_branch? ? @commit.cherry_pick_branch_name : @start_branch
89

90
    create_commit(Commits::CherryPickService, success_notice: "The #{@commit.change_type_title(current_user)} has been successfully cherry-picked.",
91
                                              success_path: -> { successful_change_path }, failure_path: failed_change_path)
92 93
  end

94 95
  private

96 97 98 99
  def create_new_branch?
    params[:create_merge_request].present? || !can?(current_user, :push_code, @project)
  end

100
  def successful_change_path
101
    referenced_merge_request_url || project_commits_url(@project, @branch_name)
102 103
  end

104
  def failed_change_path
105
    referenced_merge_request_url || project_commit_url(@project, params[:id])
106 107 108
  end

  def referenced_merge_request_url
109
    if merge_request = @commit.merged_merge_request(current_user)
110
      project_merge_request_url(merge_request.target_project, merge_request)
111
    end
112 113
  end

114
  def commit
115
    @noteable = @commit ||= @project.commit(params[:id])
116
  end
117

Sean McGivern's avatar
Sean McGivern committed
118
  def define_commit_vars
119 120
    return git_not_found! unless commit

121 122
    opts = diff_options
    opts[:ignore_whitespace_change] = true if params[:format] == 'diff'
123

124
    @diffs = commit.diffs(opts)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
125
    @notes_count = commit.notes.count
126

Douwe Maan's avatar
Douwe Maan committed
127
    @environment = EnvironmentsFinder.new(@project, current_user, commit: @commit).execute.last
Sean McGivern's avatar
Sean McGivern committed
128 129 130
  end

  def define_note_vars
131
    @noteable = @commit
Sean McGivern's avatar
Sean McGivern committed
132 133
    @note = @project.build_commit_note(commit)

134
    @new_diff_note_attrs = {
Sean McGivern's avatar
Sean McGivern committed
135 136 137
      noteable_type: 'Commit',
      commit_id: @commit.id
    }
138 139 140 141

    @grouped_diff_discussions = commit.grouped_diff_discussions
    @discussions = commit.discussions

142
    @notes = (@grouped_diff_discussions.values.flatten + @discussions).flat_map(&:notes)
143
    @notes = prepare_notes_for_rendering(@notes, @commit)
Sean McGivern's avatar
Sean McGivern committed
144
  end
Douwe Maan's avatar
Douwe Maan committed
145

146
  def assign_change_commit_vars
147 148
    @start_branch = params[:start_branch]
    @commit_params = { commit: @commit }
149
  end
Robert Speicher's avatar
Robert Speicher committed
150
end