]> git.ipfire.org Git - thirdparty/git.git/commitdiff
apply: fix new-style empty context line triggering incomplete-line check
authorJunio C Hamano <gitster@pobox.com>
Tue, 17 Mar 2026 18:01:38 +0000 (11:01 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 17 Mar 2026 18:46:47 +0000 (11:46 -0700)
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
t/t4124-apply-ws-rule.sh

diff --git a/apply.c b/apply.c
index f01204d15b4d13ef318c08e0c6b44b9ff79664e4..e88e5c77e32b01208fece90864164b17b6e36a11 100644 (file)
--- 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)
index 29ea7d4268eca4acca404e0e5173d2ef5f097e09..205d86d05edfec3d97a41f540cc1120e2293d9fe 100755 (executable)
@@ -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 &&