2018-07-17 12:50:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-03-19 11:37:50 -04:00
|
|
|
module Projects
|
2014-01-16 12:03:42 -05:00
|
|
|
class ForkService < BaseService
|
2017-12-07 03:44:55 -05:00
|
|
|
def execute(fork_to_project = nil)
|
|
|
|
if fork_to_project
|
|
|
|
link_existing_project(fork_to_project)
|
|
|
|
else
|
|
|
|
fork_new_project
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-08-31 13:16:34 -04:00
|
|
|
def allowed_fork?
|
|
|
|
current_user.can?(:fork_project, @project)
|
|
|
|
end
|
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
def link_existing_project(fork_to_project)
|
|
|
|
return if fork_to_project.forked?
|
|
|
|
|
2018-08-31 13:16:34 -04:00
|
|
|
build_fork_network_member(fork_to_project)
|
2017-12-07 03:44:55 -05:00
|
|
|
|
2018-08-31 13:16:34 -04:00
|
|
|
if link_fork_network(fork_to_project)
|
|
|
|
# A forked project stores its LFS objects in the `forked_from_project`.
|
|
|
|
# So the LFS objects become inaccessible, and therefore delete them from
|
|
|
|
# the database so they'll get cleaned up.
|
|
|
|
#
|
|
|
|
# TODO: refactor this to get the correct lfs objects when implementing
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab-ce/issues/39769
|
|
|
|
fork_to_project.lfs_objects_projects.delete_all
|
2018-08-02 07:30:24 -04:00
|
|
|
|
2018-08-31 13:16:34 -04:00
|
|
|
fork_to_project
|
|
|
|
end
|
2017-12-07 03:44:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def fork_new_project
|
2015-04-24 15:37:12 -04:00
|
|
|
new_params = {
|
2016-05-24 17:53:53 -04:00
|
|
|
visibility_level: allowed_visibility_level,
|
2015-04-24 15:37:12 -04:00
|
|
|
description: @project.description,
|
2019-02-18 09:17:29 -05:00
|
|
|
name: target_name,
|
|
|
|
path: target_path,
|
2015-12-04 06:55:23 -05:00
|
|
|
shared_runners_enabled: @project.shared_runners_enabled,
|
2018-08-31 13:16:34 -04:00
|
|
|
namespace_id: target_namespace.id,
|
|
|
|
fork_network: fork_network,
|
|
|
|
# We need to assign the fork network membership after the project has
|
|
|
|
# been instantiated to avoid ActiveRecord trying to create it when
|
|
|
|
# initializing the project, as that would cause a foreign key constraint
|
|
|
|
# exception.
|
|
|
|
relations_block: -> (project) { build_fork_network_member(project) }
|
2014-08-25 05:19:52 -04:00
|
|
|
}
|
|
|
|
|
2015-04-24 15:37:12 -04:00
|
|
|
if @project.avatar.present? && @project.avatar.image?
|
|
|
|
new_params[:avatar] = @project.avatar
|
2014-10-03 04:12:44 -04:00
|
|
|
end
|
2014-11-13 12:40:47 -05:00
|
|
|
|
Allow public forks to be deduplicated
When a project is forked, the new repository used to be a deep copy of everything
stored on disk by leveraging `git clone`. This works well, and makes isolation
between repository easy. However, the clone is at the start 100% the same as the
origin repository. And in the case of the objects in the object directory, this
is almost always going to be a lot of duplication.
Object Pools are a way to create a third repository that essentially only exists
for its 'objects' subdirectory. This third repository's object directory will be
set as alternate location for objects. This means that in the case an object is
missing in the local repository, git will look in another location. This other
location is the object pool repository.
When Git performs garbage collection, it's smart enough to check the
alternate location. When objects are duplicated, it will allow git to
throw one copy away. This copy is on the local repository, where to pool
remains as is.
These pools have an origin location, which for now will always be a
repository that itself is not a fork. When the root of a fork network is
forked by a user, the fork still clones the full repository. Async, the
pool repository will be created.
Either one of these processes can be done earlier than the other. To
handle this race condition, the Join ObjectPool operation is
idempotent. Given its idempotent, we can schedule it twice, with the
same effect.
To accommodate the holding of state two migrations have been added.
1. Added a state column to the pool_repositories column. This column is
managed by the state machine, allowing for hooks on transitions.
2. pool_repositories now has a source_project_id. This column in
convenient to have for multiple reasons: it has a unique index allowing
the database to handle race conditions when creating a new record. Also,
it's nice to know who the host is. As that's a short link to the fork
networks root.
Object pools are only available for public project, which use hashed
storage and when forking from the root of the fork network. (That is,
the project being forked from itself isn't a fork)
In this commit message I use both ObjectPool and Pool repositories,
which are alike, but different from each other. ObjectPool refers to
whatever is on the disk stored and managed by Gitaly. PoolRepository is
the record in the database.
2018-12-03 08:49:58 -05:00
|
|
|
new_params.merge!(@project.object_pool_params)
|
|
|
|
|
2015-04-24 15:37:12 -04:00
|
|
|
new_project = CreateService.new(current_user, new_params).execute
|
2016-09-19 14:28:41 -04:00
|
|
|
return new_project unless new_project.persisted?
|
|
|
|
|
2018-08-31 13:16:34 -04:00
|
|
|
# Set the forked_from_project relation after saving to avoid having to
|
|
|
|
# reload the project to reset the association information and cause an
|
|
|
|
# extra query.
|
|
|
|
new_project.forked_from_project = @project
|
|
|
|
|
2016-08-01 18:31:21 -04:00
|
|
|
builds_access_level = @project.project_feature.builds_access_level
|
2018-07-02 06:43:06 -04:00
|
|
|
new_project.project_feature.update(builds_access_level: builds_access_level)
|
2016-08-01 18:31:21 -04:00
|
|
|
|
2015-04-24 15:37:12 -04:00
|
|
|
new_project
|
2013-05-02 15:30:13 -04:00
|
|
|
end
|
2016-05-24 17:53:53 -04:00
|
|
|
|
2017-09-28 10:38:12 -04:00
|
|
|
def fork_network
|
2018-08-31 13:16:34 -04:00
|
|
|
@fork_network ||= @project.fork_network || @project.build_root_of_fork_network
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_fork_network_member(fork_to_project)
|
|
|
|
if allowed_fork?
|
|
|
|
fork_to_project.build_fork_network_member(forked_from_project: @project,
|
|
|
|
fork_network: fork_network)
|
2017-09-28 10:38:12 -04:00
|
|
|
else
|
2018-08-31 13:16:34 -04:00
|
|
|
fork_to_project.errors.add(:forked_from_project_id, 'is forbidden')
|
2017-09-28 10:38:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-12-07 03:44:55 -05:00
|
|
|
def link_fork_network(fork_to_project)
|
2018-08-31 13:16:34 -04:00
|
|
|
return if fork_to_project.errors.any?
|
2017-12-07 03:44:55 -05:00
|
|
|
|
2018-08-31 13:16:34 -04:00
|
|
|
fork_to_project.fork_network_member.save &&
|
|
|
|
refresh_forks_count
|
2017-09-28 10:38:12 -04:00
|
|
|
end
|
|
|
|
|
2017-08-14 09:22:09 -04:00
|
|
|
def refresh_forks_count
|
|
|
|
Projects::ForksCountService.new(@project).refresh_cache
|
|
|
|
end
|
|
|
|
|
2019-02-18 09:17:29 -05:00
|
|
|
def target_path
|
|
|
|
@target_path ||= @params[:path] || @project.path
|
|
|
|
end
|
|
|
|
|
|
|
|
def target_name
|
|
|
|
@target_name ||= @params[:name] || @project.name
|
|
|
|
end
|
|
|
|
|
2017-12-20 10:19:54 -05:00
|
|
|
def target_namespace
|
|
|
|
@target_namespace ||= @params[:namespace] || current_user.namespace
|
|
|
|
end
|
|
|
|
|
2016-05-24 17:53:53 -04:00
|
|
|
def allowed_visibility_level
|
2017-12-20 10:19:54 -05:00
|
|
|
target_level = [@project.visibility_level, target_namespace.visibility_level].min
|
2016-05-24 17:53:53 -04:00
|
|
|
|
2017-12-20 10:19:54 -05:00
|
|
|
Gitlab::VisibilityLevel.closest_allowed_level(target_level)
|
2016-05-24 17:53:53 -04:00
|
|
|
end
|
2013-03-19 11:37:50 -04:00
|
|
|
end
|
|
|
|
end
|