threads / discuss / 66475

test-grep-lint is uncomfortably slow

Subject: test-grep-lint is uncomfortably slow

## tl;dr

Junio C Hamano measured about 40 extra seconds across five runs of test-lint since the grep-lint topic merged, and Jeff King proposed ways to cut it. Read the story.

replies: 6people: 3as markdown or json

Junio C Hamano· Oct 6, 2026, 16:41 UTC · lore

Does anyone else feel that make test takes too long to start? There is a noticeable pause after "rm -f -r 'test-results'" before the first line of real output is shown. The time in between is likely spent on make test-lint.

Here is a time to run "make test-lint" 5 times in a row.

$ time sh -c 'for c in 1 2 3 4 5; do make test-lint; done' real 0m49.221s user 1m37.796s sys 0m4.597s

If we revert the merge c9a92e239f (Merge branch 'mm/test-grep-lint', 2026-07-19) to disable the grep-lint, it becomes somewhat tolerable.

$ time sh -c 'for c in 1 2 3 4 5; do make test-lint; done' real 0m9.166s user 0m59.156s sys 0m2.887s

Is this something we can make go faster?  
Thanks.
Jeff King· Oct 14, 2026, 11:03 UTC · re: Junio C Hamano · lore

Re: test-grep-lint is uncomfortably slow

On Tue, Oct 06, 2026 at 09:41:34AM -0700, Junio C Hamano wrote:
> Does anyone else feel that make test takes too long to start?  There
> is a noticeable pause after "rm -f -r 'test-results'" before the first
> line of real output is shown.  The time in between is likely spent on
> make test-lint.

Yeah, "make test-greplint" takes ~6.5s to run for me. The whole test suite runs in ~111s, so that is over 5% of the time. ;) And of course it is more noticeable because there it is all at once before any output happens.

Chainlint actually takes similar amounts of CPU time (which is not surprising, because they share the parser) but it runs in parallel. We could probably do the same here.

It does feel like a lot of processing just to detect one specific type of lint error. If we amortized the parsing effort across other lint checks (or even chainlint itself!) that might be more palatable.

This patch does reduce the cost by ~50%:
diff --git a/t/greplint.pl b/t/greplint.pl
index 23efe27511..68b6eba78d 100644
--- a/t/greplint.pl
+++ b/t/greplint.pl
@@ -190,6 +190,15 @@ sub check_test {
 		}
 	}
 	return unless $body;
+	# We can never match a bare "grep" if there isn't one in the first
+	# place. So we can skip all of the parsing effort. Technically you
+	# can have:
+	#
+	#   gr\
+	#   ep
+	#
+	# but that is sufficiently insane that I think we can ignore it.
+	return unless $body =~ /grep/;
 
 	my $raw_lines = $self->{raw_lines};
 

but of course it is working against the "maybe we could amortize the
parsing cost" idea.

-Peff
Tyler Cipriani· Oct 25, 2026, 06:35 UTC · re: Junio C Hamano · lore

[PATCH 0/4] t: run greplint checks inside chainlint

As Junio said[0], make test-lint is "uncomfortably slow".

make test-lint runs both t/chainlint.pl and t/greplint.pl which do mostly the same thing on mostly the same files, both:

- Open about 1100 files
- Parse/lex/tokenize the files
- Run checks - which is the only place chainlint and greplint vary

Plus, greplint processes files serially. So we're doing the same work twice and half the work happens in serial.

Following Peff's suggestion, this series teaches chainlint.pl to run the greplint checks while running checks on its own tokens. So "make test-lint" parses each file once, in parallel.

Keep greplint.pl as a standalone for GIT_TEST_CHAIN_LINT=0.

Peff's early return (patch 2) speeds up standalone greplint.pl (when greplint does its own parsing).

Peff: Can I add your Signed-off-by to your patch in the series? It does,
indeed, cut time in half when greplint does its own parsing.
Before this series:
    $ time sh -c 'for c in 1 2 3 4 5; do make test-lint; done'
    real    0m25.158s
    user    0m51.557s
    sys     0m0.899s
After this series:
    $ time sh -c 'for c in 1 2 3 4 5; do make test-lint; done'
    real    0m5.972s
    user    0m41.536s
    sys     0m0.943s
