files.rb revision 73ec6b43b29b5c85f21c9aea6645eed485e0a0e2
38N/Amodule GitRepository::Files
38N/A # depends on GitRepository
38N/A extend ActiveSupport::Concern
38N/A
38N/A class GitFile
38N/A attr_reader :path, :oid, :mime_type, :mime_category
38N/A
38N/A def initialize(repository, rugged_commit, path)
38N/A @path = path
38N/A self.repository = repository
38N/A if repository.empty?
38N/A @path = '/'
38N/A @content = []
38N/A @rugged_commit = nil
38N/A @rugged_object = nil
38N/A return
38N/A end
38N/A if !repository.path_exists?(path, rugged_commit.oid)
38N/A raise GitRepository::PathNotFoundError, "Path doesn't exist: #{path}"
38N/A end
38N/A self.rugged_object = repository.get_object(rugged_commit, path)
38N/A
38N/A @oid = rugged_commit.oid
38N/A
38N/A if file?
38N/A mime_info = repository.class.mime_info(name)
38N/A @mime_type = mime_info[:mime_type]
38N/A @mime_category = mime_info[:mime_category]
38N/A end
42N/A end
49N/A
38N/A def size
38N/A case type
38N/A when :file
38N/A rugged_object.size
38N/A when :dir
38N/A content.size
38N/A end
38N/A end
38N/A
42N/A def content
42N/A @content ||= case type
42N/A when :file
42N/A rugged_object.content
42N/A when :dir
42N/A repository.folder_contents(oid, path)
38N/A end
42N/A end
42N/A
42N/A def file?
42N/A type == :file
42N/A end
42N/A
49N/A def dir?
49N/A type == :dir
42N/A end
42N/A
42N/A def type
42N/A return :dir if rugged_object.nil?
42N/A case rugged_object.type
42N/A when :blob
42N/A :file
40N/A when :tree
40N/A :dir
49N/A end
49N/A end
49N/A
49N/A def name
49N/A @name ||= path.split('/')[-1]
49N/A end
49N/A
49N/A def last_change
49N/A @last_change ||= repository.entry_info(path, oid)
49N/A end
49N/A
49N/A def ==(other)
49N/A [:repository, :path, :oid].all? do |attr|
49N/A self.send(attr) == other.send(attr)
49N/A end
49N/A end
49N/A
49N/A protected
49N/A attr_accessor :repository, :rugged_object
49N/A end
38N/Aend
38N/A