[PATCH 10/12] apply: check and fix incomplete lines
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Nov 4, 2025, 02:09 UTC
- Message-ID
- <20251104020928.582199-11-gitster@pobox.com>
- In-Reply-To
- <20251104020928.582199-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.
Introduce a new whitespace rule "incomplete-line", 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> --- t/t4124-apply-ws-rule.sh | 112 +++++++++++++++++++++++++++++++++++++++ ws.c | 20 +++++++ ws.h | 1 + 3 files changed, 133 insertions(+)
diff --git a/t/t4124-apply-ws-rule.sh b/t/t4124-apply-ws-rule.sh index 485c7d2d12..568805df31 100755 --- a/t/t4124-apply-ws-rule.sh +++ b/t/t4124-apply-ws-rule.sh @@ -556,4 +556,116 @@ 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 sample2-i >target && + git apply --whitespace=error -R <patch && + test_cmp sample-i target +' + +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-i >target && + git apply --whitespace=error -R <patch && + test_cmp sample target && + + 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 >target && + test_must_fail git apply --whitespace=error -R <patch 2>error && + test_grep "no newline" error && + + 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 sample3 >target && + test_must_fail git apply --whitespace=error -R <patch 2>error && + test_grep "no newline" error && + + 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 sample3-i >target && + test_must_fail git apply --whitespace=error -R <patch 2>error && + test_grep "no newline" error && + + 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 70acee3337..6cc2466c0c 100644 --- a/ws.c +++ b/ws.c @@ -26,6 +26,7 @@ static struct whitespace_rule { { "blank-at-eol", WS_BLANK_AT_EOL, 0 }, { "blank-at-eof", WS_BLANK_AT_EOF, 0 }, { "tab-in-indent", WS_TAB_IN_INDENT, 0, 1 }, + { "incomplete-line", WS_INCOMPLETE_LINE, 0, 0 }, }; unsigned parse_whitespace_rule(const char *string) @@ -139,6 +140,11 @@ char *whitespace_error_string(unsigned ws) strbuf_addstr(&err, ", "); strbuf_addstr(&err, "tab in indent"); } + if (ws & WS_INCOMPLETE_LINE) { + if (err.len) + strbuf_addstr(&err, ", "); + strbuf_addstr(&err, "no newline at the end of file"); + } return strbuf_detach(&err, NULL); } @@ -180,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] == ' ') @@ -291,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 */ diff --git a/ws.h b/ws.h index c77d3b6b19..06d5cb73f8 100644 --- a/ws.h +++ b/ws.h @@ -15,6 +15,7 @@ struct strbuf; #define WS_CR_AT_EOL (1<<9) #define WS_BLANK_AT_EOF (1<<10) #define WS_TAB_IN_INDENT (1<<11) +#define WS_INCOMPLETE_LINE (1<<12) #define WS_TRAILING_SPACE (WS_BLANK_AT_EOL|WS_BLANK_AT_EOF) #define WS_DEFAULT_RULE (WS_TRAILING_SPACE|WS_SPACE_BEFORE_TAB|8)
-- 2.52.0-rc0