ref_service.rb 12.4 KB
Newer Older
1 2
# frozen_string_literal: true

3 4
module Gitlab
  module GitalyClient
Andrew Newdigate's avatar
Andrew Newdigate committed
5
    class RefService
6 7
      include Gitlab::EncodingHelper

8 9
      # 'repository' is a Gitlab::Git::Repository
      def initialize(repository)
10
        @repository = repository
11
        @gitaly_repo = repository.gitaly_repository
12
        @storage = repository.storage
13 14
      end

15 16
      def branches
        request = Gitaly::FindAllBranchesRequest.new(repository: @gitaly_repo)
17
        response = GitalyClient.call(@storage, :ref_service, :find_all_branches, request, timeout: GitalyClient.fast_timeout)
18

19 20 21
        consume_find_all_branches_response(response)
      end

22 23 24 25 26 27 28
      def remote_branches(remote_name)
        request = Gitaly::FindAllRemoteBranchesRequest.new(repository: @gitaly_repo, remote_name: remote_name)
        response = GitalyClient.call(@repository.storage, :ref_service, :find_all_remote_branches, request)

        consume_find_all_remote_branches_response(remote_name, response)
      end

29 30 31 32 33 34
      def merged_branches(branch_names = [])
        request = Gitaly::FindAllBranchesRequest.new(
          repository: @gitaly_repo,
          merged_only: true,
          merged_branches: branch_names.map { |s| encode_binary(s) }
        )
35
        response = GitalyClient.call(@storage, :ref_service, :find_all_branches, request, timeout: GitalyClient.fast_timeout)
36 37

        consume_find_all_branches_response(response)
38 39
      end

40
      def default_branch_name
41
        request = Gitaly::FindDefaultBranchNameRequest.new(repository: @gitaly_repo)
42
        response = GitalyClient.call(@storage, :ref_service, :find_default_branch_name, request, timeout: GitalyClient.fast_timeout)
43
        Gitlab::Git.branch_name(response.name)
44 45 46
      end

      def branch_names
47
        request = Gitaly::FindAllBranchNamesRequest.new(repository: @gitaly_repo)
48
        response = GitalyClient.call(@storage, :ref_service, :find_all_branch_names, request, timeout: GitalyClient.fast_timeout)
49
        consume_refs_response(response) { |name| Gitlab::Git.branch_name(name) }
50 51 52
      end

      def tag_names
53
        request = Gitaly::FindAllTagNamesRequest.new(repository: @gitaly_repo)
54
        response = GitalyClient.call(@storage, :ref_service, :find_all_tag_names, request, timeout: GitalyClient.fast_timeout)
55
        consume_refs_response(response) { |name| Gitlab::Git.tag_name(name) }
56 57
      end

58
      def find_ref_name(commit_id, ref_prefix)
Jacob Vosmaer's avatar
Jacob Vosmaer committed
59
        request = Gitaly::FindRefNameRequest.new(
60
          repository: @gitaly_repo,
61 62 63
          commit_id: commit_id,
          prefix: ref_prefix
        )
64 65
        response = GitalyClient.call(@storage, :ref_service, :find_ref_name, request, timeout: GitalyClient.medium_timeout)
        encode!(response.name.dup)
66 67
      end

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
      def list_new_commits(newrev)
        request = Gitaly::ListNewCommitsRequest.new(
          repository: @gitaly_repo,
          commit_id: newrev
        )

        response = GitalyClient
          .call(@storage, :ref_service, :list_new_commits, request, timeout: GitalyClient.medium_timeout)

        commits = []
        response.each do |msg|
          msg.commits.each do |c|
            commits << Gitlab::Git::Commit.new(@repository, c)
          end
        end

        commits
      end

87
      def list_new_blobs(newrev, limit = 0, dynamic_timeout: nil)
88 89 90 91 92 93
        request = Gitaly::ListNewBlobsRequest.new(
          repository: @gitaly_repo,
          commit_id: newrev,
          limit: limit
        )

94 95 96 97 98 99 100
        timeout =
          if dynamic_timeout
            [dynamic_timeout, GitalyClient.medium_timeout].min
          else
            GitalyClient.medium_timeout
          end

101
        response = GitalyClient
102
          .call(@storage, :ref_service, :list_new_blobs, request, timeout: timeout)
103 104 105 106 107 108 109 110

        response.flat_map do |msg|
          # Returns an Array of Gitaly::NewBlobObject objects
          # Available methods are: #size, #oid and #path
          msg.new_blob_objects
        end
      end

