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
@ -131,6 +131,7 @@ struct fragment {
@@ -131,6 +131,7 @@ struct fragment {
const char *patch;
int size;
int rejected;
int linenr;
struct fragment *next;
};
@ -1149,23 +1150,29 @@ static int find_header(char *line, unsigned long size, int *hdrsize, struct patc
@@ -1149,23 +1150,29 @@ static int find_header(char *line, unsigned long size, int *hdrsize, struct patc
return -1;
}
static void check_whitespace(const char *line, int len, unsigned ws_rule)
static void record_ws_error(unsigned result, const char *line, int len, int linenr)
{
char *err;
unsigned result = ws_check(line + 1, len - 1, ws_rule);
if (!result)
return;
whitespace_error++;
if (squelch_whitespace_errors &&
squelch_whitespace_errors < whitespace_error)
;
else {
err = whitespace_error_string(result);
fprintf(stderr, "%s:%d: %s.\n%.*s\n",
patch_input_file, linenr, err, len - 2, line + 1);
free(err);
}
return;
err = whitespace_error_string(result);
fprintf(stderr, "%s:%d: %s.\n%.*s\n",
patch_input_file, linenr, err, len, line);
free(err);
}
static void check_whitespace(const char *line, int len, unsigned ws_rule)
{
unsigned result = ws_check(line + 1, len - 1, ws_rule);
record_ws_error(result, line + 1, len - 2, linenr);
}
/*
@ -1281,6 +1288,7 @@ static int parse_single_patch(char *line, unsigned long size, struct patch *patc
@@ -1281,6 +1288,7 @@ static int parse_single_patch(char *line, unsigned long size, struct patch *patc
int len;
fragment = xcalloc(1, sizeof(*fragment));
fragment->linenr = linenr;
len = parse_fragment(line, size, patch, fragment);
if (len <= 0)
die("corrupt patch at line %d", linenr);
@ -2005,6 +2013,7 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
@@ -2005,6 +2013,7 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
int len = linelen(patch, size);
int plen, added;
int added_blank_line = 0;
int is_blank_context = 0;
if (!len)
break;
@ -2037,8 +2046,12 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
@@ -2037,8 +2046,12 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
*new++ = '\n';
add_line_info(&preimage, "\n", 1, LINE_COMMON);
add_line_info(&postimage, "\n", 1, LINE_COMMON);
is_blank_context = 1;
break;
case ' ':
if (plen && (ws_rule & WS_BLANK_AT_EOF) &&
ws_blank_line(patch + 1, plen, ws_rule))
is_blank_context = 1;
case '-':
memcpy(old, patch + 1, plen);
add_line_info(&preimage, old, plen,
@ -2065,7 +2078,8 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
@@ -2065,7 +2078,8 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
(first == '+' ? 0 : LINE_COMMON));
new += added;
if (first == '+' &&
added == 1 && new[-1] == '\n')
(ws_rule & WS_BLANK_AT_EOF) &&
ws_blank_line(patch + 1, plen, ws_rule))
added_blank_line = 1;
break;
case '@': case '\\':
@ -2078,6 +2092,8 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
@@ -2078,6 +2092,8 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
}
if (added_blank_line)
new_blank_lines_at_end++;
else if (is_blank_context)
;
else
new_blank_lines_at_end = 0;
patch += len;
@ -2159,17 +2175,24 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
@@ -2159,17 +2175,24 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
@ -190,4 +190,13 @@ test_expect_success 'do not color trailing cr in context' '
@@ -190,4 +190,13 @@ test_expect_success 'do not color trailing cr in context' '
'
test_expect_success 'color new trailing blank lines' '
@ -170,4 +170,95 @@ test_expect_success 'trailing whitespace & no newline at the end of file' '
@@ -170,4 +170,95 @@ test_expect_success 'trailing whitespace & no newline at the end of file' '
grep "^$" target
'
test_expect_success 'blank at EOF with --whitespace=fix (1)' '
: these can fail depending on what we did before
git config --unset core.whitespace
rm -f .gitattributes
{ echo a; echo b; echo c; } >one &&
git add one &&
{ echo a; echo b; echo c; } >expect &&
{ cat expect; echo; } >one &&
git diff -- one >patch &&
git checkout one &&
git apply --whitespace=fix patch &&
test_cmp expect one
'
test_expect_success 'blank at EOF with --whitespace=fix (2)' '
{ echo a; echo b; echo c; } >one &&
git add one &&
{ echo a; echo c; } >expect &&
{ cat expect; echo; echo; } >one &&
git diff -- one >patch &&
git checkout one &&
git apply --whitespace=fix patch &&
test_cmp expect one
'
test_expect_success 'blank at EOF with --whitespace=fix (3)' '
{ echo a; echo b; echo; } >one &&
git add one &&
{ echo a; echo c; echo; } >expect &&
{ cat expect; echo; echo; } >one &&
git diff -- one >patch &&
git checkout one &&
git apply --whitespace=fix patch &&
test_cmp expect one
'
test_expect_success 'blank at end of hunk, not at EOF with --whitespace=fix' '