Skip to content

Commit

Permalink
Merge pull request github-linguist#1346 from github/vmg/binary-fix
Browse files Browse the repository at this point in the history
Properly detect binary blobs
  • Loading branch information
Vicent Marti committed Jul 4, 2014
2 parents 1b712d2 + 458831b commit a8c9556
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
13 changes: 10 additions & 3 deletions lib/linguist/language.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,20 @@ def self.create(attributes = {})

# Public: Detects the Language of the blob.
#
# blob - an object that implements the Linguist `Blob` interface;
# blob - an object that includes the Linguist `BlobHelper` interface;
# see Linguist::LazyBlob and Linguist::FileBlob for examples
#
# Returns Language or nil.
def self.detect(blob)
name = blob.name.to_s

# Check if the blob is possibly binary and bail early; this is a cheap
# test that uses the extension name to guess a binary binary mime type.
#
# We'll perform a more comprehensive test later which actually involves
# looking for binary characters in the blob
return nil if blob.likely_binary?

# A bit of an elegant hack. If the file is executable but extensionless,
# append a "magic" extension so it can be classified with other
# languages that have shebang scripts.
Expand All @@ -116,8 +123,8 @@ def self.detect(blob)
data = blob.data
possible_language_names = possible_languages.map(&:name)

# Don't bother with emptiness
if data.nil? || data == ""
# Don't bother with binary contents or an empty file
if blob.binary? || data.nil? || data == ""
nil
# Check if there's a shebang line and use that as authoritative
elsif (result = find_by_shebang(data)) && !result.empty?
Expand Down
2 changes: 1 addition & 1 deletion lib/linguist/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Linguist
VERSION = "3.0.0"
VERSION = "3.0.2"
end

0 comments on commit a8c9556

Please sign in to comment.