The sixteenth batch
[git/gitster.git] / rerere.c
blob8bb97c98229bdd5694f0a43523f1b5786dd5f676
1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
4 #include "git-compat-util.h"
5 #include "abspath.h"
6 #include "config.h"
7 #include "copy.h"
8 #include "gettext.h"
9 #include "hex.h"
10 #include "lockfile.h"
11 #include "string-list.h"
12 #include "read-cache-ll.h"
13 #include "rerere.h"
14 #include "xdiff-interface.h"
15 #include "dir.h"
16 #include "resolve-undo.h"
17 #include "merge-ll.h"
18 #include "path.h"
19 #include "pathspec.h"
20 #include "object-file.h"
21 #include "odb.h"
22 #include "strmap.h"
24 #define RESOLVED 0
25 #define PUNTED 1
26 #define THREE_STAGED 2
27 void *RERERE_RESOLVED = &RERERE_RESOLVED;
29 /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
30 static int rerere_enabled = -1;
32 /* automatically update cleanly resolved paths to the index */
33 static int rerere_autoupdate;
35 #define RR_HAS_POSTIMAGE 1
36 #define RR_HAS_PREIMAGE 2
37 struct rerere_dir {
38 int status_alloc, status_nr;
39 unsigned char *status;
40 char name[FLEX_ARRAY];
43 static struct strmap rerere_dirs = STRMAP_INIT;
45 static void free_rerere_dirs(void)
47 struct hashmap_iter iter;
48 struct strmap_entry *ent;
50 strmap_for_each_entry(&rerere_dirs, &iter, ent) {
51 struct rerere_dir *rr_dir = ent->value;
52 free(rr_dir->status);
53 free(rr_dir);
55 strmap_clear(&rerere_dirs, 0);
58 static void free_rerere_id(struct string_list_item *item)
60 free(item->util);
63 static const char *rerere_id_hex(const struct rerere_id *id)
65 return id->collection->name;
68 static void fit_variant(struct rerere_dir *rr_dir, int variant)
70 variant++;
71 ALLOC_GROW(rr_dir->status, variant, rr_dir->status_alloc);
72 if (rr_dir->status_nr < variant) {
73 memset(rr_dir->status + rr_dir->status_nr,
74 '\0', variant - rr_dir->status_nr);
75 rr_dir->status_nr = variant;
79 static void assign_variant(struct rerere_id *id)
81 int variant;
82 struct rerere_dir *rr_dir = id->collection;
84 variant = id->variant;
85 if (variant < 0) {
86 for (variant = 0; variant < rr_dir->status_nr; variant++)
87 if (!rr_dir->status[variant])
88 break;
90 fit_variant(rr_dir, variant);
91 id->variant = variant;
94 const char *rerere_path(struct strbuf *buf, const struct rerere_id *id, const char *file)
96 if (!file)
97 return repo_git_path_replace(the_repository, buf, "rr-cache/%s",
98 rerere_id_hex(id));
100 if (id->variant <= 0)
101 return repo_git_path_replace(the_repository, buf, "rr-cache/%s/%s",
102 rerere_id_hex(id), file);
104 return repo_git_path_replace(the_repository, buf, "rr-cache/%s/%s.%d",
105 rerere_id_hex(id), file, id->variant);
108 static int is_rr_file(const char *name, const char *filename, int *variant)
110 const char *suffix;
111 char *ep;
113 if (!strcmp(name, filename)) {
114 *variant = 0;
115 return 1;
117 if (!skip_prefix(name, filename, &suffix) || *suffix != '.')
118 return 0;
120 errno = 0;
121 *variant = strtol(suffix + 1, &ep, 10);
122 if (errno || *ep)
123 return 0;
124 return 1;
127 static void scan_rerere_dir(struct rerere_dir *rr_dir)
129 struct dirent *de;
130 char *path;
131 DIR *dir;
133 path = repo_git_path(the_repository, "rr-cache/%s", rr_dir->name);
134 dir = opendir(path);
135 free(path);
136 if (!dir)
137 return;
138 while ((de = readdir(dir)) != NULL) {
139 int variant;
141 if (is_rr_file(de->d_name, "postimage", &variant)) {
142 fit_variant(rr_dir, variant);
143 rr_dir->status[variant] |= RR_HAS_POSTIMAGE;
144 } else if (is_rr_file(de->d_name, "preimage", &variant)) {
145 fit_variant(rr_dir, variant);
146 rr_dir->status[variant] |= RR_HAS_PREIMAGE;
149 closedir(dir);
152 static struct rerere_dir *find_rerere_dir(const char *hex)
154 struct rerere_dir *rr_dir;
156 rr_dir = strmap_get(&rerere_dirs, hex);
157 if (!rr_dir) {
158 FLEX_ALLOC_STR(rr_dir, name, hex);
159 rr_dir->status = NULL;
160 rr_dir->status_nr = 0;
161 rr_dir->status_alloc = 0;
162 strmap_put(&rerere_dirs, hex, rr_dir);
164 scan_rerere_dir(rr_dir);
166 return rr_dir;
169 static int has_rerere_resolution(const struct rerere_id *id)
171 const int both = RR_HAS_POSTIMAGE|RR_HAS_PREIMAGE;
172 int variant = id->variant;
174 if (variant < 0)
175 return 0;
176 return ((id->collection->status[variant] & both) == both);
179 static struct rerere_id *new_rerere_id_hex(char *hex)
181 struct rerere_id *id = xmalloc(sizeof(*id));
182 id->collection = find_rerere_dir(hex);
183 id->variant = -1; /* not known yet */
184 return id;
187 static struct rerere_id *new_rerere_id(unsigned char *hash)
189 return new_rerere_id_hex(hash_to_hex(hash));
193 * $GIT_DIR/MERGE_RR file is a collection of records, each of which is
194 * "conflict ID", a HT and pathname, terminated with a NUL, and is
195 * used to keep track of the set of paths that "rerere" may need to
196 * work on (i.e. what is left by the previous invocation of "git
197 * rerere" during the current conflict resolution session).
199 static void read_rr(struct repository *r, struct string_list *rr)
201 struct strbuf buf = STRBUF_INIT;
202 FILE *in = fopen_or_warn(git_path_merge_rr(r), "r");
204 if (!in)
205 return;
206 while (!strbuf_getwholeline(&buf, in, '\0')) {
207 char *path;
208 unsigned char hash[GIT_MAX_RAWSZ];
209 struct rerere_id *id;
210 int variant;
211 const unsigned hexsz = the_hash_algo->hexsz;
213 /* There has to be the hash, tab, path and then NUL */
214 if (buf.len < hexsz + 2 || get_hash_hex(buf.buf, hash))
215 die(_("corrupt MERGE_RR"));
217 if (buf.buf[hexsz] != '.') {
218 variant = 0;
219 path = buf.buf + hexsz;
220 } else {
221 errno = 0;
222 variant = strtol(buf.buf + hexsz + 1, &path, 10);
223 if (errno)
224 die(_("corrupt MERGE_RR"));
226 if (*(path++) != '\t')
227 die(_("corrupt MERGE_RR"));
228 buf.buf[hexsz] = '\0';
229 id = new_rerere_id_hex(buf.buf);
230 id->variant = variant;
232 * make sure id->collection->status has enough space
233 * for the variant we are interested in
235 fit_variant(id->collection, variant);
236 string_list_insert(rr, path)->util = id;
238 strbuf_release(&buf);
239 fclose(in);
242 static struct lock_file write_lock;
244 static int write_rr(struct string_list *rr, int out_fd)
246 int i;
247 for (i = 0; i < rr->nr; i++) {
248 struct strbuf buf = STRBUF_INIT;
249 struct rerere_id *id;
251 assert(rr->items[i].util != RERERE_RESOLVED);
253 id = rr->items[i].util;
254 if (!id)
255 continue;
256 assert(id->variant >= 0);
257 if (0 < id->variant)
258 strbuf_addf(&buf, "%s.%d\t%s%c",
259 rerere_id_hex(id), id->variant,
260 rr->items[i].string, 0);
261 else
262 strbuf_addf(&buf, "%s\t%s%c",
263 rerere_id_hex(id),
264 rr->items[i].string, 0);
266 if (write_in_full(out_fd, buf.buf, buf.len) < 0)
267 die(_("unable to write rerere record"));
269 strbuf_release(&buf);
271 if (commit_lock_file(&write_lock) != 0)
272 die(_("unable to write rerere record"));
273 return 0;
277 * "rerere" interacts with conflicted file contents using this I/O
278 * abstraction. It reads a conflicted contents from one place via
279 * "getline()" method, and optionally can write it out after
280 * normalizing the conflicted hunks to the "output". Subclasses of
281 * rerere_io embed this structure at the beginning of their own
282 * rerere_io object.
284 struct rerere_io {
285 int (*getline)(struct strbuf *, struct rerere_io *);
286 FILE *output;
287 int wrerror;
288 /* some more stuff */
291 static void ferr_write(const void *p, size_t count, FILE *fp, int *err)
293 if (!count || *err)
294 return;
295 if (fwrite(p, count, 1, fp) != 1)
296 *err = errno;
299 static inline void ferr_puts(const char *s, FILE *fp, int *err)
301 ferr_write(s, strlen(s), fp, err);
304 static void rerere_io_putstr(const char *str, struct rerere_io *io)
306 if (io->output)
307 ferr_puts(str, io->output, &io->wrerror);
310 static void rerere_io_putmem(const char *mem, size_t sz, struct rerere_io *io)
312 if (io->output)
313 ferr_write(mem, sz, io->output, &io->wrerror);
317 * Subclass of rerere_io that reads from an on-disk file
319 struct rerere_io_file {
320 struct rerere_io io;
321 FILE *input;
325 * ... and its getline() method implementation
327 static int rerere_file_getline(struct strbuf *sb, struct rerere_io *io_)
329 struct rerere_io_file *io = (struct rerere_io_file *)io_;
330 return strbuf_getwholeline(sb, io->input, '\n');
334 * Require the exact number of conflict marker letters, no more, no
335 * less, followed by SP or any whitespace
336 * (including LF).
338 static int is_cmarker(char *buf, int marker_char, int marker_size)
340 int want_sp;
343 * The beginning of our version and the end of their version
344 * always are labeled like "<<<<< ours" or ">>>>> theirs",
345 * hence we set want_sp for them. Note that the version from
346 * the common ancestor in diff3-style output is not always
347 * labelled (e.g. "||||| common" is often seen but "|||||"
348 * alone is also valid), so we do not set want_sp.
350 want_sp = (marker_char == '<') || (marker_char == '>');
352 while (marker_size--)
353 if (*buf++ != marker_char)
354 return 0;
355 if (want_sp && *buf != ' ')
356 return 0;
357 return isspace(*buf);
360 static void rerere_strbuf_putconflict(struct strbuf *buf, int ch, size_t size)
362 strbuf_addchars(buf, ch, size);
363 strbuf_addch(buf, '\n');
366 static int handle_conflict(struct strbuf *out, struct rerere_io *io,
367 int marker_size, struct git_hash_ctx *ctx)
369 enum {
370 RR_SIDE_1 = 0, RR_SIDE_2, RR_ORIGINAL
371 } hunk = RR_SIDE_1;
372 struct strbuf one = STRBUF_INIT, two = STRBUF_INIT;
373 struct strbuf buf = STRBUF_INIT, conflict = STRBUF_INIT;
374 int has_conflicts = -1;
376 while (!io->getline(&buf, io)) {
377 if (is_cmarker(buf.buf, '<', marker_size)) {
378 if (handle_conflict(&conflict, io, marker_size, NULL) < 0)
379 break;
380 if (hunk == RR_SIDE_1)
381 strbuf_addbuf(&one, &conflict);
382 else
383 strbuf_addbuf(&two, &conflict);
384 strbuf_release(&conflict);
385 } else if (is_cmarker(buf.buf, '|', marker_size)) {
386 if (hunk != RR_SIDE_1)
387 break;
388 hunk = RR_ORIGINAL;
389 } else if (is_cmarker(buf.buf, '=', marker_size)) {
390 if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL)
391 break;
392 hunk = RR_SIDE_2;
393 } else if (is_cmarker(buf.buf, '>', marker_size)) {
394 if (hunk != RR_SIDE_2)
395 break;
396 if (strbuf_cmp(&one, &two) > 0)
397 strbuf_swap(&one, &two);
398 has_conflicts = 1;
399 rerere_strbuf_putconflict(out, '<', marker_size);
400 strbuf_addbuf(out, &one);
401 rerere_strbuf_putconflict(out, '=', marker_size);
402 strbuf_addbuf(out, &two);
403 rerere_strbuf_putconflict(out, '>', marker_size);
404 if (ctx) {
405 git_hash_update(ctx, one.buf ?
406 one.buf : "",
407 one.len + 1);
408 git_hash_update(ctx, two.buf ?
409 two.buf : "",
410 two.len + 1);
412 break;
413 } else if (hunk == RR_SIDE_1)
414 strbuf_addbuf(&one, &buf);
415 else if (hunk == RR_ORIGINAL)
416 ; /* discard */
417 else if (hunk == RR_SIDE_2)
418 strbuf_addbuf(&two, &buf);
420 strbuf_release(&one);
421 strbuf_release(&two);
422 strbuf_release(&buf);
424 return has_conflicts;
428 * Read contents a file with conflicts, normalize the conflicts
429 * by (1) discarding the common ancestor version in diff3-style,
430 * (2) reordering our side and their side so that whichever sorts
431 * alphabetically earlier comes before the other one, while
432 * computing the "conflict ID", which is just an SHA-1 hash of
433 * one side of the conflict, NUL, the other side of the conflict,
434 * and NUL concatenated together.
436 * Return 1 if conflict hunks are found, 0 if there are no conflict
437 * hunks and -1 if an error occurred.
439 static int handle_path(unsigned char *hash, struct rerere_io *io, int marker_size)
441 struct git_hash_ctx ctx;
442 struct strbuf buf = STRBUF_INIT, out = STRBUF_INIT;
443 int has_conflicts = 0;
444 if (hash)
445 the_hash_algo->init_fn(&ctx);
447 while (!io->getline(&buf, io)) {
448 if (is_cmarker(buf.buf, '<', marker_size)) {
449 has_conflicts = handle_conflict(&out, io, marker_size,
450 hash ? &ctx : NULL);
451 if (has_conflicts < 0)
452 break;
453 rerere_io_putmem(out.buf, out.len, io);
454 strbuf_reset(&out);
455 } else
456 rerere_io_putstr(buf.buf, io);
458 strbuf_release(&buf);
459 strbuf_release(&out);
461 if (hash)
462 git_hash_final(hash, &ctx);
464 return has_conflicts;
468 * Scan the path for conflicts, do the "handle_path()" thing above, and
469 * return the number of conflict hunks found.
471 static int handle_file(struct index_state *istate,
472 const char *path, unsigned char *hash, const char *output)
474 int has_conflicts = 0;
475 struct rerere_io_file io;
476 int marker_size = ll_merge_marker_size(istate, path);
478 memset(&io, 0, sizeof(io));
479 io.io.getline = rerere_file_getline;
480 io.input = fopen(path, "r");
481 io.io.wrerror = 0;
482 if (!io.input)
483 return error_errno(_("could not open '%s'"), path);
485 if (output) {
486 io.io.output = fopen(output, "w");
487 if (!io.io.output) {
488 error_errno(_("could not write '%s'"), output);
489 fclose(io.input);
490 return -1;
494 has_conflicts = handle_path(hash, (struct rerere_io *)&io, marker_size);
496 fclose(io.input);
497 if (io.io.wrerror)
498 error(_("there were errors while writing '%s' (%s)"),
499 path, strerror(io.io.wrerror));
500 if (io.io.output && fclose(io.io.output))
501 io.io.wrerror = error_errno(_("failed to flush '%s'"), path);
503 if (has_conflicts < 0) {
504 if (output)
505 unlink_or_warn(output);
506 return error(_("could not parse conflict hunks in '%s'"), path);
508 if (io.io.wrerror)
509 return -1;
510 return has_conflicts;
514 * Look at a cache entry at "i" and see if it is not conflicting,
515 * conflicting and we are willing to handle, or conflicting and
516 * we are unable to handle, and return the determination in *type.
517 * Return the cache index to be looked at next, by skipping the
518 * stages we have already looked at in this invocation of this
519 * function.
521 static int check_one_conflict(struct index_state *istate, int i, int *type)
523 const struct cache_entry *e = istate->cache[i];
525 if (!ce_stage(e)) {
526 *type = RESOLVED;
527 return i + 1;
530 *type = PUNTED;
531 while (i < istate->cache_nr && ce_stage(istate->cache[i]) == 1)
532 i++;
534 /* Only handle regular files with both stages #2 and #3 */
535 if (i + 1 < istate->cache_nr) {
536 const struct cache_entry *e2 = istate->cache[i];
537 const struct cache_entry *e3 = istate->cache[i + 1];
538 if (ce_stage(e2) == 2 &&
539 ce_stage(e3) == 3 &&
540 ce_same_name(e, e3) &&
541 S_ISREG(e2->ce_mode) &&
542 S_ISREG(e3->ce_mode))
543 *type = THREE_STAGED;
546 /* Skip the entries with the same name */
547 while (i < istate->cache_nr && ce_same_name(e, istate->cache[i]))
548 i++;
549 return i;
553 * Scan the index and find paths that have conflicts that rerere can
554 * handle, i.e. the ones that has both stages #2 and #3.
556 * NEEDSWORK: we do not record or replay a previous "resolve by
557 * deletion" for a delete-modify conflict, as that is inherently risky
558 * without knowing what modification is being discarded. The only
559 * safe case, i.e. both side doing the deletion and modification that
560 * are identical to the previous round, might want to be handled,
561 * though.
563 static int find_conflict(struct repository *r, struct string_list *conflict)
565 int i;
567 if (repo_read_index(r) < 0)
568 return error(_("index file corrupt"));
570 for (i = 0; i < r->index->cache_nr;) {
571 int conflict_type;
572 const struct cache_entry *e = r->index->cache[i];
573 i = check_one_conflict(r->index, i, &conflict_type);
574 if (conflict_type == THREE_STAGED)
575 string_list_insert(conflict, (const char *)e->name);
577 return 0;
581 * The merge_rr list is meant to hold outstanding conflicted paths
582 * that rerere could handle. Abuse the list by adding other types of
583 * entries to allow the caller to show "rerere remaining".
585 * - Conflicted paths that rerere does not handle are added
586 * - Conflicted paths that have been resolved are marked as such
587 * by storing RERERE_RESOLVED to .util field (where conflict ID
588 * is expected to be stored).
590 * Do *not* write MERGE_RR file out after calling this function.
592 * NEEDSWORK: we may want to fix the caller that implements "rerere
593 * remaining" to do this without abusing merge_rr.
595 int rerere_remaining(struct repository *r, struct string_list *merge_rr)
597 int i;
599 if (setup_rerere(r, merge_rr, RERERE_READONLY))
600 return 0;
601 if (repo_read_index(r) < 0)
602 return error(_("index file corrupt"));
604 for (i = 0; i < r->index->cache_nr;) {
605 int conflict_type;
606 const struct cache_entry *e = r->index->cache[i];
607 i = check_one_conflict(r->index, i, &conflict_type);
608 if (conflict_type == PUNTED)
609 string_list_insert(merge_rr, (const char *)e->name);
610 else if (conflict_type == RESOLVED) {
611 struct string_list_item *it;
612 it = string_list_lookup(merge_rr, (const char *)e->name);
613 if (it) {
614 free_rerere_id(it);
615 it->util = RERERE_RESOLVED;
619 return 0;
623 * Try using the given conflict resolution "ID" to see
624 * if that recorded conflict resolves cleanly what we
625 * got in the "cur".
627 static int try_merge(struct index_state *istate,
628 const struct rerere_id *id, const char *path,
629 mmfile_t *cur, mmbuffer_t *result)
631 enum ll_merge_result ret;
632 mmfile_t base = {NULL, 0}, other = {NULL, 0};
633 struct strbuf buf = STRBUF_INIT;
635 if (read_mmfile(&base, rerere_path(&buf, id, "preimage")) ||
636 read_mmfile(&other, rerere_path(&buf, id, "postimage"))) {
637 ret = LL_MERGE_CONFLICT;
638 } else {
640 * A three-way merge. Note that this honors user-customizable
641 * low-level merge driver settings.
643 ret = ll_merge(result, path, &base, NULL, cur, "", &other, "",
644 istate, NULL);
647 strbuf_release(&buf);
648 free(base.ptr);
649 free(other.ptr);
651 return ret;
655 * Find the conflict identified by "id"; the change between its
656 * "preimage" (i.e. a previous contents with conflict markers) and its
657 * "postimage" (i.e. the corresponding contents with conflicts
658 * resolved) may apply cleanly to the contents stored in "path", i.e.
659 * the conflict this time around.
661 * Returns 0 for successful replay of recorded resolution, or non-zero
662 * for failure.
664 static int merge(struct index_state *istate, const struct rerere_id *id, const char *path)
666 FILE *f;
667 int ret;
668 struct strbuf buf = STRBUF_INIT;
669 mmfile_t cur = {NULL, 0};
670 mmbuffer_t result = {NULL, 0};
673 * Normalize the conflicts in path and write it out to
674 * "thisimage" temporary file.
676 if ((handle_file(istate, path, NULL, rerere_path(&buf, id, "thisimage")) < 0) ||
677 read_mmfile(&cur, rerere_path(&buf, id, "thisimage"))) {
678 ret = 1;
679 goto out;
682 ret = try_merge(istate, id, path, &cur, &result);
683 if (ret)
684 goto out;
687 * A successful replay of recorded resolution.
688 * Mark that "postimage" was used to help gc.
690 if (utime(rerere_path(&buf, id, "postimage"), NULL) < 0)
691 warning_errno(_("failed utime() on '%s'"),
692 rerere_path(&buf, id, "postimage"));
694 /* Update "path" with the resolution */
695 f = fopen(path, "w");
696 if (!f)
697 return error_errno(_("could not open '%s'"), path);
698 if (fwrite(result.ptr, result.size, 1, f) != 1)
699 error_errno(_("could not write '%s'"), path);
700 if (fclose(f))
701 return error_errno(_("writing '%s' failed"), path);
703 out:
704 free(cur.ptr);
705 free(result.ptr);
706 strbuf_release(&buf);
708 return ret;
711 static void update_paths(struct repository *r, struct string_list *update)
713 struct lock_file index_lock = LOCK_INIT;
714 int i;
716 repo_hold_locked_index(r, &index_lock, LOCK_DIE_ON_ERROR);
718 for (i = 0; i < update->nr; i++) {
719 struct string_list_item *item = &update->items[i];
720 if (add_file_to_index(r->index, item->string, 0))
721 exit(128);
722 fprintf_ln(stderr, _("Staged '%s' using previous resolution."),
723 item->string);
726 if (write_locked_index(r->index, &index_lock,
727 COMMIT_LOCK | SKIP_IF_UNCHANGED))
728 die(_("unable to write new index file"));
731 static void remove_variant(struct rerere_id *id)
733 struct strbuf buf = STRBUF_INIT;
734 unlink_or_warn(rerere_path(&buf, id, "postimage"));
735 unlink_or_warn(rerere_path(&buf, id, "preimage"));
736 id->collection->status[id->variant] = 0;
737 strbuf_release(&buf);
741 * The path indicated by rr_item may still have conflict for which we
742 * have a recorded resolution, in which case replay it and optionally
743 * update it. Or it may have been resolved by the user and we may
744 * only have the preimage for that conflict, in which case the result
745 * needs to be recorded as a resolution in a postimage file.
747 static void do_rerere_one_path(struct index_state *istate,
748 struct string_list_item *rr_item,
749 struct string_list *update)
751 const char *path = rr_item->string;
752 struct rerere_id *id = rr_item->util;
753 struct rerere_dir *rr_dir = id->collection;
754 struct strbuf buf = STRBUF_INIT;
755 int variant;
757 variant = id->variant;
759 /* Has the user resolved it already? */
760 if (variant >= 0) {
761 if (!handle_file(istate, path, NULL, NULL)) {
762 copy_file(rerere_path(&buf, id, "postimage"), path, 0666);
763 id->collection->status[variant] |= RR_HAS_POSTIMAGE;
764 fprintf_ln(stderr, _("Recorded resolution for '%s'."), path);
765 free_rerere_id(rr_item);
766 rr_item->util = NULL;
767 goto out;
770 * There may be other variants that can cleanly
771 * replay. Try them and update the variant number for
772 * this one.
776 /* Does any existing resolution apply cleanly? */
777 for (variant = 0; variant < rr_dir->status_nr; variant++) {
778 const int both = RR_HAS_PREIMAGE | RR_HAS_POSTIMAGE;
779 struct rerere_id vid = *id;
781 if ((rr_dir->status[variant] & both) != both)
782 continue;
784 vid.variant = variant;
785 if (merge(istate, &vid, path))
786 continue; /* failed to replay */
789 * If there already is a different variant that applies
790 * cleanly, there is no point maintaining our own variant.
792 if (0 <= id->variant && id->variant != variant)
793 remove_variant(id);
795 if (rerere_autoupdate)
796 string_list_insert(update, path);
797 else
798 fprintf_ln(stderr,
799 _("Resolved '%s' using previous resolution."),
800 path);
801 free_rerere_id(rr_item);
802 rr_item->util = NULL;
803 goto out;
806 /* None of the existing one applies; we need a new variant */
807 assign_variant(id);
809 variant = id->variant;
810 handle_file(istate, path, NULL, rerere_path(&buf, id, "preimage"));
811 if (id->collection->status[variant] & RR_HAS_POSTIMAGE) {
812 const char *path = rerere_path(&buf, id, "postimage");
813 if (unlink(path))
814 die_errno(_("cannot unlink stray '%s'"), path);
815 id->collection->status[variant] &= ~RR_HAS_POSTIMAGE;
817 id->collection->status[variant] |= RR_HAS_PREIMAGE;
818 fprintf_ln(stderr, _("Recorded preimage for '%s'"), path);
820 out:
821 strbuf_release(&buf);
824 static int do_plain_rerere(struct repository *r,
825 struct string_list *rr, int fd)
827 struct string_list conflict = STRING_LIST_INIT_DUP;
828 struct string_list update = STRING_LIST_INIT_DUP;
829 struct strbuf buf = STRBUF_INIT;
830 int i;
832 find_conflict(r, &conflict);
835 * MERGE_RR records paths with conflicts immediately after
836 * merge failed. Some of the conflicted paths might have been
837 * hand resolved in the working tree since then, but the
838 * initial run would catch all and register their preimages.
840 for (i = 0; i < conflict.nr; i++) {
841 struct rerere_id *id;
842 unsigned char hash[GIT_MAX_RAWSZ];
843 const char *path = conflict.items[i].string;
844 int ret;
847 * Ask handle_file() to scan and assign a
848 * conflict ID. No need to write anything out
849 * yet.
851 ret = handle_file(r->index, path, hash, NULL);
852 if (ret != 0 && string_list_has_string(rr, path)) {
853 remove_variant(string_list_lookup(rr, path)->util);
854 string_list_remove(rr, path, 1);
856 if (ret < 1)
857 continue;
859 id = new_rerere_id(hash);
860 string_list_insert(rr, path)->util = id;
862 /* Ensure that the directory exists. */
863 safe_create_dir_in_gitdir(the_repository, rerere_path(&buf, id, NULL));
866 for (i = 0; i < rr->nr; i++)
867 do_rerere_one_path(r->index, &rr->items[i], &update);
869 if (update.nr)
870 update_paths(r, &update);
872 string_list_clear(&conflict, 0);
873 string_list_clear(&update, 0);
874 strbuf_release(&buf);
875 return write_rr(rr, fd);
878 static void git_rerere_config(void)
880 git_config_get_bool("rerere.enabled", &rerere_enabled);
881 git_config_get_bool("rerere.autoupdate", &rerere_autoupdate);
882 git_config(git_default_config, NULL);
885 static GIT_PATH_FUNC(git_path_rr_cache, "rr-cache")
887 static int is_rerere_enabled(void)
889 int rr_cache_exists;
891 if (!rerere_enabled)
892 return 0;
894 rr_cache_exists = is_directory(git_path_rr_cache());
895 if (rerere_enabled < 0)
896 return rr_cache_exists;
898 if (!rr_cache_exists &&
899 safe_create_dir_in_gitdir(the_repository, git_path_rr_cache()))
900 die(_("could not create directory '%s'"), git_path_rr_cache());
901 return 1;
904 int setup_rerere(struct repository *r, struct string_list *merge_rr, int flags)
906 int fd;
908 git_rerere_config();
909 if (!is_rerere_enabled())
910 return -1;
912 if (flags & (RERERE_AUTOUPDATE|RERERE_NOAUTOUPDATE))
913 rerere_autoupdate = !!(flags & RERERE_AUTOUPDATE);
914 if (flags & RERERE_READONLY)
915 fd = 0;
916 else
917 fd = hold_lock_file_for_update(&write_lock,
918 git_path_merge_rr(r),
919 LOCK_DIE_ON_ERROR);
920 read_rr(r, merge_rr);
921 return fd;
925 * The main entry point that is called internally from codepaths that
926 * perform mergy operations, possibly leaving conflicted index entries
927 * and working tree files.
929 int repo_rerere(struct repository *r, int flags)
931 struct string_list merge_rr = STRING_LIST_INIT_DUP;
932 int fd, status;
934 fd = setup_rerere(r, &merge_rr, flags);
935 if (fd < 0)
936 return 0;
937 status = do_plain_rerere(r, &merge_rr, fd);
938 free_rerere_dirs();
939 string_list_clear(&merge_rr, 1);
940 return status;
944 * Subclass of rerere_io that reads from an in-core buffer that is a
945 * strbuf
947 struct rerere_io_mem {
948 struct rerere_io io;
949 struct strbuf input;
953 * ... and its getline() method implementation
955 static int rerere_mem_getline(struct strbuf *sb, struct rerere_io *io_)
957 struct rerere_io_mem *io = (struct rerere_io_mem *)io_;
958 char *ep;
959 size_t len;
961 strbuf_release(sb);
962 if (!io->input.len)
963 return -1;
964 ep = memchr(io->input.buf, '\n', io->input.len);
965 if (!ep)
966 ep = io->input.buf + io->input.len;
967 else if (*ep == '\n')
968 ep++;
969 len = ep - io->input.buf;
970 strbuf_add(sb, io->input.buf, len);
971 strbuf_remove(&io->input, 0, len);
972 return 0;
975 static int handle_cache(struct index_state *istate,
976 const char *path, unsigned char *hash, const char *output)
978 mmfile_t mmfile[3] = {{NULL}};
979 mmbuffer_t result = {NULL, 0};
980 const struct cache_entry *ce;
981 int pos, len, i, has_conflicts;
982 struct rerere_io_mem io;
983 int marker_size = ll_merge_marker_size(istate, path);
986 * Reproduce the conflicted merge in-core
988 len = strlen(path);
989 pos = index_name_pos(istate, path, len);
990 if (0 <= pos)
991 return -1;
992 pos = -pos - 1;
994 while (pos < istate->cache_nr) {
995 enum object_type type;
996 unsigned long size;
998 ce = istate->cache[pos++];
999 if (ce_namelen(ce) != len || memcmp(ce->name, path, len))
1000 break;
1001 i = ce_stage(ce) - 1;
1002 if (!mmfile[i].ptr) {
1003 mmfile[i].ptr = odb_read_object(the_repository->objects,
1004 &ce->oid, &type, &size);
1005 if (!mmfile[i].ptr)
1006 die(_("unable to read %s"),
1007 oid_to_hex(&ce->oid));
1008 mmfile[i].size = size;
1011 for (i = 0; i < 3; i++)
1012 if (!mmfile[i].ptr && !mmfile[i].size)
1013 mmfile[i].ptr = xstrdup("");
1016 * NEEDSWORK: handle conflicts from merges with
1017 * merge.renormalize set, too?
1019 ll_merge(&result, path, &mmfile[0], NULL,
1020 &mmfile[1], "ours",
1021 &mmfile[2], "theirs",
1022 istate, NULL);
1023 for (i = 0; i < 3; i++)
1024 free(mmfile[i].ptr);
1026 memset(&io, 0, sizeof(io));
1027 io.io.getline = rerere_mem_getline;
1028 if (output)
1029 io.io.output = fopen(output, "w");
1030 else
1031 io.io.output = NULL;
1032 strbuf_init(&io.input, 0);
1033 strbuf_attach(&io.input, result.ptr, result.size, result.size);
1036 * Grab the conflict ID and optionally write the original
1037 * contents with conflict markers out.
1039 has_conflicts = handle_path(hash, (struct rerere_io *)&io, marker_size);
1040 strbuf_release(&io.input);
1041 if (io.io.output)
1042 fclose(io.io.output);
1043 return has_conflicts;
1046 static int rerere_forget_one_path(struct index_state *istate,
1047 const char *path,
1048 struct string_list *rr)
1050 const char *filename;
1051 struct rerere_id *id;
1052 unsigned char hash[GIT_MAX_RAWSZ];
1053 int ret;
1054 struct strbuf buf = STRBUF_INIT;
1055 struct string_list_item *item;
1058 * Recreate the original conflict from the stages in the
1059 * index and compute the conflict ID
1061 ret = handle_cache(istate, path, hash, NULL);
1062 if (ret < 1)
1063 return error(_("could not parse conflict hunks in '%s'"), path);
1065 /* Nuke the recorded resolution for the conflict */
1066 id = new_rerere_id(hash);
1068 for (id->variant = 0;
1069 id->variant < id->collection->status_nr;
1070 id->variant++) {
1071 mmfile_t cur = { NULL, 0 };
1072 mmbuffer_t result = {NULL, 0};
1073 int cleanly_resolved;
1075 if (!has_rerere_resolution(id))
1076 continue;
1078 handle_cache(istate, path, hash, rerere_path(&buf, id, "thisimage"));
1079 if (read_mmfile(&cur, rerere_path(&buf, id, "thisimage"))) {
1080 free(cur.ptr);
1081 error(_("failed to update conflicted state in '%s'"), path);
1082 goto fail_exit;
1084 cleanly_resolved = !try_merge(istate, id, path, &cur, &result);
1085 free(result.ptr);
1086 free(cur.ptr);
1087 if (cleanly_resolved)
1088 break;
1091 if (id->collection->status_nr <= id->variant) {
1092 error(_("no remembered resolution for '%s'"), path);
1093 goto fail_exit;
1096 filename = rerere_path(&buf, id, "postimage");
1097 if (unlink(filename)) {
1098 if (errno == ENOENT)
1099 error(_("no remembered resolution for '%s'"), path);
1100 else
1101 error_errno(_("cannot unlink '%s'"), filename);
1102 goto fail_exit;
1106 * Update the preimage so that the user can resolve the
1107 * conflict in the working tree, run us again to record
1108 * the postimage.
1110 handle_cache(istate, path, hash, rerere_path(&buf, id, "preimage"));
1111 fprintf_ln(stderr, _("Updated preimage for '%s'"), path);
1114 * And remember that we can record resolution for this
1115 * conflict when the user is done.
1117 item = string_list_insert(rr, path);
1118 free_rerere_id(item);
1119 item->util = id;
1120 fprintf(stderr, _("Forgot resolution for '%s'\n"), path);
1121 strbuf_release(&buf);
1122 return 0;
1124 fail_exit:
1125 strbuf_release(&buf);
1126 free(id);
1127 return -1;
1130 int rerere_forget(struct repository *r, struct pathspec *pathspec)
1132 int i, fd, ret;
1133 struct string_list conflict = STRING_LIST_INIT_DUP;
1134 struct string_list merge_rr = STRING_LIST_INIT_DUP;
1136 if (repo_read_index(r) < 0)
1137 return error(_("index file corrupt"));
1139 fd = setup_rerere(r, &merge_rr, RERERE_NOAUTOUPDATE);
1140 if (fd < 0)
1141 return 0;
1144 * The paths may have been resolved (incorrectly);
1145 * recover the original conflicted state and then
1146 * find the conflicted paths.
1148 unmerge_index(r->index, pathspec, 0);
1149 find_conflict(r, &conflict);
1150 for (i = 0; i < conflict.nr; i++) {
1151 struct string_list_item *it = &conflict.items[i];
1152 if (!match_pathspec(r->index, pathspec, it->string,
1153 strlen(it->string), 0, NULL, 0))
1154 continue;
1155 rerere_forget_one_path(r->index, it->string, &merge_rr);
1158 ret = write_rr(&merge_rr, fd);
1160 string_list_clear(&conflict, 0);
1161 string_list_clear(&merge_rr, 1);
1162 return ret;
1166 * Garbage collection support
1169 static timestamp_t rerere_created_at(struct rerere_id *id)
1171 struct strbuf buf = STRBUF_INIT;
1172 struct stat st;
1173 timestamp_t ret;
1175 ret = stat(rerere_path(&buf, id, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
1177 strbuf_release(&buf);
1178 return ret;
1181 static timestamp_t rerere_last_used_at(struct rerere_id *id)
1183 struct strbuf buf = STRBUF_INIT;
1184 struct stat st;
1185 timestamp_t ret;
1187 ret = stat(rerere_path(&buf, id, "postimage"), &st) ? (time_t) 0 : st.st_mtime;
1189 strbuf_release(&buf);
1190 return ret;
1194 * Remove the recorded resolution for a given conflict ID
1196 static void unlink_rr_item(struct rerere_id *id)
1198 struct strbuf buf = STRBUF_INIT;
1199 unlink_or_warn(rerere_path(&buf, id, "thisimage"));
1200 remove_variant(id);
1201 id->collection->status[id->variant] = 0;
1202 strbuf_release(&buf);
1205 static void prune_one(struct rerere_id *id,
1206 timestamp_t cutoff_resolve, timestamp_t cutoff_noresolve)
1208 timestamp_t then;
1209 timestamp_t cutoff;
1211 then = rerere_last_used_at(id);
1212 if (then)
1213 cutoff = cutoff_resolve;
1214 else {
1215 then = rerere_created_at(id);
1216 if (!then)
1217 return;
1218 cutoff = cutoff_noresolve;
1220 if (then < cutoff)
1221 unlink_rr_item(id);
1224 /* Does the basename in "path" look plausibly like an rr-cache entry? */
1225 static int is_rr_cache_dirname(const char *path)
1227 struct object_id oid;
1228 const char *end;
1229 return !parse_oid_hex(path, &oid, &end) && !*end;
1232 void rerere_gc(struct repository *r, struct string_list *rr)
1234 struct string_list to_remove = STRING_LIST_INIT_DUP;
1235 DIR *dir;
1236 struct dirent *e;
1237 int i;
1238 timestamp_t now = time(NULL);
1239 timestamp_t cutoff_noresolve = now - 15 * 86400;
1240 timestamp_t cutoff_resolve = now - 60 * 86400;
1241 struct strbuf buf = STRBUF_INIT;
1243 if (setup_rerere(r, rr, 0) < 0)
1244 return;
1246 repo_config_get_expiry_in_days(the_repository, "gc.rerereresolved",
1247 &cutoff_resolve, now);
1248 repo_config_get_expiry_in_days(the_repository, "gc.rerereunresolved",
1249 &cutoff_noresolve, now);
1250 git_config(git_default_config, NULL);
1251 dir = opendir(repo_git_path_replace(the_repository, &buf, "rr-cache"));
1252 if (!dir)
1253 die_errno(_("unable to open rr-cache directory"));
1254 /* Collect stale conflict IDs ... */
1255 while ((e = readdir_skip_dot_and_dotdot(dir))) {
1256 struct rerere_dir *rr_dir;
1257 struct rerere_id id;
1258 int now_empty;
1260 if (!is_rr_cache_dirname(e->d_name))
1261 continue; /* or should we remove e->d_name? */
1263 rr_dir = find_rerere_dir(e->d_name);
1265 now_empty = 1;
1266 for (id.variant = 0, id.collection = rr_dir;
1267 id.variant < id.collection->status_nr;
1268 id.variant++) {
1269 prune_one(&id, cutoff_resolve, cutoff_noresolve);
1270 if (id.collection->status[id.variant])
1271 now_empty = 0;
1273 if (now_empty)
1274 string_list_append(&to_remove, e->d_name);
1276 closedir(dir);
1278 /* ... and then remove the empty directories */
1279 for (i = 0; i < to_remove.nr; i++)
1280 rmdir(repo_git_path_replace(the_repository, &buf,
1281 "rr-cache/%s", to_remove.items[i].string));
1283 string_list_clear(&to_remove, 0);
1284 rollback_lock_file(&write_lock);
1285 strbuf_release(&buf);
1289 * During a conflict resolution, after "rerere" recorded the
1290 * preimages, abandon them if the user did not resolve them or
1291 * record their resolutions. And drop $GIT_DIR/MERGE_RR.
1293 * NEEDSWORK: shouldn't we be calling this from "reset --hard"?
1295 void rerere_clear(struct repository *r, struct string_list *merge_rr)
1297 int i;
1299 if (setup_rerere(r, merge_rr, 0) < 0)
1300 return;
1302 for (i = 0; i < merge_rr->nr; i++) {
1303 struct rerere_id *id = merge_rr->items[i].util;
1304 struct strbuf buf = STRBUF_INIT;
1306 if (!has_rerere_resolution(id)) {
1307 unlink_rr_item(id);
1308 rmdir(rerere_path(&buf, id, NULL));
1311 strbuf_release(&buf);
1313 unlink_or_warn(git_path_merge_rr(r));
1314 rollback_lock_file(&write_lock);