@matheusbsbs / matheus-bagliano / commits / 81bf96bb2e
Fork 0

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>

Junio C Hamano committed Nov 23, 2007 at 02:37 UTC 81bf96bb2ecb8c869cc365a7fee2c4d2e937a43c
1 file changed +91 -57
builtin-apply.c
+91 -57
@@ -47,12 +47,12 @@ static unsigned long p_context = ULONG_MAX;
47 static const char apply_usage[] =
48 "git-apply [--stat] [--numstat] [--summary] [--check] [--index] [--cached] [--apply] [--no-add] [--index-info] [--allow-binary-replacement] [--reverse] [--reject] [--verbose] [-z] [-pNUM] [-CNUM] [--whitespace=<nowarn|warn|error|error-all|strip>] <patch>...";
49
50 -static enum whitespace_eol {
51 - nowarn_whitespace,
52 - warn_on_whitespace,
53 - error_on_whitespace,
54 - strip_whitespace,
55 -} new_whitespace = warn_on_whitespace;
50 +static enum ws_error_action {
51 + nowarn_ws_error,
52 + warn_on_ws_error,
53 + die_on_ws_error,
54 + correct_ws_error,
55 +} ws_error_action = warn_on_ws_error;
56 static int whitespace_error;
57 static int squelch_whitespace_errors = 5;
58 static int applied_after_fixing_ws;
@@ -61,28 +61,28 @@ static const char *patch_input_file;
61 static void parse_whitespace_option(const char *option)
62 {
63 if (!option) {
64 - new_whitespace = warn_on_whitespace;
64 + ws_error_action = warn_on_ws_error;
65 return;
66 }
67 if (!strcmp(option, "warn")) {
68 - new_whitespace = warn_on_whitespace;
68 + ws_error_action = warn_on_ws_error;
69 return;
70 }
71 if (!strcmp(option, "nowarn")) {
72 - new_whitespace = nowarn_whitespace;
72 + ws_error_action = nowarn_ws_error;
73 return;
74 }
75 if (!strcmp(option, "error")) {
76 - new_whitespace = error_on_whitespace;
76 + ws_error_action = die_on_ws_error;
77 return;
78 }
79 if (!strcmp(option, "error-all")) {
80 - new_whitespace = error_on_whitespace;
80 + ws_error_action = die_on_ws_error;
81 squelch_whitespace_errors = 0;
82 return;
83 }
84 - if (!strcmp(option, "strip")) {
85 - new_whitespace = strip_whitespace;
84 + if (!strcmp(option, "strip") || !strcmp(option, "fix")) {
85 + ws_error_action = correct_ws_error;
86 return;
87 }
88 die("unrecognized whitespace option '%s'", option);
@@ -90,11 +90,8 @@ static void parse_whitespace_option(const char *option)
90
91 static void set_default_whitespace_mode(const char *whitespace_option)
92 {
93 - if (!whitespace_option && !apply_default_whitespace) {
94 - new_whitespace = (apply
95 - ? warn_on_whitespace
96 - : nowarn_whitespace);
97 - }
93 + if (!whitespace_option && !apply_default_whitespace)
94 + ws_error_action = (apply ? warn_on_ws_error : nowarn_ws_error);
95 }
96
97 /*
@@ -137,6 +134,11 @@ struct fragment {
134 #define BINARY_DELTA_DEFLATED 1
135 #define BINARY_LITERAL_DEFLATED 2
136
137 +/*
138 + * This represents a "patch" to a file, both metainfo changes
139 + * such as creation/deletion, filemode and content changes represented
140 + * as a series of fragments.
141 + */
142 struct patch {
143 char *new_name, *old_name, *def_name;
144 unsigned int old_mode, new_mode;
@@ -158,7 +160,8 @@ struct patch {
160 struct patch *next;
161 };
162
161 -static void say_patch_name(FILE *output, const char *pre, struct patch *patch, const char *post)
163 +static void say_patch_name(FILE *output, const char *pre,
164 + struct patch *patch, const char *post)
165 {
166 fputs(pre, output);
167 if (patch->old_name && patch->new_name &&
@@ -229,7 +232,8 @@ static char *find_name(const char *line, char *def, int p_value, int terminate)
232 if (*line == '"') {
233 struct strbuf name;
234
232 - /* Proposed "new-style" GNU patch/diff format; see
235 + /*
236 + * Proposed "new-style" GNU patch/diff format; see
237 * http://marc.theaimsgroup.com/?l=git&m=112927316408690&w=2
238 */
239 strbuf_init(&name, 0);
@@ -499,7 +503,8 @@ static int gitdiff_dissimilarity(const char *line, struct patch *patch)
503
504 static int gitdiff_index(const char *line, struct patch *patch)
505 {
502 - /* index line is N hexadecimal, "..", N hexadecimal,
506 + /*
507 + * index line is N hexadecimal, "..", N hexadecimal,
508 * and optional space with octal mode.
509 */
510 const char *ptr, *eol;
@@ -550,7 +555,8 @@ static const char *stop_at_slash(const char *line, int llen)
555 return NULL;
556 }
557
553 -/* This is to extract the same name that appears on "diff --git"
558 +/*
559 + * This is to extract the same name that appears on "diff --git"
560 * line. We do not find and return anything if it is a rename
561 * patch, and it is OK because we will find the name elsewhere.
562 * 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)
590 goto free_and_fail1;
591 strbuf_remove(&first, 0, cp + 1 - first.buf);
592
587 - /* second points at one past closing dq of name.
593 + /*
594 + * second points at one past closing dq of name.
595 * find the second name.
596 */
597 while ((second < line + llen) && isspace(*second))
@@ -627,7 +634,8 @@ static char *git_header_name(char *line, int llen)
634 return NULL;
635 name++;
636
630 - /* since the first name is unquoted, a dq if exists must be
637 + /*
638 + * since the first name is unquoted, a dq if exists must be
639 * the beginning of the second name.
640 */
641 for (second = name; second < line + llen; second++) {
@@ -759,7 +767,7 @@ static int parse_num(const char *line, unsigned long *p)
767 }
768
769 static int parse_range(const char *line, int len, int offset, const char *expect,
762 - unsigned long *p1, unsigned long *p2)
770 + unsigned long *p1, unsigned long *p2)
771 {
772 int digits, ex;
773
@@ -868,14 +876,14 @@ static int find_header(char *line, unsigned long size, int *hdrsize, struct patc
876 return offset;
877 }
878
871 - /** --- followed by +++ ? */
879 + /* --- followed by +++ ? */
880 if (memcmp("--- ", line, 4) || memcmp("+++ ", line + len, 4))
881 continue;
882
883 /*
884 * We only accept unified patches, so we want it to
885 * at least have "@@ -a,b +c,d @@\n", which is 14 chars
878 - * minimum
886 + * minimum ("@@ -0,0 +1 @@\n" is the shortest).
887 */
888 nextlen = linelen(line + len, size - len);
889 if (size < nextlen + 14 || memcmp("@@ -", line + len + nextlen, 4))
@@ -932,14 +940,14 @@ static void check_whitespace(const char *line, int len)
940 err, patch_input_file, linenr, len-2, line+1);
941 }
942
935 -
943 /*
944 * Parse a unified diff. Note that this really needs to parse each
945 * fragment separately, since the only way to know the difference
946 * between a "---" that is part of a patch, and a "---" that starts
947 * the next patch is to look at the line counts..
948 */
942 -static int parse_fragment(char *line, unsigned long size, struct patch *patch, struct fragment *fragment)
949 +static int parse_fragment(char *line, unsigned long size,
950 + struct patch *patch, struct fragment *fragment)
951 {
952 int added, deleted;
953 int len = linelen(line, size), offset;
@@ -980,7 +988,7 @@ static int parse_fragment(char *line, unsigned long size, struct patch *patch, s
988 break;
989 case '-':
990 if (apply_in_reverse &&
983 - new_whitespace != nowarn_whitespace)
991 + ws_error_action != nowarn_ws_error)
992 check_whitespace(line, len);
993 deleted++;
994 oldlines--;
@@ -988,14 +996,15 @@ static int parse_fragment(char *line, unsigned long size, struct patch *patch, s
996 break;
997 case '+':
998 if (!apply_in_reverse &&
991 - new_whitespace != nowarn_whitespace)
999 + ws_error_action != nowarn_ws_error)
1000 check_whitespace(line, len);
1001 added++;
1002 newlines--;
1003 trailing = 0;
1004 break;
1005
998 - /* We allow "\ No newline at end of file". Depending
1006 + /*
1007 + * We allow "\ No newline at end of file". Depending
1008 * on locale settings when the patch was produced we
1009 * don't know what this line looks like. The only
1010 * 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
1022 fragment->leading = leading;
1023 fragment->trailing = trailing;
1024
1016 - /* If a fragment ends with an incomplete line, we failed to include
1025 + /*
1026 + * If a fragment ends with an incomplete line, we failed to include
1027 * it in the above loop because we hit oldlines == newlines == 0
1028 * before seeing it.
1029 */
@@ -1141,7 +1151,8 @@ static struct fragment *parse_binary_hunk(char **buf_p,
1151 int *status_p,
1152 int *used_p)
1153 {
1144 - /* Expect a line that begins with binary patch method ("literal"
1154 + /*
1155 + * Expect a line that begins with binary patch method ("literal"
1156 * or "delta"), followed by the length of data before deflating.
1157 * a sequence of 'length-byte' followed by base-85 encoded data
1158 * should follow, terminated by a newline.
@@ -1190,7 +1201,8 @@ static struct fragment *parse_binary_hunk(char **buf_p,
1201 size--;
1202 break;
1203 }
1193 - /* Minimum line is "A00000\n" which is 7-byte long,
1204 + /*
1205 + * Minimum line is "A00000\n" which is 7-byte long,
1206 * and the line length must be multiple of 5 plus 2.
1207 */
1208 if ((llen < 7) || (llen-2) % 5)
@@ -1241,7 +1253,8 @@ static struct fragment *parse_binary_hunk(char **buf_p,
1253
1254 static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
1255 {
1244 - /* We have read "GIT binary patch\n"; what follows is a line
1256 + /*
1257 + * We have read "GIT binary patch\n"; what follows is a line
1258 * that says the patch method (currently, either "literal" or
1259 * "delta") and the length of data before deflating; a
1260 * 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)
1284 if (reverse)
1285 used += used_1;
1286 else if (status) {
1274 - /* not having reverse hunk is not an error, but having
1287 + /*
1288 + * Not having reverse hunk is not an error, but having
1289 * a corrupt reverse hunk is.
1290 */
1291 free((void*) forward->patch);
@@ -1292,7 +1306,8 @@ static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
1306 if (offset < 0)
1307 return offset;
1308
1295 - patchsize = parse_single_patch(buffer + offset + hdrsize, size - offset - hdrsize, patch);
1309 + patchsize = parse_single_patch(buffer + offset + hdrsize,
1310 + size - offset - hdrsize, patch);
1311
1312 if (!patchsize) {
1313 static const char *binhdr[] = {
@@ -1368,8 +1383,10 @@ static void reverse_patches(struct patch *p)
1383 }
1384 }
1385
1371 -static const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
1372 -static const char minuses[]= "----------------------------------------------------------------------";
1386 +static const char pluses[] =
1387 +"++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
1388 +static const char minuses[]=
1389 +"----------------------------------------------------------------------";
1390
1391 static void show_stats(struct patch *patch)
1392 {
@@ -1438,7 +1455,9 @@ static int read_old_data(struct stat *st, const char *path, struct strbuf *buf)
1455 }
1456 }
1457
1441 -static int find_offset(const char *buf, unsigned long size, const char *fragment, unsigned long fragsize, int line, int *lines)
1458 +static int find_offset(const char *buf, unsigned long size,
1459 + const char *fragment, unsigned long fragsize,
1460 + int line, int *lines)
1461 {
1462 int i;
1463 unsigned long start, backwards, forwards;
@@ -1539,7 +1558,8 @@ static void remove_last_line(const char **rbuf, int *rsize)
1558
1559 static int apply_line(char *output, const char *patch, int plen)
1560 {
1542 - /* plen is number of bytes to be copied from patch,
1561 + /*
1562 + * plen is number of bytes to be copied from patch,
1563 * starting at patch+1 (patch[0] is '+'). Typically
1564 * patch[plen] is '\n', unless this is the incomplete
1565 * last line.
@@ -1552,12 +1572,15 @@ static int apply_line(char *output, const char *patch, int plen)
1572 int need_fix_leading_space = 0;
1573 char *buf;
1574
1555 - if ((new_whitespace != strip_whitespace) || !whitespace_error ||
1575 + if ((ws_error_action != correct_ws_error) || !whitespace_error ||
1576 *patch != '+') {
1577 memcpy(output, patch + 1, plen);
1578 return plen;
1579 }
1580
1581 + /*
1582 + * Strip trailing whitespace
1583 + */
1584 if (1 < plen && isspace(patch[plen-1])) {
1585 if (patch[plen] == '\n')
1586 add_nl_to_tail = 1;
@@ -1567,6 +1590,9 @@ static int apply_line(char *output, const char *patch, int plen)
1590 fixed = 1;
1591 }
1592
1593 + /*
1594 + * Check leading whitespaces (indent)
1595 + */
1596 for (i = 1; i < plen; i++) {
1597 char ch = patch[i];
1598 if (ch == '\t') {
@@ -1583,7 +1609,8 @@ static int apply_line(char *output, const char *patch, int plen)
1609 buf = output;
1610 if (need_fix_leading_space) {
1611 int consecutive_spaces = 0;
1586 - /* between patch[1..last_tab_in_indent] strip the
1612 + /*
1613 + * between patch[1..last_tab_in_indent] strip the
1614 * funny spaces, updating them to tab as needed.
1615 */
1616 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)
1640 return output + plen - buf;
1641 }
1642
1616 -static int apply_one_fragment(struct strbuf *buf, struct fragment *frag, int inaccurate_eof)
1643 +static int apply_one_fragment(struct strbuf *buf, struct fragment *frag,
1644 + int inaccurate_eof)
1645 {
1646 int match_beginning, match_end;
1647 const char *patch = frag->patch;
@@ -1695,8 +1723,9 @@ static int apply_one_fragment(struct strbuf *buf, struct fragment *frag, int ina
1723 size -= len;
1724 }
1725
1698 - if (inaccurate_eof && oldsize > 0 && old[oldsize - 1] == '\n' &&
1699 - newsize > 0 && new[newsize - 1] == '\n') {
1726 + if (inaccurate_eof &&
1727 + oldsize > 0 && old[oldsize - 1] == '\n' &&
1728 + newsize > 0 && new[newsize - 1] == '\n') {
1729 oldsize--;
1730 newsize--;
1731 }
@@ -1733,7 +1762,7 @@ static int apply_one_fragment(struct strbuf *buf, struct fragment *frag, int ina
1762 if (match_beginning && offset)
1763 offset = -1;
1764 if (offset >= 0) {
1736 - if (new_whitespace == strip_whitespace &&
1765 + if (ws_error_action == correct_ws_error &&
1766 (buf->len - oldsize - offset == 0)) /* end of file? */
1767 newsize -= new_blank_lines_at_end;
1768
@@ -1758,9 +1787,10 @@ static int apply_one_fragment(struct strbuf *buf, struct fragment *frag, int ina
1787 match_beginning = match_end = 0;
1788 continue;
1789 }
1761 - /* Reduce the number of context lines
1762 - * Reduce both leading and trailing if they are equal
1763 - * otherwise just reduce the larger context.
1790 + /*
1791 + * Reduce the number of context lines; reduce both
1792 + * leading and trailing if they are equal otherwise
1793 + * just reduce the larger context.
1794 */
1795 if (leading >= trailing) {
1796 remove_first_line(&oldlines, &oldsize);
@@ -1820,7 +1850,8 @@ static int apply_binary(struct strbuf *buf, struct patch *patch)
1850 const char *name = patch->old_name ? patch->old_name : patch->new_name;
1851 unsigned char sha1[20];
1852
1823 - /* For safety, we require patch index line to contain
1853 + /*
1854 + * For safety, we require patch index line to contain
1855 * full 40-byte textual SHA1 for old and new, at least for now.
1856 */
1857 if (strlen(patch->old_sha1_prefix) != 40 ||
@@ -1831,7 +1862,8 @@ static int apply_binary(struct strbuf *buf, struct patch *patch)
1862 "without full index line", name);
1863
1864 if (patch->old_name) {
1834 - /* See if the old one matches what the patch
1865 + /*
1866 + * See if the old one matches what the patch
1867 * applies to.
1868 */
1869 hash_sha1_file(buf->buf, buf->len, blob_type, sha1);
@@ -1868,7 +1900,8 @@ static int apply_binary(struct strbuf *buf, struct patch *patch)
1900 /* XXX read_sha1_file NUL-terminates */
1901 strbuf_attach(buf, result, size, size + 1);
1902 } else {
1871 - /* We have verified buf matches the preimage;
1903 + /*
1904 + * We have verified buf matches the preimage;
1905 * apply the patch data to it, which is stored
1906 * in the patch->fragments->{patch,size}.
1907 */
@@ -2067,7 +2100,8 @@ static int check_patch(struct patch *patch, struct patch *prev_patch)
2100
2101 if (new_name && prev_patch && 0 < prev_patch->is_delete &&
2102 !strcmp(prev_patch->old_name, new_name))
2070 - /* A type-change diff is always split into a patch to
2103 + /*
2104 + * A type-change diff is always split into a patch to
2105 * delete old, immediately followed by a patch to
2106 * create new (see diff.c::run_diff()); in such a case
2107 * it is Ok that the entry to be deleted by the
@@ -2671,7 +2705,7 @@ static int apply_patch(int fd, const char *filename, int inaccurate_eof)
2705 offset += nr;
2706 }
2707
2674 - if (whitespace_error && (new_whitespace == error_on_whitespace))
2708 + if (whitespace_error && (ws_error_action == die_on_ws_error))
2709 apply = 0;
2710
2711 update_index = check_index && apply;
@@ -2866,7 +2900,7 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
2900 squelched,
2901 squelched == 1 ? "" : "s");
2902 }
2869 - if (new_whitespace == error_on_whitespace)
2903 + if (ws_error_action == die_on_ws_error)
2904 die("%d line%s add%s whitespace errors.",
2905 whitespace_error,
2906 whitespace_error == 1 ? "" : "s",