Check for multiline shebang script hacks

Fixes #8
This commit is contained in:
Joshua Peek
2011-06-27 17:35:48 -05:00
parent 19720117a1
commit ebba204ba3
4 changed files with 31 additions and 1 deletions

View File

@@ -308,7 +308,7 @@ module Linguist
# Fail fast if blob isn't viewable?
return unless viewable?
if data && (match = data.match(/(.+)\n?/)) && (bang = match[0]) =~ /^#!/
if data && (match = lines[0].match(/(.+)\n?/)) && (bang = match[0]) =~ /^#!/
bang.sub!(/^#! /, '#!')
tokens = bang.split(' ')
pieces = tokens.first.split('/')
@@ -325,6 +325,16 @@ module Linguist
script.sub! $1, ''
end
# Check for multiline shebang hacks that exec themselves
#
# #!/bin/sh
# exec foo "$0" "$@"
#
if script == 'sh' &&
lines[0...5].any? { |l| l.match(/exec (\w+).+\$0.+\$@/) }
script = $1
end
script
end
end

7
test/fixtures/script.rkt vendored Normal file
View File

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

9
test/fixtures/script.scala vendored Normal file
View File

@@ -0,0 +1,9 @@
#!/bin/sh
exec scala "$0" "$@"
!#
object HelloWorld {
def main(args: Array[String]) {
println("Hello, world!")
}
}

View File

@@ -286,6 +286,8 @@ class TestBlob < Test::Unit::TestCase
assert_equal 'rake', blob("script.rake").shebang_script
assert_equal 'foo', blob("script.foo").shebang_script
assert_equal 'nush', blob("script.nu").shebang_script
assert_equal 'scala', blob("script.scala").shebang_script
assert_equal 'racket', blob("script.rkt").shebang_script
assert_equal nil, blob("foo.rb").shebang_script
end
@@ -301,6 +303,8 @@ class TestBlob < Test::Unit::TestCase
assert_equal Language['Ruby'], blob("script.mrb").shebang_language
assert_equal Language['Ruby'], blob("script.rake").shebang_language
assert_equal Language['Nu'], blob("script.nu").shebang_language
assert_equal Language['Scala'], blob("script.scala").shebang_language
assert_equal Language['Racket'], blob("script.rkt").shebang_language
assert_equal nil, blob("script.foo").shebang_language
assert_equal nil, blob("foo.rb").shebang_language
end