Skip to content

Commit afd9db4

Browse files
committed
Merge branch 'jc/maint-1.6.0-blank-at-eof' (early part) into jc/maint-blank-at-eof
* 'jc/maint-1.6.0-blank-at-eof' (early part): diff --whitespace: fix blank lines at end core.whitespace: split trailing-space into blank-at-{eol,eof} diff --color: color blank-at-eof diff --whitespace=warn/error: fix blank-at-eof check diff --whitespace=warn/error: obey blank-at-eof diff.c: the builtin_diff() deals with only two-file comparison apply --whitespace: warn blank but not necessarily empty lines at EOF apply --whitespace=warn/error: diagnose blank at EOF apply.c: split check_whitespace() into two apply --whitespace=fix: detect new blank lines at eof correctly apply --whitespace=fix: fix handling of blank lines at the eof
2 parents 4197ce3 + d68fe26 commit afd9db4

File tree

8 files changed

+288
-70
lines changed

8 files changed

+288
-70
lines changed

Documentation/config.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,13 +401,17 @@ core.whitespace::
401401
consider them as errors. You can prefix `-` to disable
402402
any of them (e.g. `-trailing-space`):
403403
+
404-
* `trailing-space` treats trailing whitespaces at the end of the line
404+
* `blank-at-eol` treats trailing whitespaces at the end of the line
405405
as an error (enabled by default).
406406
* `space-before-tab` treats a space character that appears immediately
407407
before a tab character in the initial indent part of the line as an
408408
error (enabled by default).
409409
* `indent-with-non-tab` treats a line that is indented with 8 or more
410410
space characters as an error (not enabled by default).
411+
* `blank-at-eof` treats blank lines added at the end of file as an error
412+
(enabled by default).
413+
* `trailing-space` is a short-hand to cover both `blank-at-eol` and
414+
`blank-at-eof`.
411415
* `cr-at-eol` treats a carriage-return at the end of line as
412416
part of the line terminator, i.e. with it, `trailing-space`
413417
does not trigger if the character before such a carriage-return

builtin-apply.c

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ struct fragment {
131131
const char *patch;
132132
int size;
133133
int rejected;
134+
int linenr;
134135
struct fragment *next;
135136
};
136137

@@ -1149,23 +1150,29 @@ static int find_header(char *line, unsigned long size, int *hdrsize, struct patc
11491150
return -1;
11501151
}
11511152

1152-
static void check_whitespace(const char *line, int len, unsigned ws_rule)
1153+
static void record_ws_error(unsigned result, const char *line, int len, int linenr)
11531154
{
11541155
char *err;
1155-
unsigned result = ws_check(line + 1, len - 1, ws_rule);
1156+
11561157
if (!result)
11571158
return;
11581159

11591160
whitespace_error++;
11601161
if (squelch_whitespace_errors &&
11611162
squelch_whitespace_errors < whitespace_error)
1162-
;
1163-
else {
1164-
err = whitespace_error_string(result);
1165-
fprintf(stderr, "%s:%d: %s.\n%.*s\n",
1166-
patch_input_file, linenr, err, len - 2, line + 1);
1167-
free(err);
1168-
}
1163+
return;
1164+
1165+
err = whitespace_error_string(result);
1166+
fprintf(stderr, "%s:%d: %s.\n%.*s\n",
1167+
patch_input_file, linenr, err, len, line);
1168+
free(err);
1169+
}
1170+
1171+
static void check_whitespace(const char *line, int len, unsigned ws_rule)
1172+
{
1173+
unsigned result = ws_check(line + 1, len - 1, ws_rule);
1174+
1175+
record_ws_error(result, line + 1, len - 2, linenr);
11691176
}
11701177

