summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xbin/gitlab-projects2
-rw-r--r--lib/gitlab_projects.rb23
-rw-r--r--spec/gitlab_projects_spec.rb21
3 files changed, 45 insertions, 1 deletions
diff --git a/bin/gitlab-projects b/bin/gitlab-projects
index 8803931..01de20b 100755
--- a/bin/gitlab-projects
+++ b/bin/gitlab-projects
@@ -17,6 +17,8 @@ require_relative '../lib/gitlab_init'
#
# /bin/gitlab-projects import-project randx/six.git https://github.com/randx/six.git
#
+# /bin/gitlab-projects update-head gitlab/gitlab-ci.git 5-2-stable
+#
require File.join(ROOT_PATH, 'lib', 'gitlab_projects')
# Return non-zero if command execution was not successful
diff --git a/lib/gitlab_projects.rb b/lib/gitlab_projects.rb
index e60438e..8c280ea 100644
--- a/lib/gitlab_projects.rb
+++ b/lib/gitlab_projects.rb
@@ -31,6 +31,7 @@ class GitlabProjects
when 'mv-project'; mv_project
when 'import-project'; import_project
when 'fork-project'; fork_project
+ when 'update-head'; update_head
else
$logger.warn "Attempt to execute invalid gitlab-projects command #{@command.inspect}."
puts 'not allowed'
@@ -127,6 +128,27 @@ class GitlabProjects
system(cmd)
end
+ def update_head
+ new_head = ARGV.shift
+
+ unless new_head
+ $logger.error "update-head failed: no branch provided."
+ return false
+ end
+
+ unless File.exists?(File.join(full_path, 'refs/heads', new_head))
+ $logger.error "update-head failed: specified branch does not exist in ref/heads."
+ return false
+ end
+
+ File.open(File.join(full_path, 'HEAD'), 'w') do |f|
+ f.write("ref: refs/heads/#{new_head}")
+ end
+
+ $logger.info "Update head in project #{project_name} to <#{new_head}>."
+ true
+ end
+
private
def create_hooks_to(dest_path)
@@ -135,5 +157,4 @@ class GitlabProjects
"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 5bcc5c8..ba8187e 100644
--- a/spec/gitlab_projects_spec.rb
+++ b/spec/gitlab_projects_spec.rb
@@ -106,6 +106,27 @@ describe GitlabProjects do
end
end
+ describe :update_head do
+ let(:gl_projects) { build_gitlab_projects('update-head', repo_name, 'stable') }
+
+ before do
+ FileUtils.mkdir_p(tmp_repo_path)
+ system("git init --bare #{tmp_repo_path}")
+ system("touch #{tmp_repo_path}/refs/heads/stable")
+ File.read(File.join(tmp_repo_path, 'HEAD')).strip.should == 'ref: refs/heads/master'
+ end
+
+ it "should update head for repo" do
+ gl_projects.exec.should be_true
+ File.read(File.join(tmp_repo_path, 'HEAD')).strip.should == 'ref: refs/heads/stable'
+ end
+
+ it "should log an update_head event" do
+ $logger.should_receive(:info).with("Update head in project #{repo_name} to <stable>.")
+ gl_projects.exec
+ end
+ end
+
describe :import_project do
let(:gl_projects) { build_gitlab_projects('import-project', repo_name, 'https://github.com/randx/six.git') }