111 112 113 114 115 116 117 118
      def count_tag_names
        tag_names.count
      end

      def count_branch_names
        branch_names.count
      end

119 120 121
      def local_branches(sort_by: nil)
        request = Gitaly::FindLocalBranchesRequest.new(repository: @gitaly_repo)
        request.sort_by = sort_by_param(sort_by) if sort_by
122
        response = GitalyClient.call(@storage, :ref_service, :find_local_branches, request, timeout: GitalyClient.fast_timeout)
123
        consume_find_local_branches_response(response)
124 125
      end

126 127
      def tags
        request = Gitaly::FindAllTagsRequest.new(repository: @gitaly_repo)
128
        response = GitalyClient.call(@storage, :ref_service, :find_all_tags, request, timeout: GitalyClient.medium_timeout)
129 130 131
        consume_tags_response(response)
      end

132
      def ref_exists?(ref_name)
133
        request = Gitaly::RefExistsRequest.new(repository: @gitaly_repo, ref: encode_binary(ref_name))
134
        response = GitalyClient.call(@storage, :ref_service, :ref_exists, request, timeout: GitalyClient.fast_timeout)
135 136 137 138 139
        response.value
      rescue GRPC::InvalidArgument => e
        raise ArgumentError, e.message
      end

140
      def find_branch(branch_name)
141
        request = Gitaly::FindBranchRequest.new(
142
          repository: @gitaly_repo,
143
          name: encode_binary(branch_name)
144 145
        )

146
        response = GitalyClient.call(@repository.storage, :ref_service, :find_branch, request, timeout: GitalyClient.medium_timeout)
147 148 149 150 151 152 153
        branch = response.branch
        return unless branch

        target_commit = Gitlab::Git::Commit.decorate(@repository, branch.target_commit)
        Gitlab::Git::Branch.new(@repository, encode!(branch.name.dup), branch.target_commit.id, target_commit)
      end

154 155 156
      def create_branch(ref, start_point)
        request = Gitaly::CreateBranchRequest.new(
          repository: @gitaly_repo,
157 158
          name: encode_binary(ref),
          start_point: encode_binary(start_point)
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
        )

        response = GitalyClient.call(@repository.storage, :ref_service, :create_branch, request)

        case response.status
        when :OK
          branch = response.branch
          target_commit = Gitlab::Git::Commit.decorate(@repository, branch.target_commit)
          Gitlab::Git::Branch.new(@repository, branch.name, branch.target_commit.id, target_commit)
        when :ERR_INVALID
          invalid_ref!("Invalid ref name")
        when :ERR_EXISTS
          invalid_ref!("Branch #{ref} already exists")
        when :ERR_INVALID_START_POINT
          invalid_ref!("Invalid reference #{start_point}")
        else
          raise "Unknown response status: #{response.status}"
        end
      end

      def delete_branch(branch_name)
        request = Gitaly::DeleteBranchRequest.new(
          repository: @gitaly_repo,
182
          name: encode_binary(branch_name)
183 184 185 186 187
        )

        GitalyClient.call(@repository.storage, :ref_service, :delete_branch, request)
      end

188
      def delete_refs(refs: [], except_with_prefixes: [])
189 190
        request = Gitaly::DeleteRefsRequest.new(
          repository: @gitaly_repo,
191 192
          refs: refs.map { |r| encode_binary(r) },
          except_with_prefix: except_with_prefixes.map { |r| encode_binary(r) }
193 194
        )

195
        response = GitalyClient.call(@repository.storage, :ref_service, :delete_refs, request, timeout: GitalyClient.default_timeout)
196 197

        raise Gitlab::Git::Repository::GitError, response.git_error if response.git_error.present?
198 199
      end

200
      # Limit: 0 implies no limit, thus all tag names will be returned
201
      def tag_names_contains_sha(sha, limit: 0)
202 203 204 205 206 207
        request = Gitaly::ListTagNamesContainingCommitRequest.new(
          repository: @gitaly_repo,
          commit_id: sha,
          limit: limit
        )

208
        stream = GitalyClient.call(@repository.storage, :ref_service, :list_tag_names_containing_commit, request, timeout: GitalyClient.medium_timeout)
209

210
        consume_ref_contains_sha_response(stream, :tag_names)
211 212 213 214 215 216 217 218 219 220
      end

      # Limit: 0 implies no limit, thus all tag names will be returned
      def branch_names_contains_sha(sha, limit: 0)
        request = Gitaly::ListBranchNamesContainingCommitRequest.new(
          repository: @gitaly_repo,
          commit_id: sha,
          limit: limit
        )

221
        stream = GitalyClient.call(@repository.storage, :ref_service, :list_branch_names_containing_commit, request, timeout: GitalyClient.medium_timeout)
222

223
        consume_ref_contains_sha_response(stream, :branch_names)
224 225
      end

226 227
      def get_tag_messages(tag_ids)
        request = Gitaly::GetTagMessagesRequest.new(repository: @gitaly_repo, tag_ids: tag_ids)
228
        response = GitalyClient.call(@repository.storage, :ref_service, :get_tag_messages, request, timeout: GitalyClient.fast_timeout)
229

230
        messages = Hash.new { |h, k| h[k] = +''.b }
231 232 233 234 235 236 237 238 239 240 241
        current_tag_id = nil

        response.each do |rpc_message|
          current_tag_id = rpc_message.tag_id if rpc_message.tag_id.present?

          messages[current_tag_id] << rpc_message.message
        end

        messages
      end

242 243
      private

244 245
      def consume_refs_response(response)
        response.flat_map { |message| message.names.map { |name| yield(name) } }
246
      end
247 248

      def sort_by_param(sort_by)
249 250
        sort_by = 'name' if sort_by == 'name_asc'

251 252
        enum_value = Gitaly::FindLocalBranchesRequest::SortBy.resolve(sort_by.upcase.to_sym)
        raise ArgumentError, "Invalid sort_by key `#{sort_by}`" unless enum_value
253

254 255 256
        enum_value
      end

257
      def consume_find_local_branches_response(response)
258 259 260 261 262
        response.flat_map do |message|
          message.branches.map do |gitaly_branch|
            Gitlab::Git::Branch.new(
              @repository,
              encode!(gitaly_branch.name.dup),
263 264
              gitaly_branch.commit_id,
              commit_from_local_branches_response(gitaly_branch)
265 266 267
            )
          end
        end
268
      end
269

270 271 272 273 274 275 276 277 278
      def consume_find_all_branches_response(response)
        response.flat_map do |message|
          message.branches.map do |branch|
            target_commit = Gitlab::Git::Commit.decorate(@repository, branch.target)
            Gitlab::Git::Branch.new(@repository, branch.name, branch.target.id, target_commit)
          end
        end
      end

279 280 281 282 283 284 285 286 287 288 289 290
      def consume_find_all_remote_branches_response(remote_name, response)
        remote_name += '/' unless remote_name.ends_with?('/')

        response.flat_map do |message|
          message.branches.map do |branch|
            target_commit = Gitlab::Git::Commit.decorate(@repository, branch.target_commit)
            branch_name = branch.name.sub(remote_name, '')
            Gitlab::Git::Branch.new(@repository, branch_name, branch.target_commit.id, target_commit)
          end
        end
      end

291 292
      def consume_tags_response(response)
        response.flat_map do |message|
293
          message.tags.map { |gitaly_tag| Gitlab::Git::Tag.new(@repository, gitaly_tag) }
294 295 296
        end
      end

297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
      def commit_from_local_branches_response(response)
        # Git messages have no encoding enforcements. However, in the UI we only
        # handle UTF-8, so basically we cross our fingers that the message force
        # encoded to UTF-8 is readable.
        message = response.commit_subject.dup.force_encoding('UTF-8')

        # NOTE: For ease of parsing in Gitaly, we have only the subject of
        # the commit and not the full message. This is ok, since all the
        # code that uses `local_branches` only cares at most about the
        # commit message.
        # TODO: Once gitaly "takes over" Rugged consider separating the
        # subject from the message to make it clearer when there's one
        # available but not the other.
        hash = {
          id: response.commit_id,
          message: message,
          authored_date: Time.at(response.commit_author.date.seconds),
314 315
          author_name: response.commit_author.name.dup,
          author_email: response.commit_author.email.dup,
316
          committed_date: Time.at(response.commit_committer.date.seconds),
317 318
          committer_name: response.commit_committer.name.dup,
          committer_email: response.commit_committer.email.dup
319 320
        }

321
        Gitlab::Git::Commit.decorate(@repository, hash)
322
      end
323

324 325 326 327 328 329 330
      def consume_ref_contains_sha_response(stream, collection_name)
        stream.each_with_object([]) do |response, array|
          encoded_names = response.send(collection_name).map { |b| Gitlab::Git.ref_name(b) } # rubocop:disable GitlabSecurity/PublicSend
          array.concat(encoded_names)
        end
      end

331 332 333
      def invalid_ref!(message)
        raise Gitlab::Git::Repository::InvalidRef.new(message)
      end
334 335 336
    end
  end
end