2020-03-13 15:44:24 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# This concern is created to handle repository actions.
|
|
|
|
# It should be include inside any object capable
|
|
|
|
# of directly having a repository, like project or snippet.
|
|
|
|
#
|
|
|
|
# It also includes `Referable`, therefore the method
|
2020-07-28 23:09:34 +05:30
|
|
|
# `to_reference` should be overridden in case the object
|
2020-03-13 15:44:24 +05:30
|
|
|
# needs any special behavior.
|
|
|
|
module HasRepository
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
include Referable
|
2020-04-22 19:07:51 +05:30
|
|
|
include Gitlab::ShellAdapter
|
2020-03-13 15:44:24 +05:30
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
delegate :base_dir, :disk_path, to: :storage
|
|
|
|
|
2020-04-22 19:07:51 +05:30
|
|
|
class_methods do
|
|
|
|
def pick_repository_storage
|
|
|
|
# We need to ensure application settings are fresh when we pick
|
|
|
|
# a repository storage to use.
|
|
|
|
Gitlab::CurrentSettings.expire_current_application_settings
|
|
|
|
Gitlab::CurrentSettings.pick_repository_storage
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def valid_repo?
|
|
|
|
repository.exists?
|
|
|
|
rescue
|
2020-04-08 14:13:33 +05:30
|
|
|
errors.add(:base, _('Invalid repository path'))
|
2020-03-13 15:44:24 +05:30
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def repo_exists?
|
|
|
|
strong_memoize(:repo_exists) do
|
|
|
|
repository.exists?
|
|
|
|
rescue
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def repository_exists?
|
|
|
|
!!repository.exists?
|
|
|
|
end
|
|
|
|
|
|
|
|
def root_ref?(branch)
|
|
|
|
repository.root_ref == branch
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit(ref = 'HEAD')
|
|
|
|
repository.commit(ref)
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit_by(oid:)
|
|
|
|
repository.commit_by(oid: oid)
|
|
|
|
end
|
|
|
|
|
|
|
|
def commits_by(oids:)
|
|
|
|
repository.commits_by(oids: oids)
|
|
|
|
end
|
|
|
|
|
|
|
|
def repository
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def storage
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
|
|
|
def full_path
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def lfs_enabled?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def empty_repo?
|
|
|
|
repository.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_branch
|
2020-07-28 23:09:34 +05:30
|
|
|
@default_branch ||= repository.root_ref || default_branch_from_preferences
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_branch_from_preferences
|
2021-01-03 14:25:43 +05:30
|
|
|
return unless empty_repo?
|
|
|
|
|
|
|
|
group_branch_default_name = group&.default_branch_name if respond_to?(:group)
|
|
|
|
|
|
|
|
group_branch_default_name || Gitlab::CurrentSettings.default_branch_name
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def reload_default_branch
|
|
|
|
@default_branch = nil # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
|
|
|
|
|
|
|
default_branch
|
|
|
|
end
|
|
|
|
|
|
|
|
def url_to_repo
|
2020-04-22 19:07:51 +05:30
|
|
|
ssh_url_to_repo
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def ssh_url_to_repo
|
2020-04-22 19:07:51 +05:30
|
|
|
Gitlab::RepositoryUrlBuilder.build(repository.full_path, protocol: :ssh)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def http_url_to_repo
|
2020-04-22 19:07:51 +05:30
|
|
|
Gitlab::RepositoryUrlBuilder.build(repository.full_path, protocol: :http)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
# Is overridden in EE::Project for Geo support
|
|
|
|
def lfs_http_url_to_repo(_operation = nil)
|
|
|
|
http_url_to_repo
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
def web_url(only_path: nil)
|
2020-04-22 19:07:51 +05:30
|
|
|
Gitlab::UrlBuilder.build(self, only_path: only_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def repository_size_checker
|
2020-03-13 15:44:24 +05:30
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
end
|