threads / patch / 65088

patcht3700: avoid suppressing git's exit code

Subject: [PATCH] t3700: avoid suppressing git's exit code

## tl;dr

6 messages between Feb 27, 2026 and Mar 2, 2026. Diffs are folded; open one to read it.

replies: 5people: 3as markdown or json

Siddharth Shrimali· Feb 27, 2026, 16:51 UTC · lore

When piping the output of git ls-files into grep, the exit code of git ls-files is suppressed.

Avoid this by redirecting the output of git ls-files to a file and then running grep on that file. This ensures that any crash in git ls-files will be caught by the test suite.

Signed-off-by: Siddharth Shrimali <r.siddharth.shrimali@gmail.com>
---
 t/t3700-add.sh | 38 +++++++++++++++++++++++---------------
 1 file changed, 23 insertions(+), 15 deletions(-)
Show changes to t/t3700-add.sh +23 −15
diff --git a/t/t3700-add.sh b/t/t3700-add.sh
index af93e53c12..66c6114b54 100755
--- a/t/t3700-add.sh
+++ b/t/t3700-add.sh
@@ -38,7 +38,8 @@ test_expect_success 'Test with no pathspecs' '
 '
 
 test_expect_success 'Post-check that foo is in the index' '
-	git ls-files foo | grep foo
+	git ls-files foo >actual &&
+	grep foo <actual
 '
 
 test_expect_success 'Test that "git add -- -q" works' '
@@ -195,8 +196,9 @@ test_expect_success 'git add with filemode=0, symlinks=0, and unmerged entries'
 	echo new > file &&
 	echo new > symlink &&
 	git add file symlink &&
-	git ls-files --stage | grep "^100755 .* 0	file$" &&
-	git ls-files --stage | grep "^120000 .* 0	symlink$"
+	git ls-files --stage >actual &&
+	grep "^100755 .* 0	file$" <actual &&
+	grep "^120000 .* 0	symlink$" actual
 '
 
 test_expect_success 'git add with filemode=0, symlinks=0 prefers stage 2 over stage 1' '
@@ -212,8 +214,9 @@ test_expect_success 'git add with filemode=0, symlinks=0 prefers stage 2 over st
 	echo new > file &&
 	echo new > symlink &&
 	git add file symlink &&
-	git ls-files --stage | grep "^100755 .* 0	file$" &&
-	git ls-files --stage | grep "^120000 .* 0	symlink$"
+	git ls-files --stage >actual &&
+	grep "^100755 .* 0	file$" actual &&
+	grep "^120000 .* 0	symlink$" actual
 '
 
 test_expect_success 'git add --refresh' '
@@ -254,7 +257,8 @@ test_expect_success POSIXPERM,SANITY 'git add should fail atomically upon an unr
 	date >foo2 &&
 	chmod 0 foo2 &&
 	test_must_fail git add --verbose . &&
-	! ( git ls-files foo1 | grep foo1 )
+	git ls-files foo1 >actual &&
+	! grep foo1 actual
 '
 
 rm -f foo2
@@ -265,7 +269,7 @@ test_expect_success POSIXPERM,SANITY 'git add --ignore-errors' '
 	date >foo2 &&
 	chmod 0 foo2 &&
 	test_must_fail git add --verbose --ignore-errors . &&
-	git ls-files foo1 | grep foo1
+	git ls-files foo1 >actual && grep foo1 actual
 '
 
 rm -f foo2
@@ -277,7 +281,7 @@ test_expect_success POSIXPERM,SANITY 'git add (add.ignore-errors)' '
 	date >foo2 &&
 	chmod 0 foo2 &&
 	test_must_fail git add --verbose . &&
-	git ls-files foo1 | grep foo1
+	git ls-files foo1 >actual && grep foo1 actual
 '
 rm -f foo2
 
@@ -288,7 +292,8 @@ test_expect_success POSIXPERM,SANITY 'git add (add.ignore-errors = false)' '
 	date >foo2 &&
 	chmod 0 foo2 &&
 	test_must_fail git add --verbose . &&
-	! ( git ls-files foo1 | grep foo1 )
+	git ls-files foo1 >actual &&
+	! grep foo1 actual
 '
 rm -f foo2
 
