Initial commit
This commit is contained in:
80
classes/gitinfo.rb
Normal file
80
classes/gitinfo.rb
Normal file
@@ -0,0 +1,80 @@
|
||||
require "sqlite3"
|
||||
require "rugged"
|
||||
require "fileutils"
|
||||
require_relative "db"
|
||||
|
||||
class GitRepo
|
||||
attr :path, :error, :db
|
||||
|
||||
def initialize(path, db)
|
||||
@path = nil
|
||||
@error = nil
|
||||
@db = db
|
||||
if File.absolute_path?(path)
|
||||
if File.exist?(path)
|
||||
@path = path
|
||||
end
|
||||
else
|
||||
apath = File.realpath(path)
|
||||
if File.exist?(apath)
|
||||
@path = apath
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def create_git(project_name, description)
|
||||
@error = nil
|
||||
ret_val = 0
|
||||
File.open("locks/gitcreate", "r") do |f|
|
||||
f.flock(File::LOCK_EX)
|
||||
fname = File.expand_path("#{project_name}.git", @path)
|
||||
if File.exist?(fname)
|
||||
@error = "Репозиторий с таким именем уже существует: #{project_name}"
|
||||
ret_val = 1
|
||||
else
|
||||
Dir.mkdir(fname)
|
||||
Rugged::Repository.init_at(fname, :bare)
|
||||
repo = Rugged::Repository.new(fname)
|
||||
created = false
|
||||
if repo.bare?
|
||||
@error = @db.creategit(project_name, description)
|
||||
if @error.nil?
|
||||
created = true
|
||||
end
|
||||
else
|
||||
@error = "Репозиторий почему-то не пустой"
|
||||
end
|
||||
unless created
|
||||
FileUtils.rm_rf(fname, secure: true)
|
||||
end
|
||||
end
|
||||
end
|
||||
ret_val
|
||||
end
|
||||
|
||||
def create_git_db_only(project_name)
|
||||
@error = @db.creategit(project_name, "")
|
||||
@error
|
||||
end
|
||||
|
||||
def getrepos
|
||||
repos_data = []
|
||||
repos_files = Dir[File.join(@path, "*.git")]
|
||||
repos_files.each do |fl|
|
||||
repo_name = File.basename(fl, ".git")
|
||||
db_info = @db.get_repo_info_by_name(repo_name)
|
||||
unless db_info.nil?
|
||||
db_info = db_info.first
|
||||
repos_data << { :reponame => db_info.reponame, :descr => db_info.descr, :public => db_info.public }
|
||||
else
|
||||
result = create_git_db_only(repo_name)
|
||||
if result.nil?
|
||||
db_info = @db.get_repo_info_by_name(repo_name)
|
||||
db_info = db_info.first
|
||||
repos_data << { :reponame => db_info.reponame, :descr => db_info.descr, :public => db_info.public }
|
||||
end
|
||||
end
|
||||
end
|
||||
repos_data
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user