chainlint: recognize multi-line quoted strings more robustly
authorEric Sunshine <sunshine@sunshineco.com>
Mon, 13 Aug 2018 08:47:38 +0000 (04:47 -0400)
committerJunio C Hamano <gitster@pobox.com>
Mon, 13 Aug 2018 19:22:12 +0000 (12:22 -0700)
chainlint.sed recognizes multi-line quoted strings within subshells:

echo "abc
def" >out &&

so it can avoid incorrectly classifying lines internal to the string as
breaking the &&-chain. To identify the first line of a multi-line
string, it checks if the line contains a single quote. However, this is
fragile and can be easily fooled by a line containing multiple strings:

echo "xyz" "abc
def" >out &&

Make detection more robust by checking for an odd number of quotes
rather than only a single one.

(Escaped quotes are not handled, but support may be added later.)

The original multi-line string recognizer rather cavalierly threw away
all but the final quote, whereas the new one is careful to retain all
quotes, so the "expected" output of a couple existing chainlint tests is
updated to account for this new behavior.

Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/chainlint.sed
t/chainlint/here-doc-multi-line-string.expect
t/chainlint/multi-line-string.expect
t/chainlint/multi-line-string.test
index 6661e21f20059429acaadfd42b8e49b14f010f1f..8544df38dfc0c557120a8a250529147edbc3d2a4 100644 (file)
@@ -151,10 +151,10 @@ s/.*\n//
 :slurp
 # incomplete line "...\"
 /\\$/bincomplete
-# multi-line quoted string "...\n..."
-/^[^"]*"[^"]*$/bdqstring
-# multi-line quoted string '...\n...' (but not contraction in string "it's so")
-/^[^']*'[^']*$/{
+# multi-line quoted string "...\n..."?
+/"/bdqstring
+# multi-line quoted string '...\n...'? (but not contraction in string "it's")
+/'/{
        /"[^'"]*'[^'"]*"/!bsqstring
 }
 :folded
@@ -250,20 +250,32 @@ N
 s/\\\n//
 bslurp
 
-# found multi-line double-quoted string "...\n..." -- slurp until end of string
+# check for multi-line double-quoted string "...\n..." -- fold to one line
 :dqstring
-s/"//g
+# remove all quote pairs
+s/"\([^"]*\)"/@!\1@!/g
+# done if no dangling quote
+/"/!bdqdone
+# otherwise, slurp next line and try again
 N
 s/\n//
-/"/!bdqstring
+bdqstring
+:dqdone
+s/@!/"/g
 bfolded
 
-# found multi-line single-quoted string '...\n...' -- slurp until end of string
+# check for multi-line single-quoted string '...\n...' -- fold to one line
 :sqstring
-s/'//g
+# remove all quote pairs
+s/'\([^']*\)'/@!\1@!/g
+# done if no dangling quote
+/'/!bsqdone
+# otherwise, slurp next line and try again
 N
 s/\n//
-/'/!bsqstring
+bsqstring
+:sqdone
+s/@!/'/g
 bfolded
 
 # found here-doc -- swallow it to avoid false hits within its body (but keep
index 1e5b724b9d44fc06ac7e51cb3406125cb8df9f79..32038a070c2ad881f7136028ceb1be41f57c9a43 100644 (file)
@@ -1,4 +1,4 @@
 (
-?!AMP?!        cat && echo multi-line  string"
+?!AMP?!        cat && echo "multi-line string"
        bap
 >)
index 8334c4cc8e08d3e8f296de90bfd6ab3dd1983629..170cb5999322ea7798bceafb2222274c4112adde 100644 (file)
@@ -1,9 +1,15 @@
 (
-       x=line 1                line 2          line 3" &&
-?!AMP?!        y=line 1                line2'
+       x="line 1               line 2          line 3" &&
+?!AMP?!        y='line 1               line2'
        foobar
 >) &&
 (
        echo "there's nothing to see here" &&
        exit
+>) &&
+(
+       echo "xyz" "abc         def             ghi" &&
+       echo 'xyz' 'abc         def             ghi' &&
+       echo 'xyz' "abc         def             ghi" &&
+       barfoo
 >)
index 14cb44d51cbdaebc7ab16fa39d921b391e96ec5c..287ab897054874972076ec6913bf8c8144196296 100644 (file)
 # LINT: starting multi-line single-quoted string
        echo "there's nothing to see here" &&
        exit
+) &&
+(
+       echo "xyz" "abc
+               def
+               ghi" &&
+       echo 'xyz' 'abc
+               def
+               ghi' &&
+       echo 'xyz' "abc
+               def
+               ghi" &&
+       barfoo
 )