Skip to content

shell can now init repositories #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions bin/gitlab-projects
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
70 changes: 70 additions & 0 deletions lib/gitlab_projects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down