mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-12-29 05:11:00 +00:00
Merge pull request #2299 from github/shebang-fixing
Fixing up shebang detection to match new tokenizer behaviour
This commit is contained in:
@@ -23,17 +23,20 @@ module Linguist
|
|||||||
# First line must start with #!
|
# First line must start with #!
|
||||||
return unless shebang && shebang.start_with?("#!")
|
return unless shebang && shebang.start_with?("#!")
|
||||||
|
|
||||||
# Get the parts of the shebang without the #!
|
s = StringScanner.new(shebang)
|
||||||
tokens = shebang.sub(/^#!\s*/, '').strip.split(' ')
|
|
||||||
|
|
||||||
# There was nothing after the #!
|
# There was nothing after the #!
|
||||||
return if tokens.empty?
|
return unless path = s.scan(/^#!\s*\S+/)
|
||||||
|
|
||||||
# Get the name of the interpreter
|
# Keep going
|
||||||
script = File.basename(tokens.first)
|
script = path.split('/').last
|
||||||
|
|
||||||
# Get next argument if interpreter was /usr/bin/env
|
# if /usr/bin/env type shebang then walk the string
|
||||||
script = tokens[1] if script == 'env'
|
if script == 'env'
|
||||||
|
s.scan(/\s+/)
|
||||||
|
s.scan(/.*=[^\s]+\s+/) # skip over variable arguments e.g. foo=bar
|
||||||
|
script = s.scan(/\S+/)
|
||||||
|
end
|
||||||
|
|
||||||
# Interpreter was /usr/bin/env with no arguments
|
# Interpreter was /usr/bin/env with no arguments
|
||||||
return unless script
|
return unless script
|
||||||
@@ -41,6 +44,9 @@ module Linguist
|
|||||||
# "python2.6" -> "python2"
|
# "python2.6" -> "python2"
|
||||||
script.sub! /(\.\d+)$/, ''
|
script.sub! /(\.\d+)$/, ''
|
||||||
|
|
||||||
|
# #! perl -> perl
|
||||||
|
script.sub! /^#!\s*/, ''
|
||||||
|
|
||||||
# Check for multiline shebang hacks that call `exec`
|
# Check for multiline shebang hacks that call `exec`
|
||||||
if script == 'sh' &&
|
if script == 'sh' &&
|
||||||
data.lines.first(5).any? { |l| l.match(/exec (\w+).+\$0.+\$@/) }
|
data.lines.first(5).any? { |l| l.match(/exec (\w+).+\$0.+\$@/) }
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ class TestShebang < Minitest::Test
|
|||||||
assert_interpreter "perl", "#! perl"
|
assert_interpreter "perl", "#! perl"
|
||||||
|
|
||||||
assert_interpreter "ruby", "#!/bin/sh\n\n\nexec ruby $0 $@"
|
assert_interpreter "ruby", "#!/bin/sh\n\n\nexec ruby $0 $@"
|
||||||
end
|
|
||||||
|
|
||||||
|
assert_interpreter "sh", "#! /usr/bin/env A=003 B=149 C=150 D=xzd E=base64 F=tar G=gz H=head I=tail sh"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user