Welcome to mirror list, hosted at ThFree Co, Russian Federation.

operations_service.rb « gitaly_server « lib « ruby - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2d26ef15ceee18fdc7d9ef7c1cecb223b148033d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
module GitalyServer
  class OperationsService < Gitaly::OperationService::Service
    include Utils

    def user_create_tag(request, call)
      repo = Gitlab::Git::Repository.from_gitaly(request.repository, call)

      gitaly_user = get_param!(request, :user)
      user = Gitlab::Git::User.from_gitaly(gitaly_user)

      tag_name = get_param!(request, :tag_name)

      target_revision = get_param!(request, :target_revision)

      created_tag = repo.add_tag(tag_name, user: user, target: target_revision, message: request.message.presence)
      Gitaly::UserCreateTagResponse.new unless created_tag

      rugged_commit = created_tag.dereferenced_target.rugged_commit
      commit = gitaly_commit_from_rugged(rugged_commit)
      tag = gitaly_tag_from_gitlab_tag(created_tag, commit)

      Gitaly::UserCreateTagResponse.new(tag: tag)
    rescue Gitlab::Git::Repository::InvalidRef => e
      raise GRPC::FailedPrecondition.new(e.message)
    rescue Gitlab::Git::Repository::TagExistsError
      Gitaly::UserCreateTagResponse.new(exists: true)
    rescue Gitlab::Git::PreReceiveError => e
      Gitaly::UserCreateTagResponse.new(pre_receive_error: set_utf8!(e.message))
    end

    def user_delete_tag(request, call)
      repo = Gitlab::Git::Repository.from_gitaly(request.repository, call)

      gitaly_user = get_param!(request, :user)
      user = Gitlab::Git::User.from_gitaly(gitaly_user)

      tag_name = get_param!(request, :tag_name)

      repo.rm_tag(tag_name, user: user)

      Gitaly::UserDeleteTagResponse.new
    rescue Gitlab::Git::PreReceiveError => e
      Gitaly::UserDeleteTagResponse.new(pre_receive_error: set_utf8!(e.message))
    rescue Gitlab::Git::Repository::InvalidRef, Gitlab::Git::CommitError => e
      raise GRPC::FailedPrecondition.new(e.message)
    end

    def user_create_branch(request, call)
      repo = Gitlab::Git::Repository.from_gitaly(request.repository, call)
      target = get_param!(request, :start_point)
      gitaly_user = get_param!(request, :user)

      branch_name = request.branch_name
      user = Gitlab::Git::User.from_gitaly(gitaly_user)
      created_branch = repo.add_branch(branch_name, user: user, target: target)
      Gitaly::UserCreateBranchResponse.new unless created_branch

      rugged_commit = created_branch.dereferenced_target.rugged_commit
      commit = gitaly_commit_from_rugged(rugged_commit)
      branch = Gitaly::Branch.new(name: branch_name, target_commit: commit)
      Gitaly::UserCreateBranchResponse.new(branch: branch)
    rescue Gitlab::Git::Repository::InvalidRef, Gitlab::Git::CommitError => ex
      raise GRPC::FailedPrecondition.new(ex.message)
    rescue Gitlab::Git::PreReceiveError => ex
      Gitaly::UserCreateBranchResponse.new(pre_receive_error: set_utf8!(ex.message))
    end

    def user_update_branch(request, call)
      repo = Gitlab::Git::Repository.from_gitaly(request.repository, call)
      branch_name = get_param!(request, :branch_name)
      newrev = get_param!(request, :newrev)
      oldrev = get_param!(request, :oldrev)
      gitaly_user = get_param!(request, :user)

      user = Gitlab::Git::User.from_gitaly(gitaly_user)
      repo.update_branch(branch_name, user: user, newrev: newrev, oldrev: oldrev)

      Gitaly::UserUpdateBranchResponse.new
    rescue Gitlab::Git::Repository::InvalidRef, Gitlab::Git::CommitError => ex
      raise GRPC::FailedPrecondition.new(ex.message)
    rescue Gitlab::Git::PreReceiveError => ex
      Gitaly::UserUpdateBranchResponse.new(pre_receive_error: set_utf8!(ex.message))
    end

    def user_delete_branch(request, call)
      repo = Gitlab::Git::Repository.from_gitaly(request.repository, call)
      user = Gitlab::Git::User.from_gitaly(request.user)

      repo.rm_branch(request.branch_name, user: user)

      Gitaly::UserDeleteBranchResponse.new
    rescue Gitlab::Git::PreReceiveError => e
      Gitaly::UserDeleteBranchResponse.new(pre_receive_error: set_utf8!(e.message))
    rescue Gitlab::Git::Repository::InvalidRef, Gitlab::Git::CommitError => e
      raise GRPC::FailedPrecondition.new(e.message)
    end

    def user_merge_to_ref(request, call)
      repo = Gitlab::Git::Repository.from_gitaly(request.repository, call)
      user = Gitlab::Git::User.from_gitaly(request.user)

      commit_id = repo.merge_to_ref(user, request.source_sha, request.branch, request.target_ref, request.message.dup, request.first_parent_ref)

      Gitaly::UserMergeToRefResponse.new(commit_id: commit_id)
    rescue Gitlab::Git::CommitError => e
      raise GRPC::FailedPrecondition.new(e.to_s)
    rescue ArgumentError => e
      raise GRPC::InvalidArgument.new(e.to_s)
    rescue Gitlab::Git::PreReceiveError => e
      Gitaly::UserMergeToRefResponse.new(pre_receive_error: set_utf8!(e.message))
    end

    def user_merge_branch(session, call)
      Enumerator.new do |y|
        first_request = session.next

        repository = Gitlab::Git::Repository.from_gitaly(first_request.repository, call)
        user = Gitlab::Git::User.from_gitaly(first_request.user)
        source_sha = first_request.commit_id.dup
        target_branch = first_request.branch.dup
        message = first_request.message.dup

        begin
          result = repository.merge(user, source_sha, target_branch, message) do |commit_id|
            y << Gitaly::UserMergeBranchResponse.new(commit_id: commit_id)

            second_request = session.next
            raise GRPC::FailedPrecondition.new('merge aborted by client') unless second_request.apply
          end

          y << Gitaly::UserMergeBranchResponse.new(branch_update: branch_update_result(result))
        rescue Gitlab::Git::PreReceiveError => e
          y << Gitaly::UserMergeBranchResponse.new(pre_receive_error: set_utf8!(e.message))
        end
      end
    end

    def user_ff_branch(request, call)
      repo = Gitlab::Git::Repository.from_gitaly(request.repository, call)
      user = Gitlab::Git::User.from_gitaly(request.user)

      result = repo.ff_merge(user, request.commit_id, request.branch)
      branch_update = branch_update_result(result)

      Gitaly::UserFFBranchResponse.new(branch_update: branch_update)
    rescue Gitlab::Git::CommitError => e
      raise GRPC::FailedPrecondition.new(e.to_s)
    rescue ArgumentError => e
      raise GRPC::InvalidArgument.new(e.to_s)
    rescue Gitlab::Git::PreReceiveError => e
      Gitaly::UserFFBranchResponse.new(pre_receive_error: set_utf8!(e.message))
    end

    def user_cherry_pick(request, call)
      repo = Gitlab::Git::Repository.from_gitaly(request.repository, call)
      user = Gitlab::Git::User.from_gitaly(request.user)
      commit = Gitlab::Git::Commit.new(repo, request.commit)
      start_repository = Gitlab::Git::GitalyRemoteRepository.new(request.start_repository || request.repository, call)

      result = repo.cherry_pick(
        user: user,
        commit: commit,
        branch_name: request.branch_name,
        message: request.message.dup,
        start_branch_name: request.start_branch_name.presence,
        start_repository: start_repository
      )

      branch_update = branch_update_result(result)
      Gitaly::UserCherryPickResponse.new(branch_update: branch_update)
    rescue Gitlab::Git::Repository::CreateTreeError => e
      Gitaly::UserCherryPickResponse.new(
        create_tree_error: set_utf8!(e.message),
        create_tree_error_code: e.error.upcase
      )
    rescue Gitlab::Git::CommitError => e
      Gitaly::UserCherryPickResponse.new(commit_error: set_utf8!(e.message))
    rescue Gitlab::Git::PreReceiveError => e
      Gitaly::UserCherryPickResponse.new(pre_receive_error: set_utf8!(e.message))
    end

    def user_revert(request, call)
      repo = Gitlab::Git::Repository.from_gitaly(request.repository, call)
      user = Gitlab::Git::User.from_gitaly(request.user)
      commit = Gitlab::Git::Commit.new(repo, request.commit)
      start_repository = Gitlab::Git::GitalyRemoteRepository.new(request.start_repository || request.repository, call)

      result = repo.revert(
        user: user,
        commit: commit,
        branch_name: request.branch_name,
        message: request.message.dup,
        start_branch_name: request.start_branch_name.presence,
        start_repository: start_repository
      )

      branch_update = branch_update_result(result)
      Gitaly::UserRevertResponse.new(branch_update: branch_update)
    rescue Gitlab::Git::Repository::CreateTreeError => e
      Gitaly::UserRevertResponse.new(
        create_tree_error: set_utf8!(e.message),
        create_tree_error_code: e.error.upcase
      )
    rescue Gitlab::Git::CommitError => e
      Gitaly::UserRevertResponse.new(commit_error: set_utf8!(e.message))
    rescue Gitlab::Git::PreReceiveError => e
      Gitaly::UserRevertResponse.new(pre_receive_error: set_utf8!(e.message))
    end

    def user_rebase_confirmable(session, call)
      Enumerator.new do |y|
        header = session.next.header

        repo = Gitlab::Git::Repository.from_gitaly(header.repository, call)
        user = Gitlab::Git::User.from_gitaly(header.user)
        remote_repository = Gitlab::Git::GitalyRemoteRepository.new(header.remote_repository, call)

        begin
          repo.rebase(
            user,
            header.rebase_id,
            branch: header.branch,
            branch_sha: header.branch_sha,
            remote_repository: remote_repository,
            remote_branch: header.remote_branch,
            push_options: Gitlab::Git::PushOptions.new(header.git_push_options)
          ) do |rebase_sha|
            y << Gitaly::UserRebaseConfirmableResponse.new(rebase_sha: rebase_sha)

            raise GRPC::FailedPrecondition.new('rebase aborted by client') unless session.next.apply
          end

          y << Gitaly::UserRebaseConfirmableResponse.new(rebase_applied: true)
        rescue Gitlab::Git::PreReceiveError => e
          y << Gitaly::UserRebaseConfirmableResponse.new(pre_receive_error: set_utf8!(e.message))
        rescue Gitlab::Git::Repository::GitError => e
          y << Gitaly::UserRebaseConfirmableResponse.new(git_error: set_utf8!(e.message))
        rescue Gitlab::Git::CommitError => e
          raise GRPC::FailedPrecondition.new(e.message)
        end
      end
    end

    # DEPRECATED: https://gitlab.com/gitlab-org/gitaly/issues/1628
    def user_rebase(request, call)
      repo = Gitlab::Git::Repository.from_gitaly(request.repository, call)
      user = Gitlab::Git::User.from_gitaly(request.user)
      remote_repository = Gitlab::Git::GitalyRemoteRepository.new(request.remote_repository, call)
      rebase_sha = repo.rebase(user, request.rebase_id,
                               branch: request.branch,
                               branch_sha: request.branch_sha,
                               remote_repository: remote_repository,
                               remote_branch: request.remote_branch)

      Gitaly::UserRebaseResponse.new(rebase_sha: rebase_sha)
    rescue Gitlab::Git::PreReceiveError => e
      Gitaly::UserRebaseResponse.new(pre_receive_error: set_utf8!(e.message))
    rescue Gitlab::Git::Repository::GitError => e
      Gitaly::UserRebaseResponse.new(git_error: set_utf8!(e.message))
    rescue Gitlab::Git::CommitError => e
      raise GRPC::FailedPrecondition.new(e.message)
    end

    def user_commit_files(call)
      actions = []
      request_enum = call.each_remote_read
      header = request_enum.next.header

      loop do
        action = request_enum.next.action

        if action.header
          actions << commit_files_action_from_gitaly_request(action.header)
        else
          actions.last[:content] << action.content
        end
      end

      repo = Gitlab::Git::Repository.from_gitaly(header.repository, call)
      user = Gitlab::Git::User.from_gitaly(header.user)
      opts = commit_files_opts(call, header, actions)

      branch_update = branch_update_result(repo.multi_action(user, opts))

      Gitaly::UserCommitFilesResponse.new(branch_update: branch_update)
    rescue Gitlab::Git::Index::IndexError => e
      Gitaly::UserCommitFilesResponse.new(index_error: set_utf8!(e.message))
    rescue Gitlab::Git::PreReceiveError => e
      Gitaly::UserCommitFilesResponse.new(pre_receive_error: set_utf8!(e.message))
    rescue Gitlab::Git::CommitError => e
      raise GRPC::FailedPrecondition.new(e.message)
    rescue ArgumentError => e
      raise GRPC::InvalidArgument.new(e.message)
    end

    def user_squash(request, call)
      repo = Gitlab::Git::Repository.from_gitaly(request.repository, call)
      user = Gitlab::Git::User.from_gitaly(request.user)
      author = Gitlab::Git::User.from_gitaly(request.author)

      squash_sha = repo.squash(user, request.squash_id,
                               start_sha: request.start_sha,
                               end_sha: request.end_sha,
                               author: author,
                               message: request.commit_message)

      Gitaly::UserSquashResponse.new(squash_sha: squash_sha)
    rescue Gitlab::Git::Repository::GitError => e
      Gitaly::UserSquashResponse.new(git_error: set_utf8!(e.message))
    end

    def user_apply_patch(call)
      stream = call.each_remote_read
      first_request = stream.next

      header = first_request.header
      user = Gitlab::Git::User.from_gitaly(header.user)
      target_branch = header.target_branch
      patches = stream.lazy.map(&:patches)

      branch_update = Gitlab::Git::Repository.from_gitaly_with_block(header.repository, call) do |repo|
        begin
          Gitlab::Git::CommitPatches.new(user, repo, target_branch, patches).commit
        rescue Gitlab::Git::PatchError => e
          raise GRPC::FailedPrecondition.new(e.message)
        end
      end

      Gitaly::UserApplyPatchResponse.new(branch_update: branch_update_result(branch_update))
    end

    def user_update_submodule(request, call)
      user = Gitlab::Git::User.from_gitaly(request.user)

      branch_update = Gitlab::Git::Repository.from_gitaly_with_block(request.repository, call) do |repo|
        begin
          Gitlab::Git::Submodule
            .new(user, repo, request.submodule, request.branch)
            .update(request.commit_sha, request.commit_message.dup)
        rescue ArgumentError => e
          raise GRPC::InvalidArgument.new(e.to_s)
        end
      end

      Gitaly::UserUpdateSubmoduleResponse.new(branch_update: branch_update_result(branch_update))
    rescue Gitlab::Git::CommitError => e
      Gitaly::UserUpdateSubmoduleResponse.new(commit_error: set_utf8!(e.message))
    rescue Gitlab::Git::PreReceiveError => e
      Gitaly::UserUpdateSubmoduleResponse.new(pre_receive_error: set_utf8!(e.message))
    end

    private

    def commit_files_opts(call, header, actions)
      opts = {
        branch_name: header.branch_name,
        message: header.commit_message.b,
        actions: actions
      }

      opts[:start_repository] = Gitlab::Git::GitalyRemoteRepository.new(header.start_repository, call) if header.start_repository

      optional_fields = {
        start_branch_name: 'start_branch_name',
        start_sha: 'start_sha',
        author_name: 'commit_author_name',
        author_email: 'commit_author_email',
        force: 'force'
      }.transform_values { |v| header[v].presence }

      opts.merge(optional_fields)
    end

    def commit_files_action_from_gitaly_request(header)
      {
        action: header.action.downcase,
        # Forcing the encoding to UTF-8 here is unusual. But these paths get
        # compared with Rugged::Index entries, which are also force-encoded to
        # UTF-8. See
        # https://github.com/libgit2/rugged/blob/f8172c2a177a6795553f38f01248daff923f4264/ext/rugged/rugged_index.c#L514
        file_path: set_utf8!(header.file_path),
        previous_path: set_utf8!(header.previous_path),
        encoding: header.base64_content ? 'base64' : '',
        content: '',
        infer_content: header.infer_content,
        execute_filemode: header.execute_filemode
      }
    end

    def branch_update_result(gitlab_update_result)
      return if gitlab_update_result.nil?

      Gitaly::OperationBranchUpdate.new(
        commit_id: gitlab_update_result.newrev,
        repo_created: gitlab_update_result.repo_created,
        branch_created: gitlab_update_result.branch_created
      )
    end

    def get_param!(request, name)
      value = request[name.to_s]

      return value if value.present?

      field_name = name.to_s.tr('_', ' ')
      raise GRPC::InvalidArgument.new("empty #{field_name}")
    end
  end
end