Skip to content

Commit

Permalink
Merge branch 'jc/maint-1.6.0-blank-at-eof' (early part) into jc/maint…
Browse files Browse the repository at this point in the history
…-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
  • Loading branch information
gitster committed Sep 15, 2009
2 parents 4197ce3 + d68fe26 commit afd9db4
Show file tree
Hide file tree
Showing 8 changed files with 288 additions and 70 deletions.
6 changes: 5 additions & 1 deletion Documentation/config.txt
Expand Up @@ -401,13 +401,17 @@ core.whitespace::
consider them as errors. You can prefix `-` to disable
any of them (e.g. `-trailing-space`):
+
* `trailing-space` treats trailing whitespaces at the end of the line
* `blank-at-eol` treats trailing whitespaces at the end of the line
as an error (enabled by default).
* `space-before-tab` treats a space character that appears immediately
before a tab character in the initial indent part of the line as an
error (enabled by default).
* `indent-with-non-tab` treats a line that is indented with 8 or more
space characters as an error (not enabled by default).
* `blank-at-eof` treats blank lines added at the end of file as an error
(enabled by default).
* `trailing-space` is a short-hand to cover both `blank-at-eol` and
`blank-at-eof`.
* `cr-at-eol` treats a carriage-return at the end of line as
part of the line terminator, i.e. with it, `trailing-space`
does not trigger if the character before such a carriage-return
Expand Down
61 changes: 42 additions & 19 deletions builtin-apply.c
Expand Up @@ -131,6 +131,7 @@ struct fragment {
const char *patch;
int size;
int rejected;
int linenr;
struct fragment *next;
};

Expand Down Expand Up @@ -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);
}

/*
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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 '\\':
Expand All @@ -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;
Expand Down Expand Up @@ -2159,17 +2175,24 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
}

if (applied_pos >= 0) {
if (ws_error_action == correct_ws_error &&
new_blank_lines_at_end &&
postimage.nr + applied_pos == img->nr) {
if (new_blank_lines_at_end &&
preimage.nr + applied_pos == img->nr &&
(ws_rule & WS_BLANK_AT_EOF) &&
ws_error_action != nowarn_ws_error) {
record_ws_error(WS_BLANK_AT_EOF, "+", 1, frag->linenr);
if (ws_error_action == correct_ws_error) {
while (new_blank_lines_at_end--)
remove_last_line(&postimage);
}
/*
* If the patch application adds blank lines
* at the end, and if the patch applies at the
* end of the image, remove those added blank
* lines.
* We would want to prevent write_out_results()
* from taking place in apply_patch() that follows
* the callchain led us here, which is:
* apply_patch->check_patch_list->check_patch->
* apply_data->apply_fragments->apply_one_fragment
*/
while (new_blank_lines_at_end--)
remove_last_line(&postimage);
if (ws_error_action == die_on_ws_error)
apply = 0;
}

/*
Expand Down
4 changes: 3 additions & 1 deletion cache.h
Expand Up @@ -967,10 +967,12 @@ void shift_tree(const unsigned char *, const unsigned char *, unsigned char *, i
* whitespace rules.
* used by both diff and apply
*/
#define WS_TRAILING_SPACE 01
#define WS_BLANK_AT_EOL 01
#define WS_SPACE_BEFORE_TAB 02
#define WS_INDENT_WITH_NON_TAB 04
#define WS_CR_AT_EOL 010
#define WS_BLANK_AT_EOF 020
#define WS_TRAILING_SPACE (WS_BLANK_AT_EOL|WS_BLANK_AT_EOF)
#define WS_DEFAULT_RULE (WS_TRAILING_SPACE|WS_SPACE_BEFORE_TAB)
extern unsigned whitespace_rule_cfg;
extern unsigned whitespace_rule(const char *);
Expand Down

0 comments on commit afd9db4

Please sign in to comment.