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
|
|
|
)
|
|
|
|
|
|
|
|
response = GitalyClient.call(@repository.storage, :operation_service, :user_delete_tag, request)
|
|
|
|
|
|
|
|
if pre_receive_error = response.pre_receive_error.presence
|
|
|
|
raise Gitlab::Git::HooksService::PreReceiveError, pre_receive_error
|
|
|
|
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),
|
|
|
|
message: encode_binary(message.to_s)
|
2017-09-25 16:01:04 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
response = GitalyClient.call(@repository.storage, :operation_service, :user_create_tag, request)
|
|
|
|
if pre_receive_error = response.pre_receive_error.presence
|
|
|
|
raise Gitlab::Git::HooksService::PreReceiveError, pre_receive_error
|
|
|
|
elsif response.exists
|
|
|
|
raise Gitlab::Git::Repository::TagExistsError
|
|
|
|
end
|
|
|
|
|
|
|
|
Util.gitlab_tag_from_gitaly_tag(@repository, response.tag)
|
|
|
|
rescue GRPC::FailedPrecondition => e
|
|
|
|
raise Gitlab::Git::Repository::InvalidRef, e
|
|
|
|
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,
|
|
|
|
:user_create_branch, request)
|
2018-01-11 11:34:01 -05:00
|
|
|
|
2017-09-20 18:34:30 -04:00
|
|
|
if response.pre_receive_error.present?
|
|
|
|
raise Gitlab::Git::HooksService::PreReceiveError.new(response.pre_receive_error)
|
|
|
|
end
|
|
|
|
|
|
|
|
branch = response.branch
|
|
|
|
return nil unless branch
|
|
|
|
|
|
|
|
target_commit = Gitlab::Git::Commit.decorate(@repository, branch.target_commit)
|
|
|
|
Gitlab::Git::Branch.new(@repository, branch.name, target_commit.id, target_commit)
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
response = GitalyClient.call(@repository.storage, :operation_service, :user_delete_branch, request)
|
|
|
|
|
|
|
|
if pre_receive_error = response.pre_receive_error.presence
|
|
|
|
raise Gitlab::Git::HooksService::PreReceiveError, pre_receive_error
|
|
|
|
end
|
|
|
|
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,
|
|
|
|
request_enum.each
|
|
|
|
)
|
|
|
|
|
|
|
|
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),
|
|
|
|
message: encode_binary(message)
|
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
|
|
|
|
|
|
|
|
if second_response.pre_receive_error.present?
|
|
|
|
raise Gitlab::Git::HooksService::PreReceiveError, second_response.pre_receive_error
|
|
|
|
end
|
|
|
|
|
|
|
|
branch_update = second_response.branch_update
|
2017-12-22 10:58:05 -05:00
|
|
|
return if branch_update.nil?
|
2017-10-10 08:15:21 -04:00
|
|
|
raise Gitlab::Git::CommitError.new('failed to apply merge to branch') unless branch_update.commit_id.present?
|
|
|
|
|
|
|
|
Gitlab::Git::OperationService::BranchUpdate.from_gitaly(branch_update)
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
branch_update = GitalyClient.call(
|
|
|
|
@repository.storage,
|
|
|
|
:operation_service,
|
|
|
|
:user_ff_branch,
|
|
|
|
request
|
|
|
|
).branch_update
|
|
|
|
Gitlab::Git::OperationService::BranchUpdate.from_gitaly(branch_update)
|
|
|
|
end
|
2017-11-21 08:47:52 -05:00
|
|
|
|
|
|
|
def user_cherry_pick(user:, commit:, branch_name:, message:, start_branch_name:, start_repository:)
|
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,
|
|
|
|
start_repository: start_repository)
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_revert(user:, commit:, branch_name:, message:, start_branch_name:, start_repository:)
|
|
|
|
call_cherry_pick_or_revert(:revert,
|
|
|
|
user: user,
|
|
|
|
commit: commit,
|
|
|
|
branch_name: branch_name,
|
|
|
|
message: message,
|
|
|
|
start_branch_name: start_branch_name,
|
|
|
|
start_repository: start_repository)
|
|
|
|
end
|
|
|
|
|
2018-01-03 08:04:58 -05:00
|
|
|
def user_rebase(user, rebase_id, branch:, branch_sha:, remote_repository:, remote_branch:)
|
|
|
|
request = Gitaly::UserRebaseRequest.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,
|
|
|
|
remote_branch: encode_binary(remote_branch)
|
|
|
|
)
|
|
|
|
|
|
|
|
response = GitalyClient.call(
|
|
|
|
@repository.storage,
|
|
|
|
:operation_service,
|
|
|
|
:user_rebase,
|
|
|
|
request,
|
|
|
|
remote_storage: remote_repository.storage
|
|
|
|
)
|
|
|
|
|
|
|
|
if response.pre_receive_error.presence
|
|
|
|
raise Gitlab::Git::HooksService::PreReceiveError, response.pre_receive_error
|
|
|
|
elsif response.git_error.presence
|
|
|
|
raise Gitlab::Git::Repository::GitError, response.git_error
|
|
|
|
else
|
|
|
|
response.rebase_sha
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-02 16:30:03 -05:00
|
|
|
def user_squash(user, squash_id, branch, start_sha, end_sha, author, message)
|
|
|
|
request = Gitaly::UserSquashRequest.new(
|
|
|
|
repository: @gitaly_repo,
|
|
|
|
user: Gitlab::Git::User.from_gitlab(user).to_gitaly,
|
|
|
|
squash_id: squash_id.to_s,
|
|
|
|
branch: encode_binary(branch),
|
|
|
|
start_sha: start_sha,
|
|
|
|
end_sha: end_sha,
|
|
|
|
author: Gitlab::Git::User.from_gitlab(author).to_gitaly,
|
|
|
|
commit_message: encode_binary(message)
|
|
|
|
)
|
|
|
|
|
|
|
|
response = GitalyClient.call(
|
|
|
|
@repository.storage,
|
|
|
|
:operation_service,
|
|
|
|
:user_squash,
|
|
|
|
request
|
|
|
|
)
|
|
|
|
|
|
|
|
if response.git_error.presence
|
|
|
|
raise Gitlab::Git::Repository::GitError, response.git_error
|
|
|
|
end
|
|
|
|
|
|
|
|
response.squash_sha
|
|
|
|
end
|
|
|
|
|
2018-01-08 22:51:05 -05:00
|
|
|
def user_commit_files(
|
|
|
|
user, branch_name, commit_message, actions, author_email, author_name,
|
|
|
|
start_branch_name, start_repository)
|
|
|
|
|
|
|
|
req_enum = Enumerator.new do |y|
|
|
|
|
header = user_commit_files_request_header(user, branch_name,
|
|
|
|
commit_message, actions, author_email, author_name,
|
|
|
|
start_branch_name, start_repository)
|
|
|
|
|
|
|
|
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)
|
|
|
|
)
|
|
|
|
|
|
|
|
reader = binary_stringio(action[:content])
|
|
|
|
|
|
|
|
until reader.eof?
|
|
|
|
chunk = reader.read(MAX_MSG_SIZE)
|
|
|
|
|
|
|
|
y.yield Gitaly::UserCommitFilesRequest.new(
|
|
|
|
action: Gitaly::UserCommitFilesAction.new(content: chunk)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
response = GitalyClient.call(@repository.storage, :operation_service,
|
|
|
|
:user_commit_files, req_enum, remote_storage: start_repository.storage)
|
|
|
|
|
|
|
|
if (pre_receive_error = response.pre_receive_error.presence)
|
|
|
|
raise Gitlab::Git::HooksService::PreReceiveError, pre_receive_error
|
|
|
|
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
|
|
|
|
|
2017-12-04 08:13:22 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def call_cherry_pick_or_revert(rpc, user:, commit:, branch_name:, message:, start_branch_name:, start_repository:)
|
|
|
|
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),
|
2017-11-21 08:47:52 -05:00
|
|
|
start_repository: start_repository.gitaly_repository
|
|
|
|
)
|
|
|
|
|
|
|
|
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,
|
|
|
|
remote_storage: start_repository.storage
|
|
|
|
)
|
|
|
|
|
2017-12-04 08:13:22 -05:00
|
|
|
handle_cherry_pick_or_revert_response(response)
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_cherry_pick_or_revert_response(response)
|
2017-11-21 08:47:52 -05:00
|
|
|
if response.pre_receive_error.presence
|
|
|
|
raise Gitlab::Git::HooksService::PreReceiveError, response.pre_receive_error
|
|
|
|
elsif response.commit_error.presence
|
|
|
|
raise Gitlab::Git::CommitError, response.commit_error
|
|
|
|
elsif response.create_tree_error.presence
|
|
|
|
raise Gitlab::Git::Repository::CreateTreeError, response.create_tree_error
|
|
|
|
else
|
|
|
|
Gitlab::Git::OperationService::BranchUpdate.from_gitaly(response.branch_update)
|
|
|
|
end
|
|
|
|
end
|
2018-01-08 22:51:05 -05:00
|
|
|
|
|
|
|
def user_commit_files_request_header(
|
|
|
|
user, branch_name, commit_message, actions, author_email, author_name,
|
|
|
|
start_branch_name, start_repository)
|
|
|
|
|
|
|
|
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),
|
|
|
|
start_repository: start_repository.gitaly_repository
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
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]),
|
|
|
|
base64_content: action[:encoding] == 'base64'
|
|
|
|
)
|
|
|
|
rescue RangeError
|
|
|
|
raise ArgumentError, "Unknown action '#{action[:action]}'"
|
|
|
|
end
|
2017-09-20 06:11:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|