git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 3/6] t7301: demonstrate "clean -i" ask mode deleting past EOF

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 22, 2026, 14:03 UTC
Message-ID
<20261122140347.41990-4-gitster@pobox.com>
In-Reply-To
<20261122140347.41990-1-gitster@pobox.com>

After builtin/clean.c:ask_each_cmd() asks a "remove this?" question, it reads the interactive answer from the user to decide what to do. This is done in a loop to remove potentially many items.

But this assumes that strbuf_getline() clears the buffer when returning EOF. A "y" answer on an incomplete line causes the command to delete every remaining untracked item instead of stopping on EOF.

Here is a step-by-step walkthrough of this code path.

In ask_each_cmd(), a loop iterates over each candidate in 'del_list'. While '!eof', it prompts and calls git_read_line_interactively() (which calls strbuf_getline()) to read into 'confirm'. On EOF, it prints a newline, sets 'eof = 1', and then falls through to check 'if (!confirm.len || strncasecmp(confirm.buf, "yes", confirm.len))' to drop items that should not be removed, assuming EOF left 'confirm' empty.

Imagine that the user types 'a' to choose 'ask each' mode, and gives 'y' with no trailing LF:

 * For the first item, strbuf_getline() reads "y" into 'confirm', sets
   feof(stdin) while scanning for LF, and returns 0.  Because "y"
   matches a prefix of "yes", that first item stays in 'del_list' to be
   removed.
 * For the second item, '!eof' is still true, so strbuf_getline() is
   called on 'confirm'.  strbuf_getwholeline() sees feof(stdin) and
   returns EOF without resetting 'confirm'.  ask_each_cmd() sets 'eof =
   1' and falls through with 'confirm' still containing "y", keeping the
   second item in 'del_list' for removal.
 * For every remaining item in 'del_list', 'if (!eof)' is skipped while
   'confirm' remains "y", so every remaining untracked file is removed
   contrary to the "Ctrl-D should stop removing files" intent.

Add a test_expect_failure test to t7301-clean-interactive.sh illustrating this issue.

Once strbuf_getwholeline() is changed to reset the buffer before returning on feof(), the EOF read on the second item empties 'confirm', making '!confirm.len' true for the second and all subsequent items so only a.out is removed. Because there are other code paths that will be "fixed" by such a change to strbuf_getwholeline(), we will not fix it in this commit (yet), and stop at documenting the issue in the form of test_expect_failure.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 t/t7301-clean-interactive.sh | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
diff --git a/t/t7301-clean-interactive.sh b/t/t7301-clean-interactive.sh
index de59ba3724..73e923d212 100755
--- a/t/t7301-clean-interactive.sh
+++ b/t/t7301-clean-interactive.sh
@@ -381,6 +381,26 @@ test_expect_success 'git clean -id (ask)' '
 
 '
 
+test_expect_failure 'git clean -id (ask - EOF without trailing LF)' '
+	mkdir -p build docs &&
+	touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
+		docs/manual.txt obj.o build/lib.so &&
+	printf "a\ny" |
+	git clean -id &&
+	test_path_is_file Makefile &&
+	test_path_is_file README &&
+	test_path_is_file src/part1.c &&
+	test_path_is_file src/part2.c &&
+	test_path_is_missing a.out &&
+	test_path_is_file docs/manual.txt &&
+	test_path_is_file src/part3.c &&
+	test_path_is_file src/part3.h &&
+	test_path_is_file src/part4.c &&
+	test_path_is_file src/part4.h &&
+	test_path_is_file obj.o &&
+	test_path_is_file build/lib.so
+'
+
 test_expect_success 'git clean -id (ask - Ctrl+D)' '
 
 	mkdir -p build docs &&
-- 
2.56.1-766-g80003c9184
Previous: Junio C HamanoNext: Junio C Hamano
Message 4 of 8 in “getwholeline should clear the buffer upon EOF”
  1. 0/6 getwholeline should clear the buffer upon EOFJunio C Hamano, Nov 22, 2026
  2. 1/6 mailinfo: do not call handle_body() when headers end at EOFJunio C Hamano, Nov 22, 2026
  3. 2/6 t4201: demonstrate shortlog reusing stale buffer on EOFJunio C Hamano, Nov 22, 2026
  4. 3/6 t7301: demonstrate "clean -i" ask mode deleting past EOFJunio C Hamano, Nov 22, 2026
  5. 4/6 t9300: demonstrate fast-import re-parsing heredoc delimiter at EOFJunio C Hamano, Nov 22, 2026
  6. 5/6 strbuf_getwholeline(): clear strbuf upon EOFJunio C Hamano, Nov 22, 2026
  7. 6/6 t7301, t9300: test EOF loops fixed by clearing strbuf upon EOFJunio C Hamano, Nov 22, 2026
  8. Junio C HamanoNov 23, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.