11711178
/*
@@ -1281,6 +1288,7 @@ static int parse_single_patch(char *line, unsigned long size, struct patch *patc
12811288
int len;
12821289

12831290
fragment = xcalloc(1, sizeof(*fragment));
1291+
fragment->linenr = linenr;
12841292
len = parse_fragment(line, size, patch, fragment);
12851293
if (len <= 0)
12861294
die("corrupt patch at line %d", linenr);
@@ -2005,6 +2013,7 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
20052013
int len = linelen(patch, size);
20062014
int plen, added;
20072015
int added_blank_line = 0;
2016+
int is_blank_context = 0;
20082017

20092018
if (!len)
20102019
break;
@@ -2037,8 +2046,12 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
20372046
*new++ = '\n';
20382047
add_line_info(&preimage, "\n", 1, LINE_COMMON);
20392048
add_line_info(&postimage, "\n", 1, LINE_COMMON);
2049+
is_blank_context = 1;
20402050
break;
20412051
case ' ':
2052+
if (plen && (ws_rule & WS_BLANK_AT_EOF) &&
2053+
ws_blank_line(patch + 1, plen, ws_rule))
2054+
is_blank_context = 1;
20422055
case '-':
20432056
memcpy(old, patch + 1, plen);
20442057
add_line_info(&preimage, old, plen,
@@ -2065,7 +2078,8 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
20652078
(first == '+' ? 0 : LINE_COMMON));
20662079
new += added;
20672080
if (first == '+' &&
2068-
added == 1 && new[-1] == '\n')
2081+
(ws_rule & WS_BLANK_AT_EOF) &&
2082+
ws_blank_line(patch + 1, plen, ws_rule))
20692083
added_blank_line = 1;
20702084
break;
20712085
case '@': case '\\':
@@ -2078,6 +2092,8 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
20782092
}
20792093
if (added_blank_line)
20802094
new_blank_lines_at_end++;
2095+
else if (is_blank_context)
2096+
;
20812097
else
20822098
new_blank_lines_at_end = 0;
20832099
patch += len;
@@ -2159,17 +2175,24 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
21592175
}
21602176

21612177
if (applied_pos >= 0) {
2162-
if (ws_error_action == correct_ws_error &&
2163-
new_blank_lines_at_end &&
2164-
postimage.nr + applied_pos == img->nr) {
2178+
if (new_blank_lines_at_end &&
2179+
preimage.nr + applied_pos == img->nr &&
2180+
(ws_rule & WS_BLANK_AT_EOF) &&
2181+
ws_error_action != nowarn_ws_error) {
2182+
record_ws_error(WS_BLANK_AT_EOF, "+", 1, frag->linenr);
2183+
if (ws_error_action == correct_ws_error) {
2184+
while (new_blank_lines_at_end--)
2185+
remove_last_line(&postimage);
2186+
}
21652187
/*
2166-
* If the patch application adds blank lines
2167-
* at the end, and if the patch applies at the
2168-
* end of the image, remove those added blank
2169-
* lines.
2188+
* We would want to prevent write_out_results()
2189+
* from taking place in apply_patch() that follows
2190+
* the callchain led us here, which is:
2191+
* apply_patch->check_patch_list->check_patch->
2192+
* apply_data->apply_fragments->apply_one_fragment
21702193
*/
2171-
while (new_blank_lines_at_end--)
2172-
remove_last_line(&postimage);
2194+
if (ws_error_action == die_on_ws_error)
2195+
apply = 0;
21732196
}
21742197

21752198
/*

cache.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,10 +967,12 @@ void shift_tree(const unsigned char *, const unsigned char *, unsigned char *, i
967967
* whitespace rules.
968968
* used by both diff and apply
969969
*/
970-
#define WS_TRAILING_SPACE 01
970+
#define WS_BLANK_AT_EOL 01
971971
#define WS_SPACE_BEFORE_TAB 02
972972
#define WS_INDENT_WITH_NON_TAB 04
973973
#define WS_CR_AT_EOL 010
974+
#define WS_BLANK_AT_EOF 020
975+
#define WS_TRAILING_SPACE (WS_BLANK_AT_EOL|WS_BLANK_AT_EOF)
974976
#define WS_DEFAULT_RULE (WS_TRAILING_SPACE|WS_SPACE_BEFORE_TAB)
975977
extern unsigned whitespace_rule_cfg;
976978
extern unsigned whitespace_rule(const char *);

0 commit comments

Comments
 (0)