2 #include "string-list.h"
4 #include "xdiff-interface.h"
6 #include "resolve-undo.h"
12 #define THREE_STAGED 2
13 void *RERERE_RESOLVED = &RERERE_RESOLVED;
15 /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
16 static int rerere_enabled = -1;
18 /* automatically update cleanly resolved paths to the index */
19 static int rerere_autoupdate;
21 static char *merge_rr_path;
23 const char *rerere_path(const char *hex, const char *file)
25 return git_path("rr-cache/%s/%s", hex, file);
28 int has_rerere_resolution(const char *hex)
31 return !stat(rerere_path(hex, "postimage"), &st);
34 static void read_rr(struct string_list *rr)
36 unsigned char sha1[20];
38 FILE *in = fopen(merge_rr_path, "r");
41 while (fread(buf, 40, 1, in) == 1) {
44 if (get_sha1_hex(buf, sha1))
45 die("corrupt MERGE_RR");
48 if (fgetc(in) != '\t')
49 die("corrupt MERGE_RR");
50 for (i = 0; i < sizeof(buf) && (buf[i] = fgetc(in)); i++)
53 die("filename too long");
54 string_list_insert(rr, buf)->util = name;
59 static struct lock_file write_lock;
61 static int write_rr(struct string_list *rr, int out_fd)
64 for (i = 0; i < rr->nr; i++) {
67 if (!rr->items[i].util)
69 path = rr->items[i].string;
70 length = strlen(path) + 1;
71 if (write_in_full(out_fd, rr->items[i].util, 40) != 40 ||
72 write_str_in_full(out_fd, "\t") != 1 ||
73 write_in_full(out_fd, path, length) != length)
74 die("unable to write rerere record");
76 if (commit_lock_file(&write_lock) != 0)
77 die("unable to write rerere record");
81 static void ferr_write(const void *p, size_t count, FILE *fp, int *err)
85 if (fwrite(p, count, 1, fp) != 1)
89 static inline void ferr_puts(const char *s, FILE *fp, int *err)
91 ferr_write(s, strlen(s), fp, err);
95 int (*getline)(struct strbuf *, struct rerere_io *);
101 static void rerere_io_putstr(const char *str, struct rerere_io *io)
104 ferr_puts(str, io->output, &io->wrerror);
107 static void rerere_io_putconflict(int ch, int size, struct rerere_io *io)
112 if (size < sizeof(buf) - 2) {
113 memset(buf, ch, size);
115 buf[size + 1] = '\0';
118 int sz = sizeof(buf) - 1;
120 sz -= (sz - size) + 1;
125 rerere_io_putstr(buf, io);
129 static void rerere_io_putmem(const char *mem, size_t sz, struct rerere_io *io)
132 ferr_write(mem, sz, io->output, &io->wrerror);
135 struct rerere_io_file {
140 static int rerere_file_getline(struct strbuf *sb, struct rerere_io *io_)
142 struct rerere_io_file *io = (struct rerere_io_file *)io_;
143 return strbuf_getwholeline(sb, io->input, '\n');
146 static int is_cmarker(char *buf, int marker_char, int marker_size, int want_sp)
148 while (marker_size--)
149 if (*buf++ != marker_char)
151 if (want_sp && *buf != ' ')
153 return isspace(*buf);
156 static int handle_path(unsigned char *sha1, struct rerere_io *io, int marker_size)
161 RR_CONTEXT = 0, RR_SIDE_1, RR_SIDE_2, RR_ORIGINAL
163 struct strbuf one = STRBUF_INIT, two = STRBUF_INIT;
164 struct strbuf buf = STRBUF_INIT;
169 while (!io->getline(&buf, io)) {
170 if (is_cmarker(buf.buf, '<', marker_size, 1)) {
171 if (hunk != RR_CONTEXT)
174 } else if (is_cmarker(buf.buf, '|', marker_size, 0)) {
175 if (hunk != RR_SIDE_1)
178 } else if (is_cmarker(buf.buf, '=', marker_size, 0)) {
179 if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL)
182 } else if (is_cmarker(buf.buf, '>', marker_size, 1)) {
183 if (hunk != RR_SIDE_2)
185 if (strbuf_cmp(&one, &two) > 0)
186 strbuf_swap(&one, &two);
189 rerere_io_putconflict('<', marker_size, io);
190 rerere_io_putmem(one.buf, one.len, io);
191 rerere_io_putconflict('=', marker_size, io);
192 rerere_io_putmem(two.buf, two.len, io);
193 rerere_io_putconflict('>', marker_size, io);
195 git_SHA1_Update(&ctx, one.buf ? one.buf : "",
197 git_SHA1_Update(&ctx, two.buf ? two.buf : "",
202 } else if (hunk == RR_SIDE_1)
203 strbuf_addstr(&one, buf.buf);
204 else if (hunk == RR_ORIGINAL)
206 else if (hunk == RR_SIDE_2)
207 strbuf_addstr(&two, buf.buf);
209 rerere_io_putstr(buf.buf, io);
212 hunk = 99; /* force error exit */
215 strbuf_release(&one);
216 strbuf_release(&two);
217 strbuf_release(&buf);
220 git_SHA1_Final(sha1, &ctx);
221 if (hunk != RR_CONTEXT)
226 static int handle_file(const char *path, unsigned char *sha1, const char *output)
229 struct rerere_io_file io;
230 int marker_size = ll_merge_marker_size(path);
232 memset(&io, 0, sizeof(io));
233 io.io.getline = rerere_file_getline;
234 io.input = fopen(path, "r");
237 return error("Could not open %s", path);
240 io.io.output = fopen(output, "w");
243 return error("Could not write %s", output);
247 hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size);
251 error("There were errors while writing %s (%s)",
252 path, strerror(io.io.wrerror));
253 if (io.io.output && fclose(io.io.output))
254 io.io.wrerror = error("Failed to flush %s: %s",
255 path, strerror(errno));
259 unlink_or_warn(output);
260 return error("Could not parse conflict hunks in %s", path);
267 struct rerere_io_mem {
272 static int rerere_mem_getline(struct strbuf *sb, struct rerere_io *io_)
274 struct rerere_io_mem *io = (struct rerere_io_mem *)io_;
281 ep = strchrnul(io->input.buf, '\n');
284 len = ep - io->input.buf;
285 strbuf_add(sb, io->input.buf, len);
286 strbuf_remove(&io->input, 0, len);
290 static int handle_cache(const char *path, unsigned char *sha1, const char *output)
293 mmbuffer_t result = {NULL, 0};
294 struct cache_entry *ce;
295 int pos, len, i, hunk_no;
296 struct rerere_io_mem io;
297 int marker_size = ll_merge_marker_size(path);
300 * Reproduce the conflicted merge in-core
303 pos = cache_name_pos(path, len);
308 for (i = 0; i < 3; i++) {
309 enum object_type type;
313 mmfile[i].ptr = NULL;
314 if (active_nr <= pos)
316 ce = active_cache[pos++];
317 if (ce_namelen(ce) != len || memcmp(ce->name, path, len)
318 || ce_stage(ce) != i + 1)
320 mmfile[i].ptr = read_sha1_file(ce->sha1, &type, &size);
321 mmfile[i].size = size;
323 for (i = 0; i < 3; i++) {
324 if (!mmfile[i].ptr && !mmfile[i].size)
325 mmfile[i].ptr = xstrdup("");
328 * NEEDSWORK: handle conflicts from merges with
329 * merge.renormalize set, too
331 ll_merge(&result, path, &mmfile[0], NULL,
333 &mmfile[2], "theirs", NULL);
334 for (i = 0; i < 3; i++)
337 memset(&io, 0, sizeof(io));
338 io.io.getline = rerere_mem_getline;
340 io.io.output = fopen(output, "w");
343 strbuf_init(&io.input, 0);
344 strbuf_attach(&io.input, result.ptr, result.size, result.size);
346 hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size);
347 strbuf_release(&io.input);
349 fclose(io.io.output);
353 static int check_one_conflict(int i, int *type)
355 struct cache_entry *e = active_cache[i];
363 if (ce_stage(e) == 1) {
364 if (active_nr <= ++i)
368 /* Only handle regular files with both stages #2 and #3 */
369 if (i + 1 < active_nr) {
370 struct cache_entry *e2 = active_cache[i];
371 struct cache_entry *e3 = active_cache[i + 1];
372 if (ce_stage(e2) == 2 &&
374 ce_same_name(e, e3) &&
375 S_ISREG(e2->ce_mode) &&
376 S_ISREG(e3->ce_mode))
377 *type = THREE_STAGED;
380 /* Skip the entries with the same name */
381 while (i < active_nr && ce_same_name(e, active_cache[i]))
386 static int find_conflict(struct string_list *conflict)
389 if (read_cache() < 0)
390 return error("Could not read index");
392 for (i = 0; i < active_nr;) {
394 struct cache_entry *e = active_cache[i];
395 i = check_one_conflict(i, &conflict_type);
396 if (conflict_type == THREE_STAGED)
397 string_list_insert(conflict, (const char *)e->name);
402 int rerere_remaining(struct string_list *merge_rr)
405 if (read_cache() < 0)
406 return error("Could not read index");
408 for (i = 0; i < active_nr;) {
410 struct cache_entry *e = active_cache[i];
411 i = check_one_conflict(i, &conflict_type);
412 if (conflict_type == PUNTED)
413 string_list_insert(merge_rr, (const char *)e->name);
414 else if (conflict_type == RESOLVED) {
415 struct string_list_item *it;
416 it = string_list_lookup(merge_rr, (const char *)e->name);
419 it->util = RERERE_RESOLVED;
426 static int merge(const char *name, const char *path)
429 mmfile_t cur = {NULL, 0}, base = {NULL, 0}, other = {NULL, 0};
430 mmbuffer_t result = {NULL, 0};
432 if (handle_file(path, NULL, rerere_path(name, "thisimage")) < 0)
435 if (read_mmfile(&cur, rerere_path(name, "thisimage")) ||
436 read_mmfile(&base, rerere_path(name, "preimage")) ||
437 read_mmfile(&other, rerere_path(name, "postimage"))) {
441 ret = ll_merge(&result, path, &base, NULL, &cur, "", &other, "", 0);
445 if (utime(rerere_path(name, "postimage"), NULL) < 0)
446 warning("failed utime() on %s: %s",
447 rerere_path(name, "postimage"),
449 f = fopen(path, "w");
451 return error("Could not open %s: %s", path,
453 if (fwrite(result.ptr, result.size, 1, f) != 1)
454 error("Could not write %s: %s", path, strerror(errno));
456 return error("Writing %s failed: %s", path,
469 static struct lock_file index_lock;
471 static int update_paths(struct string_list *update)
474 int fd = hold_locked_index(&index_lock, 0);
480 for (i = 0; i < update->nr; i++) {
481 struct string_list_item *item = &update->items[i];
482 if (add_file_to_cache(item->string, ADD_CACHE_IGNORE_ERRORS))
486 if (!status && active_cache_changed) {
487 if (write_cache(fd, active_cache, active_nr) ||
488 commit_locked_index(&index_lock))
489 die("Unable to write new index file");
491 rollback_lock_file(&index_lock);
495 static int do_plain_rerere(struct string_list *rr, int fd)
497 struct string_list conflict = STRING_LIST_INIT_DUP;
498 struct string_list update = STRING_LIST_INIT_DUP;
501 find_conflict(&conflict);
504 * MERGE_RR records paths with conflicts immediately after merge
505 * failed. Some of the conflicted paths might have been hand resolved
506 * in the working tree since then, but the initial run would catch all
507 * and register their preimages.
510 for (i = 0; i < conflict.nr; i++) {
511 const char *path = conflict.items[i].string;
512 if (!string_list_has_string(rr, path)) {
513 unsigned char sha1[20];
516 ret = handle_file(path, sha1, NULL);
519 hex = xstrdup(sha1_to_hex(sha1));
520 string_list_insert(rr, path)->util = hex;
521 if (mkdir(git_path("rr-cache/%s", hex), 0755))
523 handle_file(path, NULL, rerere_path(hex, "preimage"));
524 fprintf(stderr, "Recorded preimage for '%s'\n", path);
529 * Now some of the paths that had conflicts earlier might have been
530 * hand resolved. Others may be similar to a conflict already that
531 * was resolved before.
534 for (i = 0; i < rr->nr; i++) {
536 const char *path = rr->items[i].string;
537 const char *name = (const char *)rr->items[i].util;
539 if (has_rerere_resolution(name)) {
540 if (!merge(name, path)) {
541 if (rerere_autoupdate)
542 string_list_insert(&update, path);
544 "%s '%s' using previous resolution.\n",
546 ? "Staged" : "Resolved",
552 /* Let's see if we have resolved it. */
553 ret = handle_file(path, NULL, NULL);
557 fprintf(stderr, "Recorded resolution for '%s'.\n", path);
558 copy_file(rerere_path(name, "postimage"), path, 0666);
560 rr->items[i].util = NULL;
564 update_paths(&update);
566 return write_rr(rr, fd);
569 static int git_rerere_config(const char *var, const char *value, void *cb)
571 if (!strcmp(var, "rerere.enabled"))
572 rerere_enabled = git_config_bool(var, value);
573 else if (!strcmp(var, "rerere.autoupdate"))
574 rerere_autoupdate = git_config_bool(var, value);
576 return git_default_config(var, value, cb);
580 static int is_rerere_enabled(void)
582 const char *rr_cache;
588 rr_cache = git_path("rr-cache");
589 rr_cache_exists = is_directory(rr_cache);
590 if (rerere_enabled < 0)
591 return rr_cache_exists;
593 if (!rr_cache_exists && mkdir_in_gitdir(rr_cache))
594 die("Could not create directory %s", rr_cache);
598 int setup_rerere(struct string_list *merge_rr, int flags)
602 git_config(git_rerere_config, NULL);
603 if (!is_rerere_enabled())
606 if (flags & (RERERE_AUTOUPDATE|RERERE_NOAUTOUPDATE))
607 rerere_autoupdate = !!(flags & RERERE_AUTOUPDATE);
608 merge_rr_path = git_pathdup("MERGE_RR");
609 fd = hold_lock_file_for_update(&write_lock, merge_rr_path,
615 int rerere(int flags)
617 struct string_list merge_rr = STRING_LIST_INIT_DUP;
620 fd = setup_rerere(&merge_rr, flags);
623 return do_plain_rerere(&merge_rr, fd);
626 static int rerere_forget_one_path(const char *path, struct string_list *rr)
628 const char *filename;
630 unsigned char sha1[20];
633 ret = handle_cache(path, sha1, NULL);
635 return error("Could not parse conflict hunks in '%s'", path);
636 hex = xstrdup(sha1_to_hex(sha1));
637 filename = rerere_path(hex, "postimage");
638 if (unlink(filename))
639 return (errno == ENOENT
640 ? error("no remembered resolution for %s", path)
641 : error("cannot unlink %s: %s", filename, strerror(errno)));
643 handle_cache(path, sha1, rerere_path(hex, "preimage"));
644 fprintf(stderr, "Updated preimage for '%s'\n", path);
647 string_list_insert(rr, path)->util = hex;
648 fprintf(stderr, "Forgot resolution for %s\n", path);
652 int rerere_forget(const char **pathspec)
655 struct string_list conflict = STRING_LIST_INIT_DUP;
656 struct string_list merge_rr = STRING_LIST_INIT_DUP;
658 if (read_cache() < 0)
659 return error("Could not read index");
661 fd = setup_rerere(&merge_rr, RERERE_NOAUTOUPDATE);
663 unmerge_cache(pathspec);
664 find_conflict(&conflict);
665 for (i = 0; i < conflict.nr; i++) {
666 struct string_list_item *it = &conflict.items[i];
667 if (!match_pathspec(pathspec, it->string, strlen(it->string),
670 rerere_forget_one_path(it->string, &merge_rr);
672 return write_rr(&merge_rr, fd);