Skip to content

Commit 31da22d

Browse files
sunshinecogitster
authored andcommitted
chainlint.sed: swallow comments consistently
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 34ba05c commit 31da22d

File tree

6 files changed

+52
-4
lines changed

6 files changed

+52
-4
lines changed

t/chainlint.sed

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,12 @@ bfolded
294294
x
295295
s/?!HERE?!/<</g
296296
n
297+
:cascom
298+
/^[ ]*#/{
299+
N
300+
s/.*\n//
301+
bcascom
302+
}
297303
/^[ ]*esac/bslurp
298304
bcase
299305

@@ -322,10 +328,15 @@ x
322328
:nstslrp
323329
s/?!HERE?!/<</g
324330
n
331+
:nstcom
332+
# comment -- not closing ")" if in comment
333+
/^[ ]*#/{
334+
N
335+
s/.*\n//
336+
bnstcom
337+
}
325338
# closing ")" on own line -- stop nested slurp
326339
/^[ ]*)/bnstcl
327-
# comment -- not closing ")" if in comment
328-
/^[ ]*#/bnstcnt
329340
# "$((...))" -- arithmetic expansion; not closing ")"
330341
/\$(([^)][^)]*))[^)]*$/bnstcnt
331342
# "$(...)" -- command substitution; not closing ")"
@@ -345,6 +356,12 @@ bchkchn
345356
x
346357
s/?!HERE?!/<</g
347358
n
359+
:blkcom
360+
/^[ ]*#/{
361+
N
362+
s/.*\n//
363+
bblkcom
364+
}
348365
# closing "}" -- stop block slurp
349366
/}/bchkchn
350367
bblock

t/chainlint/block-comment.expect

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
(
2+
{
3+
echo a &&
4+
echo b
5+
}
6+
)

t/chainlint/block-comment.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
(
2+
{
3+
# show a
4+
echo a &&
5+
# show b
6+
echo b
7+
}
8+
)

t/chainlint/case-comment.expect

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
(
2+
case "$x" in
3+
x) foo ;;
4+
*)
5+
bar
6+
;;
7+
esac
8+
)

t/chainlint/case-comment.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
(
2+
case "$x" in
3+
# found foo
4+
x) foo ;;
5+
# found other
6+
*)
7+
# treat it as bar
8+
bar
9+
;;
10+
esac
11+
)

t/chainlint/nested-subshell-comment.expect

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
foo &&
33
(
44
bar &&
5-
# bottles wobble while fiddles gobble
6-
# minor numbers of cows (or do they?)
75
baz &&
86
snaff
97
) ?!AMP?!

0 commit comments

Comments
 (0)