]>
Commit | Line | Data |
---|---|---|
5b2fd956 | 1 | #include "cache.h" |
c455c87c | 2 | #include "string-list.h" |
5b2fd956 | 3 | #include "rerere.h" |
5b2fd956 | 4 | #include "xdiff-interface.h" |
dea4562b JH |
5 | #include "dir.h" |
6 | #include "resolve-undo.h" | |
7 | #include "ll-merge.h" | |
8588567c | 8 | #include "attr.h" |
5b2fd956 SB |
9 | |
10 | /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */ | |
11 | static int rerere_enabled = -1; | |
12 | ||
13 | /* automatically update cleanly resolved paths to the index */ | |
14 | static int rerere_autoupdate; | |
15 | ||
16 | static char *merge_rr_path; | |
17 | ||
90056966 | 18 | const char *rerere_path(const char *hex, const char *file) |
5b2fd956 | 19 | { |
90056966 | 20 | return git_path("rr-cache/%s/%s", hex, file); |
5b2fd956 SB |
21 | } |
22 | ||
90056966 | 23 | int has_rerere_resolution(const char *hex) |
5b2fd956 SB |
24 | { |
25 | struct stat st; | |
90056966 | 26 | return !stat(rerere_path(hex, "postimage"), &st); |
5b2fd956 SB |
27 | } |
28 | ||
c455c87c | 29 | static void read_rr(struct string_list *rr) |
5b2fd956 SB |
30 | { |
31 | unsigned char sha1[20]; | |
32 | char buf[PATH_MAX]; | |
33 | FILE *in = fopen(merge_rr_path, "r"); | |
34 | if (!in) | |
35 | return; | |
36 | while (fread(buf, 40, 1, in) == 1) { | |
37 | int i; | |
38 | char *name; | |
39 | if (get_sha1_hex(buf, sha1)) | |
40 | die("corrupt MERGE_RR"); | |
41 | buf[40] = '\0'; | |
42 | name = xstrdup(buf); | |
43 | if (fgetc(in) != '\t') | |
44 | die("corrupt MERGE_RR"); | |
45 | for (i = 0; i < sizeof(buf) && (buf[i] = fgetc(in)); i++) | |
46 | ; /* do nothing */ | |
47 | if (i == sizeof(buf)) | |
48 | die("filename too long"); | |
c455c87c | 49 | string_list_insert(buf, rr)->util = name; |
5b2fd956 SB |
50 | } |
51 | fclose(in); | |
52 | } | |
53 | ||
54 | static struct lock_file write_lock; | |
55 | ||
c455c87c | 56 | static int write_rr(struct string_list *rr, int out_fd) |
5b2fd956 SB |
57 | { |
58 | int i; | |
59 | for (i = 0; i < rr->nr; i++) { | |
60 | const char *path; | |
61 | int length; | |
62 | if (!rr->items[i].util) | |
63 | continue; | |
c455c87c | 64 | path = rr->items[i].string; |
5b2fd956 SB |
65 | length = strlen(path) + 1; |
66 | if (write_in_full(out_fd, rr->items[i].util, 40) != 40 || | |
2b7ca830 | 67 | write_str_in_full(out_fd, "\t") != 1 || |
5b2fd956 SB |
68 | write_in_full(out_fd, path, length) != length) |
69 | die("unable to write rerere record"); | |
70 | } | |
71 | if (commit_lock_file(&write_lock) != 0) | |
72 | die("unable to write rerere record"); | |
73 | return 0; | |
74 | } | |
75 | ||
47d32af2 AR |
76 | static void ferr_write(const void *p, size_t count, FILE *fp, int *err) |
77 | { | |
78 | if (!count || *err) | |
79 | return; | |
80 | if (fwrite(p, count, 1, fp) != 1) | |
81 | *err = errno; | |
82 | } | |
83 | ||
84 | static inline void ferr_puts(const char *s, FILE *fp, int *err) | |
85 | { | |
86 | ferr_write(s, strlen(s), fp, err); | |
87 | } | |
88 | ||
27d6b085 JH |
89 | struct rerere_io { |
90 | int (*getline)(struct strbuf *, struct rerere_io *); | |
91 | FILE *output; | |
92 | int wrerror; | |
93 | /* some more stuff */ | |
94 | }; | |
95 | ||
96 | static void rerere_io_putstr(const char *str, struct rerere_io *io) | |
97 | { | |
98 | if (io->output) | |
99 | ferr_puts(str, io->output, &io->wrerror); | |
100 | } | |
101 | ||
191f2417 JH |
102 | static void rerere_io_putconflict(int ch, int size, struct rerere_io *io) |
103 | { | |
104 | char buf[64]; | |
105 | ||
106 | while (size) { | |
107 | if (size < sizeof(buf) - 2) { | |
108 | memset(buf, ch, size); | |
109 | buf[size] = '\n'; | |
110 | buf[size + 1] = '\0'; | |
111 | size = 0; | |
112 | } else { | |
113 | int sz = sizeof(buf) - 1; | |
114 | if (size <= sz) | |
115 | sz -= (sz - size) + 1; | |
116 | memset(buf, ch, sz); | |
117 | buf[sz] = '\0'; | |
118 | size -= sz; | |
119 | } | |
120 | rerere_io_putstr(buf, io); | |
121 | } | |
122 | } | |
123 | ||
27d6b085 JH |
124 | static void rerere_io_putmem(const char *mem, size_t sz, struct rerere_io *io) |
125 | { | |
126 | if (io->output) | |
127 | ferr_write(mem, sz, io->output, &io->wrerror); | |
128 | } | |
129 | ||
130 | struct rerere_io_file { | |
131 | struct rerere_io io; | |
132 | FILE *input; | |
133 | }; | |
134 | ||
135 | static int rerere_file_getline(struct strbuf *sb, struct rerere_io *io_) | |
136 | { | |
137 | struct rerere_io_file *io = (struct rerere_io_file *)io_; | |
138 | return strbuf_getwholeline(sb, io->input, '\n'); | |
139 | } | |
140 | ||
191f2417 JH |
141 | static int is_cmarker(char *buf, int marker_char, int marker_size, int want_sp) |
142 | { | |
143 | while (marker_size--) | |
144 | if (*buf++ != marker_char) | |
145 | return 0; | |
146 | if (want_sp && *buf != ' ') | |
147 | return 0; | |
148 | return isspace(*buf); | |
149 | } | |
150 | ||
151 | static int handle_path(unsigned char *sha1, struct rerere_io *io, int marker_size) | |
5b2fd956 | 152 | { |
9126f009 | 153 | git_SHA_CTX ctx; |
cc58d7df JH |
154 | int hunk_no = 0; |
155 | enum { | |
387c9d49 | 156 | RR_CONTEXT = 0, RR_SIDE_1, RR_SIDE_2, RR_ORIGINAL, |
cc58d7df | 157 | } hunk = RR_CONTEXT; |
f285a2d7 | 158 | struct strbuf one = STRBUF_INIT, two = STRBUF_INIT; |
d58ee6db | 159 | struct strbuf buf = STRBUF_INIT; |
5b2fd956 SB |
160 | |
161 | if (sha1) | |
9126f009 | 162 | git_SHA1_Init(&ctx); |
5b2fd956 | 163 | |
27d6b085 | 164 | while (!io->getline(&buf, io)) { |
191f2417 | 165 | if (is_cmarker(buf.buf, '<', marker_size, 1)) { |
cc58d7df | 166 | if (hunk != RR_CONTEXT) |
5b2fd956 | 167 | goto bad; |
cc58d7df | 168 | hunk = RR_SIDE_1; |
191f2417 | 169 | } else if (is_cmarker(buf.buf, '|', marker_size, 0)) { |
cc58d7df | 170 | if (hunk != RR_SIDE_1) |
5b2fd956 | 171 | goto bad; |
387c9d49 | 172 | hunk = RR_ORIGINAL; |
191f2417 | 173 | } else if (is_cmarker(buf.buf, '=', marker_size, 0)) { |
387c9d49 | 174 | if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL) |
5b2fd956 | 175 | goto bad; |
cc58d7df | 176 | hunk = RR_SIDE_2; |
191f2417 | 177 | } else if (is_cmarker(buf.buf, '>', marker_size, 1)) { |
cc58d7df | 178 | if (hunk != RR_SIDE_2) |
5b2fd956 SB |
179 | goto bad; |
180 | if (strbuf_cmp(&one, &two) > 0) | |
181 | strbuf_swap(&one, &two); | |
182 | hunk_no++; | |
cc58d7df | 183 | hunk = RR_CONTEXT; |
191f2417 | 184 | rerere_io_putconflict('<', marker_size, io); |
27d6b085 | 185 | rerere_io_putmem(one.buf, one.len, io); |
191f2417 | 186 | rerere_io_putconflict('=', marker_size, io); |
27d6b085 | 187 | rerere_io_putmem(two.buf, two.len, io); |
191f2417 | 188 | rerere_io_putconflict('>', marker_size, io); |
5b2fd956 | 189 | if (sha1) { |
9126f009 | 190 | git_SHA1_Update(&ctx, one.buf ? one.buf : "", |
5b2fd956 | 191 | one.len + 1); |
9126f009 | 192 | git_SHA1_Update(&ctx, two.buf ? two.buf : "", |
5b2fd956 SB |
193 | two.len + 1); |
194 | } | |
195 | strbuf_reset(&one); | |
196 | strbuf_reset(&two); | |
cc58d7df | 197 | } else if (hunk == RR_SIDE_1) |
d58ee6db | 198 | strbuf_addstr(&one, buf.buf); |
387c9d49 JH |
199 | else if (hunk == RR_ORIGINAL) |
200 | ; /* discard */ | |
cc58d7df | 201 | else if (hunk == RR_SIDE_2) |
d58ee6db | 202 | strbuf_addstr(&two, buf.buf); |
27d6b085 JH |
203 | else |
204 | rerere_io_putstr(buf.buf, io); | |
5b2fd956 SB |
205 | continue; |
206 | bad: | |
207 | hunk = 99; /* force error exit */ | |
208 | break; | |
209 | } | |
210 | strbuf_release(&one); | |
211 | strbuf_release(&two); | |
d58ee6db | 212 | strbuf_release(&buf); |
5b2fd956 | 213 | |
5b2fd956 | 214 | if (sha1) |
9126f009 | 215 | git_SHA1_Final(sha1, &ctx); |
27d6b085 JH |
216 | if (hunk != RR_CONTEXT) |
217 | return -1; | |
218 | return hunk_no; | |
219 | } | |
220 | ||
221 | static int handle_file(const char *path, unsigned char *sha1, const char *output) | |
222 | { | |
223 | int hunk_no = 0; | |
224 | struct rerere_io_file io; | |
8588567c | 225 | int marker_size = ll_merge_marker_size(path); |
27d6b085 JH |
226 | |
227 | memset(&io, 0, sizeof(io)); | |
228 | io.io.getline = rerere_file_getline; | |
229 | io.input = fopen(path, "r"); | |
230 | io.io.wrerror = 0; | |
231 | if (!io.input) | |
232 | return error("Could not open %s", path); | |
233 | ||
234 | if (output) { | |
235 | io.io.output = fopen(output, "w"); | |
236 | if (!io.io.output) { | |
237 | fclose(io.input); | |
238 | return error("Could not write %s", output); | |
239 | } | |
240 | } | |
241 | ||
191f2417 | 242 | hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size); |
27d6b085 JH |
243 | |
244 | fclose(io.input); | |
245 | if (io.io.wrerror) | |
246 | error("There were errors while writing %s (%s)", | |
247 | path, strerror(io.io.wrerror)); | |
248 | if (io.io.output && fclose(io.io.output)) | |
249 | io.io.wrerror = error("Failed to flush %s: %s", | |
250 | path, strerror(errno)); | |
251 | ||
252 | if (hunk_no < 0) { | |
5b2fd956 | 253 | if (output) |
691f1a28 | 254 | unlink_or_warn(output); |
5b2fd956 SB |
255 | return error("Could not parse conflict hunks in %s", path); |
256 | } | |
27d6b085 | 257 | if (io.io.wrerror) |
47d32af2 | 258 | return -1; |
5b2fd956 SB |
259 | return hunk_no; |
260 | } | |
261 | ||
dea4562b JH |
262 | struct rerere_io_mem { |
263 | struct rerere_io io; | |
264 | struct strbuf input; | |
265 | }; | |
266 | ||
267 | static int rerere_mem_getline(struct strbuf *sb, struct rerere_io *io_) | |
268 | { | |
269 | struct rerere_io_mem *io = (struct rerere_io_mem *)io_; | |
270 | char *ep; | |
271 | size_t len; | |
272 | ||
273 | strbuf_release(sb); | |
274 | if (!io->input.len) | |
275 | return -1; | |
276 | ep = strchrnul(io->input.buf, '\n'); | |
277 | if (*ep == '\n') | |
278 | ep++; | |
279 | len = ep - io->input.buf; | |
280 | strbuf_add(sb, io->input.buf, len); | |
281 | strbuf_remove(&io->input, 0, len); | |
282 | return 0; | |
283 | } | |
284 | ||
285 | static int handle_cache(const char *path, unsigned char *sha1, const char *output) | |
286 | { | |
287 | mmfile_t mmfile[3]; | |
288 | mmbuffer_t result = {NULL, 0}; | |
289 | struct cache_entry *ce; | |
290 | int pos, len, i, hunk_no; | |
291 | struct rerere_io_mem io; | |
8588567c | 292 | int marker_size = ll_merge_marker_size(path); |
dea4562b JH |
293 | |
294 | /* | |
295 | * Reproduce the conflicted merge in-core | |
296 | */ | |
297 | len = strlen(path); | |
298 | pos = cache_name_pos(path, len); | |
299 | if (0 <= pos) | |
47d32af2 | 300 | return -1; |
dea4562b JH |
301 | pos = -pos - 1; |
302 | ||
303 | for (i = 0; i < 3; i++) { | |
304 | enum object_type type; | |
305 | unsigned long size; | |
306 | ||
307 | mmfile[i].size = 0; | |
308 | mmfile[i].ptr = NULL; | |
309 | if (active_nr <= pos) | |
310 | break; | |
311 | ce = active_cache[pos++]; | |
312 | if (ce_namelen(ce) != len || memcmp(ce->name, path, len) | |
313 | || ce_stage(ce) != i + 1) | |
314 | break; | |
315 | mmfile[i].ptr = read_sha1_file(ce->sha1, &type, &size); | |
316 | mmfile[i].size = size; | |
317 | } | |
318 | for (i = 0; i < 3; i++) { | |
319 | if (!mmfile[i].ptr && !mmfile[i].size) | |
320 | mmfile[i].ptr = xstrdup(""); | |
321 | } | |
322 | ll_merge(&result, path, &mmfile[0], | |
323 | &mmfile[1], "ours", | |
324 | &mmfile[2], "theirs", 0); | |
325 | for (i = 0; i < 3; i++) | |
326 | free(mmfile[i].ptr); | |
327 | ||
af86debc | 328 | memset(&io, 0, sizeof(io)); |
dea4562b JH |
329 | io.io.getline = rerere_mem_getline; |
330 | if (output) | |
331 | io.io.output = fopen(output, "w"); | |
332 | else | |
333 | io.io.output = NULL; | |
334 | strbuf_init(&io.input, 0); | |
335 | strbuf_attach(&io.input, result.ptr, result.size, result.size); | |
336 | ||
191f2417 | 337 | hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size); |
dea4562b JH |
338 | strbuf_release(&io.input); |
339 | if (io.io.output) | |
340 | fclose(io.io.output); | |
5b2fd956 SB |
341 | return hunk_no; |
342 | } | |
343 | ||
c455c87c | 344 | static int find_conflict(struct string_list *conflict) |
5b2fd956 SB |
345 | { |
346 | int i; | |
347 | if (read_cache() < 0) | |
348 | return error("Could not read index"); | |
349 | for (i = 0; i+1 < active_nr; i++) { | |
350 | struct cache_entry *e2 = active_cache[i]; | |
351 | struct cache_entry *e3 = active_cache[i+1]; | |
352 | if (ce_stage(e2) == 2 && | |
353 | ce_stage(e3) == 3 && | |
354 | ce_same_name(e2, e3) && | |
355 | S_ISREG(e2->ce_mode) && | |
356 | S_ISREG(e3->ce_mode)) { | |
c455c87c | 357 | string_list_insert((const char *)e2->name, conflict); |
5b2fd956 SB |
358 | i++; /* skip over both #2 and #3 */ |
359 | } | |
360 | } | |
361 | return 0; | |
362 | } | |
363 | ||
364 | static int merge(const char *name, const char *path) | |
365 | { | |
366 | int ret; | |
689b8c29 | 367 | mmfile_t cur = {NULL, 0}, base = {NULL, 0}, other = {NULL, 0}; |
5b2fd956 | 368 | mmbuffer_t result = {NULL, 0}; |
5b2fd956 | 369 | |
90056966 | 370 | if (handle_file(path, NULL, rerere_path(name, "thisimage")) < 0) |
5b2fd956 SB |
371 | return 1; |
372 | ||
90056966 SG |
373 | if (read_mmfile(&cur, rerere_path(name, "thisimage")) || |
374 | read_mmfile(&base, rerere_path(name, "preimage")) || | |
689b8c29 BW |
375 | read_mmfile(&other, rerere_path(name, "postimage"))) { |
376 | ret = 1; | |
377 | goto out; | |
378 | } | |
88533f6d | 379 | ret = ll_merge(&result, path, &base, &cur, "", &other, "", 0); |
5b2fd956 SB |
380 | if (!ret) { |
381 | FILE *f = fopen(path, "w"); | |
382 | if (!f) | |
47d32af2 AR |
383 | return error("Could not open %s: %s", path, |
384 | strerror(errno)); | |
385 | if (fwrite(result.ptr, result.size, 1, f) != 1) | |
386 | error("Could not write %s: %s", path, strerror(errno)); | |
387 | if (fclose(f)) | |
388 | return error("Writing %s failed: %s", path, | |
389 | strerror(errno)); | |
5b2fd956 SB |
390 | } |
391 | ||
689b8c29 | 392 | out: |
5b2fd956 SB |
393 | free(cur.ptr); |
394 | free(base.ptr); | |
395 | free(other.ptr); | |
396 | free(result.ptr); | |
397 | ||
398 | return ret; | |
399 | } | |
400 | ||
401 | static struct lock_file index_lock; | |
402 | ||
c455c87c | 403 | static int update_paths(struct string_list *update) |
5b2fd956 SB |
404 | { |
405 | int i; | |
406 | int fd = hold_locked_index(&index_lock, 0); | |
407 | int status = 0; | |
408 | ||
409 | if (fd < 0) | |
410 | return -1; | |
411 | ||
412 | for (i = 0; i < update->nr; i++) { | |
c455c87c JS |
413 | struct string_list_item *item = &update->items[i]; |
414 | if (add_file_to_cache(item->string, ADD_CACHE_IGNORE_ERRORS)) | |
5b2fd956 SB |
415 | status = -1; |
416 | } | |
417 | ||
418 | if (!status && active_cache_changed) { | |
419 | if (write_cache(fd, active_cache, active_nr) || | |
420 | commit_locked_index(&index_lock)) | |
421 | die("Unable to write new index file"); | |
422 | } else if (fd >= 0) | |
423 | rollback_lock_file(&index_lock); | |
424 | return status; | |
425 | } | |
426 | ||
c455c87c | 427 | static int do_plain_rerere(struct string_list *rr, int fd) |
5b2fd956 | 428 | { |
c455c87c JS |
429 | struct string_list conflict = { NULL, 0, 0, 1 }; |
430 | struct string_list update = { NULL, 0, 0, 1 }; | |
5b2fd956 SB |
431 | int i; |
432 | ||
433 | find_conflict(&conflict); | |
434 | ||
435 | /* | |
436 | * MERGE_RR records paths with conflicts immediately after merge | |
437 | * failed. Some of the conflicted paths might have been hand resolved | |
438 | * in the working tree since then, but the initial run would catch all | |
439 | * and register their preimages. | |
440 | */ | |
441 | ||
442 | for (i = 0; i < conflict.nr; i++) { | |
c455c87c JS |
443 | const char *path = conflict.items[i].string; |
444 | if (!string_list_has_string(rr, path)) { | |
5b2fd956 SB |
445 | unsigned char sha1[20]; |
446 | char *hex; | |
447 | int ret; | |
448 | ret = handle_file(path, sha1, NULL); | |
449 | if (ret < 1) | |
450 | continue; | |
451 | hex = xstrdup(sha1_to_hex(sha1)); | |
c455c87c | 452 | string_list_insert(path, rr)->util = hex; |
5b2fd956 | 453 | if (mkdir(git_path("rr-cache/%s", hex), 0755)) |
ba19a808 | 454 | continue; |
90056966 | 455 | handle_file(path, NULL, rerere_path(hex, "preimage")); |
5b2fd956 SB |
456 | fprintf(stderr, "Recorded preimage for '%s'\n", path); |
457 | } | |
458 | } | |
459 | ||
460 | /* | |
461 | * Now some of the paths that had conflicts earlier might have been | |
462 | * hand resolved. Others may be similar to a conflict already that | |
463 | * was resolved before. | |
464 | */ | |
465 | ||
466 | for (i = 0; i < rr->nr; i++) { | |
467 | int ret; | |
c455c87c | 468 | const char *path = rr->items[i].string; |
5b2fd956 SB |
469 | const char *name = (const char *)rr->items[i].util; |
470 | ||
90056966 | 471 | if (has_rerere_resolution(name)) { |
5b2fd956 | 472 | if (!merge(name, path)) { |
5b2fd956 | 473 | if (rerere_autoupdate) |
c455c87c | 474 | string_list_insert(path, &update); |
9196e825 JH |
475 | fprintf(stderr, |
476 | "%s '%s' using previous resolution.\n", | |
477 | rerere_autoupdate | |
478 | ? "Staged" : "Resolved", | |
479 | path); | |
5b2fd956 SB |
480 | goto mark_resolved; |
481 | } | |
482 | } | |
483 | ||
484 | /* Let's see if we have resolved it. */ | |
485 | ret = handle_file(path, NULL, NULL); | |
486 | if (ret) | |
487 | continue; | |
488 | ||
489 | fprintf(stderr, "Recorded resolution for '%s'.\n", path); | |
90056966 | 490 | copy_file(rerere_path(name, "postimage"), path, 0666); |
5b2fd956 SB |
491 | mark_resolved: |
492 | rr->items[i].util = NULL; | |
493 | } | |
494 | ||
495 | if (update.nr) | |
496 | update_paths(&update); | |
497 | ||
498 | return write_rr(rr, fd); | |
499 | } | |
500 | ||
501 | static int git_rerere_config(const char *var, const char *value, void *cb) | |
502 | { | |
503 | if (!strcmp(var, "rerere.enabled")) | |
504 | rerere_enabled = git_config_bool(var, value); | |
505 | else if (!strcmp(var, "rerere.autoupdate")) | |
506 | rerere_autoupdate = git_config_bool(var, value); | |
507 | else | |
508 | return git_default_config(var, value, cb); | |
509 | return 0; | |
510 | } | |
511 | ||
512 | static int is_rerere_enabled(void) | |
513 | { | |
5b2fd956 SB |
514 | const char *rr_cache; |
515 | int rr_cache_exists; | |
516 | ||
517 | if (!rerere_enabled) | |
518 | return 0; | |
519 | ||
520 | rr_cache = git_path("rr-cache"); | |
90b4a71c | 521 | rr_cache_exists = is_directory(rr_cache); |
5b2fd956 SB |
522 | if (rerere_enabled < 0) |
523 | return rr_cache_exists; | |
524 | ||
525 | if (!rr_cache_exists && | |
526 | (mkdir(rr_cache, 0777) || adjust_shared_perm(rr_cache))) | |
527 | die("Could not create directory %s", rr_cache); | |
528 | return 1; | |
529 | } | |
530 | ||
cb6020bb | 531 | int setup_rerere(struct string_list *merge_rr, int flags) |
5b2fd956 SB |
532 | { |
533 | int fd; | |
534 | ||
535 | git_config(git_rerere_config, NULL); | |
536 | if (!is_rerere_enabled()) | |
537 | return -1; | |
538 | ||
cb6020bb JH |
539 | if (flags & (RERERE_AUTOUPDATE|RERERE_NOAUTOUPDATE)) |
540 | rerere_autoupdate = !!(flags & RERERE_AUTOUPDATE); | |
a4f34cbb | 541 | merge_rr_path = git_pathdup("MERGE_RR"); |
acd3b9ec JH |
542 | fd = hold_lock_file_for_update(&write_lock, merge_rr_path, |
543 | LOCK_DIE_ON_ERROR); | |
5b2fd956 SB |
544 | read_rr(merge_rr); |
545 | return fd; | |
546 | } | |
547 | ||
cb6020bb | 548 | int rerere(int flags) |
5b2fd956 | 549 | { |
c455c87c | 550 | struct string_list merge_rr = { NULL, 0, 0, 1 }; |
5b2fd956 SB |
551 | int fd; |
552 | ||
cb6020bb | 553 | fd = setup_rerere(&merge_rr, flags); |
5b2fd956 SB |
554 | if (fd < 0) |
555 | return 0; | |
556 | return do_plain_rerere(&merge_rr, fd); | |
557 | } | |
dea4562b JH |
558 | |
559 | static int rerere_forget_one_path(const char *path, struct string_list *rr) | |
560 | { | |
561 | const char *filename; | |
562 | char *hex; | |
563 | unsigned char sha1[20]; | |
564 | int ret; | |
565 | ||
566 | ret = handle_cache(path, sha1, NULL); | |
567 | if (ret < 1) | |
568 | return error("Could not parse conflict hunks in '%s'", path); | |
569 | hex = xstrdup(sha1_to_hex(sha1)); | |
570 | filename = rerere_path(hex, "postimage"); | |
571 | if (unlink(filename)) | |
572 | return (errno == ENOENT | |
573 | ? error("no remembered resolution for %s", path) | |
574 | : error("cannot unlink %s: %s", filename, strerror(errno))); | |
575 | ||
576 | handle_cache(path, sha1, rerere_path(hex, "preimage")); | |
577 | fprintf(stderr, "Updated preimage for '%s'\n", path); | |
578 | ||
579 | ||
580 | string_list_insert(path, rr)->util = hex; | |
581 | fprintf(stderr, "Forgot resolution for %s\n", path); | |
582 | return 0; | |
583 | } | |
584 | ||
585 | int rerere_forget(const char **pathspec) | |
586 | { | |
587 | int i, fd; | |
588 | struct string_list conflict = { NULL, 0, 0, 1 }; | |
589 | struct string_list merge_rr = { NULL, 0, 0, 1 }; | |
590 | ||
591 | if (read_cache() < 0) | |
592 | return error("Could not read index"); | |
593 | ||
6751e047 | 594 | fd = setup_rerere(&merge_rr, RERERE_NOAUTOUPDATE); |
dea4562b JH |
595 | |
596 | unmerge_cache(pathspec); | |
597 | find_conflict(&conflict); | |
598 | for (i = 0; i < conflict.nr; i++) { | |
599 | struct string_list_item *it = &conflict.items[i]; | |
600 | if (!match_pathspec(pathspec, it->string, strlen(it->string), | |
601 | 0, NULL)) | |
602 | continue; | |
603 | rerere_forget_one_path(it->string, &merge_rr); | |
604 | } | |
605 | return write_rr(&merge_rr, fd); | |
606 | } |