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