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
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
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
/*
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;
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 &&
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);
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;
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,
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))
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++) {
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
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))
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;
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--;
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 "\ ".
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
*/
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.
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)
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
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);
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[] = {
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
{
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;
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.
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;
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') {
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--) {
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;
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
}
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
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);
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 ||
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);
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
*/
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
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;
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",