From ebba204ba36db2441fa07ac82756f77d5420e6b3 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 27 Jun 2011 17:35:48 -0500 Subject: [PATCH] Check for multiline shebang script hacks Fixes #8 --- lib/linguist/blob_helper.rb | 12 +++++++++++- test/fixtures/script.rkt | 7 +++++++ test/fixtures/script.scala | 9 +++++++++ test/test_blob.rb | 4 ++++ 4 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/script.rkt create mode 100644 test/fixtures/script.scala diff --git a/lib/linguist/blob_helper.rb b/lib/linguist/blob_helper.rb index 55dba881..f69db8b5 100644 --- a/lib/linguist/blob_helper.rb +++ b/lib/linguist/blob_helper.rb @@ -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 diff --git a/test/fixtures/script.rkt b/test/fixtures/script.rkt new file mode 100644 index 00000000..bc5a8ca4 --- /dev/null +++ b/test/fixtures/script.rkt @@ -0,0 +1,7 @@ +#!/bin/sh +#| -*- scheme -*- +exec racket -um "$0" "$@" +|# + +(require racket/file racket/path racket/list racket/string + (for-syntax racket/base)) diff --git a/test/fixtures/script.scala b/test/fixtures/script.scala new file mode 100644 index 00000000..b026d24f --- /dev/null +++ b/test/fixtures/script.scala @@ -0,0 +1,9 @@ +#!/bin/sh +exec scala "$0" "$@" +!# + +object HelloWorld { + def main(args: Array[String]) { + println("Hello, world!") + } +} diff --git a/test/test_blob.rb b/test/test_blob.rb index 8272cca5..642306bb 100644 --- a/test/test_blob.rb +++ b/test/test_blob.rb @@ -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