@matheusbsbs / matheus-bagliano / commits / 82e2765f59
Fork 0

git-apply --reject: send rejects to .rej files.

... just like everybody else does, instead of sending it to the standard output, which was just silly. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Aug 18, 2006 at 03:10 UTC 82e2765f59126f96da6e5dc30adf7c6189e9697d
2 files changed +80 -49
builtin-apply.c
+50 -32
@@ -2242,36 +2242,61 @@ static void write_out_one_result(struct patch *patch, int phase)
2242
2243 static int write_out_one_reject(struct patch *patch)
2244 {
2245 + FILE *rej;
2246 + char namebuf[PATH_MAX];
2247 struct fragment *frag;
2246 - int rejects = 0;
2248 + int cnt = 0;
2249
2248 - for (rejects = 0, frag = patch->fragments; frag; frag = frag->next) {
2250 + for (cnt = 0, frag = patch->fragments; frag; frag = frag->next) {
2251 if (!frag->rejected)
2252 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");
2253 + cnt++;
2254 + }
2255 +
2256 + if (!cnt)
2257 + return 0;
2258 +
2259 + /* This should not happen, because a removal patch that leaves
2260 + * contents are marked "rejected" at the patch level.
2261 + */
2262 + if (!patch->new_name)
2263 + die("internal error");
2264 +
2265 + cnt = strlen(patch->new_name);
2266 + if (ARRAY_SIZE(namebuf) <= cnt + 5) {
2267 + cnt = ARRAY_SIZE(namebuf) - 5;
2268 + fprintf(stderr,
2269 + "warning: truncating .rej filename to %.*s.rej",
2270 + cnt - 1, patch->new_name);
2271 + }
2272 + memcpy(namebuf, patch->new_name, cnt);
2273 + memcpy(namebuf + cnt, ".rej", 5);
2274 +
2275 + rej = fopen(namebuf, "w");
2276 + if (!rej)
2277 + return error("cannot open %s: %s", namebuf, strerror(errno));
2278 +
2279 + /* Normal git tools never deal with .rej, so do not pretend
2280 + * this is a git patch by saying --git nor give extended
2281 + * headers. While at it, maybe please "kompare" that wants
2282 + * the trailing TAB and some garbage at the end of line ;-).
2283 + */
2284 + fprintf(rej, "diff a/%s b/%s\t(rejected hunks)\n",
2285 + patch->new_name, patch->new_name);
2286 + for (cnt = 0, frag = patch->fragments;
2287 + frag;
2288 + cnt++, frag = frag->next) {
2289 + if (!frag->rejected) {
2290 + fprintf(stderr, "Hunk #%d applied cleanly.\n", cnt);
2291 + continue;
2292 }
2270 - printf("%.*s", frag->size, frag->patch);
2293 + fprintf(stderr, "Rejected hunk #%d.\n", cnt);
2294 + fprintf(rej, "%.*s", frag->size, frag->patch);
2295 if (frag->patch[frag->size-1] != '\n')
2272 - putchar('\n');
2296 + fputc('\n', rej);
2297 }
2274 - return rejects;
2298 + fclose(rej);
2299 + return -1;
2300 }
2301
2302 static int write_out_results(struct patch *list, int skipped_patch)
@@ -2288,16 +2313,9 @@ static int write_out_results(struct patch *list, int skipped_patch)
2313 while (l) {
2314 if (l->rejected)
2315 errs = 1;
2291 - else
2316 + else {
2317 write_out_one_result(l, phase);
2293 - l = l->next;
2294 - }
2295 - }
2296 - if (apply_with_reject) {
2297 - l = list;
2298 - while (l) {
2299 - if (!l->rejected) {
2300 - if (write_out_one_reject(l))
2318 + if (phase == 1 && write_out_one_reject(l))
2319 errs = 1;
2320 }
2321 l = l->next;
t/t4117-apply-reject.sh
+30 -17
@@ -23,6 +23,12 @@ test_expect_success setup '
23 echo $i
24 done >file1 &&
25 git diff >patch.1 &&
26 + cat file1 >clean &&
27 +
28 + 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
29 + do
30 + echo $i
31 + done >expected &&
32
33 mv file1 file2 &&
34 git update-index --add --remove file1 file2 &&
@@ -53,25 +59,30 @@ test_expect_success 'apply without --reject should fail' '
59
60 test_expect_success 'apply with --reject should fail but update the file' '
61
56 - cat saved.file1 >file1
62 + cat saved.file1 >file1 &&
63 + rm -f file1.rej file2.rej &&
64
58 - if git apply --reject patch.1 >rejects
65 + if git apply --reject patch.1
66 then
67 echo "succeeds with --reject?"
68 exit 1
69 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 &&
70
69 - diff -u file1 expected.file1
71 + diff -u file1 expected &&
72 +
73 + cat file1.rej &&
74 +
75 + if test -f file2.rej
76 + then
77 + echo "file2 should not have been touched"
78 + exit 1
79 + fi
80 '
81
82 test_expect_success 'apply with --reject should fail but update the file' '
83
74 - cat saved.file1 >file1
84 + cat saved.file1 >file1 &&
85 + rm -f file1.rej file2.rej file2 &&
86
87 if git apply --reject patch.2 >rejects
88 then
@@ -79,18 +90,20 @@ test_expect_success 'apply with --reject should fail but update the file' '
90 exit 1
91 fi
92
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 -
93 test -f file1 && {
94 echo "file1 still exists?"
95 exit 1
96 }
93 - diff -u file2 expected.file2
97 + diff -u file2 expected &&
98 +
99 + cat file2.rej &&
100 +
101 + if test -f file1.rej
102 + then
103 + echo "file2 should not have been touched"
104 + exit 1
105 + fi
106 +
107 '
108
109 test_done