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

tag.rb « git « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5b54ba472d9c8a22c8dd2c35e700c73ac86d06c4 (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
# frozen_string_literal: true

module Gitlab
  module Git
    class Tag < Ref
      extend Gitlab::EncodingHelper

      delegate :id, to: :@raw_tag

      attr_reader :object_sha, :repository

      MAX_TAG_MESSAGE_DISPLAY_SIZE = 10.megabytes
      SERIALIZE_KEYS = %i[name target target_commit message].freeze

      attr_accessor(*SERIALIZE_KEYS)

      class << self
        def get_message(repository, tag_id)
          BatchLoader.for(tag_id).batch(key: repository) do |tag_ids, loader, args|
            get_messages(args[:key], tag_ids).each do |tag_id, message|
              loader.call(tag_id, message)
            end
          end
        end

        def get_messages(repository, tag_ids)
          repository.gitaly_ref_client.get_tag_messages(tag_ids)
        end

        def extract_signature_lazily(repository, tag_id)
          BatchLoader.for(tag_id).batch(key: repository) do |tag_ids, loader, args|
            batch_signature_extraction(args[:key], tag_ids).each do |tag_id, signature_data|
              loader.call(tag_id, signature_data)
            end
          end
        end

        def batch_signature_extraction(repository, tag_ids)
          repository.gitaly_ref_client.get_tag_signatures(tag_ids)
        end
      end

      def initialize(repository, raw_tag)
        @repository = repository
        @raw_tag = raw_tag

        case raw_tag
        when Hash
          init_from_hash
        when Gitaly::Tag
          init_from_gitaly
        end

        super(repository, name, target, target_commit)
      end

      def init_from_hash
        raw_tag = @raw_tag.symbolize_keys

        SERIALIZE_KEYS.each do |key|
          send("#{key}=", raw_tag[key]) # rubocop:disable GitlabSecurity/PublicSend
        end
      end

      def init_from_gitaly
        @name = encode_utf8_with_escaping!(@raw_tag.name.dup)
        @target = @raw_tag.id
        @message = message_from_gitaly_tag

        if @raw_tag.target_commit.present?
          @target_commit = Gitlab::Git::Commit.decorate(repository, @raw_tag.target_commit)
        end
      end

      def message
        encode! @message
      end

      def user_name
        encode! tagger.name if tagger
      end

      def user_email
        encode! tagger.email if tagger
      end

      def date
        Time.at(tagger.date.seconds).utc if tagger
      end

      def has_signature?
        signature_type != :NONE
      end

      def signature_type
        @raw_tag.signature_type || :NONE
      end

      def signature
        return unless has_signature?

        case signature_type
        when :PGP
          nil # not implemented, see https://gitlab.com/gitlab-org/gitlab/issues/19260
        when :X509
          X509::Tag.new(@repository, self).signature
        end
      end

      def cache_key
        "tag:" + Digest::SHA1.hexdigest([name, message, target, target_commit&.sha].join)
      end

      private

      def tagger
        @raw_tag.tagger
      end

      def message_from_gitaly_tag
        return @raw_tag.message.dup if full_message_fetched_from_gitaly?

        if @raw_tag.message_size > MAX_TAG_MESSAGE_DISPLAY_SIZE
          '--tag message is too big'
        else
          self.class.get_message(@repository, target)
        end
      end

      def full_message_fetched_from_gitaly?
        @raw_tag.message.bytesize == @raw_tag.message_size
      end
    end
  end
end