# encoding: utf-8 module LocalPac class GitStorage private attr_reader :storage_path, :data, :repository, :file_creator, :null_file public def initialize(storage_path, compressor_engine = JavaScriptCompressor, file_creator = LocalPac::File, null_file = LocalPac::NullFile.new) @storage_path = ::File.expand_path(storage_path) @file_creator = file_creator @null_file = null_file begin @repository = Rugged::Repository.new(storage_path) rescue Rugged::RepositoryError GitRepository.create(storage_path) @repository = Rugged::Repository.new(storage_path) end mutex = Mutex.new mutex.synchronize do @data = pac_files.reduce({}) do |memo, file| compressor_engine.new.prepare(file) memo[file.name.to_sym] = file memo end end end def [](key) data.fetch(key, null_file) end private def pac_files LocalPac.ui_logger.debug "I'm using the following storage path: \"#{storage_path}\"." files = files_in_repository.keep_if { |f| f.extension? '.pac' } LocalPac.ui_logger.debug "Found the following files: \"#{files.join(", ")}\"." files end def files_in_repository head_commit = repository.lookup(repository.head.target) files = [] head_commit.tree.walk_blobs(:postorder) do |root, entry| files << file_creator.new("#{root}#{entry[:name]}", repository.lookup(entry[:oid]).content ) end files rescue Rugged::ReferenceError [] end end end