(nproc == 16 for both)
[0]. https://lore.kernel.org/git/xmqqjynueq81.fsf@gitster.g/
Jeff King (1):
  t/greplint: skip test bodies without the word grep
Tyler Cipriani (3):
  t/interop: replace bare grep with test_grep
  t/greplint: move parsing and token check to library
  t/chainlint: add --greplint to run greplint
 t/Makefile                             |  30 +++-
 t/chainlint.pl                         |  24 ++-
 t/greplint.pl                          | 240 +------------------------
 t/interop/i5700-protocol-transition.sh |  10 +-
 t/{greplint.pl => lib-greplint.pl}     |  78 ++++----
 5 files changed, 93 insertions(+), 289 deletions(-)
 copy t/{greplint.pl => lib-greplint.pl} (85%)
base-commit: 7e34d8174dcda3435d1404c9a30bef46afe6c13e
-- 
2.47.3
Tyler Cipriani· Oct 25, 2026, 06:35 UTC · re: Tyler Cipriani · lore

[PATCH 1/4] t/interop: replace bare grep with test_grep

Use test_grep for grep assertions in interop files.

In preparation for combining chainlint and greplint into a single test command, ensure that chainlint and greplint pass on their combined list of shell scripts under test.

Signed-off-by: Tyler Cipriani <tyler@tylercipriani.com>
---
 t/interop/i5700-protocol-transition.sh | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/t/interop/i5700-protocol-transition.sh b/t/interop/i5700-protocol-transition.sh
index 97e8e580ef..b610a64dfc 100755
--- a/t/interop/i5700-protocol-transition.sh
+++ b/t/interop/i5700-protocol-transition.sh
@@ -24,7 +24,7 @@ test_expect_success "git:// clone with $VERSION_A and protocol v1" '
 	git.a -C child log -1 --format=%s >actual &&
 	git.b -C "$repo" log -1 --format=%s >expect &&
 	test_cmp expect actual &&
-	grep "version=1" log
+	test_grep "version=1" log
 '
 
 test_expect_success "git:// fetch with $VERSION_A and protocol v1" '
@@ -35,8 +35,8 @@ test_expect_success "git:// fetch with $VERSION_A and protocol v1" '
 	git.a -C child log -1 --format=%s FETCH_HEAD >actual &&
 
 	test_cmp expect actual &&
-	grep "version=1" log &&
-	! grep "version 1" log
+	test_grep "version=1" log &&
+	test_grep ! "version 1" log
 '
 
 stop_git_daemon
@@ -51,7 +51,7 @@ test_expect_success "file:// clone with $VERSION_A and protocol v1" '
 	git.a -C child2 log -1 --format=%s >actual &&
 	git.b -C parent log -1 --format=%s >expect &&
 	test_cmp expect actual &&
-	! grep "version 1" log
+	test_grep ! "version 1" log
 '
 
 test_expect_success "file:// fetch with $VERSION_A and protocol v1" '
@@ -62,7 +62,7 @@ test_expect_success "file:// fetch with $VERSION_A and protocol v1" '
 	git.a -C child2 log -1 --format=%s FETCH_HEAD >actual &&
 
 	test_cmp expect actual &&
-	! grep "version 1" log
+	test_grep ! "version 1" log
 '
 
 test_done
-- 
2.47.3
Tyler Cipriani· Oct 25, 2026, 06:35 UTC · re: Tyler Cipriani · lore

[PATCH 2/4] t/greplint: skip test bodies without the word grep

From: Jeff King <peff@peff.net>

greplint.pl spends time parsing test bodies that cannot contain a bare grep. Check for the string "grep" first and skip the parse when it isn't there. This reduces the cost of "make test-greplint" by ~50%.

Signed-off-by: Tyler Cipriani <tyler@tylercipriani.com>
---
 t/greplint.pl | 9 +++++++++
 1 file changed, 9 insertions(+)
diff --git a/t/greplint.pl b/t/greplint.pl
index 23efe27511..68b6eba78d 100644
--- a/t/greplint.pl
+++ b/t/greplint.pl
@@ -190,6 +190,15 @@ sub check_test {
 		}
 	}
 	return unless $body;