@@ -299,7 +304,8 @@ test_expect_success POSIXPERM,SANITY '--no-ignore-errors overrides config' '
 	date >foo2 &&
 	chmod 0 foo2 &&
 	test_must_fail git add --verbose --no-ignore-errors . &&
-	! ( git ls-files foo1 | grep foo1 ) &&
+	git ls-files foo1 >actual &&
+	! grep foo1 actual &&
 	git config add.ignore-errors 0
 '
 rm -f foo2
@@ -309,7 +315,8 @@ test_expect_success BSLASHPSPEC "git add 'fo\\[ou\\]bar' ignores foobar" '
 	touch fo\[ou\]bar foobar &&
 	git add '\''fo\[ou\]bar'\'' &&
 	git ls-files fo\[ou\]bar | grep -F fo\[ou\]bar &&
-	! ( git ls-files foobar | grep foobar )
+	git ls-files foobar >actual &&
+	! grep foobar actual
 '
 
 test_expect_success 'git add to resolve conflicts on otherwise ignored path' '
@@ -326,7 +333,8 @@ test_expect_success 'git add to resolve conflicts on otherwise ignored path' '
 
 test_expect_success '"add non-existent" should fail' '
 	test_must_fail git add non-existent &&
-	! (git ls-files | grep "non-existent")
+	git ls-files >actual &&
+	! grep "non-existent" actual 
 '
 
 test_expect_success 'git add -A on empty repo does not error out' '
@@ -536,9 +544,9 @@ test_expect_success 'all statuses changed in folder if . is given' '
 		touch x y z sub/a sub/dir/b &&
 		git add -A &&
 		git add --chmod=+x . &&
-		test $(git ls-files --stage | grep ^100644 | wc -l) -eq 0 &&
+		test $(git ls-files --stage >actual && grep ^100644 actual | wc -l) -eq 0 &&
 		git add --chmod=-x . &&
-		test $(git ls-files --stage | grep ^100755 | wc -l) -eq 0
+		test $(git ls-files --stage >actual && grep ^100755 actual | wc -l) -eq 0
 	)
 '
 
@@ -574,4 +582,4 @@ test_expect_success CASE_INSENSITIVE_FS 'path is case-insensitive' '
 	git add "$downcased"
 '
 
-test_done
+test_done
\ No newline at end of file
-- 
2.51.2
Junio C Hamano· Feb 27, 2026, 18:19 UTC · re: Siddharth Shrimali · lore

Re: [PATCH] t3700: avoid suppressing git's exit code

Siddharth Shrimali <r.siddharth.shrimali@gmail.com> writes:
Show 11 quoted lines
> When piping the output of git ls-files into grep, the exit code of
> git ls-files is suppressed.
>
> Avoid this by redirecting the output of git ls-files to a file and
> then running grep on that file. This ensures that any crash in
> git ls-files will be caught by the test suite.
>
> Signed-off-by: Siddharth Shrimali <r.siddharth.shrimali@gmail.com>
> ---
>  t/t3700-add.sh | 38 +++++++++++++++++++++++---------------
>  1 file changed, 23 insertions(+), 15 deletions(-)
A few things that I noticed.
 * If "git foo | grep bar" was expecting to hide exit code from
   "git" and check the output (e.g., "git diff --exit-code | grep foo"),
   a mechanical conversion "git diff --exit-code >out && grep foo out"
   would change the meaning of the test and break it.  I did not check
   if this patch has such an unintended breakage, though.
 * Many of them do this:
        > -	git ls-files foo | grep foo
        > +	git ls-files foo >actual &&
        > +	grep foo <actual
   or this
        > -	! ( git ls-files foo1 | grep foo1 )
        > +	git ls-files foo1 >actual &&
        > +	! grep foo1 actual
    in which we might consider using "test_grep" (and "test_grep !")
    to help the developer who wants to debug a breakage in ls-files
    by highlighting what is unexpected in the output in their broken
    version.
 * A rewrite like this may want to be further broken down.
        > -	test $(git ls-files --stage | grep ^100644 | wc -l) -eq 0 &&
        > +	test $(git ls-files --stage >actual && grep ^100644 actual | wc -l) -eq 0 &&
   If "ls-files --stage" segfaults, "grep | wc" would not run, $()
   may exit with non-zero and turn into an empty string, but the
   final error diagnosis would be something unfathonable like
	test: -eq unary operator expected
	test: missing argument after '0'
   
   which would not help the person debugging the test very much.
Siddharth Shrimali· Feb 28, 2026, 07:00 UTC · re: Siddharth Shrimali · lore

[PATCH v2] t3700: avoid hidden failures and use test_grep helper

Replace pipelines involving git commands with temporary files to ensure that any crashes or unexpected exit codes from the git commands are properly caught by the test suite. A simple pipeline like 'git foo | grep bar' ignores the exit code of 'git', which can hide regressions.

Additionally, replace standard 'grep' with the 'test_grep' helper. This improves debuggability by automatically dumping the contents of the 'actual' file when a match is not found. In cases where we were counting lines with 'wc -l' to ensure a pattern was absent, simplify to 'test_grep !'.

Suggested-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Siddharth Shrimali <r.siddharth.shrimali@gmail.com>
---
Inter-patch notes (v1 -> v2):
 - Replaced standard 'grep' with 'test_grep' for better diagnostics.
 - Used 'test_grep !' for negative assertions as per project style.
 - Simplified 'wc -l' logic to 'test_grep !' to avoid subshells.
 - Removed unnecessary '<' redirection from function calls.
 t/t3700-add.sh | 28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)
Show changes to t/t3700-add.sh +15 −13
diff --git a/t/t3700-add.sh b/t/t3700-add.sh
index 66c6114b54..d61bf784d2 100755
--- a/t/t3700-add.sh
+++ b/t/t3700-add.sh
@@ -39,7 +39,7 @@ test_expect_success 'Test with no pathspecs' '
 
 test_expect_success 'Post-check that foo is in the index' '
 	git ls-files foo >actual &&
-	grep foo <actual
+	test_grep foo actual
 '
 
 test_expect_success 'Test that "git add -- -q" works' '
@@ -197,8 +197,8 @@ test_expect_success 'git add with filemode=0, symlinks=0, and unmerged entries'
 	echo new > symlink &&
 	git add file symlink &&
 	git ls-files --stage >actual &&
-	grep "^100755 .* 0	file$" <actual &&
-	grep "^120000 .* 0	symlink$" actual
+	test_grep "^100755 .* 0	file$" actual &&
+	test_grep "^120000 .* 0	symlink$" actual
 '
 
 test_expect_success 'git add with filemode=0, symlinks=0 prefers stage 2 over stage 1' '
@@ -215,8 +215,8 @@ test_expect_success 'git add with filemode=0, symlinks=0 prefers stage 2 over st
 	echo new > symlink &&
 	git add file symlink &&
 	git ls-files --stage >actual &&
-	grep "^100755 .* 0	file$" actual &&
-	grep "^120000 .* 0	symlink$" actual
+	test_grep "^100755 .* 0	file$" actual &&
+	test_grep "^120000 .* 0	symlink$" actual
 '
 
 test_expect_success 'git add --refresh' '
@@ -258,7 +258,7 @@ test_expect_success POSIXPERM,SANITY 'git add should fail atomically upon an unr
 	chmod 0 foo2 &&
 	test_must_fail git add --verbose . &&
 	git ls-files foo1 >actual &&
-	! grep foo1 actual
+	test_grep ! foo1 actual
 '
 
 rm -f foo2
@@ -293,7 +293,7 @@ test_expect_success POSIXPERM,SANITY 'git add (add.ignore-errors = false)' '
 	chmod 0 foo2 &&
 	test_must_fail git add --verbose . &&
 	git ls-files foo1 >actual &&
-	! grep foo1 actual
+	test_grep ! foo1 actual
 '
 rm -f foo2
 
@@ -305,7 +305,7 @@ test_expect_success POSIXPERM,SANITY '--no-ignore-errors overrides config' '
 	chmod 0 foo2 &&
 	test_must_fail git add --verbose --no-ignore-errors . &&
 	git ls-files foo1 >actual &&
-	! grep foo1 actual &&
+	test_grep ! foo1 actual &&
 	git config add.ignore-errors 0
 '
 rm -f foo2
