git_repository.rb revision 5377dc644918632ef318825466e4f3cd861e27ec
743ea1b7b30f30520045ef04a3475f4ec24be030sascha# Wrapper for access to the local Git repository
743ea1b7b30f30520045ef04a3475f4ec24be030sascha# requires git and git-svn to be installed for the functions clone_git and clone_svn to work
743ea1b7b30f30520045ef04a3475f4ec24be030saschaclass GitRepository
743ea1b7b30f30520045ef04a3475f4ec24be030sascha require 'git_repository/config'
743ea1b7b30f30520045ef04a3475f4ec24be030sascha
743ea1b7b30f30520045ef04a3475f4ec24be030sascha include \
743ea1b7b30f30520045ef04a3475f4ec24be030sascha Config,
743ea1b7b30f30520045ef04a3475f4ec24be030sascha Cloning,
743ea1b7b30f30520045ef04a3475f4ec24be030sascha GetCommit,
743ea1b7b30f30520045ef04a3475f4ec24be030sascha GetObject,
743ea1b7b30f30520045ef04a3475f4ec24be030sascha GetDiff,
743ea1b7b30f30520045ef04a3475f4ec24be030sascha GetFolderContents,
743ea1b7b30f30520045ef04a3475f4ec24be030sascha GetInfoList,
743ea1b7b30f30520045ef04a3475f4ec24be030sascha Commit,
743ea1b7b30f30520045ef04a3475f4ec24be030sascha History
743ea1b7b30f30520045ef04a3475f4ec24be030sascha
743ea1b7b30f30520045ef04a3475f4ec24be030sascha delegate :path, to: :repo
743ea1b7b30f30520045ef04a3475f4ec24be030sascha
743ea1b7b30f30520045ef04a3475f4ec24be030sascha def initialize(path)
743ea1b7b30f30520045ef04a3475f4ec24be030sascha if File.exists?(path)
743ea1b7b30f30520045ef04a3475f4ec24be030sascha @repo = Rugged::Repository.new(path)
743ea1b7b30f30520045ef04a3475f4ec24be030sascha else
743ea1b7b30f30520045ef04a3475f4ec24be030sascha FileUtils.mkdir_p(Ontohub::Application.config.git_root)
743ea1b7b30f30520045ef04a3475f4ec24be030sascha @repo = Rugged::Repository.init_at(path, true)
743ea1b7b30f30520045ef04a3475f4ec24be030sascha end
743ea1b7b30f30520045ef04a3475f4ec24be030sascha end
743ea1b7b30f30520045ef04a3475f4ec24be030sascha
743ea1b7b30f30520045ef04a3475f4ec24be030sascha # DELETEME (exists only for debugging purpose)
743ea1b7b30f30520045ef04a3475f4ec24be030sascha def repo
743ea1b7b30f30520045ef04a3475f4ec24be030sascha @repo
743ea1b7b30f30520045ef04a3475f4ec24be030sascha end
743ea1b7b30f30520045ef04a3475f4ec24be030sascha
743ea1b7b30f30520045ef04a3475f4ec24be030sascha def destroy
743ea1b7b30f30520045ef04a3475f4ec24be030sascha FileUtils.rmtree(@repo.path)
743ea1b7b30f30520045ef04a3475f4ec24be030sascha end
743ea1b7b30f30520045ef04a3475f4ec24be030sascha
743ea1b7b30f30520045ef04a3475f4ec24be030sascha def empty?
743ea1b7b30f30520045ef04a3475f4ec24be030sascha @repo.empty?
743ea1b7b30f30520045ef04a3475f4ec24be030sascha end
743ea1b7b30f30520045ef04a3475f4ec24be030sascha
743ea1b7b30f30520045ef04a3475f4ec24be030sascha def dir?(path, commit_oid=nil)
743ea1b7b30f30520045ef04a3475f4ec24be030sascha if empty?
743ea1b7b30f30520045ef04a3475f4ec24be030sascha return false
743ea1b7b30f30520045ef04a3475f4ec24be030sascha end
743ea1b7b30f30520045ef04a3475f4ec24be030sascha
743ea1b7b30f30520045ef04a3475f4ec24be030sascha rugged_commit = repo.lookup(commit_oid || head_oid)
743ea1b7b30f30520045ef04a3475f4ec24be030sascha object = get_object(rugged_commit, path)
743ea1b7b30f30520045ef04a3475f4ec24be030sascha
743ea1b7b30f30520045ef04a3475f4ec24be030sascha !object.nil? && object.type == :tree
743ea1b7b30f30520045ef04a3475f4ec24be030sascha end
743ea1b7b30f30520045ef04a3475f4ec24be030sascha
743ea1b7b30f30520045ef04a3475f4ec24be030sascha def path_exists?(url, commit_oid=nil)
743ea1b7b30f30520045ef04a3475f4ec24be030sascha rugged_commit = get_commit(commit_oid)
743ea1b7b30f30520045ef04a3475f4ec24be030sascha if !rugged_commit && url.empty?
743ea1b7b30f30520045ef04a3475f4ec24be030sascha true
743ea1b7b30f30520045ef04a3475f4ec24be030sascha else
743ea1b7b30f30520045ef04a3475f4ec24be030sascha path_exists_rugged?(rugged_commit, url)
743ea1b7b30f30520045ef04a3475f4ec24be030sascha end
743ea1b7b30f30520045ef04a3475f4ec24be030sascha end
743ea1b7b30f30520045ef04a3475f4ec24be030sascha
743ea1b7b30f30520045ef04a3475f4ec24be030sascha def get_file(url, commit_oid=nil)
743ea1b7b30f30520045ef04a3475f4ec24be030sascha rugged_commit = get_commit(commit_oid)
743ea1b7b30f30520045ef04a3475f4ec24be030sascha if !rugged_commit && url.empty?
743ea1b7b30f30520045ef04a3475f4ec24be030sascha nil
743ea1b7b30f30520045ef04a3475f4ec24be030sascha else
743ea1b7b30f30520045ef04a3475f4ec24be030sascha get_file_rugged(rugged_commit, url)
743ea1b7b30f30520045ef04a3475f4ec24be030sascha end
743ea1b7b30f30520045ef04a3475f4ec24be030sascha end
743ea1b7b30f30520045ef04a3475f4ec24be030sascha
743ea1b7b30f30520045ef04a3475f4ec24be030sascha def get_path_of_dir(oid=nil, path=nil)
743ea1b7b30f30520045ef04a3475f4ec24be030sascha path ||= ''
path = path[0..-2] if(path[-1] == '/')
raise URLNotFoundError.new unless path_exists?(path, oid)
path
end
def self.directory(path)
path.split("/")[0..-2].join("/")
end
def branches
@repo.refs.map do |r|
{
refname: r.name,
name: r.name.split('/')[-1],
commit: r.target,
oid: r.target.oid
}
end
end
def branch_commit(name)
ref = @repo.references["refs/heads/#{name}"]
if ref.nil?
nil
else
ref.target
end
end
def branch_oid(name)
if commit = branch_commit(name)
commit.oid
else
nil
end
end
def build_target_path(url, file_name)
file_path = url.dup
file_path << '/' if file_path[-1] != '/' && !file_path.empty?
file_path << file_name
file_path
end
def is_head?(commit_oid=nil)
commit_oid == nil || (!@repo.empty? && commit_oid == head_oid)
end
def head_oid
if @repo.empty?
nil
else
@repo.head.target.oid
end
end
def self.is_repository_with_working_copy?(path)
repo = Rugged::Repository.new(path)
!repo.bare?
rescue Rugged::RepositoryError
false
end
def self.is_bare_repository?(path)
Rugged::Repository.new(path).bare?
rescue Rugged::RepositoryError
false
end
def self.mime_type_editable?(mime_type)
mime_type.to_s == 'application/xml' || mime_type.to_s.match(/^text\/.*/)
end
def self.mime_info(filename)
ext = File.extname(filename)[1..-1]
mime_type = Mime::Type.lookup_by_extension(ext) || Mime::TEXT
mime_category = mime_type.to_s.split('/')[0]
{
mime_type: mime_type,
mime_category: mime_category
}
end
protected
def path_exists_rugged?(rugged_commit, url='')
if url.empty?
true
else
tree = rugged_commit.tree
nil != get_object(rugged_commit, url)
end
rescue Rugged::OdbError
false
end
def get_file_rugged(rugged_commit, url='')
return nil unless path_exists_rugged?(rugged_commit, url)
object = get_object(rugged_commit, url)
if object.type == :blob
filename = url.split('/')[-1]
mime_info = self.class.mime_info(filename)
{
name: filename,
size: object.size,
content: object.content,
mime_type: mime_info[:mime_type],
mime_category: mime_info[:mime_category]
}
else
nil
end
end
def head
@repo.lookup(head_oid)
end
end