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