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

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

module Gitlab
  module Git
    class Tree
      include Gitlab::EncodingHelper
      extend Gitlab::Git::WrapsGitalyErrors

      attr_accessor :id, :type, :mode, :commit_id, :submodule_url, :ref_type
      attr_writer :name, :path, :flat_path

      class << self
        # Get list of tree objects
        # for repository based on commit sha and path
        def tree_entries(
          repository:,
          sha:,
          path: nil,
          recursive: false,
          skip_flat_paths: true,
          rescue_not_found:  true,
          pagination_params: nil
        )
          path = nil if path == '' || path == '/'

          wrapped_gitaly_errors do
            repository.gitaly_commit_client.tree_entries(
              repository, sha, path, recursive, skip_flat_paths, pagination_params)
          end

        # Incorrect revision or path could lead to index error.
        # We silently handle such errors by returning an empty set of entries and cursor
        # unless the parameter rescue_not_found is set to false.
        rescue Gitlab::Git::Index::IndexError => e
          return [[], nil] if rescue_not_found

          raise e
        end

        private

        # Recursive search of tree id for path
        #
        # Ex.
        #   blog/            # oid: 1a
        #     app/           # oid: 2a
        #       models/      # oid: 3a
        #       views/       # oid: 4a
        #
        #
        # Tree.find_id_by_path(repo, '1a', 'app/models') # => '3a'
        #
        def find_id_by_path(repository, root_id, path)
          root_tree = repository.lookup(root_id)
          path_arr = path.split('/')

          entry = root_tree.find do |entry|
            entry[:name] == path_arr[0] && entry[:type] == :tree
          end

          return unless entry

          if path_arr.size > 1
            path_arr.shift
            find_id_by_path(repository, entry[:oid], path_arr.join('/'))
          else
            entry[:oid]
          end
        end
      end

      def initialize(options)
        %w[id name path flat_path type mode commit_id].each do |key|
          self.send("#{key}=", options[key.to_sym]) # rubocop:disable GitlabSecurity/PublicSend
        end
      end

      def name
        encode! @name
      end

      def path
        encode! @path
      end

      def flat_path
        encode! @flat_path
      end

      def dir?
        type == :tree
      end

      def file?
        type == :blob
      end

      def submodule?
        type == :commit
      end

      def readme?
        name =~ /^readme/i
      end

      def contributing?
        name =~ /^contributing/i
      end
    end
  end
end