mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
Bail earlier if the file is empty.
This will change behavior for empty files with unique extensions, returning nil instead of the language.
This commit is contained in:
@@ -147,6 +147,13 @@ module Linguist
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Public: Is the blob empty?
|
||||||
|
#
|
||||||
|
# Return true or false
|
||||||
|
def empty?
|
||||||
|
data.nil? || data == ""
|
||||||
|
end
|
||||||
|
|
||||||
# Public: Is the blob text?
|
# Public: Is the blob text?
|
||||||
#
|
#
|
||||||
# Return true or false
|
# Return true or false
|
||||||
|
|||||||
@@ -101,12 +101,8 @@ module Linguist
|
|||||||
def self.detect(blob)
|
def self.detect(blob)
|
||||||
name = blob.name.to_s
|
name = blob.name.to_s
|
||||||
|
|
||||||
# Check if the blob is possibly binary and bail early; this is a cheap
|
# Bail early if the blob is binary or empty.
|
||||||
# test that uses the extension name to guess a binary binary mime type.
|
return nil if blob.likely_binary? || blob.binary? || blob.empty?
|
||||||
#
|
|
||||||
# We'll perform a more comprehensive test later which actually involves
|
|
||||||
# looking for binary characters in the blob
|
|
||||||
return nil if blob.likely_binary? || blob.binary?
|
|
||||||
|
|
||||||
# A bit of an elegant hack. If the file is executable but extensionless,
|
# A bit of an elegant hack. If the file is executable but extensionless,
|
||||||
# append a "magic" extension so it can be classified with other
|
# append a "magic" extension so it can be classified with other
|
||||||
@@ -131,11 +127,8 @@ module Linguist
|
|||||||
possible_language_names = heuristic_languages.map(&:name)
|
possible_language_names = heuristic_languages.map(&:name)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Don't bother with binary contents or an empty file
|
|
||||||
if data.nil? || data == ""
|
|
||||||
nil
|
|
||||||
# Check if there's a shebang line and use that as authoritative
|
# Check if there's a shebang line and use that as authoritative
|
||||||
elsif (result = find_by_shebang(data)) && !result.empty?
|
if (result = find_by_shebang(data)) && !result.empty?
|
||||||
result.first
|
result.first
|
||||||
# No shebang. Still more work to do. Try to find it with our heuristics.
|
# No shebang. Still more work to do. Try to find it with our heuristics.
|
||||||
elsif heuristic_languages.size == 1
|
elsif heuristic_languages.size == 1
|
||||||
|
|||||||
Reference in New Issue
Block a user