This commit is contained in:
Tom Preston-Werner 2008-01-06 18:59:51 -08:00
Родитель b86b48e352
Коммит 3e0955045c
3 изменённых файлов: 28 добавлений и 0 удалений

Просмотреть файл

@ -4,6 +4,10 @@ $:.unshift File.dirname(__FILE__) # For use/testing when no gem is installed
# stdlib
# third party
require 'rubygems'
require 'mime/types'
# internal requires
require 'grit/lazy'
require 'grit/errors'

Просмотреть файл

@ -1,6 +1,8 @@
module Grit
class Blob
DEFAULT_MIME_TYPE = "text/plain"
attr_reader :id
attr_reader :mode
attr_reader :name
@ -41,6 +43,14 @@ module Grit
@data ||= @repo.git.cat_file({:p => true}, id)
end
# The mime type of this file (based on the filename)
#
# Returns String
def mime_type
guesses = MIME::Types.type_for(self.name) rescue []
guesses.first ? guesses.first.simplified : DEFAULT_MIME_TYPE
end
# The blame information for the given file at the given commit
#
# Returns Array: [Grit::Commit, Array: [<line>]]

Просмотреть файл

@ -29,6 +29,20 @@ class TestBlob < Test::Unit::TestCase
assert_equal 11, blob.size
end
# data
# mime_type
def test_mime_type_should_return_mime_type_for_known_types
blob = Blob.create(@r, :id => 'abc', :name => 'foo.png')
assert_equal "image/png", blob.mime_type
end
def test_mime_type_should_return_text_plain_for_unknown_types
blob = Blob.create(@r, :id => 'abc')
assert_equal "text/plain", blob.mime_type
end
# blame
def test_blame