]> git.ipfire.org Git - thirdparty/git.git/commitdiff
chainlint.sed: swallow comments consistently
authorEric Sunshine <sunshine@sunshineco.com>
Mon, 13 Dec 2021 06:30:58 +0000 (01:30 -0500)
committerJunio C Hamano <gitster@pobox.com>
Mon, 13 Dec 2021 22:15:29 +0000 (14:15 -0800)
When checking for broken a &&-chain, chainlint.sed knows that the final
statement in a subshell should not end with `&&`, so it takes care to
make a distinction between the final line which is an actual statement
and any lines which may be mere comments preceding the closing ')'. As
such, it swallows comment lines so that they do not interfere with the
&&-chain check.

However, since `sed` does not provide any sort of real recursion,
chainlint.sed only checks &&-chains in subshells one level deep; it
doesn't do any checking in deeper subshells or in `{...}` blocks within
subshells. Furthermore, on account of potential implementation
complexity, it doesn't check &&-chains within `case` arms.

Due to an oversight, it also doesn't swallow comments inside deep
subshells, `{...}` blocks, or `case` statements, which makes its output
inconsistent (swallowing comments in some cases but not others).
Unfortunately, this inconsistency seeps into the chainlint self-test
"expect" files, which potentially makes it difficult to reuse the
self-tests should a more capable chainlint ever be developed. Therefore,
teach chainlint.sed to consistently swallow comments in all cases.

Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/chainlint.sed
t/chainlint/block-comment.expect [new file with mode: 0644]
t/chainlint/block-comment.test [new file with mode: 0644]
t/chainlint/case-comment.expect [new file with mode: 0644]
t/chainlint/case-comment.test [new file with mode: 0644]
t/chainlint/nested-subshell-comment.expect

index 0e575c0c624003292951c22915495eecd756159d..b1505ef2cd94d314a3f068ea93d74ce8e674d1eb 100644 (file)
@@ -294,6 +294,12 @@ bfolded
 x
 s/?!HERE?!/<</g
 n
+:cascom
+/^[    ]*#/{
+       N
+       s/.*\n//
+       bcascom
+}
 /^[    ]*esac/bslurp
 bcase
 
@@ -322,10 +328,15 @@ x
 :nstslrp
 s/?!HERE?!/<</g
 n
+:nstcom
+# comment -- not closing ")" if in comment
+/^[    ]*#/{
+       N
+       s/.*\n//
+       bnstcom
+}
 # closing ")" on own line -- stop nested slurp
 /^[    ]*)/bnstcl
-# comment -- not closing ")" if in comment
-/^[    ]*#/bnstcnt
 # "$((...))" -- arithmetic expansion; not closing ")"
 /\$(([^)][^)]*))[^)]*$/bnstcnt
 # "$(...)" -- command substitution; not closing ")"
@@ -345,6 +356,12 @@ bchkchn
 x
 s/?!HERE?!/<</g
 n
+:blkcom
+/^[    ]*#/{
+       N
+       s/.*\n//
+       bblkcom
+}
 # closing "}" -- stop block slurp
 /}/bchkchn
 bblock
diff --git a/t/chainlint/block-comment.expect b/t/chainlint/block-comment.expect
new file mode 100644 (file)
index 0000000..d10b2ee
--- /dev/null
@@ -0,0 +1,6 @@
+(
+       {
+               echo a &&
+               echo b
+       }
+)
diff --git a/t/chainlint/block-comment.test b/t/chainlint/block-comment.test
new file mode 100644 (file)
index 0000000..df2beea
--- /dev/null
@@ -0,0 +1,8 @@
+(
+       {
+               # show a
+               echo a &&
+               # show b
+               echo b
+       }
+)
diff --git a/t/chainlint/case-comment.expect b/t/chainlint/case-comment.expect
new file mode 100644 (file)
index 0000000..1e4b054
--- /dev/null
@@ -0,0 +1,8 @@
+(
+       case "$x" in
+       x) foo ;;
+       *)
+               bar
+               ;;
+       esac
+)
diff --git a/t/chainlint/case-comment.test b/t/chainlint/case-comment.test
new file mode 100644 (file)
index 0000000..641c157
--- /dev/null
@@ -0,0 +1,11 @@
+(
+       case "$x" in
+       # found foo
+       x) foo ;;
+       # found other
+       *)
+               # treat it as bar
+               bar
+               ;;
+       esac
+)
index 9138cf386d359267143945d13c1882b0feb5c6f4..be4b27a305bec54678ae4669a1666405ec06f966 100644 (file)
@@ -2,8 +2,6 @@
        foo &&
        (
                bar &&
-               # bottles wobble while fiddles gobble
-               # minor numbers of cows (or do they?)
                baz &&
                snaff
        ) ?!AMP?!