2018-11-09 13:39:43 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-20 06:11:51 -04:00
|
|
|
module Gitlab
|
|
|
|
module GitalyClient
|
|
|
|
class OperationService
|
2017-12-26 13:53:31 -05:00
|
|
|
include Gitlab::EncodingHelper
|
|
|
|
|
2018-01-08 22:51:05 -05:00
|
|
|
MAX_MSG_SIZE = 128.kilobytes.freeze
|
|
|
|
|
2017-09-20 06:11:51 -04:00
|
|
|
def initialize(repository)
|
|
|
|
@gitaly_repo = repository.gitaly_repository
|
|
|
|
@repository = repository
|
|
|
|
end
|
|
|
|
|
|
|
|
def rm_tag(tag_name, user)
|
|
|
|
request = Gitaly::UserDeleteTagRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
2017-12-26 13:53:31 -05:00
|
|
|
tag_name: encode_binary(tag_name),
|
2017-10-23 16:31:05 -04:00
|
|
|
user: Gitlab::Git::User.from_gitlab(user).to_gitaly
|
2017-09-20 06:11:51 -04:00
|
|
|
)
|
|
|
|
|
2019-09-19 14:06:18 -04:00
|
|
|
response = GitalyClient.call(@repository.storage, :operation_service, :user_delete_tag, request, timeout: GitalyClient.long_timeout)
|
2017-09-20 06:11:51 -04:00
|
|
|
|
|
|
|
if pre_receive_error = response.pre_receive_error.presence
|
2018-06-11 06:42:09 -04:00
|
|
|
raise Gitlab::Git::PreReceiveError, pre_receive_error
|
2017-09-20 06:11:51 -04:00
|
|
|
end
|
|
|
|
end
|
2017-09-25 16:01:04 -04:00
|
|
|
|
|
|
|
def add_tag(tag_name, user, target, message)
|
|
|
|
request = Gitaly::UserCreateTagRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
2017-10-23 16:31:05 -04:00
|
|
|
user: Gitlab::Git::User.from_gitlab(user).to_gitaly,
|
2017-12-26 13:53:31 -05:00
|
|
|
tag_name: encode_binary(tag_name),
|
|
|
|
target_revision: encode_binary(target),
|
2021-02-11 07:08:52 -05:00
|
|
|
message: encode_binary(message.to_s),
|
|
|
|
timestamp: Google::Protobuf::Timestamp.new(seconds: Time.now.utc.to_i)
|
2017-09-25 16:01:04 -04:00
|
|
|
)
|
|
|
|
|
2019-09-19 14:06:18 -04:00
|
|
|
response = GitalyClient.call(@repository.storage, :operation_service, :user_create_tag, request, timeout: GitalyClient.long_timeout)
|
2017-09-25 16:01:04 -04:00
|
|
|
if pre_receive_error = response.pre_receive_error.presence
|
2018-06-11 06:42:09 -04:00
|
|
|
raise Gitlab::Git::PreReceiveError, pre_receive_error
|
2017-09-25 16:01:04 -04:00
|
|
|
elsif response.exists
|
|
|
|
raise Gitlab::Git::Repository::TagExistsError
|
|
|
|
end
|
|
|
|
|
2018-03-20 15:20:12 -04:00
|
|
|
Gitlab::Git::Tag.new(@repository, response.tag)
|
2022-08-01 23:09:33 -04:00
|
|
|
rescue GRPC::BadStatus => e
|
|
|
|
detailed_error = GitalyClient.decode_detailed_error(e)
|
|
|
|
|
|
|
|
case detailed_error&.error
|
|
|
|
when :access_check
|
|
|
|
access_check_error = detailed_error.access_check
|
|
|
|
# These messages were returned from internal/allowed API calls
|
|
|
|
raise Gitlab::Git::PreReceiveError.new(fallback_message: access_check_error.error_message)
|
|
|
|
when :custom_hook
|
|
|
|
raise Gitlab::Git::PreReceiveError.new(custom_hook_error_message(detailed_error.custom_hook),
|
|
|
|
fallback_message: e.details)
|
|
|
|
when :reference_exists
|
|
|
|
raise Gitlab::Git::Repository::TagExistsError
|
|
|
|
else
|
|
|
|
if e.code == GRPC::Core::StatusCodes::FAILED_PRECONDITION
|
|
|
|
raise Gitlab::Git::Repository::InvalidRef, e
|
|
|
|
end
|
|
|
|
|
|
|
|
raise
|
|
|
|
end
|
2017-09-25 16:01:04 -04:00
|
|
|
end
|
2017-09-20 18:34:30 -04:00
|
|
|
|
|
|
|
def user_create_branch(branch_name, user, start_point)
|
|
|
|
request = Gitaly::UserCreateBranchRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
2017-12-26 13:53:31 -05:00
|
|
|
branch_name: encode_binary(branch_name),
|
2017-10-23 16:31:05 -04:00
|
|
|
user: Gitlab::Git::User.from_gitlab(user).to_gitaly,
|
2017-12-26 13:53:31 -05:00
|
|
|
start_point: encode_binary(start_point)
|
2017-09-20 18:34:30 -04:00
|
|
|
)
|
|
|
|
response = GitalyClient.call(@repository.storage, :operation_service,
|
2019-09-19 14:06:18 -04:00
|
|
|
:user_create_branch, request, timeout: GitalyClient.long_timeout)
|
2018-01-11 11:34:01 -05:00
|
|
|
|
2017-09-20 18:34:30 -04:00
|
|
|
if response.pre_receive_error.present?
|
2021-05-04 11:10:36 -04:00
|
|
|
raise Gitlab::Git::PreReceiveError, response.pre_receive_error
|
2017-09-20 18:34:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
branch = response.branch
|
2019-02-08 07:19:53 -05:00
|
|
|
return unless branch
|
2017-09-20 18:34:30 -04:00
|
|
|
|
|
|
|
target_commit = Gitlab::Git::Commit.decorate(@repository, branch.target_commit)
|
|
|
|
Gitlab::Git::Branch.new(@repository, branch.name, target_commit.id, target_commit)
|
2022-09-06 11:13:23 -04:00
|
|
|
rescue GRPC::BadStatus => e
|
|
|
|
detailed_error = GitalyClient.decode_detailed_error(e)
|
|
|
|
|
|
|
|
case detailed_error&.error
|
|
|
|
when :custom_hook
|
|
|
|
raise Gitlab::Git::PreReceiveError.new(custom_hook_error_message(detailed_error.custom_hook),
|
|
|
|
fallback_message: e.details)
|
|
|
|
else
|
|
|
|
if e.code == GRPC::Core::StatusCodes::FAILED_PRECONDITION
|
|
|
|
raise Gitlab::Git::Repository::InvalidRef, e
|
|
|
|
end
|
|
|
|
|
|
|
|
raise
|
|
|
|
end
|
2017-09-20 18:34:30 -04:00
|
|
|
end
|
2017-09-30 14:09:36 -04:00
|
|
|
|
2018-06-27 17:55:40 -04:00
|
|
|
def user_update_branch(branch_name, user, newrev, oldrev)
|
|
|
|
request = Gitaly::UserUpdateBranchRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
branch_name: encode_binary(branch_name),
|
|
|
|
user: Gitlab::Git::User.from_gitlab(user).to_gitaly,
|
|
|
|
newrev: encode_binary(newrev),
|
|
|
|
oldrev: encode_binary(oldrev)
|
|
|
|
)
|
|
|
|
|
2019-09-19 14:06:18 -04:00
|
|
|
response = GitalyClient.call(@repository.storage, :operation_service,
|
|
|
|
:user_update_branch, request, timeout: GitalyClient.long_timeout)
|
2018-06-27 17:55:40 -04:00
|
|
|
|
|
|
|
if pre_receive_error = response.pre_receive_error.presence
|
|
|
|
raise Gitlab::Git::PreReceiveError, pre_receive_error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-30 14:09:36 -04:00
|
|
|
def user_delete_branch(branch_name, user)
|
|
|
|
request = Gitaly::UserDeleteBranchRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
2017-12-26 13:53:31 -05:00
|
|
|
branch_name: encode_binary(branch_name),
|
2017-10-23 16:31:05 -04:00
|
|
|
user: Gitlab::Git::User.from_gitlab(user).to_gitaly
|
2017-09-30 14:09:36 -04:00
|
|
|
)
|
|
|
|
|
2019-09-19 14:06:18 -04:00
|
|
|
response = GitalyClient.call(@repository.storage, :operation_service,
|
|
|
|
:user_delete_branch, request, timeout: GitalyClient.long_timeout)
|
2017-09-30 14:09:36 -04:00
|
|
|
|
|
|
|
if pre_receive_error = response.pre_receive_error.presence
|
2018-06-11 06:42:09 -04:00
|
|
|
raise Gitlab::Git::PreReceiveError, pre_receive_error
|
2017-09-30 14:09:36 -04:00
|
|
|
end
|
2022-06-13 08:08:29 -04:00
|
|
|
rescue GRPC::BadStatus => e
|
2022-07-15 02:09:57 -04:00
|
|
|
detailed_error = GitalyClient.decode_detailed_error(e)
|
2022-06-13 08:08:29 -04:00
|
|
|
|
|
|
|
case detailed_error&.error
|
|
|
|
when :custom_hook
|
|
|
|
raise Gitlab::Git::PreReceiveError.new(custom_hook_error_message(detailed_error.custom_hook),
|
|
|
|
fallback_message: e.details)
|
|
|
|
else
|
|
|
|
raise
|
|
|
|
end
|
2017-09-30 14:09:36 -04:00
|
|
|
end
|
2017-10-10 08:15:21 -04:00
|
|
|
|
2021-04-01 14:13:56 -04:00
|
|
|
def user_merge_to_ref(user, source_sha:, branch:, target_ref:, message:, first_parent_ref:, allow_conflicts: false)
|
2019-01-31 13:32:44 -05:00
|
|
|
request = Gitaly::UserMergeToRefRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
source_sha: source_sha,
|
|
|
|
branch: encode_binary(branch),
|
|
|
|
target_ref: encode_binary(target_ref),
|
|
|
|
user: Gitlab::Git::User.from_gitlab(user).to_gitaly,
|
2019-06-20 08:45:46 -04:00
|
|
|
message: encode_binary(message),
|
2020-10-16 08:09:33 -04:00
|
|
|
first_parent_ref: encode_binary(first_parent_ref),
|
2021-02-11 07:08:52 -05:00
|
|
|
allow_conflicts: allow_conflicts,
|
|
|
|
timestamp: Google::Protobuf::Timestamp.new(seconds: Time.now.utc.to_i)
|
2019-01-31 13:32:44 -05:00
|
|
|
)
|
|
|
|
|
2019-09-19 14:06:18 -04:00
|
|
|
response = GitalyClient.call(@repository.storage, :operation_service,
|
|
|
|
:user_merge_to_ref, request, timeout: GitalyClient.long_timeout)
|
2019-01-31 13:32:44 -05:00
|
|
|
|
|
|
|
response.commit_id
|
|
|
|
end
|
|
|
|
|
2017-10-10 08:15:21 -04:00
|
|
|
def user_merge_branch(user, source_sha, target_branch, message)
|
|
|
|
request_enum = QueueEnumerator.new
|
|
|
|
response_enum = GitalyClient.call(
|
|
|
|
@repository.storage,
|
|
|
|
:operation_service,
|
|
|
|
:user_merge_branch,
|
2019-09-19 14:06:18 -04:00
|
|
|
request_enum.each,
|
|
|
|
timeout: GitalyClient.long_timeout
|
2017-10-10 08:15:21 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
request_enum.push(
|
|
|
|
Gitaly::UserMergeBranchRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
2017-10-23 16:31:05 -04:00
|
|
|
user: Gitlab::Git::User.from_gitlab(user).to_gitaly,
|
2017-10-10 08:15:21 -04:00
|
|
|
commit_id: source_sha,
|
2017-12-26 13:53:31 -05:00
|
|
|
branch: encode_binary(target_branch),
|
2021-02-11 07:08:52 -05:00
|
|
|
message: encode_binary(message),
|
|
|
|
timestamp: Google::Protobuf::Timestamp.new(seconds: Time.now.utc.to_i)
|
2017-10-10 08:15:21 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
yield response_enum.next.commit_id
|
|
|
|
|
|
|
|
request_enum.push(Gitaly::UserMergeBranchRequest.new(apply: true))
|
|
|
|
|
2018-01-29 10:11:16 -05:00
|
|
|
second_response = response_enum.next
|
|
|
|
|
|
|
|
branch_update = second_response.branch_update
|
2017-12-22 10:58:05 -05:00
|
|
|
return if branch_update.nil?
|
2021-05-04 11:10:36 -04:00
|
|
|
raise Gitlab::Git::CommitError, 'failed to apply merge to branch' unless branch_update.commit_id.present?
|
2017-10-10 08:15:21 -04:00
|
|
|
|
|
|
|
Gitlab::Git::OperationService::BranchUpdate.from_gitaly(branch_update)
|
2021-10-12 05:09:35 -04:00
|
|
|
|
|
|
|
rescue GRPC::BadStatus => e
|
2022-07-15 02:09:57 -04:00
|
|
|
detailed_error = GitalyClient.decode_detailed_error(e)
|
2021-10-12 05:09:35 -04:00
|
|
|
|
2022-03-09 04:08:34 -05:00
|
|
|
case detailed_error&.error
|
|
|
|
when :access_check
|
|
|
|
access_check_error = detailed_error.access_check
|
|
|
|
# These messages were returned from internal/allowed API calls
|
|
|
|
raise Gitlab::Git::PreReceiveError.new(fallback_message: access_check_error.error_message)
|
2022-05-20 02:08:54 -04:00
|
|
|
when :custom_hook
|
2022-06-13 08:08:29 -04:00
|
|
|
raise Gitlab::Git::PreReceiveError.new(custom_hook_error_message(detailed_error.custom_hook),
|
|
|
|
fallback_message: e.details)
|
2022-03-09 04:08:34 -05:00
|
|
|
when :reference_update
|
|
|
|
# We simply ignore any reference update errors which are typically an
|
|
|
|
# indicator of multiple RPC calls trying to update the same reference
|
|
|
|
# at the same point in time.
|
|
|
|
else
|
|
|
|
raise
|
|
|
|
end
|
2017-10-10 08:15:21 -04:00
|
|
|
ensure
|
|
|
|
request_enum.close
|
|
|
|
end
|
2017-10-25 18:00:19 -04:00
|
|
|
|
|
|
|
def user_ff_branch(user, source_sha, target_branch)
|
|
|
|
request = Gitaly::UserFFBranchRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
user: Gitlab::Git::User.from_gitlab(user).to_gitaly,
|
|
|
|
commit_id: source_sha,
|
2017-12-26 13:53:31 -05:00
|
|
|
branch: encode_binary(target_branch)
|
2017-10-25 18:00:19 -04:00
|
|
|
)
|
|
|
|
|
2018-07-19 13:48:58 -04:00
|
|
|
response = GitalyClient.call(
|
2017-10-25 18:00:19 -04:00
|
|
|
@repository.storage,
|
|
|
|
:operation_service,
|
|
|
|
:user_ff_branch,
|
2019-09-19 14:06:18 -04:00
|
|
|
request,
|
|
|
|
timeout: GitalyClient.long_timeout
|
2018-07-19 13:48:58 -04:00
|
|
|
)
|
|
|
|
|
2020-07-02 11:09:08 -04:00
|
|
|
if response.pre_receive_error.present?
|
2020-10-13 11:08:53 -04:00
|
|
|
raise Gitlab::Git::PreReceiveError.new(response.pre_receive_error, fallback_message: "pre-receive hook failed.")
|
2020-07-02 11:09:08 -04:00
|
|
|
end
|
|
|
|
|
2018-07-19 13:48:58 -04:00
|
|
|
Gitlab::Git::OperationService::BranchUpdate.from_gitaly(response.branch_update)
|
2018-07-03 05:12:03 -04:00
|
|
|
rescue GRPC::FailedPrecondition => e
|
|
|
|
raise Gitlab::Git::CommitError, e
|
2017-10-25 18:00:19 -04:00
|
|
|
end
|
2017-11-21 08:47:52 -05:00
|
|
|
|
2020-07-30 14:09:39 -04:00
|
|
|
def user_cherry_pick(user:, commit:, branch_name:, message:, start_branch_name:, start_repository:, dry_run: false)
|
2017-12-04 08:13:22 -05:00
|
|
|
call_cherry_pick_or_revert(:cherry_pick,
|
|
|
|
user: user,
|
|
|
|
commit: commit,
|
|
|
|
branch_name: branch_name,
|
|
|
|
message: message,
|
|
|
|
start_branch_name: start_branch_name,
|
2020-07-30 14:09:39 -04:00
|
|
|
start_repository: start_repository,
|
|
|
|
dry_run: dry_run)
|
2017-12-04 08:13:22 -05:00
|
|
|
end
|
|
|
|
|
2020-07-30 14:09:39 -04:00
|
|
|
def user_revert(user:, commit:, branch_name:, message:, start_branch_name:, start_repository:, dry_run: false)
|
2017-12-04 08:13:22 -05:00
|
|
|
call_cherry_pick_or_revert(:revert,
|
|
|
|
user: user,
|
|
|
|
commit: commit,
|
|
|
|
branch_name: branch_name,
|
|
|
|
message: message,
|
|
|
|
start_branch_name: start_branch_name,
|
2020-07-30 14:09:39 -04:00
|
|
|
start_repository: start_repository,
|
|
|
|
dry_run: dry_run)
|
2017-12-04 08:13:22 -05:00
|
|
|
end
|
|
|
|
|
2020-01-16 16:08:24 -05:00
|
|
|
def rebase(user, rebase_id, branch:, branch_sha:, remote_repository:, remote_branch:, push_options: [])
|
2019-05-02 13:30:07 -04:00
|
|
|
request_enum = QueueEnumerator.new
|
|
|
|
rebase_sha = nil
|
|
|
|
|
|
|
|
response_enum = GitalyClient.call(
|
|
|
|
@repository.storage,
|
|
|
|
:operation_service,
|
|
|
|
:user_rebase_confirmable,
|
|
|
|
request_enum.each,
|
2019-09-19 14:06:18 -04:00
|
|
|
timeout: GitalyClient.long_timeout,
|
2019-05-02 13:30:07 -04:00
|
|
|
remote_storage: remote_repository.storage
|
|
|
|
)
|
|
|
|
|
|
|
|
# First request
|
|
|
|
request_enum.push(
|
|
|
|
Gitaly::UserRebaseConfirmableRequest.new(
|
|
|
|
header: Gitaly::UserRebaseConfirmableRequest::Header.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
user: Gitlab::Git::User.from_gitlab(user).to_gitaly,
|
|
|
|
rebase_id: rebase_id.to_s,
|
|
|
|
branch: encode_binary(branch),
|
|
|
|
branch_sha: branch_sha,
|
|
|
|
remote_repository: remote_repository.gitaly_repository,
|
2020-01-16 16:08:24 -05:00
|
|
|
remote_branch: encode_binary(remote_branch),
|
2021-02-11 07:08:52 -05:00
|
|
|
git_push_options: push_options,
|
|
|
|
timestamp: Google::Protobuf::Timestamp.new(seconds: Time.now.utc.to_i)
|
2019-05-02 13:30:07 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
perform_next_gitaly_rebase_request(response_enum) do |response|
|
|
|
|
rebase_sha = response.rebase_sha
|
|
|
|
end
|
|
|
|
|
|
|
|
yield rebase_sha
|
|
|
|
|
|
|
|
# Second request confirms with gitaly to finalize the rebase
|
|
|
|
request_enum.push(Gitaly::UserRebaseConfirmableRequest.new(apply: true))
|
|
|
|
|
|
|
|
perform_next_gitaly_rebase_request(response_enum)
|
|
|
|
|
|
|
|
rebase_sha
|
2022-03-16 02:07:59 -04:00
|
|
|
rescue GRPC::BadStatus => e
|
2022-07-15 02:09:57 -04:00
|
|
|
detailed_error = GitalyClient.decode_detailed_error(e)
|
2022-03-16 02:07:59 -04:00
|
|
|
|
|
|
|
case detailed_error&.error
|
|
|
|
when :access_check
|
|
|
|
access_check_error = detailed_error.access_check
|
|
|
|
# These messages were returned from internal/allowed API calls
|
|
|
|
raise Gitlab::Git::PreReceiveError.new(fallback_message: access_check_error.error_message)
|
|
|
|
when :rebase_conflict
|
|
|
|
raise Gitlab::Git::Repository::GitError, e.details
|
|
|
|
else
|
|
|
|
raise e
|
|
|
|
end
|
2019-05-02 13:30:07 -04:00
|
|
|
ensure
|
|
|
|
request_enum.close
|
|
|
|
end
|
|
|
|
|
2021-09-22 11:12:06 -04:00
|
|
|
def user_squash(user, start_sha, end_sha, author, message, time = Time.now.utc)
|
2018-02-02 16:30:03 -05:00
|
|
|
request = Gitaly::UserSquashRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
user: Gitlab::Git::User.from_gitlab(user).to_gitaly,
|
|
|
|
start_sha: start_sha,
|
|
|
|
end_sha: end_sha,
|
|
|
|
author: Gitlab::Git::User.from_gitlab(author).to_gitaly,
|
2021-02-11 07:08:52 -05:00
|
|
|
commit_message: encode_binary(message),
|
|
|
|
timestamp: Google::Protobuf::Timestamp.new(seconds: time.to_i)
|
2018-02-02 16:30:03 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
response = GitalyClient.call(
|
|
|
|
@repository.storage,
|
|
|
|
:operation_service,
|
|
|
|
:user_squash,
|
2019-09-19 14:06:18 -04:00
|
|
|
request,
|
|
|
|
timeout: GitalyClient.long_timeout
|
2018-02-02 16:30:03 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
response.squash_sha
|
2022-03-09 04:08:34 -05:00
|
|
|
rescue GRPC::BadStatus => e
|
2022-07-15 02:09:57 -04:00
|
|
|
detailed_error = GitalyClient.decode_detailed_error(e)
|
2022-03-09 04:08:34 -05:00
|
|
|
|
|
|
|
case detailed_error&.error
|
|
|
|
when :resolve_revision, :rebase_conflict
|
|
|
|
# Theoretically, we could now raise specific errors based on the type
|
|
|
|
# of the detailed error. Most importantly, we get error details when
|
|
|
|
# Gitaly was not able to resolve the `start_sha` or `end_sha` via a
|
|
|
|
# ResolveRevisionError, and we get information about which files are
|
|
|
|
# conflicting via a MergeConflictError.
|
|
|
|
#
|
|
|
|
# We don't do this now though such that we can maintain backwards
|
|
|
|
# compatibility with the minimum required set of changes during the
|
|
|
|
# transitory period where we're migrating UserSquash to use
|
|
|
|
# structured errors. We thus continue to just return a GitError, like
|
|
|
|
# we previously did.
|
|
|
|
raise Gitlab::Git::Repository::GitError, e.details
|
|
|
|
else
|
|
|
|
raise
|
|
|
|
end
|
2018-02-02 16:30:03 -05:00
|
|
|
end
|
|
|
|
|
2018-07-31 12:35:02 -04:00
|
|
|
def user_update_submodule(user:, submodule:, commit_sha:, branch:, message:)
|
|
|
|
request = Gitaly::UserUpdateSubmoduleRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
user: Gitlab::Git::User.from_gitlab(user).to_gitaly,
|
|
|
|
commit_sha: commit_sha,
|
|
|
|
branch: encode_binary(branch),
|
|
|
|
submodule: encode_binary(submodule),
|
2021-02-11 07:08:52 -05:00
|
|
|
commit_message: encode_binary(message),
|
|
|
|
timestamp: Google::Protobuf::Timestamp.new(seconds: Time.now.utc.to_i)
|
2018-07-31 12:35:02 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
response = GitalyClient.call(
|
|
|
|
@repository.storage,
|
|
|
|
:operation_service,
|
|
|
|
:user_update_submodule,
|
2019-09-19 14:06:18 -04:00
|
|
|
request,
|
|
|
|
timeout: GitalyClient.long_timeout
|
2018-07-31 12:35:02 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
if response.pre_receive_error.present?
|
|
|
|
raise Gitlab::Git::PreReceiveError, response.pre_receive_error
|
|
|
|
elsif response.commit_error.present?
|
|
|
|
raise Gitlab::Git::CommitError, response.commit_error
|
|
|
|
else
|
|
|
|
Gitlab::Git::OperationService::BranchUpdate.from_gitaly(response.branch_update)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-06 05:44:59 -05:00
|
|
|
# rubocop:disable Metrics/ParameterLists
|
2018-01-08 22:51:05 -05:00
|
|
|
def user_commit_files(
|
|
|
|
user, branch_name, commit_message, actions, author_email, author_name,
|
2019-06-13 06:44:41 -04:00
|
|
|
start_branch_name, start_repository, force = false, start_sha = nil)
|
2018-01-08 22:51:05 -05:00
|
|
|
req_enum = Enumerator.new do |y|
|
|
|
|
header = user_commit_files_request_header(user, branch_name,
|
|
|
|
commit_message, actions, author_email, author_name,
|
2019-06-13 06:44:41 -04:00
|
|
|
start_branch_name, start_repository, force, start_sha)
|
2018-01-08 22:51:05 -05:00
|
|
|
|
|
|
|
y.yield Gitaly::UserCommitFilesRequest.new(header: header)
|
|
|
|
|
|
|
|
actions.each do |action|
|
|
|
|
action_header = user_commit_files_action_header(action)
|
|
|
|
y.yield Gitaly::UserCommitFilesRequest.new(
|
|
|
|
action: Gitaly::UserCommitFilesAction.new(header: action_header)
|
|
|
|
)
|
|
|
|
|
2019-03-28 15:05:27 -04:00
|
|
|
reader = binary_io(action[:content])
|
2018-01-08 22:51:05 -05:00
|
|
|
|
|
|
|
until reader.eof?
|
|
|
|
chunk = reader.read(MAX_MSG_SIZE)
|
|
|
|
|
|
|
|
y.yield Gitaly::UserCommitFilesRequest.new(
|
|
|
|
action: Gitaly::UserCommitFilesAction.new(content: chunk)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-08-23 11:09:40 -04:00
|
|
|
response = GitalyClient.call(
|
|
|
|
@repository.storage, :operation_service, :user_commit_files, req_enum,
|
|
|
|
timeout: GitalyClient.long_timeout, remote_storage: start_repository&.storage)
|
2018-01-08 22:51:05 -05:00
|
|
|
|
|
|
|
if (pre_receive_error = response.pre_receive_error.presence)
|
2018-06-11 06:42:09 -04:00
|
|
|
raise Gitlab::Git::PreReceiveError, pre_receive_error
|
2018-01-08 22:51:05 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
if (index_error = response.index_error.presence)
|
|
|
|
raise Gitlab::Git::Index::IndexError, index_error
|
|
|
|
end
|
|
|
|
|
|
|
|
Gitlab::Git::OperationService::BranchUpdate.from_gitaly(response.branch_update)
|
|
|
|
end
|
2019-03-06 05:44:59 -05:00
|
|
|
# rubocop:enable Metrics/ParameterLists
|
2018-01-08 22:51:05 -05:00
|
|
|
|
2018-10-24 12:01:44 -04:00
|
|
|
def user_commit_patches(user, branch_name, patches)
|
|
|
|
header = Gitaly::UserApplyPatchRequest::Header.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
user: Gitlab::Git::User.from_gitlab(user).to_gitaly,
|
2021-02-11 07:08:52 -05:00
|
|
|
target_branch: encode_binary(branch_name),
|
|
|
|
timestamp: Google::Protobuf::Timestamp.new(seconds: Time.now.utc.to_i)
|
2018-10-24 12:01:44 -04:00
|
|
|
)
|
2019-03-28 15:05:27 -04:00
|
|
|
reader = binary_io(patches)
|
2018-10-24 12:01:44 -04:00
|
|
|
|
|
|
|
chunks = Enumerator.new do |chunk|
|
|
|
|
chunk.yield Gitaly::UserApplyPatchRequest.new(header: header)
|
|
|
|
|
|
|
|
until reader.eof?
|
|
|
|
patch_chunk = reader.read(MAX_MSG_SIZE)
|
|
|
|
|
|
|
|
chunk.yield(Gitaly::UserApplyPatchRequest.new(patches: patch_chunk))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-19 14:06:18 -04:00
|
|
|
response = GitalyClient.call(@repository.storage, :operation_service,
|
|
|
|
:user_apply_patch, chunks, timeout: GitalyClient.long_timeout)
|
2018-10-24 12:01:44 -04:00
|
|
|
|
|
|
|
Gitlab::Git::OperationService::BranchUpdate.from_gitaly(response.branch_update)
|
|
|
|
end
|
|
|
|
|
2017-12-04 08:13:22 -05:00
|
|
|
private
|
|
|
|
|
2019-05-02 13:30:07 -04:00
|
|
|
def perform_next_gitaly_rebase_request(response_enum)
|
|
|
|
response = response_enum.next
|
|
|
|
|
|
|
|
if response.pre_receive_error.present?
|
|
|
|
raise Gitlab::Git::PreReceiveError, response.pre_receive_error
|
|
|
|
elsif response.git_error.present?
|
|
|
|
raise Gitlab::Git::Repository::GitError, response.git_error
|
|
|
|
end
|
|
|
|
|
|
|
|
yield response if block_given?
|
|
|
|
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
2020-07-30 14:09:39 -04:00
|
|
|
def call_cherry_pick_or_revert(rpc, user:, commit:, branch_name:, message:, start_branch_name:, start_repository:, dry_run:)
|
2017-12-04 08:13:22 -05:00
|
|
|
request_class = "Gitaly::User#{rpc.to_s.camelcase}Request".constantize
|
|
|
|
|
|
|
|
request = request_class.new(
|
2017-11-21 08:47:52 -05:00
|
|
|
repository: @gitaly_repo,
|
|
|
|
user: Gitlab::Git::User.from_gitlab(user).to_gitaly,
|
|
|
|
commit: commit.to_gitaly_commit,
|
2017-12-26 13:53:31 -05:00
|
|
|
branch_name: encode_binary(branch_name),
|
|
|
|
message: encode_binary(message),
|
|
|
|
start_branch_name: encode_binary(start_branch_name.to_s),
|
2020-07-30 14:09:39 -04:00
|
|
|
start_repository: start_repository.gitaly_repository,
|
|
|
|
dry_run: dry_run
|
2017-11-21 08:47:52 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
response = GitalyClient.call(
|
|
|
|
@repository.storage,
|
|
|
|
:operation_service,
|
2017-12-04 08:13:22 -05:00
|
|
|
:"user_#{rpc}",
|
2017-11-21 08:47:52 -05:00
|
|
|
request,
|
2018-07-05 12:05:24 -04:00
|
|
|
remote_storage: start_repository.storage,
|
2019-09-19 14:06:18 -04:00
|
|
|
timeout: GitalyClient.long_timeout
|
2017-11-21 08:47:52 -05:00
|
|
|
)
|
|
|
|
|
2017-12-04 08:13:22 -05:00
|
|
|
handle_cherry_pick_or_revert_response(response)
|
2022-05-23 23:08:48 -04:00
|
|
|
rescue GRPC::BadStatus => e
|
2022-07-15 02:09:57 -04:00
|
|
|
detailed_error = GitalyClient.decode_detailed_error(e)
|
2022-05-23 23:08:48 -04:00
|
|
|
|
|
|
|
case detailed_error&.error
|
|
|
|
when :access_check
|
|
|
|
access_check_error = detailed_error.access_check
|
|
|
|
# These messages were returned from internal/allowed API calls
|
|
|
|
raise Gitlab::Git::PreReceiveError.new(fallback_message: access_check_error.error_message)
|
|
|
|
when :cherry_pick_conflict
|
|
|
|
raise Gitlab::Git::Repository::CreateTreeError, 'CONFLICT'
|
2022-06-29 08:09:26 -04:00
|
|
|
when :changes_already_applied
|
|
|
|
raise Gitlab::Git::Repository::CreateTreeError, 'EMPTY'
|
2022-05-23 23:08:48 -04:00
|
|
|
when :target_branch_diverged
|
|
|
|
raise Gitlab::Git::CommitError, 'branch diverged'
|
|
|
|
else
|
|
|
|
raise e
|
|
|
|
end
|
2017-12-04 08:13:22 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def handle_cherry_pick_or_revert_response(response)
|
2017-11-21 08:47:52 -05:00
|
|
|
if response.pre_receive_error.presence
|
2018-06-11 06:42:09 -04:00
|
|
|
raise Gitlab::Git::PreReceiveError, response.pre_receive_error
|
2017-11-21 08:47:52 -05:00
|
|
|
elsif response.commit_error.presence
|
|
|
|
raise Gitlab::Git::CommitError, response.commit_error
|
|
|
|
elsif response.create_tree_error.presence
|
2019-11-13 22:06:25 -05:00
|
|
|
raise Gitlab::Git::Repository::CreateTreeError, response.create_tree_error_code
|
2017-11-21 08:47:52 -05:00
|
|
|
end
|
2018-07-19 13:48:58 -04:00
|
|
|
|
|
|
|
Gitlab::Git::OperationService::BranchUpdate.from_gitaly(response.branch_update)
|
2017-11-21 08:47:52 -05:00
|
|
|
end
|
2018-01-08 22:51:05 -05:00
|
|
|
|
2019-03-06 05:44:59 -05:00
|
|
|
# rubocop:disable Metrics/ParameterLists
|
2018-01-08 22:51:05 -05:00
|
|
|
def user_commit_files_request_header(
|
|
|
|
user, branch_name, commit_message, actions, author_email, author_name,
|
2019-06-13 06:44:41 -04:00
|
|
|
start_branch_name, start_repository, force, start_sha)
|
2018-01-08 22:51:05 -05:00
|
|
|
|
|
|
|
Gitaly::UserCommitFilesRequestHeader.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
user: Gitlab::Git::User.from_gitlab(user).to_gitaly,
|
|
|
|
branch_name: encode_binary(branch_name),
|
|
|
|
commit_message: encode_binary(commit_message),
|
|
|
|
commit_author_name: encode_binary(author_name),
|
|
|
|
commit_author_email: encode_binary(author_email),
|
|
|
|
start_branch_name: encode_binary(start_branch_name),
|
2022-08-05 11:12:12 -04:00
|
|
|
start_repository: start_repository&.gitaly_repository,
|
2019-06-13 06:44:41 -04:00
|
|
|
force: force,
|
2021-02-11 07:08:52 -05:00
|
|
|
start_sha: encode_binary(start_sha),
|
|
|
|
timestamp: Google::Protobuf::Timestamp.new(seconds: Time.now.utc.to_i)
|
2018-01-08 22:51:05 -05:00
|
|
|
)
|
|
|
|
end
|
2019-03-06 05:44:59 -05:00
|
|
|
# rubocop:enable Metrics/ParameterLists
|
2018-01-08 22:51:05 -05:00
|
|
|
|
|
|
|
def user_commit_files_action_header(action)
|
|
|
|
Gitaly::UserCommitFilesActionHeader.new(
|
|
|
|
action: action[:action].upcase.to_sym,
|
|
|
|
file_path: encode_binary(action[:file_path]),
|
|
|
|
previous_path: encode_binary(action[:previous_path]),
|
2018-09-23 06:48:29 -04:00
|
|
|
base64_content: action[:encoding] == 'base64',
|
2018-11-27 11:27:51 -05:00
|
|
|
execute_filemode: !!action[:execute_filemode],
|
|
|
|
infer_content: !!action[:infer_content]
|
2018-01-08 22:51:05 -05:00
|
|
|
)
|
|
|
|
rescue RangeError
|
|
|
|
raise ArgumentError, "Unknown action '#{action[:action]}'"
|
|
|
|
end
|
2021-10-12 05:09:35 -04:00
|
|
|
|
2022-06-13 08:08:29 -04:00
|
|
|
def custom_hook_error_message(custom_hook_error)
|
|
|
|
# Custom hooks may return messages via either stdout or stderr which have a specific prefix. If
|
|
|
|
# that prefix is present we'll want to print the hook's output, otherwise we'll want to print the
|
|
|
|
# Gitaly error as a fallback.
|
|
|
|
custom_hook_output = custom_hook_error.stderr.presence || custom_hook_error.stdout
|
|
|
|
EncodingHelper.encode!(custom_hook_output)
|
|
|
|
end
|
2017-09-20 06:11:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|