GitLab steht wegen Wartungsarbeiten am Montag, den 10. Mai, zwischen 17:00 und 19:00 Uhr nicht zur Verfügung.

commit_controller.rb 3.76 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
Filipa Lacerda's avatar
Filipa Lacerda committed
14
  before_action :define_commit_vars, only: [:show, :diff_for_path, :pipelines]
Sean McGivern's avatar
Sean McGivern committed
15
  before_action :define_note_vars, only: [:show, :diff_for_path]
16
  before_action :authorize_edit_tree!, only: [:revert, :cherry_pick]
Robert Speicher's avatar
Robert Speicher committed
17 18

  def show
19 20
    apply_diff_view_cookie!

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

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

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

    respond_to do |format|
      format.html
      format.json do
        render json: PipelineSerializer
          .new(project: @project, user: @current_user)
          .with_pagination(request, response)
          .represent(@pipelines)
      end
    end
Kamil Trzcinski's avatar
Kamil Trzcinski committed
44 45
  end

46 47 48 49 50 51
  def branches
    @branches = @project.repository.branch_names_contains(commit.id)
    @tags = @project.repository.tag_names_contains(commit.id)
    render layout: false
  end

52
  def revert
53
    assign_change_commit_vars(@commit.revert_branch_name)
54

55
    return render_404 if @target_branch.blank?
56

57
    create_commit(Commits::RevertService, success_notice: "The #{@commit.change_type_title(current_user)} has been successfully reverted.",
58
                                          success_path: successful_change_path, failure_path: failed_change_path)
59
  end
60

61 62
  def cherry_pick
    assign_change_commit_vars(@commit.cherry_pick_branch_name)
63

64
    return render_404 if @target_branch.blank?
65

66
    create_commit(Commits::CherryPickService, success_notice: "The #{@commit.change_type_title(current_user)} has been successfully cherry-picked.",
67
                                              success_path: successful_change_path, failure_path: failed_change_path)
68 69
  end

70 71 72
  private

  def successful_change_path
73
    referenced_merge_request_url || namespace_project_commits_url(@project.namespace, @project, @target_branch)
74 75
  end

76
  def failed_change_path
77
    referenced_merge_request_url || namespace_project_commit_url(@project.namespace, @project, params[:id])
78 79 80
  end

  def referenced_merge_request_url
81 82 83
    if merge_request = @commit.merged_merge_request(current_user)
      namespace_project_merge_request_url(@project.namespace, @project, merge_request)
    end
84 85
  end

86
  def commit
87
    @noteable = @commit ||= @project.commit(params[:id])
88
  end
89

Sean McGivern's avatar
Sean McGivern committed
90
  def define_commit_vars
91 92
    return git_not_found! unless commit

93 94
    opts = diff_options
    opts[:ignore_whitespace_change] = true if params[:format] == 'diff'
95

96
    @diffs = commit.diffs(opts)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
97
    @notes_count = commit.notes.count
Sean McGivern's avatar
Sean McGivern committed
98 99 100
  end

  def define_note_vars
101
    @grouped_diff_discussions = commit.notes.grouped_diff_discussions
Sean McGivern's avatar
Sean McGivern committed
102 103 104
    @notes = commit.notes.non_diff_notes.fresh

    Banzai::NoteRenderer.render(
105
      @grouped_diff_discussions.values.flat_map(&:notes) + @notes,
Sean McGivern's avatar
Sean McGivern committed
106 107 108 109 110 111 112 113 114 115 116 117
      @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
118

119
  def assign_change_commit_vars(mr_source_branch)
120
    @commit = project.commit(params[:id])
121
    @target_branch = params[:target_branch]
122
    @mr_source_branch = mr_source_branch
123
    @mr_target_branch = @target_branch
124
    @commit_params = {
125 126
      commit: @commit,
      create_merge_request: params[:create_merge_request].present? || different_project?
127 128
    }
  end
Robert Speicher's avatar
Robert Speicher committed
129
end