@matheusbsbs / matheus-bagliano / commits / 877f23ccb8
Fork 0

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>

Junio C Hamano committed Jun 26, 2008 at 15:36 UTC 877f23ccb88227203f2576abdfb5d1c15925fcb3
4 files changed +37 -2
cache.h
+1
@@ -823,6 +823,7 @@ extern unsigned ws_check(const char *line, int len, unsigned ws_rule);
823 extern void ws_check_emit(const char *line, int len, unsigned ws_rule, FILE *stream, const char *set, const char *reset, const char *ws);
824 extern char *whitespace_error_string(unsigned ws);
825 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);
827
828 /* ls-files */
829 int pathspec_match(const char **spec, char *matched, const char *filename, int skiplen);
diff.c
+15 -2
@@ -1140,6 +1140,7 @@ struct checkdiff_t {
1140 struct diff_options *o;
1141 unsigned ws_rule;
1142 unsigned status;
1143 + int trailing_blanks_start;
1144 };
1145
1146 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)
1155 if (line[0] == '+') {
1156 unsigned bad;
1157 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;
1162 bad = ws_check(line + 1, len - 1, data->ws_rule);
1163 if (!bad)
1164 return;
@@ -1165,14 +1170,16 @@ static void checkdiff_consume(void *priv, char *line, unsigned long len)
1170 emit_line(data->o->file, set, reset, line, 1);
1171 ws_check_emit(line + 1, len - 1, data->ws_rule,
1172 data->o->file, set, reset, ws);
1168 - } else if (line[0] == ' ')
1173 + } else if (line[0] == ' ') {
1174 data->lineno++;
1170 - else if (line[0] == '@') {
1175 + data->trailing_blanks_start = 0;
1176 + } else if (line[0] == '@') {
1177 char *plus = strchr(line, '+');
1178 if (plus)
1179 data->lineno = strtol(plus, NULL, 10) - 1;
1180 else
1181 die("invalid diff");
1182 + data->trailing_blanks_start = 0;
1183 }
1184 }
1185
@@ -1584,6 +1591,12 @@ static void builtin_checkdiff(const char *name_a, const char *name_b,
1591 ecb.outf = xdiff_outf;
1592 ecb.priv = &data;
1593 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 + }
1600 }
1601 free_and_return:
1602 diff_free_filespec_data(one);
t/t4015-diff-whitespace.sh
+6
@@ -335,4 +335,10 @@ test_expect_success 'line numbers in --check output are correct' '
335
336 '
337
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 +
344 test_done
ws.c
+15
@@ -225,6 +225,21 @@ unsigned ws_check(const char *line, int len, unsigned ws_rule)
225 return ws_check_emit_1(line, len, ws_rule, NULL, NULL, NULL, NULL);
226 }
227
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 +
243 /* Copy the line to the buffer while fixing whitespaces */
244 int ws_fix_copy(char *dst, const char *src, int len, unsigned ws_rule, int *error_count)
245 {