+	# We can never match a bare "grep" if there isn't one in the first
+	# place. So we can skip all of the parsing effort. Technically you
+	# can have:
+	#
+	#   gr\
+	#   ep
+	#
+	# but that is sufficiently insane that I think we can ignore it.
+	return unless $body =~ /grep/;
 
 	my $raw_lines = $self->{raw_lines};
 
-- 
2.47.3
Tyler Cipriani· Oct 25, 2026, 06:35 UTC · re: Tyler Cipriani · lore

[PATCH 3/4] t/greplint: move parsing and token check to library

Move greplint.pl's parsing and token check work to a library. Modify the greplint.pl to use the library. Break out actual checks into check_tokens() sub.

In preparation to combine the work of chainlint and greplint into a single parser invocation.

Signed-off-by: Tyler Cipriani <tyler@tylercipriani.com>
---
 t/greplint.pl                      | 249 +----------------------------
 t/{greplint.pl => lib-greplint.pl} |  69 ++++----
 2 files changed, 37 insertions(+), 281 deletions(-)
 copy t/{greplint.pl => lib-greplint.pl} (87%)
diff --git a/t/greplint.pl b/t/greplint.pl
index 68b6eba78d..e277647f11 100644
--- a/t/greplint.pl
+++ b/t/greplint.pl
@@ -1,257 +1,16 @@
 #!/usr/bin/env perl
 
-# Detect bare 'grep' used as a test assertion where 'test_grep'
-# should be used, and '! test_grep' where 'test_grep !' should
-# be used.
-#
-# The shared shell parser tokenizes test bodies so that 'grep'
-# inside heredocs, command substitutions like $(grep ...), and
-# quoted strings is collapsed into a single token and never seen
-# by our check.  A line-oriented approach would need to track
-# heredoc delimiters, nested $() depth, and cross-line pipe
-# state to avoid false positives on patterns like:
-#
-#   write_script foo.sh <<-\EOF
-#   grep pattern file    # data, not an assertion
-#   EOF
-#
-# The Lexer already handles these.
-
+# Detect bare 'grep' used as a test assertion
 use warnings;
 use strict;
 use File::Basename;
 do(dirname($0) . "/lib-shell-parser.pl")
 	or die "$0: failed to load lib-shell-parser.pl: $@$!\n";
+do(dirname($0) . "/lib-greplint.pl")
+	or die "$0: failed to load lib-greplint.pl: $@$!\n";
 
 my $exit_code = 0;
 
-# GrepLintParser inherits ScriptParser's ability to find
-# test_expect_success/failure blocks and call check_test()
-# on each body.  We override check_test() to walk the token
-# stream looking for bare grep assertions.
-package GrepLintParser;
-
-our @ISA = ('ScriptParser');
-
-# After these tokens, the next token is a command word.
-# For example, in 'echo foo && grep bar file', the 'grep'
-# after '&&' is at command position and should be flagged.
-my %cmd_start = map { $_ => 1 } qw(&& || ; ;; do then else elif), "\n", '{', '(';
-
-# Tokens indicating grep's output is piped or redirected.
-my %filter_op = map { $_ => 1 } qw(| > >> <);
-
-# A token is at "command word" position if the shell would
-# interpret it as a program name rather than an argument.
-# Only 'grep' at command position is an assertion we should
-# flag; 'grep' as an argument ('test_must_fail grep') or
-# value ('for cmd in grep sed') is not.
-sub is_command_word {
-	my ($tokens, $pos) = @_;
-	return 1 if $pos == 0;
-	for (my $j = $pos - 1; $j >= 0; $j--) {
-		my $t = $tokens->[$j]->[0];
-		# After a separator or pipe, a new command starts.
-		return 1 if $cmd_start{$t} || $t eq '|';
-		# After '}' or ')', what follows is a separator or
-		# redirect on the compound command, not a new command.
-		return 0 if $t eq '}' || $t eq ')';
-		# '!' is a prefix that does not consume command
-		# position; keep scanning to find what precedes it.
-		next if $t eq '!';
-		# Any other word means we are past the command word.
-		return 0;
-	}
-	return 1;
-}
-
-# lint_ok() reports whether a bare grep carries a trailing
-# '# lint-ok' comment telling this linter to skip it.
-#
-# In practice this is needed for just one case: a grep acting
-# as a data filter whose output is consumed by a redirect or
-# pipe on an enclosing compound command (such as a subshell or
-# brace group) rather than by grep's own pipeline, e.g.
-#
-#	( grep ... && # lint-ok
-#	  sed ... ) >out
-#
-#	{ grep ... || : # lint-ok
-#	} >out
-#
-# is_filter() only scans grep's own pipeline: it stops at the
-# separator before the compound command closes and never sees
-# the outer redirect, so it would flag such a grep as an
-# assertion.  A grep that really is an assertion is better
-# written as test_grep (or a guarded test_grep when the file's
-# presence is conditional) than annotated with lint-ok.
-sub lint_ok {
-	my ($raw_lines, $ln) = @_;
-	if ($ln < 1 || $ln > @$raw_lines) {
-		warn "lint_ok: line number $ln out of range (1.." .
-		    scalar(@$raw_lines) . ")\n";
-		return 0;
-	}
-	return $raw_lines->[$ln - 1] =~ /lint-ok/;
-}
-
-# Grep is a filter (not an assertion) if it receives piped
-# input or sends its output to a pipe or redirect.  Check
-# both directions from grep's position in the token stream.
-sub is_filter {
-	my ($tokens, $pos) = @_;
-	# Backward: is grep receiving piped input?
-	# Newlines don't break pipes ('cmd |\n grep' is one
-	# pipeline), so skip past them.
-	for (my $j = $pos - 1; $j >= 0; $j--) {
-		my $t = $tokens->[$j]->[0];
-		return 1 if $t eq '|';
-		next if $t eq "\n";
-		last if $cmd_start{$t} || $t eq '}' || $t eq ')';
-	}
-	# Forward: is grep piping or redirecting output?
-	# Unlike the backward scan, we do not skip newlines here:
-	# a bare newline is a command boundary, and redirects or
-	# pipes must appear on the same line as grep (or after a
-	# line continuation, which the Lexer consumes).
-	for (my $j = $pos + 1; $j < @$tokens; $j++) {
-		my $t = $tokens->[$j]->[0];
-		return 0 if $cmd_start{$t};
-		return 1 if $filter_op{$t};
-	}
-	return 0;
-}
-
-# Map a body-relative line number to a file line number.
-# For double-quoted bodies, backslash-continuation lines
-# (\<newline>) are consumed by the Lexer without appearing
-# in the body text, so the inner parser sees fewer lines
-# than the source file has.  We walk the source lines to
-# count continuations and adjust accordingly.
-sub body_to_file_line {
-	my ($body_lineno, $body_token, $raw_lines, $body_start) = @_;
-	my $body_text = $body_token->[0];
-	my $body_end_line = $body_token->[4];
-	unless ($body_start && $body_start >= 1) {
-		warn "body_start is not a positive integer\n";
-		return $body_lineno;
-	}
-	my $file_lineno = $body_lineno + $body_start - 1;
-	# Only double-quoted bodies have line splices.
-	return $file_lineno unless $body_text =~ /^"/;
-	my $adj = 0;
-	my $lines_seen = 0;
-	unless ($body_end_line && $body_end_line >= $body_start) {
-		warn "body_end_line is not set for double-quoted body\n";
-		return $file_lineno;
-	}
-	my $end = $body_end_line;
-	if ($end > @$raw_lines) {
-		warn "body_end_line ($end) exceeds file length (" .
-		    scalar(@$raw_lines) . ")\n";
-		return $file_lineno;
-	}
-	my $src_ln = $body_start;
-	while ($src_ln <= $end && $lines_seen < $body_lineno) {
-		my $line = $raw_lines->[$src_ln - 1];
-		# Odd trailing backslashes = continuation (\<nl>).
-		# Even = escaped backslashes (\\), not a continuation.
-		if ($line =~ /(\\*)$/ && length($1) % 2 == 1) {
-			$adj++;
-		} else {
-			$lines_seen++;
-		}
-		$src_ln++;
-	}
-	if ($lines_seen < $body_lineno) {
-		warn "body_lineno ($body_lineno) not found within body range " .
-		    "($body_start..$end)\n";
-	}
-	return $file_lineno + $adj;
-}
-
-# ScriptParser calls this for each test body found in the script.
-sub check_test {
-	my $self = shift @_;
-	my $title = ScriptParser::unwrap(shift @_);
-	my $body_token = shift @_;
-	my $body_start = $body_token->[3];
-	my $body = ScriptParser::unwrap($body_token);
-	# Handle heredoc-style test bodies:
-	#   test_expect_success 'title' - <<\EOF
-	#   grep pattern file
-	#   EOF
-	# The '-' signals that the body follows as a heredoc.
-	if ($body eq '-') {
-		my $herebody = shift @_;
-		if ($herebody) {
-			$body = $herebody->{content};
-			$body_start = $herebody->{start_line};
-		}
-	}
-	return unless $body;
-	# We can never match a bare "grep" if there isn't one in the first
-	# place. So we can skip all of the parsing effort. Technically you
-	# can have:
-	#
-	#   gr\
-	#   ep
-	#
-	# but that is sufficiently insane that I think we can ignore it.
-	return unless $body =~ /grep/;
-
-	my $raw_lines = $self->{raw_lines};
-
-	# The outer parser gives us the body as an opaque string.
-	# Parse it to get individual tokens with command boundaries.
-	my $parser = ShellParser->new(\$body);
-	my @tokens = $parser->parse();
-
-	my $file = $self->{file};
-
-	for (my $i = 0; $i < @tokens; $i++) {
-		my $text = $tokens[$i]->[0];
-		next unless is_command_word(\@tokens, $i);
-
-		my $token_lineno = $tokens[$i]->[3];
-		unless (defined($token_lineno) && $token_lineno >= 1) {
-			warn "token has no line number\n";
-			next;
-		}
-		my $file_lineno = body_to_file_line(
-			$token_lineno,
-			$body_token, $raw_lines, $body_start);
-
-		# '!' negates the exit code without consuming command
-		# position.  '! test_grep' is an anti-pattern because
-		# test_grep only prints diagnostics on grep failure,
-		# and '!' inverts after that decision is already made.
-		if ($text eq '!') {
-			if ($i + 1 < @tokens &&
-			    $tokens[$i + 1]->[0] eq 'test_grep' &&
-			    !lint_ok($raw_lines, $file_lineno)) {
-				print "$file:$file_lineno: error: ",
-				    'use "test_grep !" instead of ',
-				    '"! test_grep"', "\n";
-				$exit_code = 1;
-			}
-			next;
-		}
-
-		# Bare grep as a command (not a filter) is a test
-		# assertion that should use test_grep for better
-		# failure diagnostics.
-		if ($text eq 'grep' &&
-		    !is_filter(\@tokens, $i) &&
-		    !lint_ok($raw_lines, $file_lineno)) {
-			print "$file:$file_lineno: error: ",
-			    "bare grep outside pipeline ",
-			    "(use test_grep)\n";
-			$exit_code = 1;
-		}
-	}
-}
-
 package main;
 
 for my $file (@ARGV) {
@@ -263,5 +22,7 @@ package main;
 	$parser->{file} = $file;
 	$parser->{raw_lines} = \@raw_lines;
 	$parser->parse();
+	print "$_\n" for @{$parser->{output}};
+	$exit_code ||= $parser->{nerrs} > 0;
 }
 exit $exit_code;
