2020-02-04 15:08:40 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-02-13 15:08:52 +00:00
|
|
|
# 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-08 06:09:13 +00:00
|
|
|
# `to_reference` should be overridden in case the object
|
2020-02-13 15:08:52 +00:00
|
|
|
# needs any special behavior.
|
2020-02-04 15:08:40 +00:00
|
|
|
module HasRepository
|
|
|
|
extend ActiveSupport::Concern
|
2020-02-13 15:08:52 +00:00
|
|
|
include Referable
|
2020-04-06 15:10:04 +00:00
|
|
|
include Gitlab::ShellAdapter
|
2020-02-04 15:08:40 +00:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
|
|
|
delegate :base_dir, :disk_path, to: :storage
|
2021-07-19 12:10:08 +00:00
|
|
|
delegate :change_head, to: :repository
|
2020-02-04 15:08:40 +00:00
|
|
|
|
|
|
|
def valid_repo?
|
|
|
|
repository.exists?
|
2021-04-26 12:09:44 +00:00
|
|
|
rescue StandardError
|
2020-03-03 09:07:54 +00:00
|
|
|
errors.add(:base, _('Invalid repository path'))
|
2020-02-04 15:08:40 +00:00
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def repo_exists?
|
|
|
|
strong_memoize(:repo_exists) do
|
|
|
|
repository.exists?
|
2021-04-26 12:09:44 +00:00
|
|
|
rescue StandardError
|
2020-02-04 15:08:40 +00:00
|
|
|
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
|
|
|
|
|
2020-10-05 15:08:56 +00:00
|
|
|
def lfs_enabled?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2020-02-04 15:08:40 +00:00
|
|
|
def empty_repo?
|
|
|
|
repository.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_branch
|
2021-09-21 12:11:06 +00:00
|
|
|
@default_branch ||= repository.empty? ? default_branch_from_preferences : repository.root_ref
|
2020-07-13 15:09:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def default_branch_from_preferences
|
2021-04-13 18:11:28 +00:00
|
|
|
(default_branch_from_group_preferences || Gitlab::CurrentSettings.default_branch_name).presence
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_branch_from_group_preferences
|
|
|
|
return unless respond_to?(:group)
|
|
|
|
return unless group
|
2020-10-15 12:09:06 +00:00
|
|
|
|
2021-04-13 18:11:28 +00:00
|
|
|
group.default_branch_name || group.root_ancestor.default_branch_name
|
2020-02-04 15:08:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def reload_default_branch
|
|
|
|
@default_branch = nil # rubocop:disable Gitlab/ModuleWithInstanceVariables
|
|
|
|
|
|
|
|
default_branch
|
|
|
|
end
|
|
|
|
|
|
|
|
def url_to_repo
|
2020-04-07 15:09:30 +00:00
|
|
|
ssh_url_to_repo
|
2020-02-04 15:08:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def ssh_url_to_repo
|
2020-04-07 15:09:30 +00:00
|
|
|
Gitlab::RepositoryUrlBuilder.build(repository.full_path, protocol: :ssh)
|
2020-02-04 15:08:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def http_url_to_repo
|
2020-04-07 15:09:30 +00:00
|
|
|
Gitlab::RepositoryUrlBuilder.build(repository.full_path, protocol: :http)
|
2020-02-04 15:08:40 +00:00
|
|
|
end
|
|
|
|
|
2020-11-13 00:09:49 +00:00
|
|
|
# Is overridden in EE::Project for Geo support
|
|
|
|
def lfs_http_url_to_repo(_operation = nil)
|
|
|
|
http_url_to_repo
|
|
|
|
end
|
|
|
|
|
2020-02-04 15:08:40 +00:00
|
|
|
def web_url(only_path: nil)
|
2020-04-06 18:09:37 +00:00
|
|
|
Gitlab::UrlBuilder.build(self, only_path: only_path)
|
2020-02-04 15:08:40 +00:00
|
|
|
end
|
2020-03-31 18:07:42 +00:00
|
|
|
|
|
|
|
def repository_size_checker
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
2021-07-19 12:10:08 +00:00
|
|
|
|
|
|
|
def after_repository_change_head
|
|
|
|
reload_default_branch
|
|
|
|
end
|
2021-09-13 12:09:50 +00:00
|
|
|
|
|
|
|
def after_change_head_branch_does_not_exist(branch)
|
|
|
|
# No-op (by default)
|
|
|
|
end
|
2020-02-04 15:08:40 +00:00
|
|
|
end
|