2 #include "string-list.h"
4 #include "xdiff-interface.h"
6 #include "resolve-undo.h"
13 #define THREE_STAGED 2
14 void *RERERE_RESOLVED = &RERERE_RESOLVED;
16 /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
17 static int rerere_enabled = -1;
19 /* automatically update cleanly resolved paths to the index */
20 static int rerere_autoupdate;
22 static char *merge_rr_path;
24 const char *rerere_path(const char *hex, const char *file)
26 return git_path("rr-cache/%s/%s", hex, file);
29 static int has_rerere_resolution(const char *hex)
32 return !stat(rerere_path(hex, "postimage"), &st);
35 static void read_rr(struct string_list *rr)
37 unsigned char sha1[20];
39 FILE *in = fopen(merge_rr_path, "r");
42 while (fread(buf, 40, 1, in) == 1) {
45 if (get_sha1_hex(buf, sha1))
46 die("corrupt MERGE_RR");
49 if (fgetc(in) != '\t')
50 die("corrupt MERGE_RR");
51 for (i = 0; i < sizeof(buf); i++) {
54 die("corrupt MERGE_RR");
60 die("filename too long");
61 string_list_insert(rr, buf)->util = name;
66 static struct lock_file write_lock;
68 static int write_rr(struct string_list *rr, int out_fd)
71 for (i = 0; i < rr->nr; i++) {
74 if (!rr->items[i].util)
76 path = rr->items[i].string;
77 length = strlen(path) + 1;
78 if (write_in_full(out_fd, rr->items[i].util, 40) != 40 ||
79 write_str_in_full(out_fd, "\t") != 1 ||
80 write_in_full(out_fd, path, length) != length)
81 die("unable to write rerere record");
83 if (commit_lock_file(&write_lock) != 0)
84 die("unable to write rerere record");
88 static void ferr_write(const void *p, size_t count, FILE *fp, int *err)
92 if (fwrite(p, count, 1, fp) != 1)
96 static inline void ferr_puts(const char *s, FILE *fp, int *err)
98 ferr_write(s, strlen(s), fp, err);
102 int (*getline)(struct strbuf *, struct rerere_io *);
105 /* some more stuff */
108 static void rerere_io_putstr(const char *str, struct rerere_io *io)
111 ferr_puts(str, io->output, &io->wrerror);
114 static void rerere_io_putconflict(int ch, int size, struct rerere_io *io)
119 if (size < sizeof(buf) - 2) {
120 memset(buf, ch, size);
122 buf[size + 1] = '\0';
125 int sz = sizeof(buf) - 1;
127 sz -= (sz - size) + 1;
132 rerere_io_putstr(buf, io);
136 static void rerere_io_putmem(const char *mem, size_t sz, struct rerere_io *io)
139 ferr_write(mem, sz, io->output, &io->wrerror);
142 struct rerere_io_file {
147 static int rerere_file_getline(struct strbuf *sb, struct rerere_io *io_)
149 struct rerere_io_file *io = (struct rerere_io_file *)io_;
150 return strbuf_getwholeline(sb, io->input, '\n');
153 static int is_cmarker(char *buf, int marker_char, int marker_size, int want_sp)
155 while (marker_size--)
156 if (*buf++ != marker_char)
158 if (want_sp && *buf != ' ')
160 return isspace(*buf);
163 static int handle_path(unsigned char *sha1, struct rerere_io *io, int marker_size)
168 RR_CONTEXT = 0, RR_SIDE_1, RR_SIDE_2, RR_ORIGINAL
170 struct strbuf one = STRBUF_INIT, two = STRBUF_INIT;
171 struct strbuf buf = STRBUF_INIT;
176 while (!io->getline(&buf, io)) {
177 if (is_cmarker(buf.buf, '<', marker_size, 1)) {
178 if (hunk != RR_CONTEXT)
181 } else if (is_cmarker(buf.buf, '|', marker_size, 0)) {
182 if (hunk != RR_SIDE_1)
185 } else if (is_cmarker(buf.buf, '=', marker_size, 0)) {
186 if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL)
189 } else if (is_cmarker(buf.buf, '>', marker_size, 1)) {
190 if (hunk != RR_SIDE_2)
192 if (strbuf_cmp(&one, &two) > 0)
193 strbuf_swap(&one, &two);
196 rerere_io_putconflict('<', marker_size, io);
197 rerere_io_putmem(one.buf, one.len, io);
198 rerere_io_putconflict('=', marker_size, io);
199 rerere_io_putmem(two.buf, two.len, io);
200 rerere_io_putconflict('>', marker_size, io);
202 git_SHA1_Update(&ctx, one.buf ? one.buf : "",
204 git_SHA1_Update(&ctx, two.buf ? two.buf : "",
209 } else if (hunk == RR_SIDE_1)
210 strbuf_addbuf(&one, &buf);
211 else if (hunk == RR_ORIGINAL)
213 else if (hunk == RR_SIDE_2)
214 strbuf_addbuf(&two, &buf);
216 rerere_io_putstr(buf.buf, io);
219 hunk = 99; /* force error exit */
222 strbuf_release(&one);
223 strbuf_release(&two);
224 strbuf_release(&buf);
227 git_SHA1_Final(sha1, &ctx);
228 if (hunk != RR_CONTEXT)
233 static int handle_file(const char *path, unsigned char *sha1, const char *output)
236 struct rerere_io_file io;
237 int marker_size = ll_merge_marker_size(path);
239 memset(&io, 0, sizeof(io));
240 io.io.getline = rerere_file_getline;
241 io.input = fopen(path, "r");
244 return error("Could not open %s", path);
247 io.io.output = fopen(output, "w");
250 return error("Could not write %s", output);
254 hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size);
258 error("There were errors while writing %s (%s)",
259 path, strerror(io.io.wrerror));
260 if (io.io.output && fclose(io.io.output))
261 io.io.wrerror = error("Failed to flush %s: %s",
262 path, strerror(errno));
266 unlink_or_warn(output);
267 return error("Could not parse conflict hunks in %s", path);
274 struct rerere_io_mem {
279 static int rerere_mem_getline(struct strbuf *sb, struct rerere_io *io_)
281 struct rerere_io_mem *io = (struct rerere_io_mem *)io_;
288 ep = memchr(io->input.buf, '\n', io->input.len);
290 ep = io->input.buf + io->input.len;
291 else if (*ep == '\n')
293 len = ep - io->input.buf;
294 strbuf_add(sb, io->input.buf, len);
295 strbuf_remove(&io->input, 0, len);
299 static int handle_cache(const char *path, unsigned char *sha1, const char *output)
301 mmfile_t mmfile[3] = {{NULL}};
302 mmbuffer_t result = {NULL, 0};
303 const struct cache_entry *ce;
304 int pos, len, i, hunk_no;
305 struct rerere_io_mem io;
306 int marker_size = ll_merge_marker_size(path);
309 * Reproduce the conflicted merge in-core
312 pos = cache_name_pos(path, len);
317 for (i = 0; i < 3; i++) {
318 enum object_type type;
322 if (active_nr <= pos)
324 ce = active_cache[pos++];
325 if (ce_namelen(ce) != len || memcmp(ce->name, path, len))
327 j = ce_stage(ce) - 1;
328 mmfile[j].ptr = read_sha1_file(ce->sha1, &type, &size);
329 mmfile[j].size = size;
331 for (i = 0; i < 3; i++) {
332 if (!mmfile[i].ptr && !mmfile[i].size)
333 mmfile[i].ptr = xstrdup("");
336 * NEEDSWORK: handle conflicts from merges with
337 * merge.renormalize set, too
339 ll_merge(&result, path, &mmfile[0], NULL,
341 &mmfile[2], "theirs", NULL);
342 for (i = 0; i < 3; i++)
345 memset(&io, 0, sizeof(io));
346 io.io.getline = rerere_mem_getline;
348 io.io.output = fopen(output, "w");
351 strbuf_init(&io.input, 0);
352 strbuf_attach(&io.input, result.ptr, result.size, result.size);
354 hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size);
355 strbuf_release(&io.input);
357 fclose(io.io.output);
361 static int check_one_conflict(int i, int *type)
363 const struct cache_entry *e = active_cache[i];
371 if (ce_stage(e) == 1) {
372 if (active_nr <= ++i)
376 /* Only handle regular files with both stages #2 and #3 */
377 if (i + 1 < active_nr) {
378 const struct cache_entry *e2 = active_cache[i];
379 const struct cache_entry *e3 = active_cache[i + 1];
380 if (ce_stage(e2) == 2 &&
382 ce_same_name(e, e3) &&
383 S_ISREG(e2->ce_mode) &&
384 S_ISREG(e3->ce_mode))
385 *type = THREE_STAGED;
388 /* Skip the entries with the same name */
389 while (i < active_nr && ce_same_name(e, active_cache[i]))
394 static int find_conflict(struct string_list *conflict)
397 if (read_cache() < 0)
398 return error("Could not read index");
400 for (i = 0; i < active_nr;) {
402 const struct cache_entry *e = active_cache[i];
403 i = check_one_conflict(i, &conflict_type);
404 if (conflict_type == THREE_STAGED)
405 string_list_insert(conflict, (const char *)e->name);
410 int rerere_remaining(struct string_list *merge_rr)
413 if (read_cache() < 0)
414 return error("Could not read index");
416 for (i = 0; i < active_nr;) {
418 const struct cache_entry *e = active_cache[i];
419 i = check_one_conflict(i, &conflict_type);
420 if (conflict_type == PUNTED)
421 string_list_insert(merge_rr, (const char *)e->name);
422 else if (conflict_type == RESOLVED) {
423 struct string_list_item *it;
424 it = string_list_lookup(merge_rr, (const char *)e->name);
427 it->util = RERERE_RESOLVED;
434 static int merge(const char *name, const char *path)
437 mmfile_t cur = {NULL, 0}, base = {NULL, 0}, other = {NULL, 0};
438 mmbuffer_t result = {NULL, 0};
440 if (handle_file(path, NULL, rerere_path(name, "thisimage")) < 0)
443 if (read_mmfile(&cur, rerere_path(name, "thisimage")) ||
444 read_mmfile(&base, rerere_path(name, "preimage")) ||
445 read_mmfile(&other, rerere_path(name, "postimage"))) {
449 ret = ll_merge(&result, path, &base, NULL, &cur, "", &other, "", NULL);
453 if (utime(rerere_path(name, "postimage"), NULL) < 0)
454 warning("failed utime() on %s: %s",
455 rerere_path(name, "postimage"),
457 f = fopen(path, "w");
459 return error("Could not open %s: %s", path,
461 if (fwrite(result.ptr, result.size, 1, f) != 1)
462 error("Could not write %s: %s", path, strerror(errno));
464 return error("Writing %s failed: %s", path,
477 static struct lock_file index_lock;
479 static void update_paths(struct string_list *update)
483 hold_locked_index(&index_lock, 1);
485 for (i = 0; i < update->nr; i++) {
486 struct string_list_item *item = &update->items[i];
487 if (add_file_to_cache(item->string, 0))
491 if (active_cache_changed) {
492 if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK))
493 die("Unable to write new index file");
495 rollback_lock_file(&index_lock);
498 static int do_plain_rerere(struct string_list *rr, int fd)
500 struct string_list conflict = STRING_LIST_INIT_DUP;
501 struct string_list update = STRING_LIST_INIT_DUP;
504 find_conflict(&conflict);
507 * MERGE_RR records paths with conflicts immediately after merge
508 * failed. Some of the conflicted paths might have been hand resolved
509 * in the working tree since then, but the initial run would catch all
510 * and register their preimages.
513 for (i = 0; i < conflict.nr; i++) {
514 const char *path = conflict.items[i].string;
515 if (!string_list_has_string(rr, path)) {
516 unsigned char sha1[20];
519 ret = handle_file(path, sha1, NULL);
522 hex = xstrdup(sha1_to_hex(sha1));
523 string_list_insert(rr, path)->util = hex;
524 if (mkdir_in_gitdir(git_path("rr-cache/%s", hex)))
526 handle_file(path, NULL, rerere_path(hex, "preimage"));
527 fprintf(stderr, "Recorded preimage for '%s'\n", path);
532 * Now some of the paths that had conflicts earlier might have been
533 * hand resolved. Others may be similar to a conflict already that
534 * was resolved before.
537 for (i = 0; i < rr->nr; i++) {
539 const char *path = rr->items[i].string;
540 const char *name = (const char *)rr->items[i].util;
542 if (has_rerere_resolution(name)) {
543 if (!merge(name, path)) {
545 if (rerere_autoupdate) {
546 string_list_insert(&update, path);
547 msg = "Staged '%s' using previous resolution.\n";
549 msg = "Resolved '%s' using previous resolution.\n";
550 fprintf(stderr, msg, path);
555 /* Let's see if we have resolved it. */
556 ret = handle_file(path, NULL, NULL);
560 fprintf(stderr, "Recorded resolution for '%s'.\n", path);
561 copy_file(rerere_path(name, "postimage"), path, 0666);
563 rr->items[i].util = NULL;
567 update_paths(&update);
569 return write_rr(rr, fd);
572 static int git_rerere_config(const char *var, const char *value, void *cb)
574 if (!strcmp(var, "rerere.enabled"))
575 rerere_enabled = git_config_bool(var, value);
576 else if (!strcmp(var, "rerere.autoupdate"))
577 rerere_autoupdate = git_config_bool(var, value);
579 return git_default_config(var, value, cb);
583 static int is_rerere_enabled(void)
585 const char *rr_cache;
591 rr_cache = git_path("rr-cache");
592 rr_cache_exists = is_directory(rr_cache);
593 if (rerere_enabled < 0)
594 return rr_cache_exists;
596 if (!rr_cache_exists && mkdir_in_gitdir(rr_cache))
597 die("Could not create directory %s", rr_cache);
601 int setup_rerere(struct string_list *merge_rr, int flags)
605 git_config(git_rerere_config, NULL);
606 if (!is_rerere_enabled())
609 if (flags & (RERERE_AUTOUPDATE|RERERE_NOAUTOUPDATE))
610 rerere_autoupdate = !!(flags & RERERE_AUTOUPDATE);
611 merge_rr_path = git_pathdup("MERGE_RR");
612 fd = hold_lock_file_for_update(&write_lock, merge_rr_path,
618 int rerere(int flags)
620 struct string_list merge_rr = STRING_LIST_INIT_DUP;
623 fd = setup_rerere(&merge_rr, flags);
626 return do_plain_rerere(&merge_rr, fd);
629 static int rerere_forget_one_path(const char *path, struct string_list *rr)
631 const char *filename;
633 unsigned char sha1[20];
636 ret = handle_cache(path, sha1, NULL);
638 return error("Could not parse conflict hunks in '%s'", path);
639 hex = xstrdup(sha1_to_hex(sha1));
640 filename = rerere_path(hex, "postimage");
641 if (unlink(filename))
642 return (errno == ENOENT
643 ? error("no remembered resolution for %s", path)
644 : error("cannot unlink %s: %s", filename, strerror(errno)));
646 handle_cache(path, sha1, rerere_path(hex, "preimage"));
647 fprintf(stderr, "Updated preimage for '%s'\n", path);
650 string_list_insert(rr, path)->util = hex;
651 fprintf(stderr, "Forgot resolution for %s\n", path);
655 int rerere_forget(struct pathspec *pathspec)
658 struct string_list conflict = STRING_LIST_INIT_DUP;
659 struct string_list merge_rr = STRING_LIST_INIT_DUP;
661 if (read_cache() < 0)
662 return error("Could not read index");
664 fd = setup_rerere(&merge_rr, RERERE_NOAUTOUPDATE);
666 unmerge_cache(pathspec);
667 find_conflict(&conflict);
668 for (i = 0; i < conflict.nr; i++) {
669 struct string_list_item *it = &conflict.items[i];
670 if (!match_pathspec(pathspec, it->string,
671 strlen(it->string), 0, NULL, 0))
673 rerere_forget_one_path(it->string, &merge_rr);
675 return write_rr(&merge_rr, fd);
678 static time_t rerere_created_at(const char *name)
681 return stat(rerere_path(name, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
684 static time_t rerere_last_used_at(const char *name)
687 return stat(rerere_path(name, "postimage"), &st) ? (time_t) 0 : st.st_mtime;
690 static void unlink_rr_item(const char *name)
692 unlink(rerere_path(name, "thisimage"));
693 unlink(rerere_path(name, "preimage"));
694 unlink(rerere_path(name, "postimage"));
695 rmdir(git_path("rr-cache/%s", name));
698 struct rerere_gc_config_cb {
699 int cutoff_noresolve;
703 static int git_rerere_gc_config(const char *var, const char *value, void *cb)
705 struct rerere_gc_config_cb *cf = cb;
707 if (!strcmp(var, "gc.rerereresolved"))
708 cf->cutoff_resolve = git_config_int(var, value);
709 else if (!strcmp(var, "gc.rerereunresolved"))
710 cf->cutoff_noresolve = git_config_int(var, value);
712 return git_default_config(var, value, cb);
716 void rerere_gc(struct string_list *rr)
718 struct string_list to_remove = STRING_LIST_INIT_DUP;
722 time_t now = time(NULL), then;
723 struct rerere_gc_config_cb cf = { 15, 60 };
725 git_config(git_rerere_gc_config, &cf);
726 dir = opendir(git_path("rr-cache"));
728 die_errno("unable to open rr-cache directory");
729 while ((e = readdir(dir))) {
730 if (is_dot_or_dotdot(e->d_name))
733 then = rerere_last_used_at(e->d_name);
735 cutoff = cf.cutoff_resolve;
737 then = rerere_created_at(e->d_name);
740 cutoff = cf.cutoff_noresolve;
742 if (then < now - cutoff * 86400)
743 string_list_append(&to_remove, e->d_name);
746 for (i = 0; i < to_remove.nr; i++)
747 unlink_rr_item(to_remove.items[i].string);
748 string_list_clear(&to_remove, 0);
751 void rerere_clear(struct string_list *merge_rr)
755 for (i = 0; i < merge_rr->nr; i++) {
756 const char *name = (const char *)merge_rr->items[i].util;
757 if (!has_rerere_resolution(name))
758 unlink_rr_item(name);
760 unlink_or_warn(git_path("MERGE_RR"));