@@ -316,7 +316,7 @@ test_expect_success BSLASHPSPEC "git add 'fo\\[ou\\]bar' ignores foobar" '
 	git add '\''fo\[ou\]bar'\'' &&
 	git ls-files fo\[ou\]bar | grep -F fo\[ou\]bar &&
 	git ls-files foobar >actual &&
-	! grep foobar actual
+	test_grep ! foobar actual
 '
 
 test_expect_success 'git add to resolve conflicts on otherwise ignored path' '
@@ -334,7 +334,7 @@ test_expect_success 'git add to resolve conflicts on otherwise ignored path' '
 test_expect_success '"add non-existent" should fail' '
 	test_must_fail git add non-existent &&
 	git ls-files >actual &&
-	! grep "non-existent" actual 
+	test_grep ! "non-existent" actual 
 '
 
 test_expect_success 'git add -A on empty repo does not error out' '
@@ -544,9 +544,11 @@ test_expect_success 'all statuses changed in folder if . is given' '
 		touch x y z sub/a sub/dir/b &&
 		git add -A &&
 		git add --chmod=+x . &&
-		test $(git ls-files --stage >actual && grep ^100644 actual | wc -l) -eq 0 &&
+		git ls-files --stage >actual &&
+		test_grep ! "^100644" actual &&
 		git add --chmod=-x . &&
-		test $(git ls-files --stage >actual && grep ^100755 actual | wc -l) -eq 0
+		git ls-files --stage >actual &&
+		test_grep ! "^100755" actual
 	)
 '
 
@@ -582,4 +584,4 @@ test_expect_success CASE_INSENSITIVE_FS 'path is case-insensitive' '
 	git add "$downcased"
 '
 
-test_done
\ No newline at end of file
+test_done
-- 
2.51.2
Junio C Hamano· Mar 2, 2026, 21:24 UTC · re: Siddharth Shrimali · lore

Re: [PATCH v2] t3700: avoid hidden failures and use test_grep helper

Siddharth Shrimali <r.siddharth.shrimali@gmail.com> writes:
Show 11 quoted lines
> Replace pipelines involving git commands with temporary files to ensure
> that any crashes or unexpected exit codes from the git commands are
> properly caught by the test suite. A simple pipeline like
> 'git foo | grep bar' ignores the exit code of 'git', which can
> hide regressions.
>
> Additionally, replace standard 'grep' with the 'test_grep' helper.
> This improves debuggability by automatically dumping the contents of
> the 'actual' file when a match is not found. In cases where we were
> counting lines with 'wc -l' to ensure a pattern was absent,
> simplify to 'test_grep !'.

Counting the instances of these changes, there are too many hunks that fall into this "Additionally" category to consider them "while at it" changes. In other words, this would want to become two patches, one to break pipelines to expose the exit status of Git that is upstream of a pipeline, and the other to use test_grep where the original used grep.

>
> Suggested-by: Junio C Hamano <gitster@pobox.com>
>
> Signed-off-by: Siddharth Shrimali <r.siddharth.shrimali@gmail.com>

The trailer block does not allow blanks inside it. Remove the blank line.

Show 21 quoted lines
> @@ -544,9 +544,11 @@ test_expect_success 'all statuses changed in folder if . is given' '
>  		touch x y z sub/a sub/dir/b &&
>  		git add -A &&
>  		git add --chmod=+x . &&
> -		test $(git ls-files --stage >actual && grep ^100644 actual | wc -l) -eq 0 &&
> +		git ls-files --stage >actual &&
> +		test_grep ! "^100644" actual &&
>  		git add --chmod=-x . &&
> -		test $(git ls-files --stage >actual && grep ^100755 actual | wc -l) -eq 0
> +		git ls-files --stage >actual &&
> +		test_grep ! "^100755" actual
>  	)
>  '
>  
> @@ -582,4 +584,4 @@ test_expect_success CASE_INSENSITIVE_FS 'path is case-insensitive' '
>  	git add "$downcased"
>  '
>  
> -test_done
> \ No newline at end of file
> +test_done

