Skip to content

Commit 877f23c

Browse files
committed
Teach "diff --check" about new blank lines at end
When a patch adds new blank lines at the end, "git apply --whitespace" warns. This teaches "diff --check" to do the same. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 1ba111d commit 877f23c

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,7 @@ extern unsigned ws_check(const char *line, int len, unsigned ws_rule);
823823
extern void ws_check_emit(const char *line, int len, unsigned ws_rule, FILE *stream, const char *set, const char *reset, const char *ws);
824824
extern char *whitespace_error_string(unsigned ws);
825825
extern int ws_fix_copy(char *, const char *, int, unsigned, int *);
826+
extern int ws_blank_line(const char *line, int len, unsigned ws_rule);
826827

827828
/* ls-files */
828829
int pathspec_match(const char **spec, char *matched, const char *filename, int skiplen);

diff.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,7 @@ struct checkdiff_t {
11401140
struct diff_options *o;
11411141
unsigned ws_rule;
11421142
unsigned status;
1143+
int trailing_blanks_start;
11431144
};
11441145

11451146
static void checkdiff_consume(void *priv, char *line, unsigned long len)
@@ -1154,6 +1155,10 @@ static void checkdiff_consume(void *priv, char *line, unsigned long len)
11541155
if (line[0] == '+') {
11551156
unsigned bad;
11561157
data->lineno++;
1158+
if (!ws_blank_line(line + 1, len - 1, data->ws_rule))
1159+
data->trailing_blanks_start = 0;
1160+
else if (!data->trailing_blanks_start)
1161+
data->trailing_blanks_start = data->lineno;
11571162
bad = ws_check(line + 1, len - 1, data->ws_rule);
11581163
if (!bad)
11591164
return;
@@ -1165,14 +1170,16 @@ static void checkdiff_consume(void *priv, char *line, unsigned long len)
11651170
emit_line(data->o->file, set, reset, line, 1);
11661171
ws_check_emit(line + 1, len - 1, data->ws_rule,
11671172
data->o->file, set, reset, ws);
1168-
} else if (line[0] == ' ')
1173+
} else if (line[0] == ' ') {
11691174
data->lineno++;
1170-
else if (line[0] == '@') {
1175+
data->trailing_blanks_start = 0;
1176+
} else if (line[0] == '@') {
11711177
char *plus = strchr(line, '+');
11721178
if (plus)
11731179
data->lineno = strtol(plus, NULL, 10) - 1;
11741180
else
11751181
die("invalid diff");
1182+
data->trailing_blanks_start = 0;
11761183
}
11771184
}
11781185

@@ -1584,6 +1591,12 @@ static void builtin_checkdiff(const char *name_a, const char *name_b,
15841591
ecb.outf = xdiff_outf;
15851592
ecb.priv = &data;
15861593
xdi_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
1594+
1595+
if (data.trailing_blanks_start) {
1596+
fprintf(o->file, "%s:%d: ends with blank lines.\n",
1597+
data.filename, data.trailing_blanks_start);
1598+
data.status = 1; /* report errors */
1599+
}
15871600
}
15881601
free_and_return:
15891602
diff_free_filespec_data(one);

t/t4015-diff-whitespace.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,4 +335,10 @@ test_expect_success 'line numbers in --check output are correct' '
335335
336336
'
337337

338+
test_expect_success 'checkdiff detects trailing blank lines' '
339+
echo "foo();" >x &&
340+
echo "" >>x &&
341+
git diff --check | grep "ends with blank"
342+
'
343+
338344
test_done

ws.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,21 @@ unsigned ws_check(const char *line, int len, unsigned ws_rule)
225225
return ws_check_emit_1(line, len, ws_rule, NULL, NULL, NULL, NULL);
226226
}
227227

228+
int ws_blank_line(const char *line, int len, unsigned ws_rule)
229+
{
230+
/*
231+
* We _might_ want to treat CR differently from other
232+
* whitespace characters when ws_rule has WS_CR_AT_EOL, but
233+
* for now we just use this stupid definition.
234+
*/
235+
while (len-- > 0) {
236+
if (!isspace(*line))
237+
return 0;
238+
line++;
239+
}
240+
return 1;
241+
}
242+
228243
/* Copy the line to the buffer while fixing whitespaces */
229244
int ws_fix_copy(char *dst, const char *src, int len, unsigned ws_rule, int *error_count)
230245
{

0 commit comments

Comments
 (0)