3 #include "string-list.h"
5 #include "xdiff-interface.h"
7 #include "resolve-undo.h"
14 #define THREE_STAGED 2
15 void *RERERE_RESOLVED = &RERERE_RESOLVED;
17 /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
18 static int rerere_enabled = -1;
20 /* automatically update cleanly resolved paths to the index */
21 static int rerere_autoupdate;
23 static char *merge_rr_path;
25 const char *rerere_path(const char *hex, const char *file)
27 return git_path("rr-cache/%s/%s", hex, file);
30 static int has_rerere_resolution(const char *hex)
33 return !stat(rerere_path(hex, "postimage"), &st);
36 static void read_rr(struct string_list *rr)
38 unsigned char sha1[20];
40 FILE *in = fopen(merge_rr_path, "r");
43 while (fread(buf, 40, 1, in) == 1) {
46 if (get_sha1_hex(buf, sha1))
47 die("corrupt MERGE_RR");
50 if (fgetc(in) != '\t')
51 die("corrupt MERGE_RR");
52 for (i = 0; i < sizeof(buf); i++) {
55 die("corrupt MERGE_RR");
61 die("filename too long");
62 string_list_insert(rr, buf)->util = name;
67 static struct lock_file write_lock;
69 static int write_rr(struct string_list *rr, int out_fd)
72 for (i = 0; i < rr->nr; i++) {
75 if (!rr->items[i].util)
77 path = rr->items[i].string;
78 length = strlen(path) + 1;
79 if (write_in_full(out_fd, rr->items[i].util, 40) != 40 ||
80 write_str_in_full(out_fd, "\t") != 1 ||
81 write_in_full(out_fd, path, length) != length)
82 die("unable to write rerere record");
84 if (commit_lock_file(&write_lock) != 0)
85 die("unable to write rerere record");
89 static void ferr_write(const void *p, size_t count, FILE *fp, int *err)
93 if (fwrite(p, count, 1, fp) != 1)
97 static inline void ferr_puts(const char *s, FILE *fp, int *err)
99 ferr_write(s, strlen(s), fp, err);
103 int (*getline)(struct strbuf *, struct rerere_io *);
106 /* some more stuff */
109 static void rerere_io_putstr(const char *str, struct rerere_io *io)
112 ferr_puts(str, io->output, &io->wrerror);
115 static void rerere_io_putconflict(int ch, int size, struct rerere_io *io)
120 if (size < sizeof(buf) - 2) {
121 memset(buf, ch, size);
123 buf[size + 1] = '\0';
126 int sz = sizeof(buf) - 1;
128 sz -= (sz - size) + 1;
133 rerere_io_putstr(buf, io);
137 static void rerere_io_putmem(const char *mem, size_t sz, struct rerere_io *io)
140 ferr_write(mem, sz, io->output, &io->wrerror);
143 struct rerere_io_file {
148 static int rerere_file_getline(struct strbuf *sb, struct rerere_io *io_)
150 struct rerere_io_file *io = (struct rerere_io_file *)io_;
151 return strbuf_getwholeline(sb, io->input, '\n');
154 static int is_cmarker(char *buf, int marker_char, int marker_size, int want_sp)
156 while (marker_size--)
157 if (*buf++ != marker_char)
159 if (want_sp && *buf != ' ')
161 return isspace(*buf);
164 static int handle_path(unsigned char *sha1, struct rerere_io *io, int marker_size)
169 RR_CONTEXT = 0, RR_SIDE_1, RR_SIDE_2, RR_ORIGINAL
171 struct strbuf one = STRBUF_INIT, two = STRBUF_INIT;
172 struct strbuf buf = STRBUF_INIT;
177 while (!io->getline(&buf, io)) {
178 if (is_cmarker(buf.buf, '<', marker_size, 1)) {
179 if (hunk != RR_CONTEXT)
182 } else if (is_cmarker(buf.buf, '|', marker_size, 0)) {
183 if (hunk != RR_SIDE_1)
186 } else if (is_cmarker(buf.buf, '=', marker_size, 0)) {
187 if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL)
190 } else if (is_cmarker(buf.buf, '>', marker_size, 1)) {
191 if (hunk != RR_SIDE_2)
193 if (strbuf_cmp(&one, &two) > 0)
194 strbuf_swap(&one, &two);
197 rerere_io_putconflict('<', marker_size, io);
198 rerere_io_putmem(one.buf, one.len, io);
199 rerere_io_putconflict('=', marker_size, io);
200 rerere_io_putmem(two.buf, two.len, io);
201 rerere_io_putconflict('>', marker_size, io);
203 git_SHA1_Update(&ctx, one.buf ? one.buf : "",
205 git_SHA1_Update(&ctx, two.buf ? two.buf : "",
210 } else if (hunk == RR_SIDE_1)
211 strbuf_addbuf(&one, &buf);
212 else if (hunk == RR_ORIGINAL)
214 else if (hunk == RR_SIDE_2)
215 strbuf_addbuf(&two, &buf);
217 rerere_io_putstr(buf.buf, io);
220 hunk = 99; /* force error exit */
223 strbuf_release(&one);
224 strbuf_release(&two);
225 strbuf_release(&buf);
228 git_SHA1_Final(sha1, &ctx);
229 if (hunk != RR_CONTEXT)
234 static int handle_file(const char *path, unsigned char *sha1, const char *output)
237 struct rerere_io_file io;
238 int marker_size = ll_merge_marker_size(path);
240 memset(&io, 0, sizeof(io));
241 io.io.getline = rerere_file_getline;
242 io.input = fopen(path, "r");
245 return error("Could not open %s", path);
248 io.io.output = fopen(output, "w");
251 return error("Could not write %s", output);
255 hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size);
259 error("There were errors while writing %s (%s)",
260 path, strerror(io.io.wrerror));
261 if (io.io.output && fclose(io.io.output))
262 io.io.wrerror = error("Failed to flush %s: %s",
263 path, strerror(errno));
267 unlink_or_warn(output);
268 return error("Could not parse conflict hunks in %s", path);
275 struct rerere_io_mem {
280 static int rerere_mem_getline(struct strbuf *sb, struct rerere_io *io_)
282 struct rerere_io_mem *io = (struct rerere_io_mem *)io_;
289 ep = memchr(io->input.buf, '\n', io->input.len);
291 ep = io->input.buf + io->input.len;
292 else if (*ep == '\n')
294 len = ep - io->input.buf;
295 strbuf_add(sb, io->input.buf, len);
296 strbuf_remove(&io->input, 0, len);
300 static int handle_cache(const char *path, unsigned char *sha1, const char *output)
302 mmfile_t mmfile[3] = {{NULL}};
303 mmbuffer_t result = {NULL, 0};
304 const struct cache_entry *ce;
305 int pos, len, i, hunk_no;
306 struct rerere_io_mem io;
307 int marker_size = ll_merge_marker_size(path);
310 * Reproduce the conflicted merge in-core
313 pos = cache_name_pos(path, len);
318 for (i = 0; i < 3; i++) {
319 enum object_type type;
323 if (active_nr <= pos)
325 ce = active_cache[pos++];
326 if (ce_namelen(ce) != len || memcmp(ce->name, path, len))
328 j = ce_stage(ce) - 1;
329 mmfile[j].ptr = read_sha1_file(ce->sha1, &type, &size);
330 mmfile[j].size = size;
332 for (i = 0; i < 3; i++) {
333 if (!mmfile[i].ptr && !mmfile[i].size)
334 mmfile[i].ptr = xstrdup("");
337 * NEEDSWORK: handle conflicts from merges with
338 * merge.renormalize set, too
340 ll_merge(&result, path, &mmfile[0], NULL,
342 &mmfile[2], "theirs", NULL);
343 for (i = 0; i < 3; i++)
346 memset(&io, 0, sizeof(io));
347 io.io.getline = rerere_mem_getline;
349 io.io.output = fopen(output, "w");
352 strbuf_init(&io.input, 0);
353 strbuf_attach(&io.input, result.ptr, result.size, result.size);
355 hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size);
356 strbuf_release(&io.input);
358 fclose(io.io.output);
362 static int check_one_conflict(int i, int *type)
364 const struct cache_entry *e = active_cache[i];
372 if (ce_stage(e) == 1) {
373 if (active_nr <= ++i)
377 /* Only handle regular files with both stages #2 and #3 */
378 if (i + 1 < active_nr) {
379 const struct cache_entry *e2 = active_cache[i];
380 const struct cache_entry *e3 = active_cache[i + 1];
381 if (ce_stage(e2) == 2 &&
383 ce_same_name(e, e3) &&
384 S_ISREG(e2->ce_mode) &&
385 S_ISREG(e3->ce_mode))
386 *type = THREE_STAGED;
389 /* Skip the entries with the same name */
390 while (i < active_nr && ce_same_name(e, active_cache[i]))
395 static int find_conflict(struct string_list *conflict)
398 if (read_cache() < 0)
399 return error("Could not read index");
401 for (i = 0; i < active_nr;) {
403 const struct cache_entry *e = active_cache[i];
404 i = check_one_conflict(i, &conflict_type);
405 if (conflict_type == THREE_STAGED)
406 string_list_insert(conflict, (const char *)e->name);
411 int rerere_remaining(struct string_list *merge_rr)
414 if (read_cache() < 0)
415 return error("Could not read index");
417 for (i = 0; i < active_nr;) {
419 const struct cache_entry *e = active_cache[i];
420 i = check_one_conflict(i, &conflict_type);
421 if (conflict_type == PUNTED)
422 string_list_insert(merge_rr, (const char *)e->name);
423 else if (conflict_type == RESOLVED) {
424 struct string_list_item *it;
425 it = string_list_lookup(merge_rr, (const char *)e->name);
428 it->util = RERERE_RESOLVED;
435 static int merge(const char *name, const char *path)
438 mmfile_t cur = {NULL, 0}, base = {NULL, 0}, other = {NULL, 0};
439 mmbuffer_t result = {NULL, 0};
441 if (handle_file(path, NULL, rerere_path(name, "thisimage")) < 0)
444 if (read_mmfile(&cur, rerere_path(name, "thisimage")) ||
445 read_mmfile(&base, rerere_path(name, "preimage")) ||
446 read_mmfile(&other, rerere_path(name, "postimage"))) {
450 ret = ll_merge(&result, path, &base, NULL, &cur, "", &other, "", NULL);
454 if (utime(rerere_path(name, "postimage"), NULL) < 0)
455 warning("failed utime() on %s: %s",
456 rerere_path(name, "postimage"),
458 f = fopen(path, "w");
460 return error("Could not open %s: %s", path,
462 if (fwrite(result.ptr, result.size, 1, f) != 1)
463 error("Could not write %s: %s", path, strerror(errno));
465 return error("Writing %s failed: %s", path,
478 static struct lock_file index_lock;
480 static int update_paths(struct string_list *update)
483 int fd = hold_locked_index(&index_lock, 0);
489 for (i = 0; i < update->nr; i++) {
490 struct string_list_item *item = &update->items[i];
491 if (add_file_to_cache(item->string, ADD_CACHE_IGNORE_ERRORS))
495 if (!status && active_cache_changed) {
496 if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK))
497 die("Unable to write new index file");
499 rollback_lock_file(&index_lock);
503 static int do_plain_rerere(struct string_list *rr, int fd)
505 struct string_list conflict = STRING_LIST_INIT_DUP;
506 struct string_list update = STRING_LIST_INIT_DUP;
509 find_conflict(&conflict);
512 * MERGE_RR records paths with conflicts immediately after merge
513 * failed. Some of the conflicted paths might have been hand resolved
514 * in the working tree since then, but the initial run would catch all
515 * and register their preimages.
518 for (i = 0; i < conflict.nr; i++) {
519 const char *path = conflict.items[i].string;
520 if (!string_list_has_string(rr, path)) {
521 unsigned char sha1[20];
524 ret = handle_file(path, sha1, NULL);
527 hex = xstrdup(sha1_to_hex(sha1));
528 string_list_insert(rr, path)->util = hex;
529 if (mkdir_in_gitdir(git_path("rr-cache/%s", hex)))
531 handle_file(path, NULL, rerere_path(hex, "preimage"));
532 fprintf(stderr, "Recorded preimage for '%s'\n", path);
537 * Now some of the paths that had conflicts earlier might have been
538 * hand resolved. Others may be similar to a conflict already that
539 * was resolved before.
542 for (i = 0; i < rr->nr; i++) {
544 const char *path = rr->items[i].string;
545 const char *name = (const char *)rr->items[i].util;
547 if (has_rerere_resolution(name)) {
548 if (!merge(name, path)) {
550 if (rerere_autoupdate) {
551 string_list_insert(&update, path);
552 msg = "Staged '%s' using previous resolution.\n";
554 msg = "Resolved '%s' using previous resolution.\n";
555 fprintf(stderr, msg, path);
560 /* Let's see if we have resolved it. */
561 ret = handle_file(path, NULL, NULL);
565 fprintf(stderr, "Recorded resolution for '%s'.\n", path);
566 copy_file(rerere_path(name, "postimage"), path, 0666);
568 rr->items[i].util = NULL;
572 update_paths(&update);
574 return write_rr(rr, fd);
577 static void git_rerere_config(void)
579 git_config_get_bool("rerere.enabled", &rerere_enabled);
580 git_config_get_bool("rerere.autoupdate", &rerere_autoupdate);
581 git_config(git_default_config, NULL);
584 static int is_rerere_enabled(void)
586 const char *rr_cache;
592 rr_cache = git_path("rr-cache");
593 rr_cache_exists = is_directory(rr_cache);
594 if (rerere_enabled < 0)
595 return rr_cache_exists;
597 if (!rr_cache_exists && mkdir_in_gitdir(rr_cache))
598 die("Could not create directory %s", rr_cache);
602 int setup_rerere(struct string_list *merge_rr, int flags)
607 if (!is_rerere_enabled())
610 if (flags & (RERERE_AUTOUPDATE|RERERE_NOAUTOUPDATE))
611 rerere_autoupdate = !!(flags & RERERE_AUTOUPDATE);
612 merge_rr_path = git_pathdup("MERGE_RR");
613 fd = hold_lock_file_for_update(&write_lock, merge_rr_path,
619 int rerere(int flags)
621 struct string_list merge_rr = STRING_LIST_INIT_DUP;
624 fd = setup_rerere(&merge_rr, flags);
627 return do_plain_rerere(&merge_rr, fd);
630 static int rerere_forget_one_path(const char *path, struct string_list *rr)
632 const char *filename;
634 unsigned char sha1[20];
637 ret = handle_cache(path, sha1, NULL);
639 return error("Could not parse conflict hunks in '%s'", path);
640 hex = xstrdup(sha1_to_hex(sha1));
641 filename = rerere_path(hex, "postimage");
642 if (unlink(filename))
643 return (errno == ENOENT
644 ? error("no remembered resolution for %s", path)
645 : error("cannot unlink %s: %s", filename, strerror(errno)));
647 handle_cache(path, sha1, rerere_path(hex, "preimage"));
648 fprintf(stderr, "Updated preimage for '%s'\n", path);
651 string_list_insert(rr, path)->util = hex;
652 fprintf(stderr, "Forgot resolution for %s\n", path);
656 int rerere_forget(struct pathspec *pathspec)
659 struct string_list conflict = STRING_LIST_INIT_DUP;
660 struct string_list merge_rr = STRING_LIST_INIT_DUP;
662 if (read_cache() < 0)
663 return error("Could not read index");
665 fd = setup_rerere(&merge_rr, RERERE_NOAUTOUPDATE);
667 unmerge_cache(pathspec);
668 find_conflict(&conflict);
669 for (i = 0; i < conflict.nr; i++) {
670 struct string_list_item *it = &conflict.items[i];
671 if (!match_pathspec(pathspec, it->string,
672 strlen(it->string), 0, NULL, 0))
674 rerere_forget_one_path(it->string, &merge_rr);
676 return write_rr(&merge_rr, fd);
679 static time_t rerere_created_at(const char *name)
682 return stat(rerere_path(name, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
685 static time_t rerere_last_used_at(const char *name)
688 return stat(rerere_path(name, "postimage"), &st) ? (time_t) 0 : st.st_mtime;
691 static void unlink_rr_item(const char *name)
693 unlink(rerere_path(name, "thisimage"));
694 unlink(rerere_path(name, "preimage"));
695 unlink(rerere_path(name, "postimage"));
696 rmdir(git_path("rr-cache/%s", name));
699 void rerere_gc(struct string_list *rr)
701 struct string_list to_remove = STRING_LIST_INIT_DUP;
705 time_t now = time(NULL), then;
706 int cutoff_noresolve = 15;
707 int cutoff_resolve = 60;
709 git_config_get_int("gc.rerereresolved", &cutoff_resolve);
710 git_config_get_int("gc.rerereunresolved", &cutoff_noresolve);
711 git_config(git_default_config, NULL);
712 dir = opendir(git_path("rr-cache"));
714 die_errno("unable to open rr-cache directory");
715 while ((e = readdir(dir))) {
716 if (is_dot_or_dotdot(e->d_name))
719 then = rerere_last_used_at(e->d_name);
721 cutoff = cutoff_resolve;
723 then = rerere_created_at(e->d_name);
726 cutoff = cutoff_noresolve;
728 if (then < now - cutoff * 86400)
729 string_list_append(&to_remove, e->d_name);
732 for (i = 0; i < to_remove.nr; i++)
733 unlink_rr_item(to_remove.items[i].string);
734 string_list_clear(&to_remove, 0);
737 void rerere_clear(struct string_list *merge_rr)
741 for (i = 0; i < merge_rr->nr; i++) {
742 const char *name = (const char *)merge_rr->items[i].util;
743 if (!has_rerere_resolution(name))
744 unlink_rr_item(name);
746 unlink_or_warn(git_path("MERGE_RR"));