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

[PATCH] interactive: do strip trailing CRLF from input

From
Johannes Sixt <j6t@kdbg.org>
Date
Jul 31, 2025, 16:07 UTC
Message-ID
<2295f183-038c-4751-b04d-0f1819b67b3a@kdbg.org>

`git reset -p file` on a Windows CMD refuses to do anything useful with this error message:

    (1/5) Unstage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? n
    'nly one letter is expected, got 'n

The letter 'O' at the beginning of the line is overwritten by an apostrophe, so, clearly the parser sees the string "n\r".

strbuf_trim_trailing_newline() removes trailing CRLF from the string. In particular, it first removes LF if present, and if that was the case, it also removes CR if present.

git_read_line_interactively() clearly intends to remove CRLF as it calls strbuf_trim_trailing_newline(). However, input is gathered using strbuf_getline_lf(), which already removes the trailing LF. Now strbuf_trim_trailing_newline() does not see LF, so that it does not remove CR, either, and leaves it for the caller to process.

Call strbuf_getline() instead, which removes both LF and CR.
Signed-off-by: Johannes Sixt <j6t@kdbg.org>
---
 prompt.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
diff --git a/prompt.c b/prompt.c
index f21c5bf1c7..706fba2a50 100644
--- a/prompt.c
+++ b/prompt.c
@@ -77,12 +77,6 @@ char *git_prompt(const char *prompt, int flags)
 
 int git_read_line_interactively(struct strbuf *line)
 {
-	int ret;
-
 	fflush(stdout);
-	ret = strbuf_getline_lf(line, stdin);
-	if (ret != EOF)
-		strbuf_trim_trailing_newline(line);
-
-	return ret;
+	return strbuf_getline(line, stdin);
 }
-- 
2.50.1.665.g4c0a7305c7
Next: Junio C Hamano
Message 1 of 2 in “interactive: do strip trailing CRLF from input”
  1. interactive: do strip trailing CRLF from inputJohannes Sixt, Jul 31, 2025
  2. Junio C HamanoJul 31, 2025

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.