2021-11-11 11:23:49 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Repositories
|
2023-03-04 22:38:38 +05:30
|
|
|
class TreeFinder
|
2021-11-11 11:23:49 +05:30
|
|
|
CommitMissingError = Class.new(StandardError)
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
def initialize(project, params = {})
|
|
|
|
@project = project
|
|
|
|
@repository = project.repository
|
|
|
|
@params = params
|
2021-11-11 11:23:49 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def execute(gitaly_pagination: false)
|
|
|
|
raise CommitMissingError unless commit_exists?
|
|
|
|
|
|
|
|
request_params = { recursive: recursive }
|
|
|
|
request_params[:pagination_params] = pagination_params if gitaly_pagination
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
repository.tree(commit.id, path, **request_params).sorted_entries
|
2021-11-11 11:23:49 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def total
|
|
|
|
# This is inefficient and we'll look at replacing this implementation
|
2023-03-04 22:38:38 +05:30
|
|
|
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
|
2021-11-11 11:23:49 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit_exists?
|
|
|
|
commit.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
attr_reader :project, :repository, :params
|
|
|
|
|
2021-11-11 11:23:49 +05:30
|
|
|
def commit
|
2023-03-04 22:38:38 +05:30
|
|
|
@commit ||= project.commit(ref)
|
2021-11-11 11:23:49 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def ref
|
2023-03-04 22:38:38 +05:30
|
|
|
params[:ref] || project.default_branch
|
2021-11-11 11:23:49 +05:30
|
|
|
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
|