gitlabhq/lib/gitosis.rb

75 lines
1.7 KiB
Ruby
Raw Normal View History

2011-10-08 23:36:38 +02:00
require 'inifile'
2011-10-09 23:15:28 +02:00
require 'timeout'
2011-10-10 23:07:40 +02:00
require 'fileutils'
2011-10-08 23:36:38 +02:00
class Gitosis
2011-10-09 23:15:28 +02:00
class AccessDenied < StandardError; end
2011-10-08 23:36:38 +02:00
def pull
# create tmp dir
2011-10-10 23:07:40 +02:00
@local_dir = File.join(Dir.tmpdir,"gitlabhq-gitosis-#{Time.now.to_i}")
2011-10-08 23:36:38 +02:00
Dir.mkdir @local_dir
2011-10-13 14:57:26 +02:00
`git clone #{GITOSIS['admin_uri']} #{@local_dir}/gitosis`
2011-10-08 23:36:38 +02:00
end
def push
2011-10-13 14:57:26 +02:00
Dir.chdir(File.join(@local_dir, "gitosis"))
`git add -A`
`git commit -am "Gitlab"`
`git push`
Dir.chdir(Rails.root)
2011-10-08 23:36:38 +02:00
2011-10-13 14:57:26 +02:00
FileUtils.rm_rf(@local_dir)
2011-10-08 23:36:38 +02:00
end
def configure
2011-10-13 14:57:26 +02:00
status = Timeout::timeout(20) do
2011-10-10 23:07:40 +02:00
File.open(File.join(Dir.tmpdir,"gitlabhq-gitosis.lock"), "w+") do |f|
2011-10-18 10:21:44 +02:00
begin
f.flock(File::LOCK_EX)
pull
yield(self)
push
ensure
f.flock(File::LOCK_UN)
end
2011-10-09 23:15:28 +02:00
end
2011-10-10 23:07:40 +02:00
end
2011-10-09 23:15:28 +02:00
rescue Exception => ex
raise Gitosis::AccessDenied.new("gitosis timeout")
2011-10-08 23:36:38 +02:00
end
def destroy_project(project)
2011-10-13 14:57:26 +02:00
FileUtils.rm_rf(project.path_to_repo)
2011-10-08 23:36:38 +02:00
conf = IniFile.new(File.join(@local_dir,'gitosis','gitosis.conf'))
conf.delete_section("group #{project.path}")
conf.write
end
#update or create
def update_keys(user, key)
File.open(File.join(@local_dir, 'gitosis/keydir',"#{user}.pub"), 'w') {|f| f.write(key.gsub(/\n/,'')) }
end
def delete_key(user)
File.unlink(File.join(@local_dir, 'gitosis/keydir',"#{user}.pub"))
`cd #{File.join(@local_dir,'gitosis')} ; git rm keydir/#{user}.pub`
end
2011-10-09 23:15:28 +02:00
#update or create
2011-10-08 23:36:38 +02:00
def update_project(repo_name, name_writers)
# write config file
conf = IniFile.new(File.join(@local_dir,'gitosis','gitosis.conf'))
conf["group #{repo_name}"]['writable'] = repo_name
conf["group #{repo_name}"]['members'] = name_writers.join(' ')
conf.write
end
end