mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
Merge pull request #1854 from github/more-shebang-fixes
More shebang fixes
This commit is contained in:
@@ -18,23 +18,32 @@ module Linguist
|
||||
#
|
||||
# Returns a String or nil
|
||||
def self.interpreter(data)
|
||||
lines = data.lines
|
||||
return unless match = /^#! ?(.+)$/.match(lines.first)
|
||||
shebang = data.lines.first
|
||||
|
||||
tokens = match[1].split(' ')
|
||||
script = tokens.first.split('/').last
|
||||
# First line must start with #!
|
||||
return unless shebang && shebang.start_with?("#!")
|
||||
|
||||
# Get the parts of the shebang without the #!
|
||||
tokens = shebang.sub(/^#!\s*/, '').strip.split(' ')
|
||||
|
||||
# There was nothing after the #!
|
||||
return if tokens.empty?
|
||||
|
||||
# Get the name of the interpreter
|
||||
script = File.basename(tokens.first)
|
||||
|
||||
# Get next argument if interpreter was /usr/bin/env
|
||||
script = tokens[1] if script == 'env'
|
||||
|
||||
# If script has an invalid shebang, we might get here
|
||||
# Interpreter was /usr/bin/env with no arguments
|
||||
return unless script
|
||||
|
||||
# "python2.6" -> "python2"
|
||||
script.sub! $1, '' if script =~ /(\.\d+)$/
|
||||
script.sub! /(\.\d+)$/, ''
|
||||
|
||||
# Check for multiline shebang hacks that call `exec`
|
||||
if script == 'sh' &&
|
||||
lines.first(5).any? { |l| l.match(/exec (\w+).+\$0.+\$@/) }
|
||||
data.lines.first(5).any? { |l| l.match(/exec (\w+).+\$0.+\$@/) }
|
||||
script = $1
|
||||
end
|
||||
|
||||
|
||||
@@ -17,6 +17,8 @@ class TestShebang < Test::Unit::TestCase
|
||||
assert_interpreter nil, " #!/usr/sbin/ruby"
|
||||
assert_interpreter nil, "\n#!/usr/sbin/ruby"
|
||||
assert_interpreter nil, "#!"
|
||||
assert_interpreter nil, "#! "
|
||||
assert_interpreter nil, "#!/usr/bin/env"
|
||||
|
||||
assert_interpreter "ruby", "#!/usr/sbin/ruby\n# bar"
|
||||
assert_interpreter "ruby", "#!/usr/bin/ruby\n# foo"
|
||||
@@ -34,6 +36,8 @@ class TestShebang < Test::Unit::TestCase
|
||||
assert_interpreter "python3", "#!/usr/bin/python3\n\n\n\n"
|
||||
assert_interpreter "sbcl", "#!/usr/bin/sbcl --script\n\n"
|
||||
assert_interpreter "perl", "#! perl"
|
||||
|
||||
assert_interpreter "ruby", "#!/bin/sh\n\n\nexec ruby $0 $@"
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user