builtin-apply: rename "whitespace" variables and fix styles
The variables were somewhat misnamed.
* "What to do when whitespace errors are detected" is now called
"ws_error_action" (used to be called "new_whitespace");
* The constants to denote the possible actions are "nowarn_ws_error",
"warn_on_ws_error", "die_on_ws_error", and "correct_ws_error". The
last one used to be "strip_whitespace", but we correct whitespace
error in indent (SP followed by HT) and "strip" is not quite an
accurate name for it.
Other than the renaming of variables and constants, there is no
functional change in this patch. While we are at it, it also fixes
overly long lines and multi-line comment styles (which of course do
not affect the generated code at all).
Signed-off-by: Junio C Hamano <gitster@pobox.com>
/* This is to extract the same name that appears on "diff --git"
/*
* This is to extract the same name that appears on "diff --git"
* line. We do not find and return anything if it is a rename
* patch, and it is OK because we will find the name elsewhere.
* We need to reliably find name only when it is mode-change only,
@ -584,7 +590,8 @@ static char *git_header_name(char *line, int llen)
@@ -584,7 +590,8 @@ static char *git_header_name(char *line, int llen)
goto free_and_fail1;
strbuf_remove(&first, 0, cp + 1 - first.buf);
/* second points at one past closing dq of name.
/*
* second points at one past closing dq of name.
* find the second name.
*/
while ((second < line + llen) && isspace(*second))
@ -627,7 +634,8 @@ static char *git_header_name(char *line, int llen)
@@ -627,7 +634,8 @@ static char *git_header_name(char *line, int llen)
return NULL;
name++;
/* since the first name is unquoted, a dq if exists must be
/*
* since the first name is unquoted, a dq if exists must be
* the beginning of the second name.
*/
for (second = name; second < line + llen; second++) {
@ -759,7 +767,7 @@ static int parse_num(const char *line, unsigned long *p)
@@ -759,7 +767,7 @@ static int parse_num(const char *line, unsigned long *p)
}
static int parse_range(const char *line, int len, int offset, const char *expect,
unsigned long *p1, unsigned long *p2)
unsigned long *p1, unsigned long *p2)
{
int digits, ex;
@ -868,14 +876,14 @@ static int find_header(char *line, unsigned long size, int *hdrsize, struct patc
@@ -868,14 +876,14 @@ static int find_header(char *line, unsigned long size, int *hdrsize, struct patc
return offset;
}
/** --- followed by +++ ? */
/* --- followed by +++ ? */
if (memcmp("--- ", line, 4) || memcmp("+++ ", line + len, 4))
continue;
/*
* We only accept unified patches, so we want it to
* at least have "@@ -a,b +c,d @@\n", which is 14 chars
* minimum
* minimum ("@@ -0,0 +1 @@\n" is the shortest).
*/
nextlen = linelen(line + len, size - len);
if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4))
* Parse a unified diff. Note that this really needs to parse each
* fragment separately, since the only way to know the difference
* between a "---" that is part of a patch, and a "---" that starts
* the next patch is to look at the line counts..
*/
static int parse_fragment(char *line, unsigned long size, struct patch *patch, struct fragment *fragment)
static int parse_fragment(char *line, unsigned long size,
struct patch *patch, struct fragment *fragment)
{
int added, deleted;
int len = linelen(line, size), offset;
@ -980,7 +988,7 @@ static int parse_fragment(char *line, unsigned long size, struct patch *patch, s
@@ -980,7 +988,7 @@ static int parse_fragment(char *line, unsigned long size, struct patch *patch, s
break;
case '-':
if (apply_in_reverse &&
new_whitespace != nowarn_whitespace)
ws_error_action != nowarn_ws_error)
check_whitespace(line, len);
deleted++;
oldlines--;
@ -988,14 +996,15 @@ static int parse_fragment(char *line, unsigned long size, struct patch *patch, s
@@ -988,14 +996,15 @@ static int parse_fragment(char *line, unsigned long size, struct patch *patch, s
break;
case '+':
if (!apply_in_reverse &&
new_whitespace != nowarn_whitespace)
ws_error_action != nowarn_ws_error)
check_whitespace(line, len);
added++;
newlines--;
trailing = 0;
break;
/* We allow "\ No newline at end of file". Depending
/*
* We allow "\ No newline at end of file". Depending
* on locale settings when the patch was produced we
* don't know what this line looks like. The only
* thing we do know is that it begins with "\ ".
@ -1013,7 +1022,8 @@ static int parse_fragment(char *line, unsigned long size, struct patch *patch, s
@@ -1013,7 +1022,8 @@ static int parse_fragment(char *line, unsigned long size, struct patch *patch, s
fragment->leading = leading;
fragment->trailing = trailing;
/* If a fragment ends with an incomplete line, we failed to include
/*
* If a fragment ends with an incomplete line, we failed to include
* it in the above loop because we hit oldlines == newlines == 0
* before seeing it.
*/
@ -1141,7 +1151,8 @@ static struct fragment *parse_binary_hunk(char **buf_p,
@@ -1141,7 +1151,8 @@ static struct fragment *parse_binary_hunk(char **buf_p,
int *status_p,
int *used_p)
{
/* Expect a line that begins with binary patch method ("literal"
/*
* Expect a line that begins with binary patch method ("literal"
* or "delta"), followed by the length of data before deflating.
* a sequence of 'length-byte' followed by base-85 encoded data
* should follow, terminated by a newline.
@ -1190,7 +1201,8 @@ static struct fragment *parse_binary_hunk(char **buf_p,
@@ -1190,7 +1201,8 @@ static struct fragment *parse_binary_hunk(char **buf_p,
size--;
break;
}
/* Minimum line is "A00000\n" which is 7-byte long,
/*
* Minimum line is "A00000\n" which is 7-byte long,
* and the line length must be multiple of 5 plus 2.
*/
if ((llen < 7) || (llen-2) % 5)
@ -1241,7 +1253,8 @@ static struct fragment *parse_binary_hunk(char **buf_p,
@@ -1241,7 +1253,8 @@ static struct fragment *parse_binary_hunk(char **buf_p,
static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
{
/* We have read "GIT binary patch\n"; what follows is a line
/*
* We have read "GIT binary patch\n"; what follows is a line
* that says the patch method (currently, either "literal" or
* "delta") and the length of data before deflating; a
* sequence of 'length-byte' followed by base-85 encoded data
@ -1271,7 +1284,8 @@ static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
@@ -1271,7 +1284,8 @@ static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
if (reverse)
used += used_1;
else if (status) {
/* not having reverse hunk is not an error, but having
/*
* Not having reverse hunk is not an error, but having
* a corrupt reverse hunk is.
*/
free((void*) forward->patch);
@ -1292,7 +1306,8 @@ static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
@@ -1292,7 +1306,8 @@ static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
static int apply_line(char *output, const char *patch, int plen)
{
/* plen is number of bytes to be copied from patch,
/*
* plen is number of bytes to be copied from patch,
* starting at patch+1 (patch[0] is '+'). Typically
* patch[plen] is '\n', unless this is the incomplete
* last line.
@ -1552,12 +1572,15 @@ static int apply_line(char *output, const char *patch, int plen)
@@ -1552,12 +1572,15 @@ static int apply_line(char *output, const char *patch, int plen)
int need_fix_leading_space = 0;
char *buf;
if ((new_whitespace != strip_whitespace) || !whitespace_error ||
if ((ws_error_action != correct_ws_error) || !whitespace_error ||
*patch != '+') {
memcpy(output, patch + 1, plen);
return plen;
}
/*
* Strip trailing whitespace
*/
if (1 < plen && isspace(patch[plen-1])) {
if (patch[plen] == '\n')
add_nl_to_tail = 1;
@ -1567,6 +1590,9 @@ static int apply_line(char *output, const char *patch, int plen)
@@ -1567,6 +1590,9 @@ static int apply_line(char *output, const char *patch, int plen)
fixed = 1;
}
/*
* Check leading whitespaces (indent)
*/
for (i = 1; i < plen; i++) {
char ch = patch[i];
if (ch == '\t') {
@ -1583,7 +1609,8 @@ static int apply_line(char *output, const char *patch, int plen)
@@ -1583,7 +1609,8 @@ static int apply_line(char *output, const char *patch, int plen)
buf = output;
if (need_fix_leading_space) {
int consecutive_spaces = 0;
/* between patch[1..last_tab_in_indent] strip the
/*
* between patch[1..last_tab_in_indent] strip the
* funny spaces, updating them to tab as needed.
*/
for (i = 1; i < last_tab_in_indent; i++, plen--) {
@ -1613,7 +1640,8 @@ static int apply_line(char *output, const char *patch, int plen)
@@ -1613,7 +1640,8 @@ static int apply_line(char *output, const char *patch, int plen)
return output + plen - buf;
}
static int apply_one_fragment(struct strbuf *buf, struct fragment *frag, int inaccurate_eof)
static int apply_one_fragment(struct strbuf *buf, struct fragment *frag,
int inaccurate_eof)
{
int match_beginning, match_end;
const char *patch = frag->patch;
@ -1695,8 +1723,9 @@ static int apply_one_fragment(struct strbuf *buf, struct fragment *frag, int ina
@@ -1695,8 +1723,9 @@ static int apply_one_fragment(struct strbuf *buf, struct fragment *frag, int ina
@ -1733,7 +1762,7 @@ static int apply_one_fragment(struct strbuf *buf, struct fragment *frag, int ina
@@ -1733,7 +1762,7 @@ static int apply_one_fragment(struct strbuf *buf, struct fragment *frag, int ina
if (match_beginning && offset)
offset = -1;
if (offset >= 0) {
if (new_whitespace == strip_whitespace &&
if (ws_error_action == correct_ws_error &&
(buf->len - oldsize - offset == 0)) /* end of file? */
newsize -= new_blank_lines_at_end;
@ -1758,9 +1787,10 @@ static int apply_one_fragment(struct strbuf *buf, struct fragment *frag, int ina
@@ -1758,9 +1787,10 @@ static int apply_one_fragment(struct strbuf *buf, struct fragment *frag, int ina
match_beginning = match_end = 0;
continue;
}
/* Reduce the number of context lines
* Reduce both leading and trailing if they are equal
* otherwise just reduce the larger context.
/*
* Reduce the number of context lines; reduce both
* leading and trailing if they are equal otherwise