{"thread":{"id":"66475","subject":"test-grep-lint is uncomfortably slow","startedAt":"2026-10-06T16:41:34Z","lastAt":"2026-10-25T06:35:08Z","messageCount":7,"participants":["Junio C Hamano","Jeff King","Tyler Cipriani"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"554302","messageId":"xmqqjynueq81.fsf@gitster.g","threadId":"66475","inReplyTo":null,"subject":"test-grep-lint is uncomfortably slow","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-06T16:41:34Z","receivedAt":"2026-10-06T16:41:34Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Does anyone else feel that make test takes too long to start?  There\nis a noticeable pause after \"rm -f -r 'test-results'\" before the first\nline of real output is shown.  The time in between is likely spent on\nmake test-lint.\n\nHere is a time to run \"make test-lint\" 5 times in a row.\n\n$ time sh -c 'for c in 1 2 3 4 5; do make test-lint; done'\nreal    0m49.221s\nuser    1m37.796s\nsys     0m4.597s\n\nIf we revert the merge c9a92e239f (Merge branch 'mm/test-grep-lint',\n2026-07-19) to disable the grep-lint, it becomes somewhat tolerable.\n\n$ time sh -c 'for c in 1 2 3 4 5; do make test-lint; done'\nreal    0m9.166s\nuser    0m59.156s\nsys     0m2.887s\n\nIs this something we can make go faster?  \n\nThanks.\n\n"},{"id":"555004","messageId":"20261014110330.GB1356555@coredump.intra.peff.net","threadId":"66475","inReplyTo":"xmqqjynueq81.fsf@gitster.g","subject":"Re: test-grep-lint is uncomfortably slow","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2026-10-14T11:03:30Z","receivedAt":"2026-10-14T11:03:30Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Tue, Oct 06, 2026 at 09:41:34AM -0700, Junio C Hamano wrote:\n\n> Does anyone else feel that make test takes too long to start?  There\n> is a noticeable pause after \"rm -f -r 'test-results'\" before the first\n> line of real output is shown.  The time in between is likely spent on\n> make test-lint.\n\nYeah, \"make test-greplint\" takes ~6.5s to run for me. The whole test\nsuite runs in ~111s, so that is over 5% of the time. ;) And of course it\nis more noticeable because there it is all at once before any output\nhappens.\n\nChainlint actually takes similar amounts of CPU time (which is not\nsurprising, because they share the parser) but it runs in parallel.\nWe could probably do the same here.\n\nIt does feel like a lot of processing just to detect one specific type\nof lint error. If we amortized the parsing effort across other lint\nchecks (or even chainlint itself!) that might be more palatable.\n\nThis patch does reduce the cost by ~50%:\n\ndiff --git a/t/greplint.pl b/t/greplint.pl\nindex 23efe27511..68b6eba78d 100644\n--- a/t/greplint.pl\n+++ b/t/greplint.pl\n@@ -190,6 +190,15 @@ sub check_test {\n \t\t}\n \t}\n \treturn unless $body;\n+\t# We can never match a bare \"grep\" if there isn't one in the first\n+\t# place. So we can skip all of the parsing effort. Technically you\n+\t# can have:\n+\t#\n+\t#   gr\\\n+\t#   ep\n+\t#\n+\t# but that is sufficiently insane that I think we can ignore it.\n+\treturn unless $body =~ /grep/;\n \n \tmy $raw_lines = $self->{raw_lines};\n \n\nbut of course it is working against the \"maybe we could amortize the\nparsing cost\" idea.\n\n-Peff\n\n"},{"id":"556201","messageId":"20261025063508.3026136-1-tyler@tylercipriani.com","threadId":"66475","inReplyTo":"xmqqjynueq81.fsf@gitster.g","subject":"[PATCH 0/4] t: run greplint checks inside chainlint","fromName":"Tyler Cipriani","fromEmail":"tyler@tylercipriani.com","sentAt":"2026-10-25T06:35:04Z","receivedAt":"2026-10-25T06:35:04Z","isPatch":true,"sender":{"key":"tyler@tylercipriani.com","avatar":"https://gravatar.com/avatar/552e70f7d13c1043b4b97c2f80d1b6c4e7edcfe0f03dcce45bd6a1145c0529ec?d=mp&s=160"},"body":"As Junio said[0], make test-lint is \"uncomfortably slow\".\n\nmake test-lint runs both t/chainlint.pl and t/greplint.pl which do\nmostly the same thing on mostly the same files, both:\n\n- Open about 1100 files\n- Parse/lex/tokenize the files\n- Run checks - which is the only place chainlint and greplint vary\n\nPlus, greplint processes files serially. So we're doing the same work\ntwice and half the work happens in serial.\n\nFollowing Peff's suggestion, this series teaches chainlint.pl to run the\ngreplint checks while running checks on its own tokens. So \"make\ntest-lint\" parses each file once, in parallel.\n\nKeep greplint.pl as a standalone for GIT_TEST_CHAIN_LINT=0.\n\nPeff's early return (patch 2) speeds up standalone greplint.pl (when\ngreplint does its own parsing).\n\nPeff: Can I add your Signed-off-by to your patch in the series? It does,\nindeed, cut time in half when greplint does its own parsing.\n\nBefore this series:\n\n    $ time sh -c 'for c in 1 2 3 4 5; do make test-lint; done'\n\n    real    0m25.158s\n    user    0m51.557s\n    sys     0m0.899s\n\nAfter this series:\n\n    $ time sh -c 'for c in 1 2 3 4 5; do make test-lint; done'\n\n    real    0m5.972s\n    user    0m41.536s\n    sys     0m0.943s\n\n(nproc == 16 for both)\n\n[0]. https://lore.kernel.org/git/xmqqjynueq81.fsf@gitster.g/\n\nJeff King (1):\n  t/greplint: skip test bodies without the word grep\n\nTyler Cipriani (3):\n  t/interop: replace bare grep with test_grep\n  t/greplint: move parsing and token check to library\n  t/chainlint: add --greplint to run greplint\n\n t/Makefile                             |  30 +++-\n t/chainlint.pl                         |  24 ++-\n t/greplint.pl                          | 240 +------------------------\n t/interop/i5700-protocol-transition.sh |  10 +-\n t/{greplint.pl => lib-greplint.pl}     |  78 ++++----\n 5 files changed, 93 insertions(+), 289 deletions(-)\n copy t/{greplint.pl => lib-greplint.pl} (85%)\n\n\nbase-commit: 7e34d8174dcda3435d1404c9a30bef46afe6c13e\n-- \n2.47.3\n\n\n"},{"id":"556202","messageId":"20261025063508.3026136-2-tyler@tylercipriani.com","threadId":"66475","inReplyTo":"20261025063508.3026136-1-tyler@tylercipriani.com","subject":"[PATCH 1/4] t/interop: replace bare grep with test_grep","fromName":"Tyler Cipriani","fromEmail":"tyler@tylercipriani.com","sentAt":"2026-10-25T06:35:05Z","receivedAt":"2026-10-25T06:35:05Z","isPatch":true,"sender":{"key":"tyler@tylercipriani.com","avatar":"https://gravatar.com/avatar/552e70f7d13c1043b4b97c2f80d1b6c4e7edcfe0f03dcce45bd6a1145c0529ec?d=mp&s=160"},"body":"Use test_grep for grep assertions in interop files.\n\nIn preparation for combining chainlint and greplint into a single test\ncommand, ensure that chainlint and greplint pass on their combined list\nof shell scripts under test.\n\nSigned-off-by: Tyler Cipriani <tyler@tylercipriani.com>\n---\n t/interop/i5700-protocol-transition.sh | 10 +++++-----\n 1 file changed, 5 insertions(+), 5 deletions(-)\n\ndiff --git a/t/interop/i5700-protocol-transition.sh b/t/interop/i5700-protocol-transition.sh\nindex 97e8e580ef..b610a64dfc 100755\n--- a/t/interop/i5700-protocol-transition.sh\n+++ b/t/interop/i5700-protocol-transition.sh\n@@ -24,7 +24,7 @@ test_expect_success \"git:// clone with $VERSION_A and protocol v1\" '\n \tgit.a -C child log -1 --format=%s >actual &&\n \tgit.b -C \"$repo\" log -1 --format=%s >expect &&\n \ttest_cmp expect actual &&\n-\tgrep \"version=1\" log\n+\ttest_grep \"version=1\" log\n '\n \n test_expect_success \"git:// fetch with $VERSION_A and protocol v1\" '\n@@ -35,8 +35,8 @@ test_expect_success \"git:// fetch with $VERSION_A and protocol v1\" '\n \tgit.a -C child log -1 --format=%s FETCH_HEAD >actual &&\n \n \ttest_cmp expect actual &&\n-\tgrep \"version=1\" log &&\n-\t! grep \"version 1\" log\n+\ttest_grep \"version=1\" log &&\n+\ttest_grep ! \"version 1\" log\n '\n \n stop_git_daemon\n@@ -51,7 +51,7 @@ test_expect_success \"file:// clone with $VERSION_A and protocol v1\" '\n \tgit.a -C child2 log -1 --format=%s >actual &&\n \tgit.b -C parent log -1 --format=%s >expect &&\n \ttest_cmp expect actual &&\n-\t! grep \"version 1\" log\n+\ttest_grep ! \"version 1\" log\n '\n \n test_expect_success \"file:// fetch with $VERSION_A and protocol v1\" '\n@@ -62,7 +62,7 @@ test_expect_success \"file:// fetch with $VERSION_A and protocol v1\" '\n \tgit.a -C child2 log -1 --format=%s FETCH_HEAD >actual &&\n \n \ttest_cmp expect actual &&\n-\t! grep \"version 1\" log\n+\ttest_grep ! \"version 1\" log\n '\n \n test_done\n-- \n2.47.3\n\n\n"},{"id":"556203","messageId":"20261025063508.3026136-3-tyler@tylercipriani.com","threadId":"66475","inReplyTo":"20261025063508.3026136-1-tyler@tylercipriani.com","subject":"[PATCH 2/4] t/greplint: skip test bodies without the word grep","fromName":"Tyler Cipriani","fromEmail":"tyler@tylercipriani.com","sentAt":"2026-10-25T06:35:06Z","receivedAt":"2026-10-25T06:35:06Z","isPatch":true,"sender":{"key":"tyler@tylercipriani.com","avatar":"https://gravatar.com/avatar/552e70f7d13c1043b4b97c2f80d1b6c4e7edcfe0f03dcce45bd6a1145c0529ec?d=mp&s=160"},"body":"From: Jeff King <peff@peff.net>\n\ngreplint.pl spends time parsing test bodies that cannot contain a bare\ngrep. Check for the string \"grep\" first and skip the parse when it isn't\nthere. This reduces the cost of \"make test-greplint\" by ~50%.\n\nSigned-off-by: Tyler Cipriani <tyler@tylercipriani.com>\n---\n t/greplint.pl | 9 +++++++++\n 1 file changed, 9 insertions(+)\n\ndiff --git a/t/greplint.pl b/t/greplint.pl\nindex 23efe27511..68b6eba78d 100644\n--- a/t/greplint.pl\n+++ b/t/greplint.pl\n@@ -190,6 +190,15 @@ sub check_test {\n \t\t}\n \t}\n \treturn unless $body;\n+\t# We can never match a bare \"grep\" if there isn't one in the first\n+\t# place. So we can skip all of the parsing effort. Technically you\n+\t# can have:\n+\t#\n+\t#   gr\\\n+\t#   ep\n+\t#\n+\t# but that is sufficiently insane that I think we can ignore it.\n+\treturn unless $body =~ /grep/;\n \n \tmy $raw_lines = $self->{raw_lines};\n \n-- \n2.47.3\n\n\n"},{"id":"556204","messageId":"20261025063508.3026136-4-tyler@tylercipriani.com","threadId":"66475","inReplyTo":"20261025063508.3026136-1-tyler@tylercipriani.com","subject":"[PATCH 3/4] t/greplint: move parsing and token check to library","fromName":"Tyler Cipriani","fromEmail":"tyler@tylercipriani.com","sentAt":"2026-10-25T06:35:07Z","receivedAt":"2026-10-25T06:35:07Z","isPatch":true,"sender":{"key":"tyler@tylercipriani.com","avatar":"https://gravatar.com/avatar/552e70f7d13c1043b4b97c2f80d1b6c4e7edcfe0f03dcce45bd6a1145c0529ec?d=mp&s=160"},"body":"Move greplint.pl's parsing and token check work to a library. Modify the\ngreplint.pl to use the library. Break out actual checks into\ncheck_tokens() sub.\n\nIn preparation to combine the work of chainlint and greplint into a\nsingle parser invocation.\n\nSigned-off-by: Tyler Cipriani <tyler@tylercipriani.com>\n---\n t/greplint.pl                      | 249 +----------------------------\n t/{greplint.pl => lib-greplint.pl} |  69 ++++----\n 2 files changed, 37 insertions(+), 281 deletions(-)\n copy t/{greplint.pl => lib-greplint.pl} (87%)\n\ndiff --git a/t/greplint.pl b/t/greplint.pl\nindex 68b6eba78d..e277647f11 100644\n--- a/t/greplint.pl\n+++ b/t/greplint.pl\n@@ -1,257 +1,16 @@\n #!/usr/bin/env perl\n \n-# Detect bare 'grep' used as a test assertion where 'test_grep'\n-# should be used, and '! test_grep' where 'test_grep !' should\n-# be used.\n-#\n-# The shared shell parser tokenizes test bodies so that 'grep'\n-# inside heredocs, command substitutions like $(grep ...), and\n-# quoted strings is collapsed into a single token and never seen\n-# by our check.  A line-oriented approach would need to track\n-# heredoc delimiters, nested $() depth, and cross-line pipe\n-# state to avoid false positives on patterns like:\n-#\n-#   write_script foo.sh <<-\\EOF\n-#   grep pattern file    # data, not an assertion\n-#   EOF\n-#\n-# The Lexer already handles these.\n-\n+# Detect bare 'grep' used as a test assertion\n use warnings;\n use strict;\n use File::Basename;\n do(dirname($0) . \"/lib-shell-parser.pl\")\n \tor die \"$0: failed to load lib-shell-parser.pl: $@$!\\n\";\n+do(dirname($0) . \"/lib-greplint.pl\")\n+\tor die \"$0: failed to load lib-greplint.pl: $@$!\\n\";\n \n my $exit_code = 0;\n \n-# GrepLintParser inherits ScriptParser's ability to find\n-# test_expect_success/failure blocks and call check_test()\n-# on each body.  We override check_test() to walk the token\n-# stream looking for bare grep assertions.\n-package GrepLintParser;\n-\n-our @ISA = ('ScriptParser');\n-\n-# After these tokens, the next token is a command word.\n-# For example, in 'echo foo && grep bar file', the 'grep'\n-# after '&&' is at command position and should be flagged.\n-my %cmd_start = map { $_ => 1 } qw(&& || ; ;; do then else elif), \"\\n\", '{', '(';\n-\n-# Tokens indicating grep's output is piped or redirected.\n-my %filter_op = map { $_ => 1 } qw(| > >> <);\n-\n-# A token is at \"command word\" position if the shell would\n-# interpret it as a program name rather than an argument.\n-# Only 'grep' at command position is an assertion we should\n-# flag; 'grep' as an argument ('test_must_fail grep') or\n-# value ('for cmd in grep sed') is not.\n-sub is_command_word {\n-\tmy ($tokens, $pos) = @_;\n-\treturn 1 if $pos == 0;\n-\tfor (my $j = $pos - 1; $j >= 0; $j--) {\n-\t\tmy $t = $tokens->[$j]->[0];\n-\t\t# After a separator or pipe, a new command starts.\n-\t\treturn 1 if $cmd_start{$t} || $t eq '|';\n-\t\t# After '}' or ')', what follows is a separator or\n-\t\t# redirect on the compound command, not a new command.\n-\t\treturn 0 if $t eq '}' || $t eq ')';\n-\t\t# '!' is a prefix that does not consume command\n-\t\t# position; keep scanning to find what precedes it.\n-\t\tnext if $t eq '!';\n-\t\t# Any other word means we are past the command word.\n-\t\treturn 0;\n-\t}\n-\treturn 1;\n-}\n-\n-# lint_ok() reports whether a bare grep carries a trailing\n-# '# lint-ok' comment telling this linter to skip it.\n-#\n-# In practice this is needed for just one case: a grep acting\n-# as a data filter whose output is consumed by a redirect or\n-# pipe on an enclosing compound command (such as a subshell or\n-# brace group) rather than by grep's own pipeline, e.g.\n-#\n-#\t( grep ... && # lint-ok\n-#\t  sed ... ) >out\n-#\n-#\t{ grep ... || : # lint-ok\n-#\t} >out\n-#\n-# is_filter() only scans grep's own pipeline: it stops at the\n-# separator before the compound command closes and never sees\n-# the outer redirect, so it would flag such a grep as an\n-# assertion.  A grep that really is an assertion is better\n-# written as test_grep (or a guarded test_grep when the file's\n-# presence is conditional) than annotated with lint-ok.\n-sub lint_ok {\n-\tmy ($raw_lines, $ln) = @_;\n-\tif ($ln < 1 || $ln > @$raw_lines) {\n-\t\twarn \"lint_ok: line number $ln out of range (1..\" .\n-\t\t    scalar(@$raw_lines) . \")\\n\";\n-\t\treturn 0;\n-\t}\n-\treturn $raw_lines->[$ln - 1] =~ /lint-ok/;\n-}\n-\n-# Grep is a filter (not an assertion) if it receives piped\n-# input or sends its output to a pipe or redirect.  Check\n-# both directions from grep's position in the token stream.\n-sub is_filter {\n-\tmy ($tokens, $pos) = @_;\n-\t# Backward: is grep receiving piped input?\n-\t# Newlines don't break pipes ('cmd |\\n grep' is one\n-\t# pipeline), so skip past them.\n-\tfor (my $j = $pos - 1; $j >= 0; $j--) {\n-\t\tmy $t = $tokens->[$j]->[0];\n-\t\treturn 1 if $t eq '|';\n-\t\tnext if $t eq \"\\n\";\n-\t\tlast if $cmd_start{$t} || $t eq '}' || $t eq ')';\n-\t}\n-\t# Forward: is grep piping or redirecting output?\n-\t# Unlike the backward scan, we do not skip newlines here:\n-\t# a bare newline is a command boundary, and redirects or\n-\t# pipes must appear on the same line as grep (or after a\n-\t# line continuation, which the Lexer consumes).\n-\tfor (my $j = $pos + 1; $j < @$tokens; $j++) {\n-\t\tmy $t = $tokens->[$j]->[0];\n-\t\treturn 0 if $cmd_start{$t};\n-\t\treturn 1 if $filter_op{$t};\n-\t}\n-\treturn 0;\n-}\n-\n-# Map a body-relative line number to a file line number.\n-# For double-quoted bodies, backslash-continuation lines\n-# (\\<newline>) are consumed by the Lexer without appearing\n-# in the body text, so the inner parser sees fewer lines\n-# than the source file has.  We walk the source lines to\n-# count continuations and adjust accordingly.\n-sub body_to_file_line {\n-\tmy ($body_lineno, $body_token, $raw_lines, $body_start) = @_;\n-\tmy $body_text = $body_token->[0];\n-\tmy $body_end_line = $body_token->[4];\n-\tunless ($body_start && $body_start >= 1) {\n-\t\twarn \"body_start is not a positive integer\\n\";\n-\t\treturn $body_lineno;\n-\t}\n-\tmy $file_lineno = $body_lineno + $body_start - 1;\n-\t# Only double-quoted bodies have line splices.\n-\treturn $file_lineno unless $body_text =~ /^\"/;\n-\tmy $adj = 0;\n-\tmy $lines_seen = 0;\n-\tunless ($body_end_line && $body_end_line >= $body_start) {\n-\t\twarn \"body_end_line is not set for double-quoted body\\n\";\n-\t\treturn $file_lineno;\n-\t}\n-\tmy $end = $body_end_line;\n-\tif ($end > @$raw_lines) {\n-\t\twarn \"body_end_line ($end) exceeds file length (\" .\n-\t\t    scalar(@$raw_lines) . \")\\n\";\n-\t\treturn $file_lineno;\n-\t}\n-\tmy $src_ln = $body_start;\n-\twhile ($src_ln <= $end && $lines_seen < $body_lineno) {\n-\t\tmy $line = $raw_lines->[$src_ln - 1];\n-\t\t# Odd trailing backslashes = continuation (\\<nl>).\n-\t\t# Even = escaped backslashes (\\\\), not a continuation.\n-\t\tif ($line =~ /(\\\\*)$/ && length($1) % 2 == 1) {\n-\t\t\t$adj++;\n-\t\t} else {\n-\t\t\t$lines_seen++;\n-\t\t}\n-\t\t$src_ln++;\n-\t}\n-\tif ($lines_seen < $body_lineno) {\n-\t\twarn \"body_lineno ($body_lineno) not found within body range \" .\n-\t\t    \"($body_start..$end)\\n\";\n-\t}\n-\treturn $file_lineno + $adj;\n-}\n-\n-# ScriptParser calls this for each test body found in the script.\n-sub check_test {\n-\tmy $self = shift @_;\n-\tmy $title = ScriptParser::unwrap(shift @_);\n-\tmy $body_token = shift @_;\n-\tmy $body_start = $body_token->[3];\n-\tmy $body = ScriptParser::unwrap($body_token);\n-\t# Handle heredoc-style test bodies:\n-\t#   test_expect_success 'title' - <<\\EOF\n-\t#   grep pattern file\n-\t#   EOF\n-\t# The '-' signals that the body follows as a heredoc.\n-\tif ($body eq '-') {\n-\t\tmy $herebody = shift @_;\n-\t\tif ($herebody) {\n-\t\t\t$body = $herebody->{content};\n-\t\t\t$body_start = $herebody->{start_line};\n-\t\t}\n-\t}\n-\treturn unless $body;\n-\t# We can never match a bare \"grep\" if there isn't one in the first\n-\t# place. So we can skip all of the parsing effort. Technically you\n-\t# can have:\n-\t#\n-\t#   gr\\\n-\t#   ep\n-\t#\n-\t# but that is sufficiently insane that I think we can ignore it.\n-\treturn unless $body =~ /grep/;\n-\n-\tmy $raw_lines = $self->{raw_lines};\n-\n-\t# The outer parser gives us the body as an opaque string.\n-\t# Parse it to get individual tokens with command boundaries.\n-\tmy $parser = ShellParser->new(\\$body);\n-\tmy @tokens = $parser->parse();\n-\n-\tmy $file = $self->{file};\n-\n-\tfor (my $i = 0; $i < @tokens; $i++) {\n-\t\tmy $text = $tokens[$i]->[0];\n-\t\tnext unless is_command_word(\\@tokens, $i);\n-\n-\t\tmy $token_lineno = $tokens[$i]->[3];\n-\t\tunless (defined($token_lineno) && $token_lineno >= 1) {\n-\t\t\twarn \"token has no line number\\n\";\n-\t\t\tnext;\n-\t\t}\n-\t\tmy $file_lineno = body_to_file_line(\n-\t\t\t$token_lineno,\n-\t\t\t$body_token, $raw_lines, $body_start);\n-\n-\t\t# '!' negates the exit code without consuming command\n-\t\t# position.  '! test_grep' is an anti-pattern because\n-\t\t# test_grep only prints diagnostics on grep failure,\n-\t\t# and '!' inverts after that decision is already made.\n-\t\tif ($text eq '!') {\n-\t\t\tif ($i + 1 < @tokens &&\n-\t\t\t    $tokens[$i + 1]->[0] eq 'test_grep' &&\n-\t\t\t    !lint_ok($raw_lines, $file_lineno)) {\n-\t\t\t\tprint \"$file:$file_lineno: error: \",\n-\t\t\t\t    'use \"test_grep !\" instead of ',\n-\t\t\t\t    '\"! test_grep\"', \"\\n\";\n-\t\t\t\t$exit_code = 1;\n-\t\t\t}\n-\t\t\tnext;\n-\t\t}\n-\n-\t\t# Bare grep as a command (not a filter) is a test\n-\t\t# assertion that should use test_grep for better\n-\t\t# failure diagnostics.\n-\t\tif ($text eq 'grep' &&\n-\t\t    !is_filter(\\@tokens, $i) &&\n-\t\t    !lint_ok($raw_lines, $file_lineno)) {\n-\t\t\tprint \"$file:$file_lineno: error: \",\n-\t\t\t    \"bare grep outside pipeline \",\n-\t\t\t    \"(use test_grep)\\n\";\n-\t\t\t$exit_code = 1;\n-\t\t}\n-\t}\n-}\n-\n package main;\n \n for my $file (@ARGV) {\n@@ -263,5 +22,7 @@ package main;\n \t$parser->{file} = $file;\n \t$parser->{raw_lines} = \\@raw_lines;\n \t$parser->parse();\n+\tprint \"$_\\n\" for @{$parser->{output}};\n+\t$exit_code ||= $parser->{nerrs} > 0;\n }\n exit $exit_code;\ndiff --git a/t/greplint.pl b/t/lib-greplint.pl\nsimilarity index 87%\ncopy from t/greplint.pl\ncopy to t/lib-greplint.pl\nindex 68b6eba78d..16b85b8d3b 100644\n--- a/t/greplint.pl\n+++ b/t/lib-greplint.pl\n@@ -1,5 +1,3 @@\n-#!/usr/bin/env perl\n-\n # Detect bare 'grep' used as a test assertion where 'test_grep'\n # should be used, and '! test_grep' where 'test_grep !' should\n # be used.\n@@ -19,16 +17,13 @@\n \n use warnings;\n use strict;\n-use File::Basename;\n-do(dirname($0) . \"/lib-shell-parser.pl\")\n-\tor die \"$0: failed to load lib-shell-parser.pl: $@$!\\n\";\n-\n-my $exit_code = 0;\n \n # GrepLintParser inherits ScriptParser's ability to find\n # test_expect_success/failure blocks and call check_test()\n # on each body.  We override check_test() to walk the token\n-# stream looking for bare grep assertions.\n+# stream looking for bare grep assertions vs. test_grep.\n+#\n+# Requires lib-shell-parser.pl to be loaded.\n package GrepLintParser;\n \n our @ISA = ('ScriptParser');\n@@ -207,13 +202,25 @@ sub check_test {\n \tmy $parser = ShellParser->new(\\$body);\n \tmy @tokens = $parser->parse();\n \n-\tmy $file = $self->{file};\n+\tmy @errors = check_tokens(\n+\t\t$self->{file}, $raw_lines,\n+\t\t$body_token, $body_start, \\@tokens);\n \n-\tfor (my $i = 0; $i < @tokens; $i++) {\n-\t\tmy $text = $tokens[$i]->[0];\n-\t\tnext unless is_command_word(\\@tokens, $i);\n+\tpush(@{$self->{output}}, @errors);\n+\t$self->{nerrs} += @errors;\n+}\n \n-\t\tmy $token_lineno = $tokens[$i]->[3];\n+# check_tokens() walks a parsed test body and returns a list of\n+# error messages.\n+sub check_tokens {\n+\tmy ($file, $raw_lines, $body_token, $body_start, $tokens) = @_;\n+\tmy @errors;\n+\n+\tfor (my $i = 0; $i < @$tokens; $i++) {\n+\t\tmy $text = $tokens->[$i]->[0];\n+\t\tnext unless is_command_word($tokens, $i);\n+\n+\t\tmy $token_lineno = $tokens->[$i]->[3];\n \t\tunless (defined($token_lineno) && $token_lineno >= 1) {\n \t\t\twarn \"token has no line number\\n\";\n \t\t\tnext;\n@@ -227,13 +234,12 @@ sub check_test {\n \t\t# test_grep only prints diagnostics on grep failure,\n \t\t# and '!' inverts after that decision is already made.\n \t\tif ($text eq '!') {\n-\t\t\tif ($i + 1 < @tokens &&\n-\t\t\t    $tokens[$i + 1]->[0] eq 'test_grep' &&\n+\t\t\tif ($i + 1 < @$tokens &&\n+\t\t\t    $tokens->[$i + 1]->[0] eq 'test_grep' &&\n \t\t\t    !lint_ok($raw_lines, $file_lineno)) {\n-\t\t\t\tprint \"$file:$file_lineno: error: \",\n-\t\t\t\t    'use \"test_grep !\" instead of ',\n-\t\t\t\t    '\"! test_grep\"', \"\\n\";\n-\t\t\t\t$exit_code = 1;\n+\t\t\t\tpush(@errors, \"$file:$file_lineno: error: \" .\n+\t\t\t\t    'use \"test_grep !\" instead of ' .\n+\t\t\t\t    '\"! test_grep\"');\n \t\t\t}\n \t\t\tnext;\n \t\t}\n@@ -242,26 +248,15 @@ sub check_test {\n \t\t# assertion that should use test_grep for better\n \t\t# failure diagnostics.\n \t\tif ($text eq 'grep' &&\n-\t\t    !is_filter(\\@tokens, $i) &&\n+\t\t    !is_filter($tokens, $i) &&\n \t\t    !lint_ok($raw_lines, $file_lineno)) {\n-\t\t\tprint \"$file:$file_lineno: error: \",\n-\t\t\t    \"bare grep outside pipeline \",\n-\t\t\t    \"(use test_grep)\\n\";\n-\t\t\t$exit_code = 1;\n+\t\t\tpush(@errors, \"$file:$file_lineno: error: \" .\n+\t\t\t    \"bare grep outside pipeline \" .\n+\t\t\t    \"(use test_grep)\");\n \t\t}\n \t}\n+\n+\treturn @errors;\n }\n \n-package main;\n-\n-for my $file (@ARGV) {\n-\topen(my $fh, '<:unix:crlf', $file) or die \"$0: $file: $!\\n\";\n-\tmy @raw_lines = <$fh>;\n-\tclose $fh;\n-\tmy $s = join('', @raw_lines);\n-\tmy $parser = GrepLintParser->new(\\$s);\n-\t$parser->{file} = $file;\n-\t$parser->{raw_lines} = \\@raw_lines;\n-\t$parser->parse();\n-}\n-exit $exit_code;\n+1;\n-- \n2.47.3\n\n\n"},{"id":"556205","messageId":"20261025063508.3026136-5-tyler@tylercipriani.com","threadId":"66475","inReplyTo":"20261025063508.3026136-1-tyler@tylercipriani.com","subject":"[PATCH 4/4] t/chainlint: add --greplint to run greplint","fromName":"Tyler Cipriani","fromEmail":"tyler@tylercipriani.com","sentAt":"2026-10-25T06:35:08Z","receivedAt":"2026-10-25T06:35:08Z","isPatch":true,"sender":{"key":"tyler@tylercipriani.com","avatar":"https://gravatar.com/avatar/552e70f7d13c1043b4b97c2f80d1b6c4e7edcfe0f03dcce45bd6a1145c0529ec?d=mp&s=160"},"body":"Save duplicate file handling and parsing work (and gain parallel file\nprocessing) by teaching chainlint to do the work of greplint.\n\nAdd --greplint option to chainlint.pl so that it passes tokens to\nGrepLintParser::check_tokens(). This change allows chainlint.pl to do\ngreplint checks after chainlint has done all the hard work of file\nhandling and parsing.\n\nUpdate Makefile with new target that invokes chainlint.pl --greplint. In\nthe common case, make test-lint will use the new chainlint target.\nContinue to respect and set GIT_TEST_CHAIN_LINT=0.\n\nEnsure test-chainlint/test-greplint run on the same set of test files,\nwhich expands the set for both.\n\nLocal laptop testing shows make test-lint went from around 4.5s before\nthis change to less than 1s after (with -j$(nproc) where nproc == 16).\n\nReported-by: Junio C Hamano <gitster@pobox.com>\nSuggested-by: Jeff King <peff@peff.net>\nSigned-off-by: Tyler Cipriani <tyler@tylercipriani.com>\n---\n t/Makefile     | 30 ++++++++++++++++++++++--------\n t/chainlint.pl | 24 ++++++++++++++++++++----\n 2 files changed, 42 insertions(+), 12 deletions(-)\n\ndiff --git a/t/Makefile b/t/Makefile\nindex e3249879c4..bfe861f6e0 100644\n--- a/t/Makefile\n+++ b/t/Makefile\n@@ -56,9 +56,9 @@ UNIT_TEST_PROGRAMS += unit-tests/bin/unit-tests$(X)\n UNIT_TESTS = $(sort $(UNIT_TEST_PROGRAMS))\n UNIT_TESTS_NO_DIR = $(notdir $(UNIT_TESTS))\n \n-# `test-chainlint` (which is a dependency of `test-lint`, `test` and `prove`)\n-# checks all tests in all scripts via a single invocation, so tell individual\n-# scripts not to run the external \"chainlint.pl\" script themselves\n+# `test-chainlint-greplint` (which is a dependency of `test-lint`, `test`, and\n+# `prove`) checks all tests in all scripts via a single invocation, so tell\n+# individual scripts not to run the external \"chainlint.pl\" script themselves\n CHAINLINTSUPPRESS = GIT_TEST_EXT_CHAIN_LINT=0 && export GIT_TEST_EXT_CHAIN_LINT &&\n \n all:: $(DEFAULT_TEST_TARGET)\n@@ -127,6 +127,7 @@ check-chainlint:\n clean-greplint:\n \t$(RM) -r '$(GREPLINTTMP_SQ)'\n \n+# chainlint --jobs=1 needed: parallel workers emit output in arbitrary order\n check-greplint:\n \t@mkdir -p '$(GREPLINTTMP_SQ)' && \\\n \t'$(PERL_PATH_SQ)' greplint-cat.pl '$(GREPLINTTMP_SQ)' $(GREPLINTTESTS) && \\\n@@ -134,6 +135,11 @@ check-greplint:\n \t\t$(patsubst %,greplint/%.test,$(GREPLINTTESTS)) \\\n \t\t>'$(GREPLINTTMP_SQ)'/actual 2>&1 || true; } && \\\n \tdiff -u '$(GREPLINTTMP_SQ)'/expect '$(GREPLINTTMP_SQ)'/actual\n+\t@$(CHAINLINT) --greplint --jobs=1 \\\n+\t\t$(patsubst %,greplint/%.test,$(GREPLINTTESTS)) 2>&1 | \\\n+\t\tsed -e '/^# chainlint: /d' -e '/^[0-9][0-9]* /d' \\\n+\t\t>'$(GREPLINTTMP_SQ)'/actual-chainlint && \\\n+\tdiff -u '$(GREPLINTTMP_SQ)'/expect '$(GREPLINTTMP_SQ)'/actual-chainlint\n \n check-meson:\n \t@# awk acts up when trying to match single quotes, so we use \\047 instead.\n@@ -154,12 +160,16 @@ check-meson:\n test-lint: test-lint-duplicates test-lint-executable \\\n \ttest-lint-filenames\n ifneq ($(PERL_PATH),)\n-test-lint: test-lint-shell-syntax test-greplint\n+test-lint: test-lint-shell-syntax\n else\n GIT_TEST_CHAIN_LINT = 0\n endif\n ifneq ($(GIT_TEST_CHAIN_LINT),0)\n-test-lint: test-chainlint\n+test-lint: test-chainlint-greplint\n+else\n+ifneq ($(PERL_PATH),)\n+test-lint: test-greplint\n+endif\n endif\n \n test-lint-duplicates:\n@@ -176,7 +186,7 @@ test-lint-shell-syntax:\n \t@'$(PERL_PATH_SQ)' check-non-portable-shell.pl $(T) $(THELPERS) $(TPERF)\n \n test-greplint:\n-\t@'$(PERL_PATH_SQ)' greplint.pl $(T) $(THELPERS) $(TPERF)\n+\t@'$(PERL_PATH_SQ)' greplint.pl $(T) $(THELPERS) $(TPERF) $(TINTEROP)\n \n test-lint-filenames:\n \t@# We do *not* pass a glob to ls-files but use grep instead, to catch\n@@ -186,8 +196,11 @@ test-lint-filenames:\n \t\ttest -z \"$$bad\" || { \\\n \t\techo >&2 \"non-portable file name(s): $$bad\"; exit 1; }\n \n+test-chainlint-greplint:\n+\t@$(CHAINLINT) --greplint $(T) $(THELPERS) $(TPERF) $(TINTEROP)\n+\n test-chainlint:\n-\t@$(CHAINLINT) $(T) $(TLIBS) $(TPERF) $(TINTEROP)\n+\t@$(CHAINLINT) $(T) $(THELPERS) $(TPERF) $(TINTEROP)\n \n aggregate-results-and-cleanup: $(T)\n \t$(MAKE) aggregate-results\n@@ -204,7 +217,8 @@ perf:\n \n .PHONY: pre-clean $(T) aggregate-results clean valgrind perf \\\n \tcheck-chainlint clean-chainlint test-chainlint \\\n-\tcheck-greplint clean-greplint test-greplint $(UNIT_TESTS)\n+\tcheck-greplint clean-greplint test-greplint \\\n+\ttest-chainlint-greplint $(UNIT_TESTS)\n \n .PHONY: libgit-sys-test libgit-rs-test\n libgit-sys-test:\ndiff --git a/t/chainlint.pl b/t/chainlint.pl\nindex cededf15ee..dda3617add 100755\n--- a/t/chainlint.pl\n+++ b/t/chainlint.pl\n@@ -22,10 +22,13 @@\n my $jobs = -1;\n my $show_stats;\n my $emit_all;\n+my $greplint;\n \n use File::Basename;\n do(dirname($0) . \"/lib-shell-parser.pl\")\n \tor die \"$0: failed to load lib-shell-parser.pl: $@$!\\n\";\n+do(dirname($0) . \"/lib-greplint.pl\")\n+\tor die \"$0: failed to load lib-greplint.pl: $@$!\\n\";\n \n # TestParser is a subclass of ShellParser which, beyond parsing shell script\n # code, is also imbued with semantic knowledge of test construction, and checks\n@@ -147,9 +150,9 @@ sub format_problem {\n sub check_test {\n \tmy $self = shift @_;\n \tmy $title = ScriptParser::unwrap(shift @_);\n-\tmy $body = shift @_;\n-\tmy $lineno = $body->[3];\n-\t$body = ScriptParser::unwrap($body);\n+\tmy $body_token = shift @_;\n+\tmy $lineno = $body_token->[3];\n+\tmy $body = ScriptParser::unwrap($body_token);\n \tif ($body eq '-') {\n \t\tmy $herebody = shift @_;\n \t\t$body = $herebody->{content};\n@@ -160,6 +163,12 @@ sub check_test {\n \tmy @tokens = $parser->parse();\n \tmy $problems = $parser->{problems};\n \t$self->{nerrs} += @$problems;\n+\tif ($greplint) {\n+\t\tmy @grep_errors = GrepLintParser::check_tokens($self->{file},\n+\t\t\t$self->{raw_lines}, $body_token, $lineno, \\@tokens);\n+\t\tpush(@{$self->{greplint}}, @grep_errors);\n+\t\t$self->{nerrs} += @grep_errors;\n+\t}\n \treturn unless $emit_all || @$problems;\n \tmy $c = main::fd_colors(1);\n \tmy ($erropen, $errclose) = -t 1 ? (\"$c->{rev}$c->{red}\", $c->{reset}) : ('?!', '?!');\n@@ -296,15 +305,21 @@ sub check_script {\n \t\t\t$emit->(\"?!ERR?! $path: $!\\n\");\n \t\t\tnext;\n \t\t}\n-\t\tmy $s = do { local $/; <$fh> };\n+\t\tmy @raw_lines = <$fh>;\n \t\tclose($fh);\n+\t\tmy $s = join('', @raw_lines);\n \t\tmy $parser = ChainlintParser->new(\\$s);\n+\t\t$parser->{file} = $path;\n+\t\t$parser->{raw_lines} = \\@raw_lines;\n+\t\t$parser->{greplint} = [];\n \t\t1 while $parser->parse_cmd();\n \t\tif (@{$parser->{output}}) {\n \t\t\tmy $c = fd_colors(1);\n \t\t\tmy $s = join('', @{$parser->{output}});\n \t\t\t$emit->(\"$c->{bold}$c->{blue}# chainlint: $path$c->{reset}\\n\" . $s);\n \t\t}\n+\t\t$emit->(join('', map { \"$_\\n\" } @{$parser->{greplint}}))\n+\t\t\tif @{$parser->{greplint}};\n \t\t$ntests += $parser->{ntests};\n \t\t$nerrs += $parser->{nerrs};\n \t}\n@@ -323,6 +338,7 @@ sub exit_code {\n Getopt::Long::Configure(qw{bundling});\n GetOptions(\n \t\"emit-all!\" => \\$emit_all,\n+\t\"greplint!\" => \\$greplint,\n \t\"jobs|j=i\" => \\$jobs,\n \t\"stats|show-stats!\" => \\$show_stats) or die(\"option error\\n\");\n $jobs = ncores() if $jobs < 1;\n-- \n2.47.3\n\n\n"}]}