From: Junio C Hamano <gitster@pobox.com>
To: git@vger.kernel.org
Subject: [PATCH v4 10/12] apply: check and fix incomplete lines
Date: Wed, 12 Nov 2025 14:02:56 -0800 [thread overview]
Message-ID: <20251112220258.1009253-11-gitster@pobox.com> (raw)
In-Reply-To: <20251112220258.1009253-1-gitster@pobox.com>
The final line of a file that lacks the terminating newline at its
end is called an incomplete line. In general they are frowned upon
for many reasons (imagine concatenating two files with "cat A B" and
what happens when A ends in an incomplete line, for example), and
text-oriented tools often mishandle such a line.
Implement checks in "git apply" for incomplete lines, which is off
by default for backward compatibility's sake, so that "git apply
--whitespace={fix,warn,error}" can notice, warn against, and fix
them.
As one of the new test shows, if you modify contents on an
incomplete line in the original and leave the resulting line
incomplete, it is still considered a whitespace error, the reasoning
being that "you'd better fix it while at it if you are making a
change on an incomplete line anyway", which may controversial.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
apply.c | 13 ++-
t/t4124-apply-ws-rule.sh | 187 +++++++++++++++++++++++++++++++++++++++
ws.c | 14 +++
3 files changed, 213 insertions(+), 1 deletion(-)
diff --git a/apply.c b/apply.c
index 2b0f8bdab5..c9fb45247d 100644
--- a/apply.c
+++ b/apply.c
@@ -1640,6 +1640,14 @@ static void record_ws_error(struct apply_state *state,
state->squelch_whitespace_errors < state->whitespace_error)
return;
+ /*
+ * line[len] for an incomplete line points at the "\n" at the end
+ * of patch input line, so "%.*s" would drop the last letter on line;
+ * compensate for it.
+ */
+ if (result & WS_INCOMPLETE_LINE)
+ len++;
+
err = whitespace_error_string(result);
if (state->apply_verbosity > verbosity_silent)
fprintf(stderr, "%s:%d: %s.\n%.*s\n",
@@ -1794,7 +1802,10 @@ static int parse_fragment(struct apply_state *state,
}
/* eat the "\\ No newline..." as well, if exists */
- len += skip_len;
+ if (skip_len) {
+ len += skip_len;
+ state->linenr++;
+ }
}
if (oldlines || newlines)
return -1;
diff --git a/t/t4124-apply-ws-rule.sh b/t/t4124-apply-ws-rule.sh
index 485c7d2d12..115a0f8579 100755
--- a/t/t4124-apply-ws-rule.sh
+++ b/t/t4124-apply-ws-rule.sh
@@ -556,4 +556,191 @@ test_expect_success 'whitespace check skipped for excluded paths' '
git apply --include=used --stat --whitespace=error <patch
'
+test_expect_success 'check incomplete lines (setup)' '
+ rm -f .gitattributes &&
+ git config core.whitespace incomplete-line
+'
+
+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 &&
+ cat sample-i >target &&
+ git add target &&
+ cat sample2-i >target &&
+ git diff-files -p target >patch &&
+
+ cat sample-i >target &&
+ git apply --whitespace=error <patch &&
+ test_cmp sample2-i target &&
+
+ cat sample-i >target &&
+ git apply --whitespace=error --check <patch 2>error &&
+ test_cmp sample-i target &&
+ test_must_be_empty error &&
+
+ cat sample2-i >target &&
+ git apply --whitespace=error -R <patch &&
+ test_cmp sample-i target &&
+
+ cat sample2-i >target &&
+ git apply -R --whitespace=error --check <patch 2>error &&
+ test_cmp sample2-i target &&
+ test_must_be_empty error
+'
+
+test_expect_success 'last line made incomplete (error)' '
+ test_write_lines 1 2 3 4 5 6 >sample &&
+ (test_write_lines 1 2 3 4 5 && printf 6) >sample-i &&
+ cat sample >target &&
+ git add target &&
+ cat sample-i >target &&
+ git diff-files -p target >patch &&
+
+ cat sample >target &&
+ test_must_fail git apply --whitespace=error <patch 2>error &&
+ test_grep "no newline" error &&
+
+ cat sample >target &&
+ test_must_fail git apply --whitespace=error --check <patch 2>actual &&
+ test_cmp sample target &&
+ cat >expect <<-\EOF &&
+ <stdin>:10: no newline at the end of file.
+ 6
+ error: 1 line adds whitespace errors.
+ EOF
+ test_cmp expect actual &&
+
+ cat sample-i >target &&
+ git apply --whitespace=error -R <patch &&
+ test_cmp sample target &&
+
+ cat sample-i >target &&
+ git apply --whitespace=error --check -R <patch 2>error &&
+ test_cmp sample-i target &&
+ test_must_be_empty error &&
+
+ cat sample >target &&
+ git apply --whitespace=fix <patch &&
+ test_cmp sample target
+'
+
+test_expect_success 'incomplete line removed at the end (not an error)' '
+ (test_write_lines 1 2 3 4 5 && printf 6) >sample-i &&
+ test_write_lines 1 2 3 4 5 6 >sample &&
+ cat sample-i >target &&
+ git add target &&
+ cat sample >target &&
+ git diff-files -p target >patch &&
+
+ cat sample-i >target &&
+ git apply --whitespace=error <patch &&
+ test_cmp sample target &&
+
+ cat sample-i >target &&
+ git apply --whitespace=error --check <patch 2>error &&
+ test_cmp sample-i target &&
+ test_must_be_empty error &&
+
+ cat sample >target &&
+ test_must_fail git apply --whitespace=error -R <patch 2>error &&
+ test_grep "no newline" error &&
+
+ cat sample >target &&
+ test_must_fail git apply --whitespace=error --check -R <patch 2>actual &&
+ test_cmp sample target &&
+ cat >expect <<-\EOF &&
+ <stdin>:9: no newline at the end of file.
+ 6
+ error: 1 line adds whitespace errors.
+ EOF
+ test_cmp expect actual &&
+
+ cat sample >target &&
+ git apply --whitespace=fix -R <patch &&
+ test_cmp sample target
+'
+
+test_expect_success 'incomplete line corrected at the end (not an error)' '
+ (test_write_lines 1 2 3 4 5 && printf 6) >sample-i &&
+ test_write_lines 1 2 3 4 5 7 >sample3 &&
+ cat sample-i >target &&
+ git add target &&
+ cat sample3 >target &&
+ git diff-files -p target >patch &&
+
+ cat sample-i >target &&
+ git apply --whitespace=error <patch &&
+ test_cmp sample3 target &&
+
+ cat sample-i >target &&
+ git apply --whitespace=error --check <patch 2>error &&
+ test_cmp sample-i target &&
+ test_must_be_empty error &&
+
+ cat sample3 >target &&
+ test_must_fail git apply --whitespace=error -R <patch 2>error &&
+ test_grep "no newline" error &&
+
+ cat sample3 >target &&
+ test_must_fail git apply --whitespace=error -R --check <patch 2>actual &&
+ test_cmp sample3 target &&
+ cat >expect <<-\EOF &&
+ <stdin>:9: no newline at the end of file.
+ 6
+ error: 1 line adds whitespace errors.
+ EOF
+ test_cmp expect actual &&
+
+ cat sample3 >target &&
+ git apply --whitespace=fix -R <patch &&
+ test_cmp sample target
+'
+
+test_expect_success 'incomplete line modified at the end (error)' '
+ (test_write_lines 1 2 3 4 5 && printf 6) >sample-i &&
+ (test_write_lines 1 2 3 4 5 && printf 7) >sample3-i &&
+ test_write_lines 1 2 3 4 5 6 >sample &&
+ test_write_lines 1 2 3 4 5 7 >sample3 &&
+ cat sample-i >target &&
+ git add target &&
+ cat sample3-i >target &&
+ git diff-files -p target >patch &&
+
+ cat sample-i >target &&
+ test_must_fail git apply --whitespace=error <patch 2>error &&
+ test_grep "no newline" error &&
+
+ cat sample-i >target &&
+ test_must_fail git apply --whitespace=error --check <patch 2>actual &&
+ test_cmp sample-i target &&
+ cat >expect <<-\EOF &&
+ <stdin>:11: no newline at the end of file.
+ 7
+ error: 1 line adds whitespace errors.
+ EOF
+ test_cmp expect actual &&
+
+ cat sample3-i >target &&
+ test_must_fail git apply --whitespace=error -R <patch 2>error &&
+ test_grep "no newline" error &&
+
+ cat sample3-i >target &&
+ test_must_fail git apply --whitespace=error --check -R <patch 2>actual &&
+ test_cmp sample3-i target &&
+ cat >expect <<-\EOF &&
+ <stdin>:9: no newline at the end of file.
+ 6
+ error: 1 line adds whitespace errors.
+ EOF
+ test_cmp expect actual &&
+
+ cat sample-i >target &&
+ git apply --whitespace=fix <patch &&
+ test_cmp sample3 target &&
+
+ cat sample3-i >target &&
+ git apply --whitespace=fix -R <patch &&
+ test_cmp sample target
+'
+
test_done
diff --git a/ws.c b/ws.c
index 34a7b4fad2..6cc2466c0c 100644
--- a/ws.c
+++ b/ws.c
@@ -186,6 +186,9 @@ static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule,
if (trailing_whitespace == -1)
trailing_whitespace = len;
+ if (!trailing_newline && (ws_rule & WS_INCOMPLETE_LINE))
+ result |= WS_INCOMPLETE_LINE;
+
/* Check indentation */
for (i = 0; i < trailing_whitespace; i++) {
if (line[i] == ' ')
@@ -297,6 +300,17 @@ void ws_fix_copy(struct strbuf *dst, const char *src, int len, unsigned ws_rule,
int last_space_in_indent = -1;
int need_fix_leading_space = 0;
+ /*
+ * Remembering that we need to add '\n' at the end
+ * is sufficient to fix an incomplete line.
+ */
+ if (ws_rule & WS_INCOMPLETE_LINE) {
+ if (0 < len && src[len - 1] != '\n') {
+ fixed = 1;
+ add_nl_to_tail = 1;
+ }
+ }
+
/*
* Strip trailing whitespace
*/
--
2.52.0-rc2-441-g030905368a
next prev parent reply other threads:[~2025-11-12 22:03 UTC|newest]
Thread overview: 73+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-04 2:09 [PATCH 00/12] Incomplete lines Junio C Hamano
2025-11-04 2:09 ` [PATCH 01/12] whitespace: correct bit assignment comments Junio C Hamano
2025-11-04 2:09 ` [PATCH 02/12] diff: emit_line_ws_markup() if/else style fix Junio C Hamano
2025-11-04 2:09 ` [PATCH 03/12] diff: correct suppress_blank_empty hack Junio C Hamano
2025-11-04 2:09 ` [PATCH 04/12] diff: fix incorrect counting of line numbers Junio C Hamano
2025-11-10 14:54 ` Phillip Wood
2025-11-10 18:29 ` Junio C Hamano
2025-11-11 14:26 ` Phillip Wood
2025-11-11 14:37 ` Junio C Hamano
2025-11-04 2:09 ` [PATCH 05/12] diff: refactor output of incomplete line Junio C Hamano
2025-11-04 2:09 ` [PATCH 06/12] diff: call emit_callback ecbdata everywhere Junio C Hamano
2025-11-04 2:09 ` [PATCH 07/12] diff: update the way rewrite diff handles incomplete lines Junio C Hamano
2025-11-10 14:54 ` Phillip Wood
2025-11-10 18:33 ` Junio C Hamano
2025-11-04 2:09 ` [PATCH 08/12] apply: revamp the parsing of " Junio C Hamano
2025-11-04 2:09 ` [PATCH 09/12] whitespace: allocate a few more bits Junio C Hamano
2025-11-04 2:09 ` [PATCH 10/12] apply: check and fix incomplete lines Junio C Hamano
2025-11-04 2:09 ` [PATCH 11/12] diff: highlight and error out on " Junio C Hamano
2025-11-10 14:55 ` Phillip Wood
2025-11-10 18:38 ` Junio C Hamano
2025-11-10 23:56 ` D. Ben Knoble
2025-11-04 2:09 ` [PATCH 12/12] attr: enable incomplete-line whitespace error for this project Junio C Hamano
2025-11-10 14:55 ` Phillip Wood
2025-11-10 18:40 ` Junio C Hamano
2025-11-05 21:30 ` [PATCH v2 00/12] Incomplete lines Junio C Hamano
2025-11-05 21:30 ` [PATCH v2 01/12] whitespace: correct bit assignment comments Junio C Hamano
2025-11-05 21:30 ` [PATCH v2 02/12] diff: emit_line_ws_markup() if/else style fix Junio C Hamano
2025-11-05 21:30 ` [PATCH v2 03/12] diff: correct suppress_blank_empty hack Junio C Hamano
2025-11-05 21:30 ` [PATCH v2 04/12] diff: fix incorrect counting of line numbers Junio C Hamano
2025-11-05 21:30 ` [PATCH v2 05/12] diff: refactor output of incomplete line Junio C Hamano
2025-11-10 10:06 ` Patrick Steinhardt
2025-11-10 17:58 ` Junio C Hamano
2025-11-05 21:30 ` [PATCH v2 06/12] diff: call emit_callback ecbdata everywhere Junio C Hamano
2025-11-05 21:30 ` [PATCH v2 07/12] diff: update the way rewrite diff handles incomplete lines Junio C Hamano
2025-11-10 10:06 ` Patrick Steinhardt
2025-11-10 18:14 ` Junio C Hamano
2025-11-05 21:30 ` [PATCH v2 08/12] apply: revamp the parsing of " Junio C Hamano
2025-11-05 21:30 ` [PATCH v2 09/12] whitespace: allocate a few more bits and define WS_INCOMPLETE_LINE Junio C Hamano
2025-11-05 21:30 ` [PATCH v2 10/12] apply: check and fix incomplete lines Junio C Hamano
2025-11-05 21:30 ` [PATCH v2 11/12] diff: highlight and error out on " Junio C Hamano
2025-11-05 21:30 ` [PATCH v2 12/12] attr: enable incomplete-line whitespace error for this project Junio C Hamano
2025-11-10 10:09 ` [PATCH v2 00/12] Incomplete lines Patrick Steinhardt
2025-11-10 14:53 ` Phillip Wood
2025-11-11 0:04 ` [PATCH v3 " Junio C Hamano
2025-11-11 0:04 ` [PATCH v3 01/12] whitespace: correct bit assignment comments Junio C Hamano
2025-11-11 0:04 ` [PATCH v3 02/12] diff: emit_line_ws_markup() if/else style fix Junio C Hamano
2025-11-11 0:04 ` [PATCH v3 03/12] diff: correct suppress_blank_empty hack Junio C Hamano
2025-11-11 0:04 ` [PATCH v3 04/12] diff: fix incorrect counting of line numbers Junio C Hamano
2025-11-11 0:04 ` [PATCH v3 05/12] diff: refactor output of incomplete line Junio C Hamano
2025-11-11 0:04 ` [PATCH v3 06/12] diff: call emit_callback ecbdata everywhere Junio C Hamano
2025-11-11 0:04 ` [PATCH v3 07/12] diff: update the way rewrite diff handles incomplete lines Junio C Hamano
2025-11-11 0:04 ` [PATCH v3 08/12] apply: revamp the parsing of " Junio C Hamano
2025-11-11 0:04 ` [PATCH v3 09/12] whitespace: allocate a few more bits and define WS_INCOMPLETE_LINE Junio C Hamano
2025-11-11 0:04 ` [PATCH v3 10/12] apply: check and fix incomplete lines Junio C Hamano
2025-11-11 0:04 ` [PATCH v3 11/12] diff: highlight and error out on " Junio C Hamano
2025-11-11 0:04 ` [PATCH v3 12/12] attr: enable incomplete-line whitespace error for this project Junio C Hamano
2025-11-11 14:29 ` [PATCH v3 00/12] Incomplete lines Phillip Wood
2025-11-12 22:02 ` [PATCH v4 " Junio C Hamano
2025-11-12 22:02 ` [PATCH v4 01/12] whitespace: correct bit assignment comments Junio C Hamano
2025-11-12 22:02 ` [PATCH v4 02/12] diff: emit_line_ws_markup() if/else style fix Junio C Hamano
2025-11-12 22:02 ` [PATCH v4 03/12] diff: correct suppress_blank_empty hack Junio C Hamano
2025-11-12 22:02 ` [PATCH v4 04/12] diff: keep track of the type of the last line seen Junio C Hamano
2025-11-12 22:02 ` [PATCH v4 05/12] diff: refactor output of incomplete line Junio C Hamano
2025-11-12 22:02 ` [PATCH v4 06/12] diff: call emit_callback ecbdata everywhere Junio C Hamano
2025-11-12 22:02 ` [PATCH v4 07/12] diff: update the way rewrite diff handles incomplete lines Junio C Hamano
2025-11-12 22:02 ` [PATCH v4 08/12] apply: revamp the parsing of " Junio C Hamano
2025-11-12 22:02 ` [PATCH v4 09/12] whitespace: allocate a few more bits and define WS_INCOMPLETE_LINE Junio C Hamano
2025-11-12 22:02 ` Junio C Hamano [this message]
2025-11-12 22:02 ` [PATCH v4 11/12] diff: highlight and error out on incomplete lines Junio C Hamano
2025-11-12 22:02 ` [PATCH v4 12/12] attr: enable incomplete-line whitespace error for this project Junio C Hamano
2025-11-14 10:24 ` [PATCH v4 00/12] Incomplete lines Phillip Wood
2025-11-14 16:25 ` Junio C Hamano
2025-11-23 2:35 ` Junio C Hamano
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=20251112220258.1009253-11-gitster@pobox.com \
--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;
as well as URLs for NNTP newsgroup(s).