BigW Consortium Gitlab

ref.rb 2.58 KB
Newer Older
1 2 3
module Gitlab
  module GitalyClient
    class Ref
4 5
      include Gitlab::EncodingHelper

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

      def default_branch_name
14
        request = Gitaly::FindDefaultBranchNameRequest.new(repository: @gitaly_repo)
15 16
        response = GitalyClient.call(@storage, :ref, :find_default_branch_name, request)
        Gitlab::Git.branch_name(response.name)
17 18 19
      end

      def branch_names
20
        request = Gitaly::FindAllBranchNamesRequest.new(repository: @gitaly_repo)
21
        response = GitalyClient.call(@storage, :ref, :find_all_branch_names, request)
22
        consume_refs_response(response) { |name| Gitlab::Git.branch_name(name) }
23 24 25
      end

      def tag_names
26
        request = Gitaly::FindAllTagNamesRequest.new(repository: @gitaly_repo)
27
        response = GitalyClient.call(@storage, :ref, :find_all_tag_names, request)
28
        consume_refs_response(response) { |name| Gitlab::Git.tag_name(name) }
29 30
      end

31
      def find_ref_name(commit_id, ref_prefix)
Jacob Vosmaer committed
32
        request = Gitaly::FindRefNameRequest.new(
33
          repository: @gitaly_repo,
34 35 36
          commit_id: commit_id,
          prefix: ref_prefix
        )
37
        GitalyClient.call(@storage, :ref, :find_ref_name, request).name
38 39
      end

40 41 42 43 44 45 46 47
      def count_tag_names
        tag_names.count
      end

      def count_branch_names
        branch_names.count
      end

48 49 50
      def local_branches(sort_by: nil)
        request = Gitaly::FindLocalBranchesRequest.new(repository: @gitaly_repo)
        request.sort_by = sort_by_param(sort_by) if sort_by
51 52
        response = GitalyClient.call(@storage, :ref, :find_local_branches, request)
        consume_branches_response(response)
53 54
      end

55 56
      private

57 58
      def consume_refs_response(response)
        response.flat_map { |message| message.names.map { |name| yield(name) } }
59
      end
60 61

      def sort_by_param(sort_by)
62 63
        sort_by = 'name' if sort_by == 'name_asc'

64 65 66 67 68 69
        enum_value = Gitaly::FindLocalBranchesRequest::SortBy.resolve(sort_by.upcase.to_sym)
        raise ArgumentError, "Invalid sort_by key `#{sort_by}`" unless enum_value
        enum_value
      end

      def consume_branches_response(response)
70 71 72 73 74 75 76 77 78
        response.flat_map do |message|
          message.branches.map do |gitaly_branch|
            Gitlab::Git::Branch.new(
              @repository,
              encode!(gitaly_branch.name.dup),
              gitaly_branch.commit_id
            )
          end
        end
79
      end
80 81 82
    end
  end
end