diff options
| author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2013-04-16 23:44:37 -0700 |
|---|---|---|
| committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2013-04-16 23:44:37 -0700 |
| commit | 335f9e81e08442435140218994f96226348492c6 (patch) | |
| tree | d25f9d7fd1f852dd5fbe2986526a5cd063624005 | |
| parent | 8e75018c538d422c8273e2dbcde65404fe8fd42b (diff) | |
| parent | d8e061d73bb29374e679aef5ee6356383bcaa80e (diff) | |
| download | gitlab-shell-335f9e81e08442435140218994f96226348492c6.tar.gz | |
Merge pull request #45 from amacarthur/fork-feature
add fork_project command
| -rw-r--r-- | README.md | 4 | ||||
| -rwxr-xr-x | bin/gitlab-projects | 2 | ||||
| -rw-r--r-- | lib/gitlab_projects.rb | 35 | ||||
| -rw-r--r-- | spec/gitlab_projects_spec.rb | 33 |
4 files changed, 68 insertions, 6 deletions
@@ -33,6 +33,10 @@ Import repo ./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: diff --git a/bin/gitlab-projects b/bin/gitlab-projects index 95d0475..8803931 100755 --- a/bin/gitlab-projects +++ b/bin/gitlab-projects @@ -13,6 +13,8 @@ require_relative '../lib/gitlab_init' # # /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 # require File.join(ROOT_PATH, 'lib', 'gitlab_projects') diff --git a/lib/gitlab_projects.rb b/lib/gitlab_projects.rb index aef343e..0b9bb8c 100644 --- a/lib/gitlab_projects.rb +++ b/lib/gitlab_projects.rb @@ -29,6 +29,7 @@ class GitlabProjects when 'rm-project'; rm_project when 'mv-project'; mv_project when 'import-project'; import_project + when 'fork-project'; fork_project else puts 'not allowed' false @@ -44,10 +45,7 @@ class GitlabProjects end def create_hooks_cmd - pr_hook_path = File.join(ROOT_PATH, 'hooks', 'post-receive') - 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" + create_hooks_to(full_path) end def rm_project @@ -84,4 +82,33 @@ class GitlabProjects FileUtils.mv(full_path, new_full_path) 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 diff --git a/spec/gitlab_projects_spec.rb b/spec/gitlab_projects_spec.rb index cbe1ab2..d0d6764 100644 --- a/spec/gitlab_projects_spec.rb +++ b/spec/gitlab_projects_spec.rb @@ -18,7 +18,7 @@ describe GitlabProjects do it { @gl_projects.project_name.should == repo_name } 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 describe :add_project do @@ -77,6 +77,35 @@ describe GitlabProjects do 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 it 'should puts message if unknown command arg' do gitlab_projects = build_gitlab_projects('edit-project', repo_name) @@ -89,7 +118,7 @@ describe GitlabProjects do argv(*args) gl_projects = GitlabProjects.new 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 end |
