Skip to content

Commit f686d03

Browse files
author
Junio C Hamano
committed
git-apply --reverse: simplify reverse option.
Having is_reverse in each patch did not make sense. This will hopefully simplify the work needed to introduce reversible binary diff format. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6a0ebe8 commit f686d03

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

builtin-apply.c

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ static int numstat = 0;
3737
static int summary = 0;
3838
static int check = 0;
3939
static int apply = 1;
40+
static int apply_in_reverse = 0;
4041
static int no_add = 0;
4142
static int show_index_info = 0;
4243
static int line_termination = '\n';
@@ -120,7 +121,7 @@ struct fragment {
120121
struct patch {
121122
char *new_name, *old_name, *def_name;
122123
unsigned int old_mode, new_mode;
123-
int is_rename, is_copy, is_new, is_delete, is_binary, is_reverse;
124+
int is_rename, is_copy, is_new, is_delete, is_binary;
124125
#define BINARY_DELTA_DEFLATED 1
125126
#define BINARY_LITERAL_DEFLATED 2
126127
unsigned long deflate_origlen;
@@ -1143,7 +1144,6 @@ static void reverse_patches(struct patch *p)
11431144
swap(frag->newpos, frag->oldpos);
11441145
swap(frag->newlines, frag->oldlines);
11451146
}
1146-
p->is_reverse = !p->is_reverse;
11471147
}
11481148
}
11491149

@@ -1363,8 +1363,7 @@ static int apply_line(char *output, const char *patch, int plen)
13631363
return plen;
13641364
}
13651365

1366-
static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag,
1367-
int reverse, int inaccurate_eof)
1366+
static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, int inaccurate_eof)
13681367
{
13691368
int match_beginning, match_end;
13701369
char *buf = desc->buffer;
@@ -1396,7 +1395,7 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag,
13961395
if (len < size && patch[len] == '\\')
13971396
plen--;
13981397
first = *patch;
1399-
if (reverse) {
1398+
if (apply_in_reverse) {
14001399
if (first == '-')
14011400
first = '+';
14021401
else if (first == '+')
@@ -1536,7 +1535,7 @@ static int apply_binary_fragment(struct buffer_desc *desc, struct patch *patch)
15361535
void *result;
15371536

15381537
/* Binary patch is irreversible */
1539-
if (patch->is_reverse)
1538+
if (apply_in_reverse)
15401539
return error("cannot reverse-apply a binary patch to '%s'",
15411540
patch->new_name
15421541
? patch->new_name : patch->old_name);
@@ -1657,8 +1656,7 @@ static int apply_fragments(struct buffer_desc *desc, struct patch *patch)
16571656
return apply_binary(desc, patch);
16581657

16591658
while (frag) {
1660-
if (apply_one_fragment(desc, frag, patch->is_reverse,
1661-
patch->inaccurate_eof) < 0)
1659+
if (apply_one_fragment(desc, frag, patch->inaccurate_eof) < 0)
16621660
return error("patch failed: %s:%ld",
16631661
name, frag->oldpos);
16641662
frag = frag->next;
@@ -2194,8 +2192,7 @@ static int use_patch(struct patch *p)
21942192
return 1;
21952193
}
21962194

2197-
static int apply_patch(int fd, const char *filename,
2198-
int reverse, int inaccurate_eof)
2195+
static int apply_patch(int fd, const char *filename, int inaccurate_eof)
21992196
{
22002197
unsigned long offset, size;
22012198
char *buffer = read_patch_file(fd, &size);
@@ -2215,7 +2212,7 @@ static int apply_patch(int fd, const char *filename,
22152212
nr = parse_chunk(buffer + offset, size, patch);
22162213
if (nr < 0)
22172214
break;
2218-
if (reverse)
2215+
if (apply_in_reverse)
22192216
reverse_patches(patch);
22202217
if (use_patch(patch)) {
22212218
patch_stats(patch);
@@ -2278,7 +2275,6 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
22782275
{
22792276
int i;
22802277
int read_stdin = 1;
2281-
int reverse = 0;
22822278
int inaccurate_eof = 0;
22832279

22842280
const char *whitespace_option = NULL;
@@ -2289,7 +2285,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
22892285
int fd;
22902286

22912287
if (!strcmp(arg, "-")) {
2292-
apply_patch(0, "<stdin>", reverse, inaccurate_eof);
2288+
apply_patch(0, "<stdin>", inaccurate_eof);
22932289
read_stdin = 0;
22942290
continue;
22952291
}
@@ -2367,7 +2363,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
23672363
continue;
23682364
}
23692365
if (!strcmp(arg, "-R") || !strcmp(arg, "--reverse")) {
2370-
reverse = 1;
2366+
apply_in_reverse = 1;
23712367
continue;
23722368
}
23732369
if (!strcmp(arg, "--inaccurate-eof")) {
@@ -2390,12 +2386,12 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
23902386
usage(apply_usage);
23912387
read_stdin = 0;
23922388
set_default_whitespace_mode(whitespace_option);
2393-
apply_patch(fd, arg, reverse, inaccurate_eof);
2389+
apply_patch(fd, arg, inaccurate_eof);
23942390
close(fd);
23952391
}
23962392
set_default_whitespace_mode(whitespace_option);
23972393
if (read_stdin)
2398-
apply_patch(0, "<stdin>", reverse, inaccurate_eof);
2394+
apply_patch(0, "<stdin>", inaccurate_eof);
23992395
if (whitespace_error) {
24002396
if (squelch_whitespace_errors &&
24012397
squelch_whitespace_errors < whitespace_error) {

0 commit comments

Comments
 (0)