summaryrefslogtreecommitdiff
path: root/app/finders/repositories/tree_finder.rb
blob: 231c1de15132252226ef8d16c3baa6802fd926ee (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
# frozen_string_literal: true

module Repositories
  class TreeFinder
    CommitMissingError = Class.new(StandardError)

    def initialize(project, params = {})
      @project = project
      @repository = project.repository
      @params = params
    end

    def execute(gitaly_pagination: false)
      raise CommitMissingError unless commit_exists?

      request_params = { recursive: recursive }
      request_params[:pagination_params] = pagination_params if gitaly_pagination

      repository.tree(commit.id, path, **request_params).sorted_entries
    end

    def total
      # This is inefficient and we'll look at replacing this implementation
      cache_key = [project, repository.commit, :tree_size, commit.id, path, recursive]
      Gitlab::Cache.fetch_once(cache_key) do
        repository.tree(commit.id, path, recursive: recursive).entries.size
      end
    end

    def commit_exists?
      commit.present?
    end

    private

    attr_reader :project, :repository, :params

    def commit
      @commit ||= project.commit(ref)
    end

    def ref
      params[:ref] || project.default_branch
    end

    def path
      params[:path]
    end

    def recursive
      params[:recursive]
    end

    def pagination_params
      {
        limit: params[:per_page] || Kaminari.config.default_per_page,
        page_token: params[:page_token]
      }
    end
  end
end