summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 1ba111d)
raw | patch | inline | side by side (parent: 1ba111d)
author | Junio C Hamano <gitster@pobox.com> | |
Thu, 26 Jun 2008 22:36:59 +0000 (15:36 -0700) | ||
committer | Junio C Hamano <gitster@pobox.com> | |
Fri, 27 Jun 2008 05:07:26 +0000 (22:07 -0700) |
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>
warns. This teaches "diff --check" to do the same.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
cache.h | patch | blob | history | |
diff.c | patch | blob | history | |
t/t4015-diff-whitespace.sh | patch | blob | history | |
ws.c | patch | blob | history |
index 3dfa53c5668fda37bfce453d381445e5269fb979..188428dd26b7d9bf8dc0b9cd1a02ebca6d459400 100644 (file)
--- a/cache.h
+++ b/cache.h
extern void ws_check_emit(const char *line, int len, unsigned ws_rule, FILE *stream, const char *set, const char *reset, const char *ws);
extern char *whitespace_error_string(unsigned ws);
extern int ws_fix_copy(char *, const char *, int, unsigned, int *);
+extern int ws_blank_line(const char *line, int len, unsigned ws_rule);
/* ls-files */
int pathspec_match(const char **spec, char *matched, const char *filename, int skiplen);
index 6bcbe2082873c7f311931890d39afdbd5345838d..f31c7211683802844f9aab3e86105695a17f79e5 100644 (file)
--- a/diff.c
+++ b/diff.c
struct diff_options *o;
unsigned ws_rule;
unsigned status;
+ int trailing_blanks_start;
};
static void checkdiff_consume(void *priv, char *line, unsigned long len)
if (line[0] == '+') {
unsigned bad;
data->lineno++;
+ if (!ws_blank_line(line + 1, len - 1, data->ws_rule))
+ data->trailing_blanks_start = 0;
+ else if (!data->trailing_blanks_start)
+ data->trailing_blanks_start = data->lineno;
bad = ws_check(line + 1, len - 1, data->ws_rule);
if (!bad)
return;
emit_line(data->o->file, set, reset, line, 1);
ws_check_emit(line + 1, len - 1, data->ws_rule,
data->o->file, set, reset, ws);
- } else if (line[0] == ' ')
+ } else if (line[0] == ' ') {
data->lineno++;
- else if (line[0] == '@') {
+ data->trailing_blanks_start = 0;
+ } else if (line[0] == '@') {
char *plus = strchr(line, '+');
if (plus)
data->lineno = strtol(plus, NULL, 10) - 1;
else
die("invalid diff");
+ data->trailing_blanks_start = 0;
}
}
ecb.outf = xdiff_outf;
ecb.priv = &data;
xdi_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
+
+ if (data.trailing_blanks_start) {
+ fprintf(o->file, "%s:%d: ends with blank lines.\n",
+ data.filename, data.trailing_blanks_start);
+ data.status = 1; /* report errors */
+ }
}
free_and_return:
diff_free_filespec_data(one);
index b7cc6b28e61e73af3206a9613620a635a467ea0e..0922c708f16fea490a85d16d5d4d366df0fb2088 100755 (executable)
'
+test_expect_success 'checkdiff detects trailing blank lines' '
+ echo "foo();" >x &&
+ echo "" >>x &&
+ git diff --check | grep "ends with blank"
+'
+
test_done
index 24d3e3de078c699ca6bb0dfd524c097bf220b4b2..7a7ff130a34942506e6068105ac5946c9404bf18 100644 (file)
--- a/ws.c
+++ b/ws.c
return ws_check_emit_1(line, len, ws_rule, NULL, NULL, NULL, NULL);
}
+int ws_blank_line(const char *line, int len, unsigned ws_rule)
+{
+ /*
+ * We _might_ want to treat CR differently from other
+ * whitespace characters when ws_rule has WS_CR_AT_EOL, but
+ * for now we just use this stupid definition.
+ */
+ while (len-- > 0) {
+ if (!isspace(*line))
+ return 0;
+ line++;
+ }
+ return 1;
+}
+
/* Copy the line to the buffer while fixing whitespaces */
int ws_fix_copy(char *dst, const char *src, int len, unsigned ws_rule, int *error_count)
{