commit_controller.rb 4.46 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
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
      format.html  { render }
26
      format.diff  { render text: @commit.to_diff }
27
      format.patch { render text: @commit.to_patch }
Robert Speicher's avatar
Robert Speicher committed
28 29
    end
  end
30

31
  def diff_for_path
32
    render_diff_for_path(@commit.diffs(diff_options))
33 34
  end

Kamil Trzcinski's avatar
Kamil Trzcinski committed
35
  def pipelines
36 37 38 39 40
    @pipelines = @commit.pipelines.order(id: :desc)

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

43 44 45 46 47 48 49 50
        render json: {
          pipelines: PipelineSerializer
            .new(project: @project, current_user: @current_user)
            .represent(@pipelines),
          count: {
            all: @pipelines.count
          }
        }
51 52
      end
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
53 54
  end

55
  def branches
56 57 58
    # 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
59 60 61 62 63
    @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)
64 65 66
    render layout: false
  end

67
  def revert
68
    assign_change_commit_vars
69

70 71
    return render_404 if @start_branch.blank?

72
    @branch_name = create_new_branch? ? @commit.revert_branch_name : @start_branch
73

74
    create_commit(Commits::RevertService, success_notice: "The #{@commit.change_type_title(current_user)} has been successfully reverted.",
75
                                          success_path: -> { successful_change_path }, failure_path: failed_change_path)
76
  end
77

78
  def cherry_pick
79
    assign_change_commit_vars
80

81 82
    return render_404 if @start_branch.blank?

83
    @branch_name = create_new_branch? ? @commit.cherry_pick_branch_name : @start_branch
84

85
    create_commit(Commits::CherryPickService, success_notice: "The #{@commit.change_type_title(current_user)} has been successfully cherry-picked.",
86
                                              success_path: -> { successful_change_path }, failure_path: failed_change_path)
87 88
  end

89 90
  private

91 92 93 94
  def create_new_branch?
    params[:create_merge_request].present? || !can?(current_user, :push_code, @project)
  end

95
  def successful_change_path
96
    referenced_merge_request_url || project_commits_url(@project, @branch_name)
97 98
  end

99
  def failed_change_path
100
    referenced_merge_request_url || project_commit_url(@project, params[:id])
101 102 103
  end

  def referenced_merge_request_url
104
    if merge_request = @commit.merged_merge_request(current_user)
105
      project_merge_request_url(merge_request.target_project, merge_request)
106
    end
107 108
  end

109
  def commit
110
    @noteable = @commit ||= @project.commit_by(oid: params[:id])
111
  end
112

Sean McGivern's avatar
Sean McGivern committed
113
  def define_commit_vars
114 115
    return git_not_found! unless commit

116 117
    opts = diff_options
    opts[:ignore_whitespace_change] = true if params[:format] == 'diff'
118

119
    @diffs = commit.diffs(opts)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
120
    @notes_count = commit.notes.count
121

Douwe Maan's avatar
Douwe Maan committed
122
    @environment = EnvironmentsFinder.new(@project, current_user, commit: @commit).execute.last
Sean McGivern's avatar
Sean McGivern committed
123 124 125
  end

  def define_note_vars
126
    @noteable = @commit
Sean McGivern's avatar
Sean McGivern committed
127 128
    @note = @project.build_commit_note(commit)

129
    @new_diff_note_attrs = {
Sean McGivern's avatar
Sean McGivern committed
130 131 132
      noteable_type: 'Commit',
      commit_id: @commit.id
    }
133 134 135 136

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

137
    @notes = (@grouped_diff_discussions.values.flatten + @discussions).flat_map(&:notes)
138
    @notes = prepare_notes_for_rendering(@notes, @commit)
Sean McGivern's avatar
Sean McGivern committed
139
  end
Douwe Maan's avatar
Douwe Maan committed
140

141
  def assign_change_commit_vars
142 143
    @start_branch = params[:start_branch]
    @commit_params = { commit: @commit }
144
  end
Robert Speicher's avatar
Robert Speicher committed
145
end