[PATCH 00/12] Incomplete lines
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Nov 4, 2025, 02:09 UTC
- Message-ID
- <20251104020928.582199-1-gitster@pobox.com>
One of the common kind of whitespace errors is to lack the terminating newline at the end of a file, but so far, neither "git diff" or "git apply" did anything about them.
This series introduces "incomplete-line" whitespace error class, that you can add to either the core.whitespace configuration variable, or the whitespace attribute in your .gitattributes files. The class is disabled by default.
When incomplete-line whitespace error is enabled, a patch file that has "\ No newline at the end of file" line for the postimage file is considered to introduce a whitespace error. This is true even if the corresponding preimage file ends in a different contents on the final line that is incomplete. The incomplete line marker that is given for a context line is not considered an error. The reasoning is that your preimage did have incomplete line, but you did not touch the contents on that incomplete line in your patch, so you left the line intact. It is not a new breakage you are responsible for. If the incomplete line marker follows a postimage line, on the other hand, it means that you either made a file that used to end with a complete line to end with an incomplete line, or the file ended with an incomplete line before your change, and you did not fix it even though you modified other bytes on that same last line, which you could have easily fixed while at it, and that is flagged as an error.
* "git diff --check" notices and errors out.
* "git diff" and friends highlight the offending "\ No newline ..." line.
* "git apply --whitespace=(error|warn)" triggers an error, and "git apply --whitespace=fix" would correct it by appending a newline.
The organization of the series is
* The first patch [01/12] is a clean-up we have seen earlier on the list already (https://lore.kernel.org/git/xmqqfrb4hyjl.fsf@gitster.g/).
* The patches [02/12] - [08/12] are preliminary clean-up made to both "git diff" and "git apply" machinery.
* The patch [09/12] shifts the bit assignment (cleaned-up in [01/12] without changing any values) to make room for new whitespace error class (which was last updated in 2007 IIRC, so the set of whitespace errors surprisingly haven't changed for quite some time).
* The patch [10/12] teaches "git apply --whitespace=<mode>" about the incomplete-line error class.
* The patch [11/12] teaches "git diff [--check]" about the incomplete-line error class.
* The final patch [12/12] enables the incomplete-line error class for our project for C source files and shell scripts. I didn't touch the cover-all * entry.
Junio C Hamano (12): whitespace: correct bit assignment comments diff: emit_line_ws_markup() if/else style fix diff: correct suppress_blank_empty hack diff: fix incorrect counting of line numbers diff: refactor output of incomplete line diff: call emit_callback ecbdata everywhere diff: update the way rewrite diff handles incomplete lines apply: revamp the parsing of incomplete lines whitespace: allocate a few more bits apply: check and fix incomplete lines diff: highlight and error out on incomplete lines attr: enable incomplete-line whitespace error for this project
.gitattributes | 4 +- apply.c | 70 ++++++++++++------ diff.c | 148 +++++++++++++++++++++++++------------ diff.h | 6 +- t/t4015-diff-whitespace.sh | 63 +++++++++++++++- t/t4124-apply-ws-rule.sh | 112 ++++++++++++++++++++++++++++ ws.c | 20 +++++ ws.h | 26 ++++--- 8 files changed, 361 insertions(+), 88 deletions(-)
-- 2.52.0-rc0