diff options
Diffstat (limited to 'lib/gitlab_projects.rb')
-rw-r--r-- | lib/gitlab_projects.rb | 108 |
1 files changed, 72 insertions, 36 deletions
diff --git a/lib/gitlab_projects.rb b/lib/gitlab_projects.rb index c1d175a..767ab79 100644 --- a/lib/gitlab_projects.rb +++ b/lib/gitlab_projects.rb @@ -1,5 +1,6 @@ require 'fileutils' require 'timeout' +require 'open3' require_relative 'gitlab_config' require_relative 'gitlab_logger' @@ -49,10 +50,7 @@ class GitlabProjects def exec case @command - when 'create-branch'; create_branch - when 'rm-branch'; rm_branch when 'create-tag'; create_tag - when 'rm-tag'; rm_tag when 'add-project'; add_project when 'list-projects'; puts list_projects when 'rm-project'; rm_project @@ -60,7 +58,9 @@ class GitlabProjects when 'import-project'; import_project when 'fork-project'; fork_project when 'fetch-remote'; fetch_remote - when 'update-head'; update_head + when 'push-branches'; push_branches + when 'delete-remote-branches'; delete_remote_branches + when 'list-remote-tags'; list_remote_tags when 'gc'; gc else $logger.warn "Attempt to execute invalid gitlab-projects command #{@command.inspect}." @@ -71,17 +71,66 @@ class GitlabProjects protected - def create_branch - branch_name = ARGV.shift - ref = ARGV.shift || "HEAD" - cmd = %W(git --git-dir=#{full_path} branch -- #{branch_name} #{ref}) - system(*cmd) + def list_remote_tags + remote_name = ARGV.shift + + tag_list, exit_code, error = nil + cmd = %W(git --git-dir=#{full_path} ls-remote --tags #{remote_name}) + + Open3.popen3(*cmd) do |stdin, stdout, stderr, wait_thr| + tag_list = stdout.read + error = stderr.read + exit_code = wait_thr.value.exitstatus + end + + if exit_code.zero? + puts tag_list + true + else + puts error + false + end end - def rm_branch - branch_name = ARGV.shift - cmd = %W(git --git-dir=#{full_path} branch -D -- #{branch_name}) - system(*cmd) + def push_branches + remote_name = ARGV.shift + + $logger.info "Pushing branches from #{full_path} to remote #{remote_name}: #{ARGV}" + cmd = %W(git --git-dir=#{full_path} push -- #{remote_name}).concat(ARGV) + pid = Process.spawn(*cmd) + + begin + Process.wait(pid) + + $?.exitstatus.zero? + rescue => exception + $logger.error "Pushing branches to remote #{remote_name} failed due to: #{exception.message}" + + Process.kill('KILL', pid) + Process.wait + false + end + end + + def delete_remote_branches + remote_name = ARGV.shift + branches = ARGV.map { |branch_name| ":#{branch_name}" } + + $logger.info "Pushing deleted branches from #{full_path} to remote #{remote_name}: #{ARGV}" + cmd = %W(git --git-dir=#{full_path} push -- #{remote_name}).concat(branches) + pid = Process.spawn(*cmd) + + begin + Process.wait(pid) + + $?.exitstatus.zero? + rescue => exception + $logger.error "Pushing deleted branches to remote #{remote_name} failed due to: #{exception.message}" + + Process.kill('KILL', pid) + Process.wait + false + end end def create_tag @@ -96,12 +145,6 @@ class GitlabProjects system(*cmd) end - def rm_tag - tag_name = ARGV.shift - cmd = %W(git --git-dir=#{full_path} tag -d -- #{tag_name}) - system(*cmd) - end - def add_project $logger.info "Adding project #{@project_name} at <#{full_path}>." FileUtils.mkdir_p(full_path, mode: 0770) @@ -135,8 +178,17 @@ class GitlabProjects # timeout for fetch timeout = (ARGV.shift || 120).to_i + + # fetch with --force ? + forced = ARGV.include?('--force') + + # fetch with --tags or --no-tags + tags_option = ARGV.include?('--no-tags') ? '--no-tags' : '--tags' + $logger.info "Fetching remote #{@name} for project #{@project_name}." - cmd = %W(git --git-dir=#{full_path} fetch #{@name} --tags) + cmd = %W(git --git-dir=#{full_path} fetch #{@name}) + cmd << '--force' if forced + cmd << tags_option pid = Process.spawn(*cmd) begin @@ -261,22 +313,6 @@ class GitlabProjects system(*cmd) && self.class.create_hooks(full_destination_path) end - def update_head - new_head = ARGV.shift - - unless new_head - $logger.error "update-head failed: no branch provided." - 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 - def gc $logger.info "Running git gc for <#{full_path}>." unless File.exists?(full_path) |