diff --git a/t/greplint.pl b/t/lib-greplint.pl
similarity index 87%
copy from t/greplint.pl
copy to t/lib-greplint.pl
index 68b6eba78d..16b85b8d3b 100644
--- a/t/greplint.pl
+++ b/t/lib-greplint.pl
@@ -1,5 +1,3 @@
-#!/usr/bin/env perl
-
 # Detect bare 'grep' used as a test assertion where 'test_grep'
 # should be used, and '! test_grep' where 'test_grep !' should
 # be used.
@@ -19,16 +17,13 @@
 
 use warnings;
 use strict;
-use File::Basename;
-do(dirname($0) . "/lib-shell-parser.pl")
-	or die "$0: failed to load lib-shell-parser.pl: $@$!\n";
-
-my $exit_code = 0;
 
 # GrepLintParser inherits ScriptParser's ability to find
 # test_expect_success/failure blocks and call check_test()
 # on each body.  We override check_test() to walk the token
-# stream looking for bare grep assertions.
+# stream looking for bare grep assertions vs. test_grep.
+#
+# Requires lib-shell-parser.pl to be loaded.
 package GrepLintParser;
 
 our @ISA = ('ScriptParser');
@@ -207,13 +202,25 @@ sub check_test {
 	my $parser = ShellParser->new(\$body);
 	my @tokens = $parser->parse();
 
-	my $file = $self->{file};
+	my @errors = check_tokens(
+		$self->{file}, $raw_lines,
+		$body_token, $body_start, \@tokens);
 
-	for (my $i = 0; $i < @tokens; $i++) {
-		my $text = $tokens[$i]->[0];
-		next unless is_command_word(\@tokens, $i);
+	push(@{$self->{output}}, @errors);
+	$self->{nerrs} += @errors;
+}
 
-		my $token_lineno = $tokens[$i]->[3];
+# check_tokens() walks a parsed test body and returns a list of
+# error messages.
+sub check_tokens {
+	my ($file, $raw_lines, $body_token, $body_start, $tokens) = @_;
+	my @errors;
+
+	for (my $i = 0; $i < @$tokens; $i++) {
+		my $text = $tokens->[$i]->[0];
+		next unless is_command_word($tokens, $i);
+
+		my $token_lineno = $tokens->[$i]->[3];
 		unless (defined($token_lineno) && $token_lineno >= 1) {
 			warn "token has no line number\n";
 			next;
@@ -227,13 +234,12 @@ sub check_test {
 		# test_grep only prints diagnostics on grep failure,
 		# and '!' inverts after that decision is already made.
 		if ($text eq '!') {
-			if ($i + 1 < @tokens &&
-			    $tokens[$i + 1]->[0] eq 'test_grep' &&
+			if ($i + 1 < @$tokens &&
+			    $tokens->[$i + 1]->[0] eq 'test_grep' &&
 			    !lint_ok($raw_lines, $file_lineno)) {
-				print "$file:$file_lineno: error: ",
-				    'use "test_grep !" instead of ',
-				    '"! test_grep"', "\n";
-				$exit_code = 1;
+				push(@errors, "$file:$file_lineno: error: " .
+				    'use "test_grep !" instead of ' .
+				    '"! test_grep"');
 			}
 			next;
 		}
@@ -242,26 +248,15 @@ sub check_test {
 		# assertion that should use test_grep for better
 		# failure diagnostics.
 		if ($text eq 'grep' &&
-		    !is_filter(\@tokens, $i) &&
+		    !is_filter($tokens, $i) &&
 		    !lint_ok($raw_lines, $file_lineno)) {
-			print "$file:$file_lineno: error: ",
-			    "bare grep outside pipeline ",
-			    "(use test_grep)\n";
-			$exit_code = 1;
+			push(@errors, "$file:$file_lineno: error: " .
+			    "bare grep outside pipeline " .
+			    "(use test_grep)");
 		}
 	}
+
+	return @errors;
 }
 
-package main;
-
-for my $file (@ARGV) {
-	open(my $fh, '<:unix:crlf', $file) or die "$0: $file: $!\n";
-	my @raw_lines = <$fh>;
-	close $fh;
-	my $s = join('', @raw_lines);
-	my $parser = GrepLintParser->new(\$s);
-	$parser->{file} = $file;
-	$parser->{raw_lines} = \@raw_lines;
-	$parser->parse();
-}
-exit $exit_code;
+1;
-- 
2.47.3
Tyler Cipriani· Oct 25, 2026, 06:35 UTC · re: Tyler Cipriani · lore

[PATCH 4/4] t/chainlint: add --greplint to run greplint

Save duplicate file handling and parsing work (and gain parallel file processing) by teaching chainlint to do the work of greplint.

Add --greplint option to chainlint.pl so that it passes tokens to GrepLintParser::check_tokens(). This change allows chainlint.pl to do greplint checks after chainlint has done all the hard work of file handling and parsing.

Update Makefile with new target that invokes chainlint.pl --greplint. In the common case, make test-lint will use the new chainlint target. Continue to respect and set GIT_TEST_CHAIN_LINT=0.

Ensure test-chainlint/test-greplint run on the same set of test files, which expands the set for both.

Local laptop testing shows make test-lint went from around 4.5s before this change to less than 1s after (with -j$(nproc) where nproc == 16).

Reported-by: Junio C Hamano <gitster@pobox.com>
Suggested-by: Jeff King <peff@peff.net>
Signed-off-by: Tyler Cipriani <tyler@tylercipriani.com>
---
 t/Makefile     | 30 ++++++++++++++++++++++--------
 t/chainlint.pl | 24 ++++++++++++++++++++----
 2 files changed, 42 insertions(+), 12 deletions(-)
diff --git a/t/Makefile b/t/Makefile
index e3249879c4..bfe861f6e0 100644
--- a/t/Makefile
+++ b/t/Makefile
@@ -56,9 +56,9 @@ UNIT_TEST_PROGRAMS += unit-tests/bin/unit-tests$(X)
 UNIT_TESTS = $(sort $(UNIT_TEST_PROGRAMS))
 UNIT_TESTS_NO_DIR = $(notdir $(UNIT_TESTS))
 
-# `test-chainlint` (which is a dependency of `test-lint`, `test` and `prove`)
-# checks all tests in all scripts via a single invocation, so tell individual
-# scripts not to run the external "chainlint.pl" script themselves
+# `test-chainlint-greplint` (which is a dependency of `test-lint`, `test`, and
+# `prove`) checks all tests in all scripts via a single invocation, so tell
+# individual scripts not to run the external "chainlint.pl" script themselves
 CHAINLINTSUPPRESS = GIT_TEST_EXT_CHAIN_LINT=0 && export GIT_TEST_EXT_CHAIN_LINT &&
 
 all:: $(DEFAULT_TEST_TARGET)
@@ -127,6 +127,7 @@ check-chainlint:
 clean-greplint:
 	$(RM) -r '$(GREPLINTTMP_SQ)'
 
+# chainlint --jobs=1 needed: parallel workers emit output in arbitrary order
 check-greplint:
 	@mkdir -p '$(GREPLINTTMP_SQ)' && \
 	'$(PERL_PATH_SQ)' greplint-cat.pl '$(GREPLINTTMP_SQ)' $(GREPLINTTESTS) && \
@@ -134,6 +135,11 @@ check-greplint:
 		$(patsubst %,greplint/%.test,$(GREPLINTTESTS)) \
 		>'$(GREPLINTTMP_SQ)'/actual 2>&1 || true; } && \
 	diff -u '$(GREPLINTTMP_SQ)'/expect '$(GREPLINTTMP_SQ)'/actual
+	@$(CHAINLINT) --greplint --jobs=1 \
+		$(patsubst %,greplint/%.test,$(GREPLINTTESTS)) 2>&1 | \
+		sed -e '/^# chainlint: /d' -e '/^[0-9][0-9]* /d' \
+		>'$(GREPLINTTMP_SQ)'/actual-chainlint && \
+	diff -u '$(GREPLINTTMP_SQ)'/expect '$(GREPLINTTMP_SQ)'/actual-chainlint
 
 check-meson:
 	@# awk acts up when trying to match single quotes, so we use \047 instead.
@@ -154,12 +160,16 @@ check-meson:
 test-lint: test-lint-duplicates test-lint-executable \
 	test-lint-filenames
 ifneq ($(PERL_PATH),)
-test-lint: test-lint-shell-syntax test-greplint
+test-lint: test-lint-shell-syntax
 else
 GIT_TEST_CHAIN_LINT = 0
 endif
 ifneq ($(GIT_TEST_CHAIN_LINT),0)
-test-lint: test-chainlint
+test-lint: test-chainlint-greplint
+else
+ifneq ($(PERL_PATH),)
+test-lint: test-greplint
+endif
 endif
 
 test-lint-duplicates:
@@ -176,7 +186,7 @@ test-lint-shell-syntax:
 	@'$(PERL_PATH_SQ)' check-non-portable-shell.pl $(T) $(THELPERS) $(TPERF)
 
 test-greplint:
-	@'$(PERL_PATH_SQ)' greplint.pl $(T) $(THELPERS) $(TPERF)
+	@'$(PERL_PATH_SQ)' greplint.pl $(T) $(THELPERS) $(TPERF) $(TINTEROP)
 
 test-lint-filenames:
 	@# We do *not* pass a glob to ls-files but use grep instead, to catch
@@ -186,8 +196,11 @@ test-lint-filenames:
 		test -z "$$bad" || { \
 		echo >&2 "non-portable file name(s): $$bad"; exit 1; }
 
+test-chainlint-greplint:
+	@$(CHAINLINT) --greplint $(T) $(THELPERS) $(TPERF) $(TINTEROP)
+
 test-chainlint:
-	@$(CHAINLINT) $(T) $(TLIBS) $(TPERF) $(TINTEROP)
+	@$(CHAINLINT) $(T) $(THELPERS) $(TPERF) $(TINTEROP)
 
 aggregate-results-and-cleanup: $(T)
 	$(MAKE) aggregate-results
@@ -204,7 +217,8 @@ perf:
 
 .PHONY: pre-clean $(T) aggregate-results clean valgrind perf \
 	check-chainlint clean-chainlint test-chainlint \
-	check-greplint clean-greplint test-greplint $(UNIT_TESTS)
+	check-greplint clean-greplint test-greplint \
+	test-chainlint-greplint $(UNIT_TESTS)
 
 .PHONY: libgit-sys-test libgit-rs-test
 libgit-sys-test:
diff --git a/t/chainlint.pl b/t/chainlint.pl
index cededf15ee..dda3617add 100755
--- a/t/chainlint.pl
+++ b/t/chainlint.pl
@@ -22,10 +22,13 @@
 my $jobs = -1;
 my $show_stats;
 my $emit_all;
+my $greplint;
 
 use File::Basename;
 do(dirname($0) . "/lib-shell-parser.pl")
 	or die "$0: failed to load lib-shell-parser.pl: $@$!\n";
+do(dirname($0) . "/lib-greplint.pl")
+	or die "$0: failed to load lib-greplint.pl: $@$!\n";
 
 # TestParser is a subclass of ShellParser which, beyond parsing shell script
 # code, is also imbued with semantic knowledge of test construction, and checks
@@ -147,9 +150,9 @@ sub format_problem {
 sub check_test {
 	my $self = shift @_;
 	my $title = ScriptParser::unwrap(shift @_);
-	my $body = shift @_;
-	my $lineno = $body->[3];
-	$body = ScriptParser::unwrap($body);
+	my $body_token = shift @_;
+	my $lineno = $body_token->[3];
+	my $body = ScriptParser::unwrap($body_token);
 	if ($body eq '-') {
 		my $herebody = shift @_;
 		$body = $herebody->{content};
@@ -160,6 +163,12 @@ sub check_test {
 	my @tokens = $parser->parse();
 	my $problems = $parser->{problems};
 	$self->{nerrs} += @$problems;
+	if ($greplint) {
+		my @grep_errors = GrepLintParser::check_tokens($self->{file},
+			$self->{raw_lines}, $body_token, $lineno, \@tokens);
+		push(@{$self->{greplint}}, @grep_errors);
+		$self->{nerrs} += @grep_errors;
+	}
 	return unless $emit_all || @$problems;
 	my $c = main::fd_colors(1);
 	my ($erropen, $errclose) = -t 1 ? ("$c->{rev}$c->{red}", $c->{reset}) : ('?!', '?!');
@@ -296,15 +305,21 @@ sub check_script {
 			$emit->("?!ERR?! $path: $!\n");
 			next;
 		}
-		my $s = do { local $/; <$fh> };
+		my @raw_lines = <$fh>;
 		close($fh);
+		my $s = join('', @raw_lines);
 		my $parser = ChainlintParser->new(\$s);
+		$parser->{file} = $path;
+		$parser->{raw_lines} = \@raw_lines;
+		$parser->{greplint} = [];
 		1 while $parser->parse_cmd();
 		if (@{$parser->{output}}) {
 			my $c = fd_colors(1);
 			my $s = join('', @{$parser->{output}});
 			$emit->("$c->{bold}$c->{blue}# chainlint: $path$c->{reset}\n" . $s);
 		}
+		$emit->(join('', map { "$_\n" } @{$parser->{greplint}}))
+			if @{$parser->{greplint}};
 		$ntests += $parser->{ntests};
 		$nerrs += $parser->{nerrs};
 	}
@@ -323,6 +338,7 @@ sub exit_code {
 Getopt::Long::Configure(qw{bundling});
 GetOptions(
 	"emit-all!" => \$emit_all,
+	"greplint!" => \$greplint,
 	"jobs|j=i" => \$jobs,
 	"stats|show-stats!" => \$show_stats) or die("option error\n");
 $jobs = ncores() if $jobs < 1;
-- 
2.47.3

← back to recent threads