@matheusbsbs / matheus-bagliano / commits / e5a94313c0
Fork 0

Teach git-apply about '-R'

Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>

Johannes Schindelin committed Jul 28, 2006 at 17:46 UTC e5a94313c06b83e034179ca6092ea23343e48dec
2 files changed +77 -12
builtin-apply.c
+55 -10
@@ -120,7 +120,7 @@ struct fragment {
120 struct patch {
121 char *new_name, *old_name, *def_name;
122 unsigned int old_mode, new_mode;
123 - int is_rename, is_copy, is_new, is_delete, is_binary;
123 + int is_rename, is_copy, is_new, is_delete, is_binary, is_reverse;
124 #define BINARY_DELTA_DEFLATED 1
125 #define BINARY_LITERAL_DEFLATED 2
126 unsigned long deflate_origlen;
@@ -1119,6 +1119,34 @@ static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
1119 return offset + hdrsize + patchsize;
1120 }
1121
1122 +#define swap(a,b) myswap((a),(b),sizeof(a))
1123 +
1124 +#define myswap(a, b, size) do { \
1125 + unsigned char mytmp[size]; \
1126 + memcpy(mytmp, &a, size); \
1127 + memcpy(&a, &b, size); \
1128 + memcpy(&b, mytmp, size); \
1129 +} while (0)
1130 +
1131 +static void reverse_patches(struct patch *p)
1132 +{
1133 + for (; p; p = p->next) {
1134 + struct fragment *frag = p->fragments;
1135 +
1136 + swap(p->new_name, p->old_name);
1137 + swap(p->new_mode, p->old_mode);
1138 + swap(p->is_new, p->is_delete);
1139 + swap(p->lines_added, p->lines_deleted);
1140 + swap(p->old_sha1_prefix, p->new_sha1_prefix);
1141 +
1142 + for (; frag; frag = frag->next) {
1143 + swap(frag->newpos, frag->oldpos);
1144 + swap(frag->newlines, frag->oldlines);
1145 + }
1146 + p->is_reverse = !p->is_reverse;
1147 + }
1148 +}
1149 +
1150 static const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
1151 static const char minuses[]= "----------------------------------------------------------------------";
1152
@@ -1336,7 +1364,7 @@ static int apply_line(char *output, const char *patch, int plen)
1364 }
1365
1366 static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag,
1339 - int inaccurate_eof)
1367 + int reverse, int inaccurate_eof)
1368 {
1369 int match_beginning, match_end;
1370 char *buf = desc->buffer;
@@ -1350,6 +1378,7 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag,
1378 int pos, lines;
1379
1380 while (size > 0) {
1381 + char first;
1382 int len = linelen(patch, size);
1383 int plen;
1384
@@ -1366,16 +1395,23 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag,
1395 plen = len-1;
1396 if (len < size && patch[len] == '\\')
1397 plen--;
1369 - switch (*patch) {
1398 + first = *patch;
1399 + if (reverse) {
1400 + if (first == '-')
1401 + first = '+';
1402 + else if (first == '+')
1403 + first = '-';
1404 + }
1405 + switch (first) {
1406 case ' ':
1407 case '-':
1408 memcpy(old + oldsize, patch + 1, plen);
1409 oldsize += plen;
1374 - if (*patch == '-')
1410 + if (first == '-')
1411 break;
1412 /* Fall-through for ' ' */
1413 case '+':
1378 - if (*patch != '+' || !no_add)
1414 + if (first != '+' || !no_add)
1415 newsize += apply_line(new + newsize, patch,
1416 plen);
1417 break;
@@ -1615,7 +1651,8 @@ static int apply_fragments(struct buffer_desc *desc, struct patch *patch)
1651 return apply_binary(desc, patch);
1652
1653 while (frag) {
1618 - if (apply_one_fragment(desc, frag, patch->inaccurate_eof) < 0)
1654 + if (apply_one_fragment(desc, frag, patch->is_reverse,
1655 + patch->inaccurate_eof) < 0)
1656 return error("patch failed: %s:%ld",
1657 name, frag->oldpos);
1658 frag = frag->next;
@@ -2142,7 +2179,8 @@ static int use_patch(struct patch *p)
2179 return 1;
2180 }
2181
2145 -static int apply_patch(int fd, const char *filename, int inaccurate_eof)
2182 +static int apply_patch(int fd, const char *filename,
2183 + int reverse, int inaccurate_eof)
2184 {
2185 unsigned long offset, size;
2186 char *buffer = read_patch_file(fd, &size);
@@ -2162,6 +2200,8 @@ static int apply_patch(int fd, const char *filename, int inaccurate_eof)
2200 nr = parse_chunk(buffer + offset, size, patch);
2201 if (nr < 0)
2202 break;
2203 + if (reverse)
2204 + reverse_patches(patch);
2205 if (use_patch(patch)) {
2206 patch_stats(patch);
2207 *listp = patch;
@@ -2226,6 +2266,7 @@ int cmd_apply(int argc, const char **argv, char **envp)
2266 {
2267 int i;
2268 int read_stdin = 1;
2269 + int reverse = 0;
2270 int inaccurate_eof = 0;
2271
2272 const char *whitespace_option = NULL;
@@ -2236,7 +2277,7 @@ int cmd_apply(int argc, const char **argv, char **envp)
2277 int fd;
2278
2279 if (!strcmp(arg, "-")) {
2239 - apply_patch(0, "<stdin>", inaccurate_eof);
2280 + apply_patch(0, "<stdin>", reverse, inaccurate_eof);
2281 read_stdin = 0;
2282 continue;
2283 }
@@ -2313,6 +2354,10 @@ int cmd_apply(int argc, const char **argv, char **envp)
2354 parse_whitespace_option(arg + 13);
2355 continue;
2356 }
2357 + if (!strcmp(arg, "-R") || !strcmp(arg, "--reverse")) {
2358 + reverse = 1;
2359 + continue;
2360 + }
2361 if (!strcmp(arg, "--inaccurate-eof")) {
2362 inaccurate_eof = 1;
2363 continue;
@@ -2333,12 +2378,12 @@ int cmd_apply(int argc, const char **argv, char **envp)
2378 usage(apply_usage);
2379 read_stdin = 0;
2380 set_default_whitespace_mode(whitespace_option);
2336 - apply_patch(fd, arg, inaccurate_eof);
2381 + apply_patch(fd, arg, reverse, inaccurate_eof);
2382 close(fd);
2383 }
2384 set_default_whitespace_mode(whitespace_option);
2385 if (read_stdin)
2341 - apply_patch(0, "<stdin>", inaccurate_eof);
2386 + apply_patch(0, "<stdin>", reverse, inaccurate_eof);
2387 if (whitespace_error) {
2388 if (squelch_whitespace_errors &&
2389 squelch_whitespace_errors < whitespace_error) {
t/t4102-apply-rename.sh
+22 -2
@@ -13,8 +13,8 @@ test_description='git-apply handling copy/rename patch.
13 cat >test-patch <<\EOF
14 diff --git a/foo b/bar
15 similarity index 47%
16 -copy from foo
17 -copy to bar
16 +rename from foo
17 +rename to bar
18 --- a/foo
19 +++ b/bar
20 @@ -1 +1 @@
@@ -39,4 +39,24 @@ else
39 'test -f bar && ls -l bar | grep "^-..x......"'
40 fi
41
42 +test_expect_success 'apply reverse' \
43 + 'git-apply -R --index --stat --summary --apply test-patch &&
44 + test "$(cat foo)" = "This is foo"'
45 +
46 +cat >test-patch <<\EOF
47 +diff --git a/foo b/bar
48 +similarity index 47%
49 +copy from foo
50 +copy to bar
51 +--- a/foo
52 ++++ b/bar
53 +@@ -1 +1 @@
54 +-This is foo
55 ++This is bar
56 +EOF
57 +
58 +test_expect_success 'apply copy' \
59 + 'git-apply --index --stat --summary --apply test-patch &&
60 + test "$(cat bar)" = "This is bar" -a "$(cat foo)" = "This is foo"'
61 +
62 test_done