2018-09-26 03:45:43 +00:00
# frozen_string_literal: true
2012-09-17 14:08:02 +00:00
# Controller for a specific Commit
#
# Not to be confused with CommitsController, plural.
2013-06-23 16:47:22 +00:00
class Projects :: CommitController < Projects :: ApplicationController
2017-03-31 03:06:09 +00:00
include RendersNotes
2016-02-03 21:11:59 +00:00
include CreatesCommit
2016-07-06 17:15:27 +00:00
include DiffForPath
2016-03-03 17:38:44 +00:00
include DiffHelper
2020-01-08 06:08:13 +00:00
include SourcegraphDecorator
2016-02-03 21:11:59 +00:00
2012-09-17 14:08:02 +00:00
# Authorize
2015-04-16 12:03:37 +00:00
before_action :require_non_empty_project
2016-11-29 13:02:28 +00:00
before_action :authorize_download_code!
2016-09-13 11:44:01 +00:00
before_action :authorize_read_pipeline! , only : [ :pipelines ]
2015-04-16 12:03:37 +00:00
before_action :commit
2020-08-12 12:10:25 +00:00
before_action :define_commit_vars , only : [ :show , :diff_for_path , :diff_files , :pipelines , :merge_requests ]
before_action :define_note_vars , only : [ :show , :diff_for_path , :diff_files ]
2016-04-18 07:39:07 +00:00
before_action :authorize_edit_tree! , only : [ :revert , :cherry_pick ]
2012-09-17 14:08:02 +00:00
2017-10-13 17:11:11 +00:00
BRANCH_SEARCH_LIMIT = 1000
2020-10-08 18:08:32 +00:00
feature_category :source_code_management
2012-09-17 14:08:02 +00:00
def show
2015-10-23 01:52:17 +00:00
apply_diff_view_cookie!
2012-09-25 22:46:19 +00:00
respond_to do | format |
2019-01-16 12:09:29 +00:00
format . html do
2018-07-16 16:18:52 +00:00
render
end
2019-01-16 12:09:29 +00:00
format . diff do
2018-05-15 15:05:19 +00:00
send_git_diff ( @project . repository , @commit . diff_refs )
end
format . patch do
send_git_patch ( @project . repository , @commit . diff_refs )
end
2012-09-17 14:08:02 +00:00
end
end
2014-01-15 13:06:12 +00:00
2016-06-28 16:25:32 +00:00
def diff_for_path
2016-07-27 17:00:34 +00:00
render_diff_for_path ( @commit . diffs ( diff_options ) )
2016-06-28 16:25:32 +00:00
end
2020-08-12 12:10:25 +00:00
def diff_files
render json : { html : view_to_html_string ( 'projects/commit/diff_files' , diffs : @diffs , environment : @environment ) }
end
2018-08-27 15:31:01 +00:00
# rubocop: disable CodeReuse/ActiveRecord
2016-09-13 11:44:01 +00:00
def pipelines
2017-01-27 12:32:21 +00:00
@pipelines = @commit . pipelines . order ( id : :desc )
2018-10-30 15:56:30 +00:00
@pipelines = @pipelines . where ( ref : params [ :ref ] ) . page ( params [ :page ] ) . per ( 30 ) if params [ :ref ]
2017-01-27 12:32:21 +00:00
respond_to do | format |
format . html
format . json do
2017-04-05 14:36:14 +00:00
Gitlab :: PollingInterval . set_header ( response , interval : 10_000 )
2017-07-14 15:52:54 +00:00
render json : {
pipelines : PipelineSerializer
. new ( project : @project , current_user : @current_user )
2018-10-30 15:56:30 +00:00
. with_pagination ( request , response )
2017-07-14 15:52:54 +00:00
. represent ( @pipelines ) ,
count : {
all : @pipelines . count
}
}
2017-01-27 12:32:21 +00:00
end
end
2016-09-13 11:44:01 +00:00
end
2018-08-27 15:31:01 +00:00
# rubocop: enable CodeReuse/ActiveRecord
2016-09-13 11:44:01 +00:00
2018-01-12 20:38:36 +00:00
def merge_requests
2019-01-25 09:22:48 +00:00
@merge_requests = MergeRequestsFinder . new (
current_user ,
project_id : @project . id ,
commit_sha : @commit . sha
) . execute . map do | mr |
2018-01-12 20:38:36 +00:00
{ iid : mr . iid , path : merge_request_path ( mr ) , title : mr . title }
end
respond_to do | format |
format . json do
render json : @merge_requests . to_json
end
end
end
2015-02-03 05:30:11 +00:00
def branches
2017-10-12 13:31:43 +00:00
# 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
2017-10-13 17:11:11 +00:00
@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 )
2015-02-03 05:30:11 +00:00
render layout : false
end
2016-02-03 21:11:59 +00:00
def revert
2017-01-05 18:11:27 +00:00
assign_change_commit_vars
2016-02-18 16:58:04 +00:00
2017-02-17 00:24:56 +00:00
return render_404 if @start_branch . blank?
2017-04-20 00:37:44 +00:00
@branch_name = create_new_branch? ? @commit . revert_branch_name : @start_branch
2016-02-03 21:11:59 +00:00
2016-11-29 13:47:43 +00:00
create_commit ( Commits :: RevertService , success_notice : " The #{ @commit . change_type_title ( current_user ) } has been successfully reverted. " ,
2017-02-17 00:24:56 +00:00
success_path : - > { successful_change_path } , failure_path : failed_change_path )
2016-02-03 21:11:59 +00:00
end
2016-05-10 22:41:46 +00:00
2016-04-18 07:39:07 +00:00
def cherry_pick
2017-01-05 18:11:27 +00:00
assign_change_commit_vars
2016-05-10 22:41:46 +00:00
2017-02-17 00:24:56 +00:00
return render_404 if @start_branch . blank?
2017-04-20 00:37:44 +00:00
@branch_name = create_new_branch? ? @commit . cherry_pick_branch_name : @start_branch
2016-02-03 21:11:59 +00:00
2018-08-19 20:28:54 +00:00
create_commit ( Commits :: CherryPickService , success_notice : " The #{ @commit . change_type_title ( current_user ) } has been successfully cherry-picked into #{ @branch_name } . " ,
2017-02-17 00:24:56 +00:00
success_path : - > { successful_change_path } , failure_path : failed_change_path )
2016-02-08 20:37:27 +00:00
end
2016-04-18 07:39:07 +00:00
private
2017-02-17 00:24:56 +00:00
def create_new_branch?
params [ :create_merge_request ] . present? || ! can? ( current_user , :push_code , @project )
end
2016-04-18 07:39:07 +00:00
def successful_change_path
2017-06-29 17:06:35 +00:00
referenced_merge_request_url || project_commits_url ( @project , @branch_name )
2016-02-08 20:37:27 +00:00
end
2016-04-18 07:39:07 +00:00
def failed_change_path
2017-06-29 17:06:35 +00:00
referenced_merge_request_url || project_commit_url ( @project , params [ :id ] )
2016-02-08 20:37:27 +00:00
end
def referenced_merge_request_url
2016-11-29 13:47:43 +00:00
if merge_request = @commit . merged_merge_request ( current_user )
2017-06-29 17:06:35 +00:00
project_merge_request_url ( merge_request . target_project , merge_request )
2016-11-29 13:47:43 +00:00
end
2016-02-08 20:37:27 +00:00
end
2014-01-15 13:06:12 +00:00
def commit
2018-07-16 16:18:52 +00:00
@noteable = @commit || = @project . commit_by ( oid : params [ :id ] ) . tap do | commit |
# preload author and their status for rendering
commit & . author & . status
end
2014-01-15 13:06:12 +00:00
end
2015-10-23 09:41:22 +00:00
2016-07-06 17:15:27 +00:00
def define_commit_vars
2016-02-15 20:48:16 +00:00
return git_not_found! unless commit
2016-03-03 17:38:44 +00:00
opts = diff_options
opts [ :ignore_whitespace_change ] = true if params [ :format ] == 'diff'
2015-11-30 09:30:44 +00:00
2016-07-27 17:00:34 +00:00
@diffs = commit . diffs ( opts )
2015-11-03 10:44:07 +00:00
@notes_count = commit . notes . count
2017-02-17 00:24:56 +00:00
2019-12-18 18:08:04 +00:00
@environment = EnvironmentsFinder . new ( @project , current_user , commit : @commit , find_latest : true ) . execute . last
2016-07-06 17:15:27 +00:00
end
2018-08-27 15:31:01 +00:00
# rubocop: disable CodeReuse/ActiveRecord
2016-07-06 17:15:27 +00:00
def define_note_vars
2017-03-10 01:29:11 +00:00
@noteable = @commit
2016-07-06 17:15:27 +00:00
@note = @project . build_commit_note ( commit )
2017-03-10 01:29:11 +00:00
@new_diff_note_attrs = {
2016-07-06 17:15:27 +00:00
noteable_type : 'Commit' ,
commit_id : @commit . id
}
2017-03-10 01:29:11 +00:00
@grouped_diff_discussions = commit . grouped_diff_discussions
@discussions = commit . discussions
2017-06-29 21:19:09 +00:00
if merge_request_iid = params [ :merge_request_iid ]
@merge_request = MergeRequestsFinder . new ( current_user , project_id : @project . id ) . find_by ( iid : merge_request_iid )
if @merge_request
@new_diff_note_attrs . merge! (
noteable_type : 'MergeRequest' ,
noteable_id : @merge_request . id
)
merge_request_commit_notes = @merge_request . notes . where ( commit_id : @commit . id ) . inc_relations_for_view
merge_request_commit_diff_discussions = merge_request_commit_notes . grouped_diff_discussions ( @commit . diff_refs )
@grouped_diff_discussions . merge! ( merge_request_commit_diff_discussions ) do | line_code , left , right |
left + right
end
end
end
2017-03-31 02:34:14 +00:00
@notes = ( @grouped_diff_discussions . values . flatten + @discussions ) . flat_map ( & :notes )
2017-07-29 15:04:42 +00:00
@notes = prepare_notes_for_rendering ( @notes , @commit )
2016-07-06 17:15:27 +00:00
end
2018-08-27 15:31:01 +00:00
# rubocop: enable CodeReuse/ActiveRecord
2015-12-08 12:03:28 +00:00
2017-01-05 18:11:27 +00:00
def assign_change_commit_vars
2017-02-17 00:24:56 +00:00
@start_branch = params [ :start_branch ]
@commit_params = { commit : @commit }
2016-02-03 23:28:40 +00:00
end
2012-09-17 14:08:02 +00:00
end