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

[PATCH v2 10/12] apply: check and fix incomplete lines

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 5, 2025, 21:30 UTC
Message-ID
<20251105213052.1499224-11-gitster@pobox.com>
In-Reply-To
<20251105213052.1499224-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-rc0-105-gc08128fbb6
Previous: Junio C HamanoNext: Junio C Hamano
Message 39 of 73 in “Incomplete lines”
  1. 00/12 Incomplete linesJunio C Hamano, Nov 4, 2025
  2. 01/12 whitespace: correct bit assignment commentsJunio C Hamano, Nov 4, 2025
  3. 02/12 diff: emit_line_ws_markup() if/else style fixJunio C Hamano, Nov 4, 2025
  4. 03/12 diff: correct suppress_blank_empty hackJunio C Hamano, Nov 4, 2025
  5. 04/12 diff: fix incorrect counting of line numbersJunio C Hamano, Nov 4, 2025
  6. Phillip WoodNov 10, 2025
  7. Junio C HamanoNov 10, 2025
  8. Phillip WoodNov 11, 2025
  9. Junio C HamanoNov 11, 2025
  10. 05/12 diff: refactor output of incomplete lineJunio C Hamano, Nov 4, 2025
  11. 06/12 diff: call emit_callback ecbdata everywhereJunio C Hamano, Nov 4, 2025
  12. 07/12 diff: update the way rewrite diff handles incomplete linesJunio C Hamano, Nov 4, 2025
  13. Phillip WoodNov 10, 2025
  14. Junio C HamanoNov 10, 2025
  15. 08/12 apply: revamp the parsing of incomplete linesJunio C Hamano, Nov 4, 2025
  16. 09/12 whitespace: allocate a few more bitsJunio C Hamano, Nov 4, 2025
  17. 10/12 apply: check and fix incomplete linesJunio C Hamano, Nov 4, 2025
  18. 11/12 diff: highlight and error out on incomplete linesJunio C Hamano, Nov 4, 2025
  19. Phillip WoodNov 10, 2025
  20. Junio C HamanoNov 10, 2025
  21. D. Ben KnobleNov 10, 2025
  22. 12/12 attr: enable incomplete-line whitespace error for this projectJunio C Hamano, Nov 4, 2025
  23. Phillip WoodNov 10, 2025
  24. Junio C HamanoNov 10, 2025
  25. 00/12 Incomplete linesJunio C Hamano, Nov 5, 2025
  26. 01/12 whitespace: correct bit assignment commentsJunio C Hamano, Nov 5, 2025
  27. 02/12 diff: emit_line_ws_markup() if/else style fixJunio C Hamano, Nov 5, 2025
  28. 03/12 diff: correct suppress_blank_empty hackJunio C Hamano, Nov 5, 2025
  29. 04/12 diff: fix incorrect counting of line numbersJunio C Hamano, Nov 5, 2025
  30. 05/12 diff: refactor output of incomplete lineJunio C Hamano, Nov 5, 2025
  31. Patrick SteinhardtNov 10, 2025
  32. Junio C HamanoNov 10, 2025
  33. 06/12 diff: call emit_callback ecbdata everywhereJunio C Hamano, Nov 5, 2025
  34. 07/12 diff: update the way rewrite diff handles incomplete linesJunio C Hamano, Nov 5, 2025
  35. Patrick SteinhardtNov 10, 2025
  36. Junio C HamanoNov 10, 2025
  37. 08/12 apply: revamp the parsing of incomplete linesJunio C Hamano, Nov 5, 2025
  38. 09/12 whitespace: allocate a few more bits and define WS_INCOMPLETE_LINEJunio C Hamano, Nov 5, 2025
  39. 10/12 apply: check and fix incomplete linesJunio C Hamano, Nov 5, 2025
  40. 11/12 diff: highlight and error out on incomplete linesJunio C Hamano, Nov 5, 2025
  41. 12/12 attr: enable incomplete-line whitespace error for this projectJunio C Hamano, Nov 5, 2025
  42. Patrick SteinhardtNov 10, 2025
  43. Phillip WoodNov 10, 2025
  44. 00/12 Incomplete linesJunio C Hamano, Nov 11, 2025
  45. 01/12 whitespace: correct bit assignment commentsJunio C Hamano, Nov 11, 2025
  46. 02/12 diff: emit_line_ws_markup() if/else style fixJunio C Hamano, Nov 11, 2025
  47. 03/12 diff: correct suppress_blank_empty hackJunio C Hamano, Nov 11, 2025
  48. 04/12 diff: fix incorrect counting of line numbersJunio C Hamano, Nov 11, 2025
  49. 05/12 diff: refactor output of incomplete lineJunio C Hamano, Nov 11, 2025
  50. 06/12 diff: call emit_callback ecbdata everywhereJunio C Hamano, Nov 11, 2025
  51. 07/12 diff: update the way rewrite diff handles incomplete linesJunio C Hamano, Nov 11, 2025
  52. 08/12 apply: revamp the parsing of incomplete linesJunio C Hamano, Nov 11, 2025
  53. 09/12 whitespace: allocate a few more bits and define WS_INCOMPLETE_LINEJunio C Hamano, Nov 11, 2025
  54. 10/12 apply: check and fix incomplete linesJunio C Hamano, Nov 11, 2025
  55. 11/12 diff: highlight and error out on incomplete linesJunio C Hamano, Nov 11, 2025
  56. 12/12 attr: enable incomplete-line whitespace error for this projectJunio C Hamano, Nov 11, 2025
  57. Phillip WoodNov 11, 2025
  58. 00/12 Incomplete linesJunio C Hamano, Nov 12, 2025
  59. 01/12 whitespace: correct bit assignment commentsJunio C Hamano, Nov 12, 2025
  60. 02/12 diff: emit_line_ws_markup() if/else style fixJunio C Hamano, Nov 12, 2025
  61. 03/12 diff: correct suppress_blank_empty hackJunio C Hamano, Nov 12, 2025
  62. 04/12 diff: keep track of the type of the last line seenJunio C Hamano, Nov 12, 2025
  63. 05/12 diff: refactor output of incomplete lineJunio C Hamano, Nov 12, 2025
  64. 06/12 diff: call emit_callback ecbdata everywhereJunio C Hamano, Nov 12, 2025
  65. 07/12 diff: update the way rewrite diff handles incomplete linesJunio C Hamano, Nov 12, 2025
  66. 08/12 apply: revamp the parsing of incomplete linesJunio C Hamano, Nov 12, 2025
  67. 09/12 whitespace: allocate a few more bits and define WS_INCOMPLETE_LINEJunio C Hamano, Nov 12, 2025
  68. 10/12 apply: check and fix incomplete linesJunio C Hamano, Nov 12, 2025
  69. 11/12 diff: highlight and error out on incomplete linesJunio C Hamano, Nov 12, 2025
  70. 12/12 attr: enable incomplete-line whitespace error for this projectJunio C Hamano, Nov 12, 2025
  71. Phillip WoodNov 14, 2025
  72. Junio C HamanoNov 14, 2025
  73. Junio C HamanoNov 23, 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.