summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohan Sørensen <johan@johansorensen.com>2009-06-11 14:40:11 +0200
committerJohan Sørensen <johan@johansorensen.com>2009-06-11 14:40:11 +0200
commit7dc6a9684ad21be290aa29816cf1b6875fef67a1 (patch)
tree54b34f2f6a6859f3bd6a70e00f3ff25966875e1d
parent178d47b3007a0fba4e1cf4f9006bda2135d18504 (diff)
downloadgitorious-mainline-outdated-7dc6a9684ad21be290aa29816cf1b6875fef67a1.zip
gitorious-mainline-outdated-7dc6a9684ad21be290aa29816cf1b6875fef67a1.tar.gz
gitorious-mainline-outdated-7dc6a9684ad21be290aa29816cf1b6875fef67a1.tar.bz2
Moved the binary blob detection into Grit::Blob
-rw-r--r--app/helpers/blobs_helper.rb2
-rw-r--r--test/functional/blobs_controller_test.rb1
-rw-r--r--vendor/grit/lib/grit/blob.rb9
-rw-r--r--vendor/grit/test/test_blob.rb18
4 files changed, 29 insertions, 1 deletions
diff --git a/app/helpers/blobs_helper.rb b/app/helpers/blobs_helper.rb
index 0c6b97e..5c07466 100644
--- a/app/helpers/blobs_helper.rb
+++ b/app/helpers/blobs_helper.rb
@@ -41,7 +41,7 @@ module BlobsHelper
end
def binary?(blob)
- blob.data[0..1024].include?("\000")
+ blob.binary?
end
def image?(blob)
diff --git a/test/functional/blobs_controller_test.rb b/test/functional/blobs_controller_test.rb
index 2a04dd3..c624309 100644
--- a/test/functional/blobs_controller_test.rb
+++ b/test/functional/blobs_controller_test.rb
@@ -50,6 +50,7 @@ class BlobsControllerTest < ActionController::TestCase
blob_mock.stubs(:mime_type).returns("text/plain")
blob_mock.stubs(:size).returns(666)
blob_mock.stubs(:id).returns("a"*40)
+ blob_mock.stubs(:binary?).returns(false)
commit_stub = mock("commit")
commit_stub.stubs(:id).returns("a"*40)
commit_stub.stubs(:tree).returns(commit_stub)
diff --git a/vendor/grit/lib/grit/blob.rb b/vendor/grit/lib/grit/blob.rb
index b630b45..71e2390 100644
--- a/vendor/grit/lib/grit/blob.rb
+++ b/vendor/grit/lib/grit/blob.rb
@@ -52,6 +52,15 @@ module Grit
guesses.first ? guesses.first.simplified : DEFAULT_MIME_TYPE
end
+ # Returns true if the contents of this blob looks like
+ # binary data, false otherwise
+ def binary?
+ data[0..1024].include?("\000")
+ rescue Grit::Git::GitTimeout
+ # assuming binary for large blobs might be a tad too clever...
+ return true
+ end
+
def sha
@id
end
diff --git a/vendor/grit/test/test_blob.rb b/vendor/grit/test/test_blob.rb
index 0d15cfe..a114ea7 100644
--- a/vendor/grit/test/test_blob.rb
+++ b/vendor/grit/test/test_blob.rb
@@ -86,4 +86,22 @@ class TestBlob < Test::Unit::TestCase
blob2 = Blob.create(@r, :name => 'foo/bar.rb', :id => "634396b2f541a9f2d58b00be1a07f0c358b999b3")
assert_equal blob1, blob2
end
+
+ def test_should_know_if_something_is_binary
+ assert !Blob.create(@r, :name => "foo.rb", :data => "class Lulz; end").binary?
+ assert !Blob.create(@r, :name => "foo.m", :data => "[lulz isForTehLaffs:YES]").binary?
+ assert !Blob.create(@r, :name => "foo.txt", :data => "x"*5000).binary?
+
+ assert Blob.create(@r, :name => "foo.gif", :data => "GIF89a\v\x00\r\x00\xD5!\x00\xBD").binary?
+ assert Blob.create(@r, :name => "foo.exe", :data => "rabuf\06si\000ezodniw").binary?
+ assert Blob.create(@r, :name => "foo", :data => "a"*1024 + "\000").binary?
+ assert Blob.create(@r, {
+ :name => "foo.data",
+ :data => File.read(File.join(File.dirname(__FILE__), "../test/dot_git/objects/pack/pack-c8881c2613522dc3ac69af9c7b4881a061aaec8c.pack"))
+ }).binary?
+
+ blob = Blob.create(@r, :name => "foo")
+ blob.expects(:data).raises(Grit::Git::GitTimeout)
+ assert blob.binary?
+ end
end \ No newline at end of file