git_repository.rb revision 1167711784176c2c46f3b69a3530a8aff0d575f7
# Wrapper for access to the local Git repository
#requires git and git-svn to be installed for the functions clone_git and clone_svn to work
require 'open3'
include \
if File.exists?(path)
else
end
end
@repo
end
end
end
if !rugged_commit && url.empty?
true
else
end
end
if !rugged_commit && url.empty?
else
end
end
path ||= ''
end
end
@repo.refs.map do |r|
{
}
end
end
if ref.nil?
else
end
end
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
@repo.head.target
end
def self.clone_git(source, target_path, depth=nil)
if depth.nil? || depth == 0
stdin, stdout, stderr, wait_thr = Open3.popen3 'git', 'clone', '--bare', source, target_path
else
stdin, stdout, stderr, wait_thr = Open3.popen3 'git', 'clone', '--bare', source, target_path, '--depth', "#{depth}"
end
{ out: stdout.gets(nil), err: stderr.gets(nil), success: wait_thr.value.success? }
end
def self.clone_svn(source, target_path)
target_path = target_path[0..-2] if target_path[-1] == '/'
target_path_svn = target_path_svn_base = "#{target_path}_svn"
if File.exists? target_path
{ out: nil, err: "#{target_path} already exists.", success: false}
else
i = 0
while File.exists? target_path_svn do
target_path_svn = "#{target_path_svn_base}#{i}"
i = i+1
end
stdin, stdout, stderr, wait_thr = Open3.popen3 'git', 'svn', 'clone', '-r', 'HEAD', source, target_path_svn
out = stdout.gets(nil)
err = stderr.gets(nil)
if wait_thr.value.success?
result = GitRepository.clone_git(target_path_svn, target_path)
FileUtils.rmtree target_path_svn
result
else
{ out: out, err: err, success: false }
end
end
end
def self.is_bare_repository?(path)
repo = Rugged::Repository.new(path)
repo.bare?
rescue Rugged::RepositoryError
false
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 = 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 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
def head
@repo.lookup(head_oid)
end
end