From dd88afab80d38ba38c6e61d7acda01672ba402a8 Mon Sep 17 00:00:00 2001 From: Administrator Date: Wed, 22 Jan 2014 20:57:40 +0100 Subject: [PATCH] shell can now init repositories --- README.md | 4 +++ bin/gitlab-projects | 2 ++ lib/gitlab_projects.rb | 70 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) diff --git a/README.md b/README.md index fee7b53ce6..6bfaeb7fd3 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,10 @@ Add repo ./bin/gitlab-projects add-project gitlab/gitlab-ci.git +Init repo + + ./bin/gitlab-projects init-project root/example /home/git/gitlab/tmp/gitlab-autoinit-template/example true /home/git/gitlab-autoinit-template Administrator admin@local.host + Remove repo ./bin/gitlab-projects rm-project gitlab/gitlab-ci.git diff --git a/bin/gitlab-projects b/bin/gitlab-projects index 01de20b9ef..2a6c2d990e 100755 --- a/bin/gitlab-projects +++ b/bin/gitlab-projects @@ -9,6 +9,8 @@ require_relative '../lib/gitlab_init' # Ex. # /bin/gitlab-projects add-project gitlab/gitlab-ci.git # +# /bin/gitlab-projects init-project root/example /home/git/gitlab/tmp/gitlab-autoinit-template/example true/false /home/git/gitlab-autoinit-template Administrator admin@local.host +# # /bin/gitlab-projects rm-project gitlab/gitlab-ci.git # # /bin/gitlab-projects mv-project gitlab/gitlab-ci.git randx/fork.git diff --git a/lib/gitlab_projects.rb b/lib/gitlab_projects.rb index fec204c7ab..31b4a1bf12 100644 --- a/lib/gitlab_projects.rb +++ b/lib/gitlab_projects.rb @@ -30,6 +30,7 @@ def exec when 'create-tag'; create_tag when 'rm-tag'; rm_tag when 'add-project'; add_project + when 'init-project'; init_project when 'rm-project'; rm_project when 'mv-project'; mv_project when 'import-project'; import_project @@ -77,6 +78,75 @@ def add_project system(*cmd) && create_hooks(full_path) end + def init_project + tmp_path = ARGV.shift + init_from_template = ARGV.shift + config_auto_init_template_dir = ARGV.shift + username = ARGV.shift + user_email = ARGV.shift + $logger.info "Initializing project" + + # create the tmp directory + FileUtils.mkdir_p(tmp_path, mode: 0770) + + # set some git config stuff + cmd = "git config --global user.name '#{username}'" + result = system(*cmd) + $logger.info "#{cmd} #{result}" + cmd = "git config --global user.email '#{user_email}'" + result = system(*cmd) + $logger.info "#{cmd} #{result}" + + # change workind dir to tmp_path to init the repo + FileUtils.cd(tmp_path) do + # if user (which installed gitlab) created the autoinit-template-dir, copy everything + if init_from_template == "true" + FileUtils.cp_r(Dir.glob("#{config_auto_init_template_dir}/*"), tmp_path) + # else create a simple README.md file + else + FileUtils.touch "README.md" + begin + file = File.open("README.md", "w") + file.write("# README") + file.write("\n") + file.write("This file was automatically created by GitLab.") + file.write("\n") + rescue IOError => e + $logger.warn "Could not write to README.md" + ensure + file.close unless file == nil + end + end + + # add everything to the repo and push it into the repo + git_init_repo(tmp_path) + end + end + + def git_init_repo(tmp_path) + cmd = "git init" + result = system(*cmd) + $logger.info "#{cmd} #{result}" + + cmd = "git add ." + result = system(*cmd) + $logger.info "#{cmd} #{result}" + + cmd = "git commit -m 'initial commit by GitLab'" + result = system(*cmd) + $logger.info "#{cmd} #{result}" + + cmd = "git remote add origin #{full_path}" + result = system(*cmd) + $logger.info "#{cmd} #{result}" + + cmd = "git push -u origin master" + result = system(*cmd) + $logger.info "#{cmd} #{result}" + + FileUtils.rm_rf("#{tmp_path}") + end + def create_hooks(path) hook = File.join(path, 'hooks', 'update') File.delete(hook) if File.exists?(hook)