Simplify shebang detection

This commit is contained in:
Brandon Keepers
2014-11-27 12:44:55 -05:00
parent fd85f7f112
commit cd3defda42

View File

@@ -5,45 +5,31 @@ module Linguist
Language.find_by_interpreter(new(blob.data).interpreter) Language.find_by_interpreter(new(blob.data).interpreter)
end end
attr_reader :data
def initialize(data) def initialize(data)
@data = data @lines = data.lines
end end
def interpreter def interpreter
lines = data.lines.to_a return unless match = /^#! ?(.*)$/.match(@lines.first)
if lines.any? && (match = lines[0].match(/(.+)\n?/)) && (bang = match[0]) =~ /^#!/ tokens = match[0].split(' ')
bang.sub!(/^#! /, '#!') script = tokens.first.split('/').last
tokens = bang.split(' ')
pieces = tokens.first.split('/')
if pieces.size > 1 script = tokens[1] if script == 'env'
script = pieces.last
else
script = pieces.first.sub('#!', '')
end
script = script == 'env' ? tokens[1] : script # If script has an invalid shebang, we might get here
return unless script
# If script has an invalid shebang, we might get here # "python2.6" -> "python2"
return unless script script.sub! $1, '' if script =~ /(\.\d+)$/
# "python2.6" -> "python2" # Check for multiline shebang hacks that call `exec`
script.sub! $1, '' if script =~ /(\.\d+)$/ if script == 'sh' &&
@lines[0...5].any? { |l| l.match(/exec (\w+).+\$0.+\$@/) }
# Check for multiline shebang hacks that call `exec` script = $1
if script == 'sh' &&
lines[0...5].any? { |l| l.match(/exec (\w+).+\$0.+\$@/) }
script = $1
end
File.basename(script)
else
nil
end end
File.basename(script)
end end
end end
end end