Git development
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: git@vger.kernel.org
Subject: Re: [PATCH] apply: fix new-style empty context line triggering incomplete-line check
Date: Tue, 31 Mar 2026 14:54:19 -0700	[thread overview]
Message-ID: <xmqq7bqry8ac.fsf@gitster.g> (raw)
In-Reply-To: <xmqqldfql4hp.fsf@gitster.g> (Junio C. Hamano's message of "Tue, 17 Mar 2026 11:01:38 -0700")

Junio C Hamano <gitster@pobox.com> writes:

> A new-style unified context diff represents an empty context line
> with an empty line (instead of a line with a single SP on it).  The
> code to check whitespace errors in an incoming patch is designed to
> omit the first byte of a line (typically SP, "-", or "+") and pass the
> remainder of the line to the whitespace checker.
>
> Usually we do not pass a context line to the whitespace error checker,
> but when we are correcting errors, we do.  This "remove the first
> byte and send the remainder" strategy of checking a line ended up
> sending a zero-length string to the whitespace checker when seeing a
> new-style empty context line, which caused the whitespace checker to
> say "ah, you do not even have a newline at the end!", leading to an
> "incomplete line" in the middle of the patch!
>
> Fix this by pretending that we got a traditional empty context line
> when we drive the whitespace checker.
>
> Signed-off-by: Junio C Hamano <gitster@pobox.com>
> ---
>  apply.c                  | 12 ++++++++++--
>  t/t4124-apply-ws-rule.sh | 16 ++++++++++++++++
>  2 files changed, 26 insertions(+), 2 deletions(-)

There were only comments on the unnecessary uses of subshell in
test, which were fixed since then, and I've been using them in
production without problems, so let me mark this for 'next' now.

Objections and better yet polishing on top are of course welcome.

> diff --git a/apply.c b/apply.c
> index f01204d15b..e88e5c77e3 100644
> --- a/apply.c
> +++ b/apply.c
> @@ -1796,8 +1796,16 @@ static int parse_fragment(struct apply_state *state,
>  			trailing++;
>  			check_old_for_crlf(patch, line, len);
>  			if (!state->apply_in_reverse &&
> -			    state->ws_error_action == correct_ws_error)
> -				check_whitespace(state, line, len, patch->ws_rule);
> +			    state->ws_error_action == correct_ws_error) {
> +				const char *test_line = line;
> +				int test_len = len;
> +				if (*line == '\n') {
> +					test_line = " \n";
> +					test_len = 2;
> +				}
> +				check_whitespace(state, test_line, test_len,
> +						 patch->ws_rule);
> +			}
>  			break;
>  		case '-':
>  			if (!state->apply_in_reverse)
> diff --git a/t/t4124-apply-ws-rule.sh b/t/t4124-apply-ws-rule.sh
> index 29ea7d4268..8573e12f46 100755
> --- a/t/t4124-apply-ws-rule.sh
> +++ b/t/t4124-apply-ws-rule.sh
> @@ -561,6 +561,22 @@ test_expect_success 'check incomplete lines (setup)' '
>  	git config core.whitespace incomplete-line
>  '
>  
> +test_expect_success 'no incomplete context line (not an error)' '
> +	test_when_finished "rm -f sample*-i patch patch-new target" &&
> +	(test_write_lines 1 2 3 "" 4 5 ) >sample-i &&
> +	(test_write_lines 1 2 3 "" 0 5 ) >sample2-i &&
> +	cat sample-i >target &&
> +	git add target &&
> +	cat sample2-i >target &&
> +	git diff-files -p target >patch &&
> +	sed -e "s/^ $//" <patch >patch-new &&
> +
> +	cat sample-i >target &&
> +	git apply --whitespace=fix <patch-new 2>error &&
> +	test_cmp sample2-i target &&
> +	test_must_be_empty error
> +'
> +
>  test_expect_success 'incomplete context line (not an error)' '
>  	(test_write_lines 1 2 3 4 5 && printf 6) >sample-i &&
>  	(test_write_lines 1 2 3 0 5 && printf 6) >sample2-i &&

      parent reply	other threads:[~2026-03-31 21:54 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-17 18:01 [PATCH] apply: fix new-style empty context line triggering incomplete-line check Junio C Hamano
2026-03-17 18:12 ` Eric Sunshine
2026-03-17 18:45   ` Junio C Hamano
2026-03-18 16:36     ` D. Ben Knoble
2026-03-18 16:43       ` Eric Sunshine
2026-03-18 17:12       ` Junio C Hamano
2026-03-31 21:54 ` Junio C Hamano [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqq7bqry8ac.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox