Commit 335f9e81 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge pull request #45 from amacarthur/fork-feature

add fork_project command
parents 8e75018c d8e061d7
...@@ -33,6 +33,10 @@ Import repo ...@@ -33,6 +33,10 @@ Import repo
./bin/gitlab-projects import-project randx/six.git https://github.com/randx/six.git ./bin/gitlab-projects import-project randx/six.git https://github.com/randx/six.git
Fork repo
./bin/gitlab-projects fork-project gitlab/gitlab-ci.git randx
### Keys: ### Keys:
......
...@@ -13,6 +13,8 @@ require_relative '../lib/gitlab_init' ...@@ -13,6 +13,8 @@ require_relative '../lib/gitlab_init'
# #
# /bin/gitlab-projects mv-project gitlab/gitlab-ci.git randx/fork.git # /bin/gitlab-projects mv-project gitlab/gitlab-ci.git randx/fork.git
# #
# /bin/gitlab-projects fork-project gitlab/gitlab-ci.git randx
#
# /bin/gitlab-projects import-project randx/six.git https://github.com/randx/six.git # /bin/gitlab-projects import-project randx/six.git https://github.com/randx/six.git
# #
require File.join(ROOT_PATH, 'lib', 'gitlab_projects') require File.join(ROOT_PATH, 'lib', 'gitlab_projects')
......
...@@ -29,6 +29,7 @@ class GitlabProjects ...@@ -29,6 +29,7 @@ class GitlabProjects
when 'rm-project'; rm_project when 'rm-project'; rm_project
when 'mv-project'; mv_project when 'mv-project'; mv_project
when 'import-project'; import_project when 'import-project'; import_project
when 'fork-project'; fork_project
else else
puts 'not allowed' puts 'not allowed'
false false
...@@ -44,10 +45,7 @@ class GitlabProjects ...@@ -44,10 +45,7 @@ class GitlabProjects
end end
def create_hooks_cmd def create_hooks_cmd
pr_hook_path = File.join(ROOT_PATH, 'hooks', 'post-receive') create_hooks_to(full_path)
up_hook_path = File.join(ROOT_PATH, 'hooks', 'update')
"ln -s #{pr_hook_path} #{full_path}/hooks/post-receive && ln -s #{up_hook_path} #{full_path}/hooks/update"
end end
def rm_project def rm_project
...@@ -84,4 +82,33 @@ class GitlabProjects ...@@ -84,4 +82,33 @@ class GitlabProjects
FileUtils.mv(full_path, new_full_path) FileUtils.mv(full_path, new_full_path)
end end
def fork_project
new_namespace = ARGV.shift
# destination namespace must be provided
return false unless new_namespace
#destination namespace must exist
namespaced_path = File.join(repos_path, new_namespace)
return false unless File.exists?(namespaced_path)
#a project of the same name cannot already be within the destination namespace
full_destination_path = File.join(namespaced_path, project_name.split('/')[-1])
return false if File.exists?(full_destination_path)
cmd = "cd #{namespaced_path} && git clone --bare #{full_path} && #{create_hooks_to(full_destination_path)}"
system(cmd)
end
private
def create_hooks_to(dest_path)
pr_hook_path = File.join(ROOT_PATH, 'hooks', 'post-receive')
up_hook_path = File.join(ROOT_PATH, 'hooks', 'update')
"ln -s #{pr_hook_path} #{dest_path}/hooks/post-receive && ln -s #{up_hook_path} #{dest_path}/hooks/update"
end
end end
...@@ -18,7 +18,7 @@ describe GitlabProjects do ...@@ -18,7 +18,7 @@ describe GitlabProjects do
it { @gl_projects.project_name.should == repo_name } it { @gl_projects.project_name.should == repo_name }
it { @gl_projects.instance_variable_get(:@command).should == 'add-project' } it { @gl_projects.instance_variable_get(:@command).should == 'add-project' }
it { @gl_projects.instance_variable_get(:@full_path).should == '/home/git/repositories/gitlab-ci.git' } it { @gl_projects.instance_variable_get(:@full_path).should == "#{GitlabConfig.new.repos_path}/gitlab-ci.git" }
end end
describe :add_project do describe :add_project do
...@@ -77,6 +77,35 @@ describe GitlabProjects do ...@@ -77,6 +77,35 @@ describe GitlabProjects do
end end
end end
describe :fork_project do
let(:source_repo_name) { File.join('source-namespace', repo_name) }
let(:dest_repo) { File.join(tmp_repos_path, 'forked-to-namespace', repo_name) }
let(:gl_projects_fork) { build_gitlab_projects('fork-project', source_repo_name, 'forked-to-namespace') }
let(:gl_projects_import) { build_gitlab_projects('import-project', source_repo_name, 'https://github.com/randx/six.git') }
before do
gl_projects_import.exec
end
it "should not fork into a namespace that doesn't exist" do
gl_projects_fork.exec.should be_false
end
it "should fork the repo" do
# create destination namespace
FileUtils.mkdir_p(File.join(tmp_repos_path, 'forked-to-namespace'))
gl_projects_fork.exec.should be_true
File.exists?(dest_repo).should be_true
File.exists?(File.join(dest_repo, '/hooks/update')).should be_true
File.exists?(File.join(dest_repo, '/hooks/post-receive')).should be_true
end
it "should not fork if a project of the same name already exists" do
#trying to fork again should fail as the repo already exists
gl_projects_fork.exec.should be_false
end
end
describe :exec do describe :exec do
it 'should puts message if unknown command arg' do it 'should puts message if unknown command arg' do
gitlab_projects = build_gitlab_projects('edit-project', repo_name) gitlab_projects = build_gitlab_projects('edit-project', repo_name)
...@@ -89,7 +118,7 @@ describe GitlabProjects do ...@@ -89,7 +118,7 @@ describe GitlabProjects do
argv(*args) argv(*args)
gl_projects = GitlabProjects.new gl_projects = GitlabProjects.new
gl_projects.stub(repos_path: tmp_repos_path) gl_projects.stub(repos_path: tmp_repos_path)
gl_projects.stub(full_path: tmp_repo_path) gl_projects.stub(full_path: File.join(tmp_repos_path, gl_projects.project_name))
gl_projects gl_projects
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment