aboutsummaryrefslogtreecommitdiffstats
path: root/ws.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2025-11-30 18:31:40 -0800
committerJunio C Hamano <gitster@pobox.com>2025-11-30 18:31:40 -0800
commit3b212a83feea0962d437a3d5883cdd635403fe3c (patch)
tree446e71b654752ef15a60d8a57869f322c75cc9af /ws.c
parentffd9bb1bc7ffc635ff8687b79c23a6a0a3551185 (diff)
parent51358a1ede7f4b6b50e4e5a86558af5204691fe0 (diff)
downloadgit-3b212a83feea0962d437a3d5883cdd635403fe3c.tar.gz
Merge branch 'jc/whitespace-incomplete-line'
Both "git apply" and "git diff" learn a new whitespace error class, "incomplete-line". * jc/whitespace-incomplete-line: attr: enable incomplete-line whitespace error for this project diff: highlight and error out on incomplete lines apply: check and fix incomplete lines whitespace: allocate a few more bits and define WS_INCOMPLETE_LINE apply: revamp the parsing of incomplete lines diff: update the way rewrite diff handles incomplete lines diff: call emit_callback ecbdata everywhere diff: refactor output of incomplete line diff: keep track of the type of the last line seen diff: correct suppress_blank_empty hack diff: emit_line_ws_markup() if/else style fix whitespace: correct bit assignment comments
Diffstat (limited to 'ws.c')
-rw-r--r--ws.c20
1 files changed, 20 insertions, 0 deletions
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] == ' ')
@@ -292,6 +301,17 @@ void ws_fix_copy(struct strbuf *dst, const char *src, int len, unsigned ws_rule,
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
*/
if (ws_rule & WS_BLANK_AT_EOL) {