public inbox for git@vger.kernel.org 
 help / color / mirror / Atom feed
* [PATCH] interactive: do strip trailing CRLF from input
@ 2025-07-31 16:07 Johannes Sixt
  2025-07-31 16:37 ` Junio C Hamano
  0 siblings, 1 reply; 2+ messages in thread
From: Johannes Sixt @ 2025-07-31 16:07 UTC (permalink / raw)
  To: Git Mailing List; +Cc: Johannes Schindelin

`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

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] interactive: do strip trailing CRLF from input
  2025-07-31 16:07 [PATCH] interactive: do strip trailing CRLF from input Johannes Sixt
@ 2025-07-31 16:37 ` Junio C Hamano
  0 siblings, 0 replies; 2+ messages in thread
From: Junio C Hamano @ 2025-07-31 16:37 UTC (permalink / raw)
  To: Johannes Sixt; +Cc: Git Mailing List, Johannes Schindelin

Johannes Sixt <j6t@kdbg•org> writes:

> `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.

I was looking at vicinity of the code this morning, so I am more
familiar with this codepath than I usually am ;-)

The above explanation makes perfect sense.  It is quite an old piece
of code, and I am kind of surprised that nobody complained for the
past 5 years.

Will queue.  Thanks.

>
> 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);
>  }

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-07-31 16:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-31 16:07 [PATCH] interactive: do strip trailing CRLF from input Johannes Sixt
2025-07-31 16:37 ` Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox