Skip to content

Commit

Permalink
Merge pull request github-linguist#247 from github/check-size-first
Browse files Browse the repository at this point in the history
When testing if a blob is indexable or safe to colorize, check size first
  • Loading branch information
Scott J. Goldman committed Sep 2, 2012
2 parents 6ec907a + fc435a2 commit 5443dc5
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
3 changes: 2 additions & 1 deletion github-linguist.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'github-linguist'
s.version = '2.3.1'
s.version = '2.3.2'
s.summary = "GitHub Language detection"

s.authors = "GitHub"
Expand All @@ -12,6 +12,7 @@ Gem::Specification.new do |s|
s.add_dependency 'escape_utils', '~> 0.2.3'
s.add_dependency 'mime-types', '~> 1.19'
s.add_dependency 'pygments.rb', '>= 0.2.13'
s.add_development_dependency 'mocha'
s.add_development_dependency 'json'
s.add_development_dependency 'rake'
s.add_development_dependency 'yajl-ruby'
Expand Down
8 changes: 4 additions & 4 deletions lib/linguist/blob_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def large?
#
# Return true or false
def safe_to_colorize?
text? && !large? && !high_ratio_of_long_lines?
!large? && text? && !high_ratio_of_long_lines?
end

# Internal: Does the blob have a ratio of long lines?
Expand Down Expand Up @@ -250,7 +250,9 @@ def generated?
#
# Return true or false
def indexable?
if binary?
if size > 100 * 1024
false
elsif binary?
false
elsif extname == '.txt'
true
Expand All @@ -260,8 +262,6 @@ def indexable?
false
elsif generated?
false
elsif size > 100 * 1024
false
else
true
end
Expand Down
7 changes: 7 additions & 0 deletions test/test_blob.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'linguist/samples'

require 'test/unit'
require 'mocha'
require 'mime/types'
require 'pygments'

Expand Down Expand Up @@ -261,6 +262,12 @@ def test_indexable
assert !blob("Text/dump.sql").indexable?
assert !blob("Binary/github.po").indexable?
assert !blob("Binary/linguist.gem").indexable?

# large binary blobs should fail on size check first, not call
# into charlock_holmes and alloc big buffers for testing encoding
b = blob("Binary/octocat.ai")
b.expects(:binary?).never
assert !b.indexable?
end

def test_language
Expand Down

0 comments on commit 5443dc5

Please sign in to comment.