git-apply --reject
With the new flag "--reject", hunks that do not apply are sent to the standard output, and the usable hunks are applied. The command itself exits with non-zero status when this happens, so that the user or wrapper can take notice and sort the remaining mess out. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Aug 16, 2006 at 17:55 UTC
57dc397cff09bfabd79ddbc38b704cdd0c2bc6e3
3 files changed
+198
-23
Documentation/git-apply.txt
+8
-1
@@ -11,7 +11,7 @@ SYNOPSIS
11
[verse]
12
'git-apply' [--stat] [--numstat] [--summary] [--check] [--index] [--apply]
13
[--no-add] [--index-info] [--allow-binary-replacement]
14
- [--reverse] [-z] [-pNUM]
14
+ [--reverse] [--reject] [-z] [-pNUM]
15
[-CNUM] [--whitespace=<nowarn|warn|error|error-all|strip>]
16
[<patch>...]
17
@@ -66,6 +66,13 @@ OPTIONS
66
--reverse::
67
Apply the patch in reverse.
68
69
+--reject::
70
+ For atomicity, `git apply` fails the whole patch and
71
+ does not touch the working tree when some of the hunks
72
+ do not apply by default. This option makes it apply
73
+ parts of the patch that are applicable, and send the
74
+ rejected hunks to the standard output of the command.
75
+
76
-z::
77
When showing the index information, do not munge paths,
78
but use NUL terminated machine readable format. Without
builtin-apply.c
+94
-22
@@ -38,12 +38,13 @@ static int summary;
38
static int check;
39
static int apply = 1;
40
static int apply_in_reverse;
41
+static int apply_with_reject;
42
static int no_add;
43
static int show_index_info;
44
static int line_termination = '\n';
45
static unsigned long p_context = -1;
46
static const char apply_usage[] =
46
-"git-apply [--stat] [--numstat] [--summary] [--check] [--index] [--cached] [--apply] [--no-add] [--index-info] [--allow-binary-replacement] [--reverse] [-z] [-pNUM] [-CNUM] [--whitespace=<nowarn|warn|error|error-all|strip>] <patch>...";
47
+"git-apply [--stat] [--numstat] [--summary] [--check] [--index] [--cached] [--apply] [--no-add] [--index-info] [--allow-binary-replacement] [--reverse] [--reject] [-z] [-pNUM] [-CNUM] [--whitespace=<nowarn|warn|error|error-all|strip>] <patch>...";
48
49
static enum whitespace_eol {
50
nowarn_whitespace,
@@ -122,6 +123,7 @@ struct fragment {
123
unsigned long newpos, newlines;
124
const char *patch;
125
int size;
126
+ int rejected;
127
struct fragment *next;
128
};
129
@@ -138,6 +140,7 @@ struct patch {
140
char *new_name, *old_name, *def_name;
141
unsigned int old_mode, new_mode;
142
int is_rename, is_copy, is_new, is_delete, is_binary;
143
+ int rejected;
144
unsigned long deflate_origlen;
145
int lines_added, lines_deleted;
146
int score;
@@ -1548,7 +1551,8 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
1551
lines = 0;
1552
pos = frag->newpos;
1553
for (;;) {
1551
- offset = find_offset(buf, desc->size, oldlines, oldsize, pos, &lines);
1554
+ offset = find_offset(buf, desc->size,
1555
+ oldlines, oldsize, pos, &lines);
1556
if (match_end && offset + oldsize != desc->size)
1557
offset = -1;
1558
if (match_beginning && offset)
@@ -1561,8 +1565,10 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
1565
/* Warn if it was necessary to reduce the number
1566
* of context lines.
1567
*/
1564
- if ((leading != frag->leading) || (trailing != frag->trailing))
1565
- fprintf(stderr, "Context reduced to (%ld/%ld) to apply fragment at %d\n",
1568
+ if ((leading != frag->leading) ||
1569
+ (trailing != frag->trailing))
1570
+ fprintf(stderr, "Context reduced to (%ld/%ld)"
1571
+ " to apply fragment at %d\n",
1572
leading, trailing, pos + lines);
1573
1574
if (size > alloc) {
@@ -1572,7 +1578,9 @@ static int apply_one_fragment(struct buffer_desc *desc, struct fragment *frag, i
1578
desc->buffer = buf;
1579
}
1580
desc->size = size;
1575
- memmove(buf + offset + newsize, buf + offset + oldsize, size - offset - newsize);
1581
+ memmove(buf + offset + newsize,
1582
+ buf + offset + oldsize,
1583
+ size - offset - newsize);
1584
memcpy(buf + offset, newlines, newsize);
1585
offset = 0;
1586
@@ -1736,9 +1744,12 @@ static int apply_fragments(struct buffer_desc *desc, struct patch *patch)
1744
return apply_binary(desc, patch);
1745
1746
while (frag) {
1739
- if (apply_one_fragment(desc, frag, patch->inaccurate_eof) < 0)
1740
- return error("patch failed: %s:%ld",
1741
- name, frag->oldpos);
1747
+ if (apply_one_fragment(desc, frag, patch->inaccurate_eof)) {
1748
+ error("patch failed: %s:%ld", name, frag->oldpos);
1749
+ if (!apply_with_reject)
1750
+ return -1;
1751
+ frag->rejected = 1;
1752
+ }
1753
frag = frag->next;
1754
}
1755
return 0;
@@ -1774,8 +1785,9 @@ static int apply_data(struct patch *patch, struct stat *st, struct cache_entry *
1785
desc.size = size;
1786
desc.alloc = alloc;
1787
desc.buffer = buf;
1788
+
1789
if (apply_fragments(&desc, patch) < 0)
1778
- return -1;
1790
+ return -1; /* note with --reject this succeeds. */
1791
1792
/* NUL terminate the result */
1793
if (desc.alloc <= desc.size)
@@ -1800,6 +1812,7 @@ static int check_patch(struct patch *patch, struct patch *prev_patch)
1812
struct cache_entry *ce = NULL;
1813
int ok_if_exists;
1814
1815
+ patch->rejected = 1; /* we will drop this after we succeed */
1816
if (old_name) {
1817
int changed = 0;
1818
int stat_ret = 0;
@@ -1905,6 +1918,7 @@ static int check_patch(struct patch *patch, struct patch *prev_patch)
1918
1919
if (apply_data(patch, &st, ce) < 0)
1920
return error("%s: patch does not apply", name);
1921
+ patch->rejected = 0;
1922
return 0;
1923
}
1924
@@ -2223,23 +2237,73 @@ static void write_out_one_result(struct patch *patch, int phase)
2237
if (phase == 0)
2238
remove_file(patch);
2239
if (phase == 1)
2226
- create_file(patch);
2240
+ create_file(patch);
2241
}
2242
2229
-static void write_out_results(struct patch *list, int skipped_patch)
2243
+static int write_out_one_reject(struct patch *patch)
2244
+{
2245
+ struct fragment *frag;
2246
+ int rejects = 0;
2247
+
2248
+ for (rejects = 0, frag = patch->fragments; frag; frag = frag->next) {
2249
+ if (!frag->rejected)
2250
+ continue;
2251
+ if (rejects == 0) {
2252
+ rejects = 1;
2253
+ printf("** Rejected hunk(s) for ");
2254
+ if (patch->old_name && patch->new_name &&
2255
+ strcmp(patch->old_name, patch->new_name)) {
2256
+ write_name_quoted(NULL, 0,
2257
+ patch->old_name, 1, stdout);
2258
+ fputs(" => ", stdout);
2259
+ write_name_quoted(NULL, 0,
2260
+ patch->new_name, 1, stdout);
2261
+ }
2262
+ else {
2263
+ const char *n = patch->new_name;
2264
+ if (!n)
2265
+ n = patch->old_name;
2266
+ write_name_quoted(NULL, 0, n, 1, stdout);
2267
+ }
2268
+ printf(" **\n");
2269
+ }
2270
+ printf("%.*s", frag->size, frag->patch);
2271
+ if (frag->patch[frag->size-1] != '\n')
2272
+ putchar('\n');
2273
+ }
2274
+ return rejects;
2275
+}
2276
+
2277
+static int write_out_results(struct patch *list, int skipped_patch)
2278
{
2279
int phase;
2280
+ int errs = 0;
2281
+ struct patch *l;
2282
2283
if (!list && !skipped_patch)
2234
- die("No changes");
2284
+ return error("No changes");
2285
2286
for (phase = 0; phase < 2; phase++) {
2237
- struct patch *l = list;
2287
+ l = list;
2288
+ while (l) {
2289
+ if (l->rejected)
2290
+ errs = 1;
2291
+ else
2292
+ write_out_one_result(l, phase);
2293
+ l = l->next;
2294
+ }
2295
+ }
2296
+ if (apply_with_reject) {
2297
+ l = list;
2298
while (l) {
2239
- write_out_one_result(l, phase);
2299
+ if (!l->rejected) {
2300
+ if (write_out_one_reject(l))
2301
+ errs = 1;
2302
+ }
2303
l = l->next;
2304
}
2305
}
2306
+ return errs;
2307
}
2308
2309
static struct lock_file lock_file;
@@ -2314,11 +2378,13 @@ static int apply_patch(int fd, const char *filename, int inaccurate_eof)
2378
die("unable to read index file");
2379
}
2380
2317
- if ((check || apply) && check_patch_list(list) < 0)
2381
+ if ((check || apply) &&
2382
+ check_patch_list(list) < 0 &&
2383
+ !apply_with_reject)
2384
exit(1);
2385
2320
- if (apply)
2321
- write_out_results(list, skipped_patch);
2386
+ if (apply && write_out_results(list, skipped_patch))
2387
+ exit(1);
2388
2389
if (show_index_info)
2390
show_index_list(list);
@@ -2351,6 +2417,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
2417
int i;
2418
int read_stdin = 1;
2419
int inaccurate_eof = 0;
2420
+ int errs = 0;
2421
2422
const char *whitespace_option = NULL;
2423
@@ -2360,7 +2427,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
2427
int fd;
2428
2429
if (!strcmp(arg, "-")) {
2363
- apply_patch(0, "<stdin>", inaccurate_eof);
2430
+ errs |= apply_patch(0, "<stdin>", inaccurate_eof);
2431
read_stdin = 0;
2432
continue;
2433
}
@@ -2441,6 +2508,10 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
2508
apply_in_reverse = 1;
2509
continue;
2510
}
2511
+ if (!strcmp(arg, "--reject")) {
2512
+ apply = apply_with_reject = 1;
2513
+ continue;
2514
+ }
2515
if (!strcmp(arg, "--inaccurate-eof")) {
2516
inaccurate_eof = 1;
2517
continue;
@@ -2461,18 +2532,19 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
2532
usage(apply_usage);
2533
read_stdin = 0;
2534
set_default_whitespace_mode(whitespace_option);
2464
- apply_patch(fd, arg, inaccurate_eof);
2535
+ errs |= apply_patch(fd, arg, inaccurate_eof);
2536
close(fd);
2537
}
2538
set_default_whitespace_mode(whitespace_option);
2539
if (read_stdin)
2469
- apply_patch(0, "<stdin>", inaccurate_eof);
2540
+ errs |= apply_patch(0, "<stdin>", inaccurate_eof);
2541
if (whitespace_error) {
2542
if (squelch_whitespace_errors &&
2543
squelch_whitespace_errors < whitespace_error) {
2544
int squelched =
2545
whitespace_error - squelch_whitespace_errors;
2475
- fprintf(stderr, "warning: squelched %d whitespace error%s\n",
2546
+ fprintf(stderr, "warning: squelched %d "
2547
+ "whitespace error%s\n",
2548
squelched,
2549
squelched == 1 ? "" : "s");
2550
}
@@ -2500,5 +2572,5 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
2572
die("Unable to write new index file");
2573
}
2574
2503
- return 0;
2575
+ return !!errs;
2576
}
t/t4117-apply-reject.sh
new
+96
@@ -0,0 +1,96 @@
1
+#!/bin/sh
2
+#
3
+# Copyright (c) 2005 Junio C Hamano
4
+#
5
+
6
+test_description='git-apply with rejects
7
+
8
+'
9
+
10
+. ./test-lib.sh
11
+
12
+test_expect_success setup '
13
+ for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
14
+ do
15
+ echo $i
16
+ done >file1 &&
17
+ cat file1 >saved.file1 &&
18
+ git update-index --add file1 &&
19
+ git commit -m initial &&
20
+
21
+ for i in 1 2 A B 4 5 6 7 8 9 10 11 12 C 13 14 15 16 17 18 19 20 D 21
22
+ do
23
+ echo $i
24
+ done >file1 &&
25
+ git diff >patch.1 &&
26
+
27
+ mv file1 file2 &&
28
+ git update-index --add --remove file1 file2 &&
29
+ git diff -M HEAD >patch.2 &&
30
+
31
+ rm -f file1 file2 &&
32
+ mv saved.file1 file1 &&
33
+ git update-index --add --remove file1 file2 &&
34
+
35
+ for i in 1 E 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 F 21
36
+ do
37
+ echo $i
38
+ done >file1 &&
39
+
40
+ cat file1 >saved.file1
41
+'
42
+
43
+test_expect_success 'apply without --reject should fail' '
44
+
45
+ if git apply patch.1
46
+ then
47
+ echo "Eh? Why?"
48
+ exit 1
49
+ fi
50
+
51
+ diff -u file1 saved.file1
52
+'
53
+
54
+test_expect_success 'apply with --reject should fail but update the file' '
55
+
56
+ cat saved.file1 >file1
57
+
58
+ if git apply --reject patch.1 >rejects
59
+ then
60
+ echo "succeeds with --reject?"
61
+ exit 1
62
+ fi
63
+ cat rejects
64
+ for i in 1 E 2 3 4 5 6 7 8 9 10 11 12 C 13 14 15 16 17 18 19 20 F 21
65
+ do
66
+ echo $i
67
+ done >expected.file1 &&
68
+
69
+ diff -u file1 expected.file1
70
+'
71
+
72
+test_expect_success 'apply with --reject should fail but update the file' '
73
+
74
+ cat saved.file1 >file1
75
+
76
+ if git apply --reject patch.2 >rejects
77
+ then
78
+ echo "succeeds with --reject?"
79
+ exit 1
80
+ fi
81
+
82
+ cat rejects
83
+
84
+ for i in 1 E 2 3 4 5 6 7 8 9 10 11 12 C 13 14 15 16 17 18 19 20 F 21
85
+ do
86
+ echo $i
87
+ done >expected.file2 &&
88
+
89
+ test -f file1 && {
90
+ echo "file1 still exists?"
91
+ exit 1
92
+ }
93
+ diff -u file2 expected.file2
94
+'
95
+
96
+test_done