2 #include "string-list.h"
4 #include "xdiff/xdiff.h"
5 #include "xdiff-interface.h"
7 #include "resolve-undo.h"
10 /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
11 static int rerere_enabled = -1;
13 /* automatically update cleanly resolved paths to the index */
14 static int rerere_autoupdate;
16 static char *merge_rr_path;
18 const char *rerere_path(const char *hex, const char *file)
20 return git_path("rr-cache/%s/%s", hex, file);
23 int has_rerere_resolution(const char *hex)
26 return !stat(rerere_path(hex, "postimage"), &st);
29 static void read_rr(struct string_list *rr)
31 unsigned char sha1[20];
33 FILE *in = fopen(merge_rr_path, "r");
36 while (fread(buf, 40, 1, in) == 1) {
39 if (get_sha1_hex(buf, sha1))
40 die("corrupt MERGE_RR");
43 if (fgetc(in) != '\t')
44 die("corrupt MERGE_RR");
45 for (i = 0; i < sizeof(buf) && (buf[i] = fgetc(in)); i++)
48 die("filename too long");
49 string_list_insert(buf, rr)->util = name;
54 static struct lock_file write_lock;
56 static int write_rr(struct string_list *rr, int out_fd)
59 for (i = 0; i < rr->nr; i++) {
62 if (!rr->items[i].util)
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");
71 if (commit_lock_file(&write_lock) != 0)
72 die("unable to write rerere record");
76 static void ferr_write(const void *p, size_t count, FILE *fp, int *err)
80 if (fwrite(p, count, 1, fp) != 1)
84 static inline void ferr_puts(const char *s, FILE *fp, int *err)
86 ferr_write(s, strlen(s), fp, err);
90 int (*getline)(struct strbuf *, struct rerere_io *);
96 static void rerere_io_putstr(const char *str, struct rerere_io *io)
99 ferr_puts(str, io->output, &io->wrerror);
102 static void rerere_io_putmem(const char *mem, size_t sz, struct rerere_io *io)
105 ferr_write(mem, sz, io->output, &io->wrerror);
108 struct rerere_io_file {
113 static int rerere_file_getline(struct strbuf *sb, struct rerere_io *io_)
115 struct rerere_io_file *io = (struct rerere_io_file *)io_;
116 return strbuf_getwholeline(sb, io->input, '\n');
119 static int handle_path(unsigned char *sha1, struct rerere_io *io)
124 RR_CONTEXT = 0, RR_SIDE_1, RR_SIDE_2, RR_ORIGINAL,
126 struct strbuf one = STRBUF_INIT, two = STRBUF_INIT;
127 struct strbuf buf = STRBUF_INIT;
132 while (!io->getline(&buf, io)) {
133 if (!prefixcmp(buf.buf, "<<<<<<< ")) {
134 if (hunk != RR_CONTEXT)
137 } else if (!prefixcmp(buf.buf, "|||||||") && isspace(buf.buf[7])) {
138 if (hunk != RR_SIDE_1)
141 } else if (!prefixcmp(buf.buf, "=======") && isspace(buf.buf[7])) {
142 if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL)
145 } else if (!prefixcmp(buf.buf, ">>>>>>> ")) {
146 if (hunk != RR_SIDE_2)
148 if (strbuf_cmp(&one, &two) > 0)
149 strbuf_swap(&one, &two);
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);
158 git_SHA1_Update(&ctx, one.buf ? one.buf : "",
160 git_SHA1_Update(&ctx, two.buf ? two.buf : "",
165 } else if (hunk == RR_SIDE_1)
166 strbuf_addstr(&one, buf.buf);
167 else if (hunk == RR_ORIGINAL)
169 else if (hunk == RR_SIDE_2)
170 strbuf_addstr(&two, buf.buf);
172 rerere_io_putstr(buf.buf, io);
175 hunk = 99; /* force error exit */
178 strbuf_release(&one);
179 strbuf_release(&two);
180 strbuf_release(&buf);
183 git_SHA1_Final(sha1, &ctx);
184 if (hunk != RR_CONTEXT)
189 static int handle_file(const char *path, unsigned char *sha1, const char *output)
192 struct rerere_io_file io;
194 memset(&io, 0, sizeof(io));
195 io.io.getline = rerere_file_getline;
196 io.input = fopen(path, "r");
199 return error("Could not open %s", path);
202 io.io.output = fopen(output, "w");
205 return error("Could not write %s", output);
209 hunk_no = handle_path(sha1, (struct rerere_io *)&io);
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));
221 unlink_or_warn(output);
222 return error("Could not parse conflict hunks in %s", path);
229 struct rerere_io_mem {
234 static int rerere_mem_getline(struct strbuf *sb, struct rerere_io *io_)
236 struct rerere_io_mem *io = (struct rerere_io_mem *)io_;
243 ep = strchrnul(io->input.buf, '\n');
246 len = ep - io->input.buf;
247 strbuf_add(sb, io->input.buf, len);
248 strbuf_remove(&io->input, 0, len);
252 static int handle_cache(const char *path, unsigned char *sha1, const char *output)
255 mmbuffer_t result = {NULL, 0};
256 struct cache_entry *ce;
257 int pos, len, i, hunk_no;
258 struct rerere_io_mem io;
261 * Reproduce the conflicted merge in-core
264 pos = cache_name_pos(path, len);
269 for (i = 0; i < 3; i++) {
270 enum object_type type;
274 mmfile[i].ptr = NULL;
275 if (active_nr <= pos)
277 ce = active_cache[pos++];
278 if (ce_namelen(ce) != len || memcmp(ce->name, path, len)
279 || ce_stage(ce) != i + 1)
281 mmfile[i].ptr = read_sha1_file(ce->sha1, &type, &size);
282 mmfile[i].size = size;
284 for (i = 0; i < 3; i++) {
285 if (!mmfile[i].ptr && !mmfile[i].size)
286 mmfile[i].ptr = xstrdup("");
288 ll_merge(&result, path, &mmfile[0],
290 &mmfile[2], "theirs", 0);
291 for (i = 0; i < 3; i++)
294 memset(&io, 0, sizeof(&io));
295 io.io.getline = rerere_mem_getline;
297 io.io.output = fopen(output, "w");
300 strbuf_init(&io.input, 0);
301 strbuf_attach(&io.input, result.ptr, result.size, result.size);
303 hunk_no = handle_path(sha1, (struct rerere_io *)&io);
304 strbuf_release(&io.input);
306 fclose(io.io.output);
310 static int find_conflict(struct string_list *conflict)
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 &&
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 */
330 static int merge(const char *name, const char *path)
333 mmfile_t cur, base, other;
334 mmbuffer_t result = {NULL, 0};
335 xmparam_t xmp = {{XDF_NEED_MINIMAL}};
337 if (handle_file(path, NULL, rerere_path(name, "thisimage")) < 0)
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")))
344 ret = xdl_merge(&base, &cur, "", &other, "",
345 &xmp, XDL_MERGE_ZEALOUS, &result);
347 FILE *f = fopen(path, "w");
349 return error("Could not open %s: %s", path,
351 if (fwrite(result.ptr, result.size, 1, f) != 1)
352 error("Could not write %s: %s", path, strerror(errno));
354 return error("Writing %s failed: %s", path,
366 static struct lock_file index_lock;
368 static int update_paths(struct string_list *update)
371 int fd = hold_locked_index(&index_lock, 0);
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))
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");
388 rollback_lock_file(&index_lock);
392 static int do_plain_rerere(struct string_list *rr, int fd)
394 struct string_list conflict = { NULL, 0, 0, 1 };
395 struct string_list update = { NULL, 0, 0, 1 };
398 find_conflict(&conflict);
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.
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];
413 ret = handle_file(path, sha1, NULL);
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))
420 handle_file(path, NULL, rerere_path(hex, "preimage"));
421 fprintf(stderr, "Recorded preimage for '%s'\n", path);
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.
431 for (i = 0; i < rr->nr; i++) {
433 const char *path = rr->items[i].string;
434 const char *name = (const char *)rr->items[i].util;
436 if (has_rerere_resolution(name)) {
437 if (!merge(name, path)) {
438 if (rerere_autoupdate)
439 string_list_insert(path, &update);
441 "%s '%s' using previous resolution.\n",
443 ? "Staged" : "Resolved",
449 /* Let's see if we have resolved it. */
450 ret = handle_file(path, NULL, NULL);
454 fprintf(stderr, "Recorded resolution for '%s'.\n", path);
455 copy_file(rerere_path(name, "postimage"), path, 0666);
457 rr->items[i].util = NULL;
461 update_paths(&update);
463 return write_rr(rr, fd);
466 static int git_rerere_config(const char *var, const char *value, void *cb)
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);
473 return git_default_config(var, value, cb);
477 static int is_rerere_enabled(void)
479 const char *rr_cache;
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;
490 if (!rr_cache_exists &&
491 (mkdir(rr_cache, 0777) || adjust_shared_perm(rr_cache)))
492 die("Could not create directory %s", rr_cache);
496 int setup_rerere(struct string_list *merge_rr)
500 git_config(git_rerere_config, NULL);
501 if (!is_rerere_enabled())
504 merge_rr_path = git_pathdup("MERGE_RR");
505 fd = hold_lock_file_for_update(&write_lock, merge_rr_path,
513 struct string_list merge_rr = { NULL, 0, 0, 1 };
516 fd = setup_rerere(&merge_rr);
519 return do_plain_rerere(&merge_rr, fd);
522 static int rerere_forget_one_path(const char *path, struct string_list *rr)
524 const char *filename;
526 unsigned char sha1[20];
529 ret = handle_cache(path, sha1, NULL);
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)));
539 handle_cache(path, sha1, rerere_path(hex, "preimage"));
540 fprintf(stderr, "Updated preimage for '%s'\n", path);
543 string_list_insert(path, rr)->util = hex;
544 fprintf(stderr, "Forgot resolution for %s\n", path);
548 int rerere_forget(const char **pathspec)
551 struct string_list conflict = { NULL, 0, 0, 1 };
552 struct string_list merge_rr = { NULL, 0, 0, 1 };
554 if (read_cache() < 0)
555 return error("Could not read index");
557 fd = setup_rerere(&merge_rr);
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),
566 rerere_forget_one_path(it->string, &merge_rr);
568 return write_rr(&merge_rr, fd);