diff options
Diffstat (limited to 'lib/gitlab_projects.rb')
-rw-r--r-- | lib/gitlab_projects.rb | 100 |
1 files changed, 69 insertions, 31 deletions
diff --git a/lib/gitlab_projects.rb b/lib/gitlab_projects.rb index bea5686..85eb167 100644 --- a/lib/gitlab_projects.rb +++ b/lib/gitlab_projects.rb @@ -1,5 +1,5 @@ -require 'open3' require 'fileutils' +require 'timeout' require_relative 'gitlab_config' require_relative 'gitlab_logger' @@ -17,6 +17,14 @@ class GitlabProjects # Ex /home/git/repositories/test.git attr_reader :full_path + def self.create_hooks(path) + %w(pre-receive post-receive).each do |hook_name| + hook = File.join(path, 'hooks', hook_name) + File.delete(hook) if File.exists?(hook) + File.symlink(File.join(ROOT_PATH, 'hooks', hook_name), hook) + end + end + def initialize @command = ARGV.shift @project_name = ARGV.shift @@ -48,38 +56,39 @@ class GitlabProjects def create_branch branch_name = ARGV.shift ref = ARGV.shift || "HEAD" - cmd = "cd #{full_path} && git branch #{branch_name} #{ref}" - system(cmd) + cmd = %W(git --git-dir=#{full_path} branch -- #{branch_name} #{ref}) + system(*cmd) end def rm_branch branch_name = ARGV.shift - cmd = "cd #{full_path} && git branch -D #{branch_name}" - system(cmd) + cmd = %W(git --git-dir=#{full_path} branch -D #{branch_name}) + system(*cmd) end def create_tag tag_name = ARGV.shift ref = ARGV.shift || "HEAD" - cmd = "cd #{full_path} && git tag #{tag_name} #{ref}" - system(cmd) + cmd = %W(git --git-dir=#{full_path} tag) + if ARGV.size > 0 + msg = ARGV.shift + cmd += %W(-a -m #{msg}) + end + cmd += %W(-- #{tag_name} #{ref}) + system(*cmd) end def rm_tag tag_name = ARGV.shift - cmd = "cd #{full_path} && git tag -d #{tag_name}" - system(cmd) + 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) - cmd = "cd #{full_path} && git init --bare && #{create_hooks_cmd}" - system(cmd) - end - - def create_hooks_cmd - create_hooks_to(full_path) + cmd = %W(git --git-dir=#{full_path} init --bare) + system(*cmd) && self.class.create_hooks(full_path) end def rm_project @@ -87,13 +96,53 @@ class GitlabProjects FileUtils.rm_rf(full_path) end + def mask_password_in_url(url) + result = URI(url) + result.password = "*****" unless result.password.nil? + result + rescue + url + end + + def remove_origin_in_repo + cmd = %W(git --git-dir=#{full_path} remote remove origin) + pid = Process.spawn(*cmd) + Process.wait(pid) + end + # Import project via git clone --bare # URL must be publicly cloneable def import_project + # Skip import if repo already exists + return false if File.exists?(full_path) + @source = ARGV.shift - $logger.info "Importing project #{@project_name} from <#{@source}> to <#{full_path}>." - cmd = "cd #{repos_path} && git clone --bare #{@source} #{project_name} && #{create_hooks_cmd}" - system(cmd) + masked_source = mask_password_in_url(@source) + + # timeout for clone + timeout = (ARGV.shift || 120).to_i + $logger.info "Importing project #{@project_name} from <#{masked_source}> to <#{full_path}>." + cmd = %W(git clone --bare -- #{@source} #{full_path}) + + pid = Process.spawn(*cmd) + + begin + Timeout.timeout(timeout) do + Process.wait(pid) + end + rescue Timeout::Error + $logger.error "Importing project #{@project_name} from <#{masked_source}> failed due to timeout." + + Process.kill('KILL', pid) + Process.wait + FileUtils.rm_rf(full_path) + false + else + self.class.create_hooks(full_path) + # The project was imported successfully. + # Remove the origin URL since it may contain password. + remove_origin_in_repo + end end # Move repository from one directory to another @@ -154,8 +203,8 @@ class GitlabProjects end $logger.info "Forking project from <#{full_path}> to <#{full_destination_path}>." - cmd = "cd #{namespaced_path} && git clone --bare #{full_path} && #{create_hooks_to(full_destination_path)}" - system(cmd) + cmd = %W(git clone --bare -- #{full_path} #{full_destination_path}) + system(*cmd) && self.class.create_hooks(full_destination_path) end def update_head @@ -166,11 +215,6 @@ class GitlabProjects 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 @@ -178,10 +222,4 @@ class GitlabProjects $logger.info "Update head in project #{project_name} to <#{new_head}>." true end - - private - - def create_hooks_to(dest_path) - "ln -s #{File.join(ROOT_PATH, 'hooks', 'update')} #{dest_path}/hooks/update" - end end |