Wait. What tree state is this patch meant to apply? If you made a botched change in an earlier attempt, your "v2" patch should *not* be relative to the tree state _with_ that botched attempt. It should instead be a change relative to somewhere stable in my tree, pretending as if your "v1" (which introduced an incomplete line to this file, among possibly other changes) never happened.

So, I'd suggest a two-patch series that is:
 - refine your v1 to remove mistakes (like the "incomplete last
   line"; there might have been others but I do not remember),
   keeping your conversion to break pipelines, without changing
   "grep" to "test_grep".  Make it [PATCH v2 1/2].
 - turn "grep" you touched in [PATCH v2 1/2] above to use
   "test_grep" instead.  In this patch, if the parts of the file you
   did not touch in [PATCH v2 1/2] has only small number of similar
   uses of "grep" that is better written with "test_grep", it is OK
   to change them to use "test_grep" as a "while at it" change.
Thanks.
Johannes Sixt· Feb 28, 2026, 08:12 UTC · re: Siddharth Shrimali · lore

Re: [PATCH] t3700: avoid suppressing git's exit code

Am 27.02.26 um 17:51 schrieb Siddharth Shrimali:
Show 9 quoted lines
> @@ -536,9 +544,9 @@ test_expect_success 'all statuses changed in folder if . is given' '
>  		touch x y z sub/a sub/dir/b &&
>  		git add -A &&
>  		git add --chmod=+x . &&
> -		test $(git ls-files --stage | grep ^100644 | wc -l) -eq 0 &&
> +		test $(git ls-files --stage >actual && grep ^100644 actual | wc -l) -eq 0 &&
>  		git add --chmod=-x . &&
> -		test $(git ls-files --stage | grep ^100755 | wc -l) -eq 0
> +		test $(git ls-files --stage >actual && grep ^100755 actual | wc -l) -eq 0

This doesn't help. The exit code of $( ) that is substituted into a command is ignored, too. You must move the git invocation out of the subshell.

Show 10 quoted lines
>  	)
>  '
>  
> @@ -574,4 +582,4 @@ test_expect_success CASE_INSENSITIVE_FS 'path is case-insensitive' '
>  	git add "$downcased"
>  '
>  
> -test_done
> +test_done
> \ No newline at end of file
Please keep the newline at the end of file.
-- Hannes
Siddharth Shrimali· Feb 28, 2026, 10:15 UTC · re: Johannes Sixt · lore

Re: [PATCH] t3700: avoid suppressing git's exit code

Hi Hannes,
Thank you for the review!

It looks like our emails crossed paths; I sent out a v2 [1] just before your comments arrived. In that version, I have addressed both of your points:

1. Moved the git invocations out of the subshells and replaced
   the logic with 'test_grep !' to properly catch exit codes.
2. Restored the trailing newline at the end of the file.
[1] https://lore.kernel.org/git/20260228070020.89668-1-r.siddharth.shrimali@gmail.com/

Best regards, Siddharth

On Sat, 28 Feb 2026 at 13:42, Johannes Sixt <j6t@kdbg.org> wrote:
Show 31 quoted lines
>
> Am 27.02.26 um 17:51 schrieb Siddharth Shrimali:
> > @@ -536,9 +544,9 @@ test_expect_success 'all statuses changed in folder if . is given' '
> >               touch x y z sub/a sub/dir/b &&
> >               git add -A &&
> >               git add --chmod=+x . &&
> > -             test $(git ls-files --stage | grep ^100644 | wc -l) -eq 0 &&
> > +             test $(git ls-files --stage >actual && grep ^100644 actual | wc -l) -eq 0 &&
> >               git add --chmod=-x . &&
> > -             test $(git ls-files --stage | grep ^100755 | wc -l) -eq 0
> > +             test $(git ls-files --stage >actual && grep ^100755 actual | wc -l) -eq 0
>
> This doesn't help. The exit code of $( ) that is substituted into a
> command is ignored, too. You must move the git invocation out of the
> subshell.
>
> >       )
> >  '
> >
> > @@ -574,4 +582,4 @@ test_expect_success CASE_INSENSITIVE_FS 'path is case-insensitive' '
> >       git add "$downcased"
> >  '
> >
> > -test_done
> > +test_done
> > \ No newline at end of file
>
> Please keep the newline at the end of file.
>
> -- Hannes
>

← back to recent threads