Initial commit

This commit is contained in:
alexey
2025-02-17 17:21:56 +03:00
parent db1cc72b8c
commit 982ea3322c
22 changed files with 841 additions and 2 deletions

41
classes/config.rb Normal file
View File

@@ -0,0 +1,41 @@
require "inifile"
class IniConfig
attr :path
def initialize(in_path = "config.ini")
@path = in_path
@config = {}
parse_ini
end
def parse_ini()
return if path.nil?
return unless File.exist? path
@config = IniFile.load(path)
end
def get_port()
unless @config["server"]["port"].nil?
@config["server"]["port"].to_i
else
8080
end
end
def get_repo()
unless @config["repo"]["repo"].nil?
@config["repo"]["repo"].to_s
else
"repo"
end
end
def get_db()
unless @config["server"]["db"].nil?
@config["server"]["db"].to_s
else
"db"
end
end
end

27
classes/db.rb Normal file
View File

@@ -0,0 +1,27 @@
require "sequel"
cfg_internal = IniConfig.new()
Sequel.connect(cfg_internal.get_db)
class Repos < Sequel::Model(:repos)
end
class DBase
attr :error, :last_id
def creategit(project_name, description)
@error = nil
data = Repos.where(reponame: project_name)
unless data.nil?
id = Repos.insert(reponame: project_name, descr: description, public: 1)
@last_id = id
else
@error = "Данный репозиторий уже существует"
end
@error
end
def get_repo_info_by_name(repo_name)
Repos.where(reponame: repo_name)
end
end

80
classes/gitinfo.rb Normal file
View 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