Place guards, checks for multiline shell hacks

This commit is contained in:
Ted Nyman
2013-12-06 20:39:02 -08:00
parent 8603760ebe
commit 7e178cc416
5 changed files with 18 additions and 14 deletions

View File

@@ -190,9 +190,9 @@ module Linguist
# Public: Is the blob safe to colorize? # Public: Is the blob safe to colorize?
# #
# We use Pygments for syntax highlighting blobs. Pygments # We use Pygments for syntax highlighting blobs. Pygments
# can be too slow for very large blobs or for certain # can be too slow for very large blobs or for certain
# corner-case blobs. # corner-case blobs.
# #
# Return true or false # Return true or false
def safe_to_colorize? def safe_to_colorize?
!large? && text? && !high_ratio_of_long_lines? !large? && text? && !high_ratio_of_long_lines?

View File

@@ -110,10 +110,10 @@ module Linguist
data = data.call() if data.respond_to?(:call) data = data.call() if data.respond_to?(:call)
if data.nil? || data == "" if data.nil? || data == ""
nil nil
elsif result = find_by_shebang(data) elsif (result = find_by_shebang(data)) && !result.empty?
result.first result.first
elsif result = Classifier.classify(Samples::DATA, data, possible_languages.map(&:name)).first elsif classified = Classifier.classify(Samples::DATA, data, possible_languages.map(&:name)).first
Language[result[0]] Language[classified[0]]
end end
else else
possible_languages.first possible_languages.first

View File

@@ -421,6 +421,9 @@
"Xtend": [ "Xtend": [
".xtend" ".xtend"
] ]
},
"interpreters": {
}, },
"filenames": { "filenames": {
"ApacheConf": [ "ApacheConf": [
@@ -43881,5 +43884,5 @@
"Xtend": 2, "Xtend": 2,
"YAML": 1 "YAML": 1
}, },
"md5": "647da23cd1eb02653f50ff9bfbb6e70d" "md5": "9ef710bbe7098e21726a69720f0922b5"
} }

View File

@@ -114,7 +114,7 @@ module Linguist
# Used to retrieve the interpreter from the shebang line of a file's # Used to retrieve the interpreter from the shebang line of a file's
# data. # data.
def self.interpreter_from_shebang(data) def self.interpreter_from_shebang(data)
lines = data.lines lines = data.lines.to_a
if lines.any? && (match = lines[0].match(/(.+)\n?/)) && (bang = match[0]) =~ /^#!/ if lines.any? && (match = lines[0].match(/(.+)\n?/)) && (bang = match[0]) =~ /^#!/
bang.sub!(/^#! /, '#!') bang.sub!(/^#! /, '#!')
@@ -134,7 +134,15 @@ module Linguist
script.sub! $1, '' script.sub! $1, ''
end end
# Check for multiline shebang hacks that call `exec`
if script == 'sh' &&
lines[0...5].any? { |l| l.match(/exec (\w+).+\$0.+\$@/) }
script = $1
end
script script
else
nil
end end
end end

View File

@@ -1,7 +0,0 @@
#!/bin/sh
#| -*- scheme -*-
exec racket -um "$0" "$@"
|#
(require racket/file racket/path racket/list racket/string
(for-syntax racket/base))