t3404: check root commit in 'rebase -i --root reword root commit'
[git] / rerere.c
1 #include "cache.h"
2 #include "config.h"
3 #include "lockfile.h"
4 #include "string-list.h"
5 #include "rerere.h"
6 #include "xdiff-interface.h"
7 #include "dir.h"
8 #include "resolve-undo.h"
9 #include "ll-merge.h"
10 #include "attr.h"
11 #include "pathspec.h"
12 #include "sha1-lookup.h"
13
14 #define RESOLVED 0
15 #define PUNTED 1
16 #define THREE_STAGED 2
17 void *RERERE_RESOLVED = &RERERE_RESOLVED;
18
19 /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
20 static int rerere_enabled = -1;
21
22 /* automatically update cleanly resolved paths to the index */
23 static int rerere_autoupdate;
24
25 static int rerere_dir_nr;
26 static int rerere_dir_alloc;
27
28 #define RR_HAS_POSTIMAGE 1
29 #define RR_HAS_PREIMAGE 2
30 static struct rerere_dir {
31         unsigned char sha1[20];
32         int status_alloc, status_nr;
33         unsigned char *status;
34 } **rerere_dir;
35
36 static void free_rerere_dirs(void)
37 {
38         int i;
39         for (i = 0; i < rerere_dir_nr; i++) {
40                 free(rerere_dir[i]->status);
41                 free(rerere_dir[i]);
42         }
43         FREE_AND_NULL(rerere_dir);
44         rerere_dir_nr = rerere_dir_alloc = 0;
45 }
46
47 static void free_rerere_id(struct string_list_item *item)
48 {
49         free(item->util);
50 }
51
52 static const char *rerere_id_hex(const struct rerere_id *id)
53 {
54         return sha1_to_hex(id->collection->sha1);
55 }
56
57 static void fit_variant(struct rerere_dir *rr_dir, int variant)
58 {
59         variant++;
60         ALLOC_GROW(rr_dir->status, variant, rr_dir->status_alloc);
61         if (rr_dir->status_nr < variant) {
62                 memset(rr_dir->status + rr_dir->status_nr,
63                        '\0', variant - rr_dir->status_nr);
64                 rr_dir->status_nr = variant;
65         }
66 }
67
68 static void assign_variant(struct rerere_id *id)
69 {
70         int variant;
71         struct rerere_dir *rr_dir = id->collection;
72
73         variant = id->variant;
74         if (variant < 0) {
75                 for (variant = 0; variant < rr_dir->status_nr; variant++)
76                         if (!rr_dir->status[variant])
77                                 break;
78         }
79         fit_variant(rr_dir, variant);
80         id->variant = variant;
81 }
82
83 const char *rerere_path(const struct rerere_id *id, const char *file)
84 {
85         if (!file)
86                 return git_path("rr-cache/%s", rerere_id_hex(id));
87
88         if (id->variant <= 0)
89                 return git_path("rr-cache/%s/%s", rerere_id_hex(id), file);
90
91         return git_path("rr-cache/%s/%s.%d",
92                         rerere_id_hex(id), file, id->variant);
93 }
94
95 static int is_rr_file(const char *name, const char *filename, int *variant)
96 {
97         const char *suffix;
98         char *ep;
99
100         if (!strcmp(name, filename)) {
101                 *variant = 0;
102                 return 1;
103         }
104         if (!skip_prefix(name, filename, &suffix) || *suffix != '.')
105                 return 0;
106
107         errno = 0;
108         *variant = strtol(suffix + 1, &ep, 10);
109         if (errno || *ep)
110                 return 0;
111         return 1;
112 }
113
114 static void scan_rerere_dir(struct rerere_dir *rr_dir)
115 {
116         struct dirent *de;
117         DIR *dir = opendir(git_path("rr-cache/%s", sha1_to_hex(rr_dir->sha1)));
118
119         if (!dir)
120                 return;
121         while ((de = readdir(dir)) != NULL) {
122                 int variant;
123
124                 if (is_rr_file(de->d_name, "postimage", &variant)) {
125                         fit_variant(rr_dir, variant);
126                         rr_dir->status[variant] |= RR_HAS_POSTIMAGE;
127                 } else if (is_rr_file(de->d_name, "preimage", &variant)) {
128                         fit_variant(rr_dir, variant);
129                         rr_dir->status[variant] |= RR_HAS_PREIMAGE;
130                 }
131         }
132         closedir(dir);
133 }
134
135 static const unsigned char *rerere_dir_sha1(size_t i, void *table)
136 {
137         struct rerere_dir **rr_dir = table;
138         return rr_dir[i]->sha1;
139 }
140
141 static struct rerere_dir *find_rerere_dir(const char *hex)
142 {
143         unsigned char sha1[20];
144         struct rerere_dir *rr_dir;
145         int pos;
146
147         if (get_sha1_hex(hex, sha1))
148                 return NULL; /* BUG */
149         pos = sha1_pos(sha1, rerere_dir, rerere_dir_nr, rerere_dir_sha1);
150         if (pos < 0) {
151                 rr_dir = xmalloc(sizeof(*rr_dir));
152                 hashcpy(rr_dir->sha1, sha1);
153                 rr_dir->status = NULL;
154                 rr_dir->status_nr = 0;
155                 rr_dir->status_alloc = 0;
156                 pos = -1 - pos;
157
158                 /* Make sure the array is big enough ... */
159                 ALLOC_GROW(rerere_dir, rerere_dir_nr + 1, rerere_dir_alloc);
160                 /* ... and add it in. */
161                 rerere_dir_nr++;
162                 MOVE_ARRAY(rerere_dir + pos + 1, rerere_dir + pos,
163                            rerere_dir_nr - pos - 1);
164                 rerere_dir[pos] = rr_dir;
165                 scan_rerere_dir(rr_dir);
166         }
167         return rerere_dir[pos];
168 }
169
170 static int has_rerere_resolution(const struct rerere_id *id)
171 {
172         const int both = RR_HAS_POSTIMAGE|RR_HAS_PREIMAGE;
173         int variant = id->variant;
174
175         if (variant < 0)
176                 return 0;
177         return ((id->collection->status[variant] & both) == both);
178 }
179
180 static struct rerere_id *new_rerere_id_hex(char *hex)
181 {
182         struct rerere_id *id = xmalloc(sizeof(*id));
183         id->collection = find_rerere_dir(hex);
184         id->variant = -1; /* not known yet */
185         return id;
186 }
187
188 static struct rerere_id *new_rerere_id(unsigned char *sha1)
189 {
190         return new_rerere_id_hex(sha1_to_hex(sha1));
191 }
192
193 /*
194  * $GIT_DIR/MERGE_RR file is a collection of records, each of which is
195  * "conflict ID", a HT and pathname, terminated with a NUL, and is
196  * used to keep track of the set of paths that "rerere" may need to
197  * work on (i.e. what is left by the previous invocation of "git
198  * rerere" during the current conflict resolution session).
199  */
200 static void read_rr(struct string_list *rr)
201 {
202         struct strbuf buf = STRBUF_INIT;
203         FILE *in = fopen_or_warn(git_path_merge_rr(), "r");
204
205         if (!in)
206                 return;
207         while (!strbuf_getwholeline(&buf, in, '\0')) {
208                 char *path;
209                 unsigned char sha1[20];
210                 struct rerere_id *id;
211                 int variant;
212
213                 /* There has to be the hash, tab, path and then NUL */
214                 if (buf.len < 42 || get_sha1_hex(buf.buf, sha1))
215                         die("corrupt MERGE_RR");
216
217                 if (buf.buf[40] != '.') {
218                         variant = 0;
219                         path = buf.buf + 40;
220                 } else {
221                         errno = 0;
222                         variant = strtol(buf.buf + 41, &path, 10);
223                         if (errno)
224                                 die("corrupt MERGE_RR");
225                 }
226                 if (*(path++) != '\t')
227                         die("corrupt MERGE_RR");
228                 buf.buf[40] = '\0';
229                 id = new_rerere_id_hex(buf.buf);
230                 id->variant = variant;
231                 string_list_insert(rr, path)->util = id;
232         }
233         strbuf_release(&buf);
234         fclose(in);
235 }
236
237 static struct lock_file write_lock;
238
239 static int write_rr(struct string_list *rr, int out_fd)
240 {
241         int i;
242         for (i = 0; i < rr->nr; i++) {
243                 struct strbuf buf = STRBUF_INIT;
244                 struct rerere_id *id;
245
246                 assert(rr->items[i].util != RERERE_RESOLVED);
247
248                 id = rr->items[i].util;
249                 if (!id)
250                         continue;
251                 assert(id->variant >= 0);
252                 if (0 < id->variant)
253                         strbuf_addf(&buf, "%s.%d\t%s%c",
254                                     rerere_id_hex(id), id->variant,
255                                     rr->items[i].string, 0);
256                 else
257                         strbuf_addf(&buf, "%s\t%s%c",
258                                     rerere_id_hex(id),
259                                     rr->items[i].string, 0);
260
261                 if (write_in_full(out_fd, buf.buf, buf.len) < 0)
262                         die("unable to write rerere record");
263
264                 strbuf_release(&buf);
265         }
266         if (commit_lock_file(&write_lock) != 0)
267                 die("unable to write rerere record");
268         return 0;
269 }
270
271 /*
272  * "rerere" interacts with conflicted file contents using this I/O
273  * abstraction.  It reads a conflicted contents from one place via
274  * "getline()" method, and optionally can write it out after
275  * normalizing the conflicted hunks to the "output".  Subclasses of
276  * rerere_io embed this structure at the beginning of their own
277  * rerere_io object.
278  */
279 struct rerere_io {
280         int (*getline)(struct strbuf *, struct rerere_io *);
281         FILE *output;
282         int wrerror;
283         /* some more stuff */
284 };
285
286 static void ferr_write(const void *p, size_t count, FILE *fp, int *err)
287 {
288         if (!count || *err)
289                 return;
290         if (fwrite(p, count, 1, fp) != 1)
291                 *err = errno;
292 }
293
294 static inline void ferr_puts(const char *s, FILE *fp, int *err)
295 {
296         ferr_write(s, strlen(s), fp, err);
297 }
298
299 static void rerere_io_putstr(const char *str, struct rerere_io *io)
300 {
301         if (io->output)
302                 ferr_puts(str, io->output, &io->wrerror);
303 }
304
305 /*
306  * Write a conflict marker to io->output (if defined).
307  */
308 static void rerere_io_putconflict(int ch, int size, struct rerere_io *io)
309 {
310         char buf[64];
311
312         while (size) {
313                 if (size <= sizeof(buf) - 2) {
314                         memset(buf, ch, size);
315                         buf[size] = '\n';
316                         buf[size + 1] = '\0';
317                         size = 0;
318                 } else {
319                         int sz = sizeof(buf) - 1;
320
321                         /*
322                          * Make sure we will not write everything out
323                          * in this round by leaving at least 1 byte
324                          * for the next round, giving the next round
325                          * a chance to add the terminating LF.  Yuck.
326                          */
327                         if (size <= sz)
328                                 sz -= (sz - size) + 1;
329                         memset(buf, ch, sz);
330                         buf[sz] = '\0';
331                         size -= sz;
332                 }
333                 rerere_io_putstr(buf, io);
334         }
335 }
336
337 static void rerere_io_putmem(const char *mem, size_t sz, struct rerere_io *io)
338 {
339         if (io->output)
340                 ferr_write(mem, sz, io->output, &io->wrerror);
341 }
342
343 /*
344  * Subclass of rerere_io that reads from an on-disk file
345  */
346 struct rerere_io_file {
347         struct rerere_io io;
348         FILE *input;
349 };
350
351 /*
352  * ... and its getline() method implementation
353  */
354 static int rerere_file_getline(struct strbuf *sb, struct rerere_io *io_)
355 {
356         struct rerere_io_file *io = (struct rerere_io_file *)io_;
357         return strbuf_getwholeline(sb, io->input, '\n');
358 }
359
360 /*
361  * Require the exact number of conflict marker letters, no more, no
362  * less, followed by SP or any whitespace
363  * (including LF).
364  */
365 static int is_cmarker(char *buf, int marker_char, int marker_size)
366 {
367         int want_sp;
368
369         /*
370          * The beginning of our version and the end of their version
371          * always are labeled like "<<<<< ours" or ">>>>> theirs",
372          * hence we set want_sp for them.  Note that the version from
373          * the common ancestor in diff3-style output is not always
374          * labelled (e.g. "||||| common" is often seen but "|||||"
375          * alone is also valid), so we do not set want_sp.
376          */
377         want_sp = (marker_char == '<') || (marker_char == '>');
378
379         while (marker_size--)
380                 if (*buf++ != marker_char)
381                         return 0;
382         if (want_sp && *buf != ' ')
383                 return 0;
384         return isspace(*buf);
385 }
386
387 /*
388  * Read contents a file with conflicts, normalize the conflicts
389  * by (1) discarding the common ancestor version in diff3-style,
390  * (2) reordering our side and their side so that whichever sorts
391  * alphabetically earlier comes before the other one, while
392  * computing the "conflict ID", which is just an SHA-1 hash of
393  * one side of the conflict, NUL, the other side of the conflict,
394  * and NUL concatenated together.
395  *
396  * Return the number of conflict hunks found.
397  *
398  * NEEDSWORK: the logic and theory of operation behind this conflict
399  * normalization may deserve to be documented somewhere, perhaps in
400  * Documentation/technical/rerere.txt.
401  */
402 static int handle_path(unsigned char *sha1, struct rerere_io *io, int marker_size)
403 {
404         git_SHA_CTX ctx;
405         int hunk_no = 0;
406         enum {
407                 RR_CONTEXT = 0, RR_SIDE_1, RR_SIDE_2, RR_ORIGINAL
408         } hunk = RR_CONTEXT;
409         struct strbuf one = STRBUF_INIT, two = STRBUF_INIT;
410         struct strbuf buf = STRBUF_INIT;
411
412         if (sha1)
413                 git_SHA1_Init(&ctx);
414
415         while (!io->getline(&buf, io)) {
416                 if (is_cmarker(buf.buf, '<', marker_size)) {
417                         if (hunk != RR_CONTEXT)
418                                 goto bad;
419                         hunk = RR_SIDE_1;
420                 } else if (is_cmarker(buf.buf, '|', marker_size)) {
421                         if (hunk != RR_SIDE_1)
422                                 goto bad;
423                         hunk = RR_ORIGINAL;
424                 } else if (is_cmarker(buf.buf, '=', marker_size)) {
425                         if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL)
426                                 goto bad;
427                         hunk = RR_SIDE_2;
428                 } else if (is_cmarker(buf.buf, '>', marker_size)) {
429                         if (hunk != RR_SIDE_2)
430                                 goto bad;
431                         if (strbuf_cmp(&one, &two) > 0)
432                                 strbuf_swap(&one, &two);
433                         hunk_no++;
434                         hunk = RR_CONTEXT;
435                         rerere_io_putconflict('<', marker_size, io);
436                         rerere_io_putmem(one.buf, one.len, io);
437                         rerere_io_putconflict('=', marker_size, io);
438                         rerere_io_putmem(two.buf, two.len, io);
439                         rerere_io_putconflict('>', marker_size, io);
440                         if (sha1) {
441                                 git_SHA1_Update(&ctx, one.buf ? one.buf : "",
442                                             one.len + 1);
443                                 git_SHA1_Update(&ctx, two.buf ? two.buf : "",
444                                             two.len + 1);
445                         }
446                         strbuf_reset(&one);
447                         strbuf_reset(&two);
448                 } else if (hunk == RR_SIDE_1)
449                         strbuf_addbuf(&one, &buf);
450                 else if (hunk == RR_ORIGINAL)
451                         ; /* discard */
452                 else if (hunk == RR_SIDE_2)
453                         strbuf_addbuf(&two, &buf);
454                 else
455                         rerere_io_putstr(buf.buf, io);
456                 continue;
457         bad:
458                 hunk = 99; /* force error exit */
459                 break;
460         }
461         strbuf_release(&one);
462         strbuf_release(&two);
463         strbuf_release(&buf);
464
465         if (sha1)
466                 git_SHA1_Final(sha1, &ctx);
467         if (hunk != RR_CONTEXT)
468                 return -1;
469         return hunk_no;
470 }
471
472 /*
473  * Scan the path for conflicts, do the "handle_path()" thing above, and
474  * return the number of conflict hunks found.
475  */
476 static int handle_file(const char *path, unsigned char *sha1, const char *output)
477 {
478         int hunk_no = 0;
479         struct rerere_io_file io;
480         int marker_size = ll_merge_marker_size(path);
481
482         memset(&io, 0, sizeof(io));
483         io.io.getline = rerere_file_getline;
484         io.input = fopen(path, "r");
485         io.io.wrerror = 0;
486         if (!io.input)
487                 return error_errno("Could not open %s", path);
488
489         if (output) {
490                 io.io.output = fopen(output, "w");
491                 if (!io.io.output) {
492                         error_errno("Could not write %s", output);
493                         fclose(io.input);
494                         return -1;
495                 }
496         }
497
498         hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size);
499
500         fclose(io.input);
501         if (io.io.wrerror)
502                 error("There were errors while writing %s (%s)",
503                       path, strerror(io.io.wrerror));
504         if (io.io.output && fclose(io.io.output))
505                 io.io.wrerror = error_errno("Failed to flush %s", path);
506
507         if (hunk_no < 0) {
508                 if (output)
509                         unlink_or_warn(output);
510                 return error("Could not parse conflict hunks in %s", path);
511         }
512         if (io.io.wrerror)
513                 return -1;
514         return hunk_no;
515 }
516
517 /*
518  * Look at a cache entry at "i" and see if it is not conflicting,
519  * conflicting and we are willing to handle, or conflicting and
520  * we are unable to handle, and return the determination in *type.
521  * Return the cache index to be looked at next, by skipping the
522  * stages we have already looked at in this invocation of this
523  * function.
524  */
525 static int check_one_conflict(int i, int *type)
526 {
527         const struct cache_entry *e = active_cache[i];
528
529         if (!ce_stage(e)) {
530                 *type = RESOLVED;
531                 return i + 1;
532         }
533
534         *type = PUNTED;
535         while (ce_stage(active_cache[i]) == 1)
536                 i++;
537
538         /* Only handle regular files with both stages #2 and #3 */
539         if (i + 1 < active_nr) {
540                 const struct cache_entry *e2 = active_cache[i];
541                 const struct cache_entry *e3 = active_cache[i + 1];
542                 if (ce_stage(e2) == 2 &&
543                     ce_stage(e3) == 3 &&
544                     ce_same_name(e, e3) &&
545                     S_ISREG(e2->ce_mode) &&
546                     S_ISREG(e3->ce_mode))
547                         *type = THREE_STAGED;
548         }
549
550         /* Skip the entries with the same name */
551         while (i < active_nr && ce_same_name(e, active_cache[i]))
552                 i++;
553         return i;
554 }
555
556 /*
557  * Scan the index and find paths that have conflicts that rerere can
558  * handle, i.e. the ones that has both stages #2 and #3.
559  *
560  * NEEDSWORK: we do not record or replay a previous "resolve by
561  * deletion" for a delete-modify conflict, as that is inherently risky
562  * without knowing what modification is being discarded.  The only
563  * safe case, i.e. both side doing the deletion and modification that
564  * are identical to the previous round, might want to be handled,
565  * though.
566  */
567 static int find_conflict(struct string_list *conflict)
568 {
569         int i;
570         if (read_cache() < 0)
571                 return error("Could not read index");
572
573         for (i = 0; i < active_nr;) {
574                 int conflict_type;
575                 const struct cache_entry *e = active_cache[i];
576                 i = check_one_conflict(i, &conflict_type);
577                 if (conflict_type == THREE_STAGED)
578                         string_list_insert(conflict, (const char *)e->name);
579         }
580         return 0;
581 }
582
583 /*
584  * The merge_rr list is meant to hold outstanding conflicted paths
585  * that rerere could handle.  Abuse the list by adding other types of
586  * entries to allow the caller to show "rerere remaining".
587  *
588  * - Conflicted paths that rerere does not handle are added
589  * - Conflicted paths that have been resolved are marked as such
590  *   by storing RERERE_RESOLVED to .util field (where conflict ID
591  *   is expected to be stored).
592  *
593  * Do *not* write MERGE_RR file out after calling this function.
594  *
595  * NEEDSWORK: we may want to fix the caller that implements "rerere
596  * remaining" to do this without abusing merge_rr.
597  */
598 int rerere_remaining(struct string_list *merge_rr)
599 {
600         int i;
601         if (setup_rerere(merge_rr, RERERE_READONLY))
602                 return 0;
603         if (read_cache() < 0)
604                 return error("Could not read index");
605
606         for (i = 0; i < active_nr;) {
607                 int conflict_type;
608                 const struct cache_entry *e = active_cache[i];
609                 i = check_one_conflict(i, &conflict_type);
610                 if (conflict_type == PUNTED)
611                         string_list_insert(merge_rr, (const char *)e->name);
612                 else if (conflict_type == RESOLVED) {
613                         struct string_list_item *it;
614                         it = string_list_lookup(merge_rr, (const char *)e->name);
615                         if (it != NULL) {
616                                 free_rerere_id(it);
617                                 it->util = RERERE_RESOLVED;
618                         }
619                 }
620         }
621         return 0;
622 }
623
624 /*
625  * Try using the given conflict resolution "ID" to see
626  * if that recorded conflict resolves cleanly what we
627  * got in the "cur".
628  */
629 static int try_merge(const struct rerere_id *id, const char *path,
630                      mmfile_t *cur, mmbuffer_t *result)
631 {
632         int ret;
633         mmfile_t base = {NULL, 0}, other = {NULL, 0};
634
635         if (read_mmfile(&base, rerere_path(id, "preimage")) ||
636             read_mmfile(&other, rerere_path(id, "postimage")))
637                 ret = 1;
638         else
639                 /*
640                  * A three-way merge. Note that this honors user-customizable
641                  * low-level merge driver settings.
642                  */
643                 ret = ll_merge(result, path, &base, NULL, cur, "", &other, "", NULL);
644
645         free(base.ptr);
646         free(other.ptr);
647
648         return ret;
649 }
650
651 /*
652  * Find the conflict identified by "id"; the change between its
653  * "preimage" (i.e. a previous contents with conflict markers) and its
654  * "postimage" (i.e. the corresponding contents with conflicts
655  * resolved) may apply cleanly to the contents stored in "path", i.e.
656  * the conflict this time around.
657  *
658  * Returns 0 for successful replay of recorded resolution, or non-zero
659  * for failure.
660  */
661 static int merge(const struct rerere_id *id, const char *path)
662 {
663         FILE *f;
664         int ret;
665         mmfile_t cur = {NULL, 0};
666         mmbuffer_t result = {NULL, 0};
667
668         /*
669          * Normalize the conflicts in path and write it out to
670          * "thisimage" temporary file.
671          */
672         if ((handle_file(path, NULL, rerere_path(id, "thisimage")) < 0) ||
673             read_mmfile(&cur, rerere_path(id, "thisimage"))) {
674                 ret = 1;
675                 goto out;
676         }
677
678         ret = try_merge(id, path, &cur, &result);
679         if (ret)
680                 goto out;
681
682         /*
683          * A successful replay of recorded resolution.
684          * Mark that "postimage" was used to help gc.
685          */
686         if (utime(rerere_path(id, "postimage"), NULL) < 0)
687                 warning_errno("failed utime() on %s",
688                               rerere_path(id, "postimage"));
689
690         /* Update "path" with the resolution */
691         f = fopen(path, "w");
692         if (!f)
693                 return error_errno("Could not open %s", path);
694         if (fwrite(result.ptr, result.size, 1, f) != 1)
695                 error_errno("Could not write %s", path);
696         if (fclose(f))
697                 return error_errno("Writing %s failed", path);
698
699 out:
700         free(cur.ptr);
701         free(result.ptr);
702
703         return ret;
704 }
705
706 static struct lock_file index_lock;
707
708 static void update_paths(struct string_list *update)
709 {
710         int i;
711
712         hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
713
714         for (i = 0; i < update->nr; i++) {
715                 struct string_list_item *item = &update->items[i];
716                 if (add_file_to_cache(item->string, 0))
717                         exit(128);
718                 fprintf(stderr, "Staged '%s' using previous resolution.\n",
719                         item->string);
720         }
721
722         if (write_locked_index(&the_index, &index_lock,
723                                COMMIT_LOCK | SKIP_IF_UNCHANGED))
724                 die("Unable to write new index file");
725 }
726
727 static void remove_variant(struct rerere_id *id)
728 {
729         unlink_or_warn(rerere_path(id, "postimage"));
730         unlink_or_warn(rerere_path(id, "preimage"));
731         id->collection->status[id->variant] = 0;
732 }
733
734 /*
735  * The path indicated by rr_item may still have conflict for which we
736  * have a recorded resolution, in which case replay it and optionally
737  * update it.  Or it may have been resolved by the user and we may
738  * only have the preimage for that conflict, in which case the result
739  * needs to be recorded as a resolution in a postimage file.
740  */
741 static void do_rerere_one_path(struct string_list_item *rr_item,
742                                struct string_list *update)
743 {
744         const char *path = rr_item->string;
745         struct rerere_id *id = rr_item->util;
746         struct rerere_dir *rr_dir = id->collection;
747         int variant;
748
749         variant = id->variant;
750
751         /* Has the user resolved it already? */
752         if (variant >= 0) {
753                 if (!handle_file(path, NULL, NULL)) {
754                         copy_file(rerere_path(id, "postimage"), path, 0666);
755                         id->collection->status[variant] |= RR_HAS_POSTIMAGE;
756                         fprintf(stderr, "Recorded resolution for '%s'.\n", path);
757                         free_rerere_id(rr_item);
758                         rr_item->util = NULL;
759                         return;
760                 }
761                 /*
762                  * There may be other variants that can cleanly
763                  * replay.  Try them and update the variant number for
764                  * this one.
765                  */
766         }
767
768         /* Does any existing resolution apply cleanly? */
769         for (variant = 0; variant < rr_dir->status_nr; variant++) {
770                 const int both = RR_HAS_PREIMAGE | RR_HAS_POSTIMAGE;
771                 struct rerere_id vid = *id;
772
773                 if ((rr_dir->status[variant] & both) != both)
774                         continue;
775
776                 vid.variant = variant;
777                 if (merge(&vid, path))
778                         continue; /* failed to replay */
779
780                 /*
781                  * If there already is a different variant that applies
782                  * cleanly, there is no point maintaining our own variant.
783                  */
784                 if (0 <= id->variant && id->variant != variant)
785                         remove_variant(id);
786
787                 if (rerere_autoupdate)
788                         string_list_insert(update, path);
789                 else
790                         fprintf(stderr,
791                                 "Resolved '%s' using previous resolution.\n",
792                                 path);
793                 free_rerere_id(rr_item);
794                 rr_item->util = NULL;
795                 return;
796         }
797
798         /* None of the existing one applies; we need a new variant */
799         assign_variant(id);
800
801         variant = id->variant;
802         handle_file(path, NULL, rerere_path(id, "preimage"));
803         if (id->collection->status[variant] & RR_HAS_POSTIMAGE) {
804                 const char *path = rerere_path(id, "postimage");
805                 if (unlink(path))
806                         die_errno("cannot unlink stray '%s'", path);
807                 id->collection->status[variant] &= ~RR_HAS_POSTIMAGE;
808         }
809         id->collection->status[variant] |= RR_HAS_PREIMAGE;
810         fprintf(stderr, "Recorded preimage for '%s'\n", path);
811 }
812
813 static int do_plain_rerere(struct string_list *rr, int fd)
814 {
815         struct string_list conflict = STRING_LIST_INIT_DUP;
816         struct string_list update = STRING_LIST_INIT_DUP;
817         int i;
818
819         find_conflict(&conflict);
820
821         /*
822          * MERGE_RR records paths with conflicts immediately after
823          * merge failed.  Some of the conflicted paths might have been
824          * hand resolved in the working tree since then, but the
825          * initial run would catch all and register their preimages.
826          */
827         for (i = 0; i < conflict.nr; i++) {
828                 struct rerere_id *id;
829                 unsigned char sha1[20];
830                 const char *path = conflict.items[i].string;
831                 int ret;
832
833                 if (string_list_has_string(rr, path))
834                         continue;
835
836                 /*
837                  * Ask handle_file() to scan and assign a
838                  * conflict ID.  No need to write anything out
839                  * yet.
840                  */
841                 ret = handle_file(path, sha1, NULL);
842                 if (ret < 1)
843                         continue;
844
845                 id = new_rerere_id(sha1);
846                 string_list_insert(rr, path)->util = id;
847
848                 /* Ensure that the directory exists. */
849                 mkdir_in_gitdir(rerere_path(id, NULL));
850         }
851
852         for (i = 0; i < rr->nr; i++)
853                 do_rerere_one_path(&rr->items[i], &update);
854
855         if (update.nr)
856                 update_paths(&update);
857
858         return write_rr(rr, fd);
859 }
860
861 static void git_rerere_config(void)
862 {
863         git_config_get_bool("rerere.enabled", &rerere_enabled);
864         git_config_get_bool("rerere.autoupdate", &rerere_autoupdate);
865         git_config(git_default_config, NULL);
866 }
867
868 static GIT_PATH_FUNC(git_path_rr_cache, "rr-cache")
869
870 static int is_rerere_enabled(void)
871 {
872         int rr_cache_exists;
873
874         if (!rerere_enabled)
875                 return 0;
876
877         rr_cache_exists = is_directory(git_path_rr_cache());
878         if (rerere_enabled < 0)
879                 return rr_cache_exists;
880
881         if (!rr_cache_exists && mkdir_in_gitdir(git_path_rr_cache()))
882                 die("Could not create directory %s", git_path_rr_cache());
883         return 1;
884 }
885
886 int setup_rerere(struct string_list *merge_rr, int flags)
887 {
888         int fd;
889
890         git_rerere_config();
891         if (!is_rerere_enabled())
892                 return -1;
893
894         if (flags & (RERERE_AUTOUPDATE|RERERE_NOAUTOUPDATE))
895                 rerere_autoupdate = !!(flags & RERERE_AUTOUPDATE);
896         if (flags & RERERE_READONLY)
897                 fd = 0;
898         else
899                 fd = hold_lock_file_for_update(&write_lock, git_path_merge_rr(),
900                                                LOCK_DIE_ON_ERROR);
901         read_rr(merge_rr);
902         return fd;
903 }
904
905 /*
906  * The main entry point that is called internally from codepaths that
907  * perform mergy operations, possibly leaving conflicted index entries
908  * and working tree files.
909  */
910 int rerere(int flags)
911 {
912         struct string_list merge_rr = STRING_LIST_INIT_DUP;
913         int fd, status;
914
915         fd = setup_rerere(&merge_rr, flags);
916         if (fd < 0)
917                 return 0;
918         status = do_plain_rerere(&merge_rr, fd);
919         free_rerere_dirs();
920         return status;
921 }
922
923 /*
924  * Subclass of rerere_io that reads from an in-core buffer that is a
925  * strbuf
926  */
927 struct rerere_io_mem {
928         struct rerere_io io;
929         struct strbuf input;
930 };
931
932 /*
933  * ... and its getline() method implementation
934  */
935 static int rerere_mem_getline(struct strbuf *sb, struct rerere_io *io_)
936 {
937         struct rerere_io_mem *io = (struct rerere_io_mem *)io_;
938         char *ep;
939         size_t len;
940
941         strbuf_release(sb);
942         if (!io->input.len)
943                 return -1;
944         ep = memchr(io->input.buf, '\n', io->input.len);
945         if (!ep)
946                 ep = io->input.buf + io->input.len;
947         else if (*ep == '\n')
948                 ep++;
949         len = ep - io->input.buf;
950         strbuf_add(sb, io->input.buf, len);
951         strbuf_remove(&io->input, 0, len);
952         return 0;
953 }
954
955 static int handle_cache(const char *path, unsigned char *sha1, const char *output)
956 {
957         mmfile_t mmfile[3] = {{NULL}};
958         mmbuffer_t result = {NULL, 0};
959         const struct cache_entry *ce;
960         int pos, len, i, hunk_no;
961         struct rerere_io_mem io;
962         int marker_size = ll_merge_marker_size(path);
963
964         /*
965          * Reproduce the conflicted merge in-core
966          */
967         len = strlen(path);
968         pos = cache_name_pos(path, len);
969         if (0 <= pos)
970                 return -1;
971         pos = -pos - 1;
972
973         while (pos < active_nr) {
974                 enum object_type type;
975                 unsigned long size;
976
977                 ce = active_cache[pos++];
978                 if (ce_namelen(ce) != len || memcmp(ce->name, path, len))
979                         break;
980                 i = ce_stage(ce) - 1;
981                 if (!mmfile[i].ptr) {
982                         mmfile[i].ptr = read_object_file(&ce->oid, &type,
983                                                          &size);
984                         mmfile[i].size = size;
985                 }
986         }
987         for (i = 0; i < 3; i++)
988                 if (!mmfile[i].ptr && !mmfile[i].size)
989                         mmfile[i].ptr = xstrdup("");
990
991         /*
992          * NEEDSWORK: handle conflicts from merges with
993          * merge.renormalize set, too?
994          */
995         ll_merge(&result, path, &mmfile[0], NULL,
996                  &mmfile[1], "ours",
997                  &mmfile[2], "theirs", NULL);
998         for (i = 0; i < 3; i++)
999                 free(mmfile[i].ptr);
1000
1001         memset(&io, 0, sizeof(io));
1002         io.io.getline = rerere_mem_getline;
1003         if (output)
1004                 io.io.output = fopen(output, "w");
1005         else
1006                 io.io.output = NULL;
1007         strbuf_init(&io.input, 0);
1008         strbuf_attach(&io.input, result.ptr, result.size, result.size);
1009
1010         /*
1011          * Grab the conflict ID and optionally write the original
1012          * contents with conflict markers out.
1013          */
1014         hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size);
1015         strbuf_release(&io.input);
1016         if (io.io.output)
1017                 fclose(io.io.output);
1018         return hunk_no;
1019 }
1020
1021 static int rerere_forget_one_path(const char *path, struct string_list *rr)
1022 {
1023         const char *filename;
1024         struct rerere_id *id;
1025         unsigned char sha1[20];
1026         int ret;
1027         struct string_list_item *item;
1028
1029         /*
1030          * Recreate the original conflict from the stages in the
1031          * index and compute the conflict ID
1032          */
1033         ret = handle_cache(path, sha1, NULL);
1034         if (ret < 1)
1035                 return error("Could not parse conflict hunks in '%s'", path);
1036
1037         /* Nuke the recorded resolution for the conflict */
1038         id = new_rerere_id(sha1);
1039
1040         for (id->variant = 0;
1041              id->variant < id->collection->status_nr;
1042              id->variant++) {
1043                 mmfile_t cur = { NULL, 0 };
1044                 mmbuffer_t result = {NULL, 0};
1045                 int cleanly_resolved;
1046
1047                 if (!has_rerere_resolution(id))
1048                         continue;
1049
1050                 handle_cache(path, sha1, rerere_path(id, "thisimage"));
1051                 if (read_mmfile(&cur, rerere_path(id, "thisimage"))) {
1052                         free(cur.ptr);
1053                         error("Failed to update conflicted state in '%s'", path);
1054                         goto fail_exit;
1055                 }
1056                 cleanly_resolved = !try_merge(id, path, &cur, &result);
1057                 free(result.ptr);
1058                 free(cur.ptr);
1059                 if (cleanly_resolved)
1060                         break;
1061         }
1062
1063         if (id->collection->status_nr <= id->variant) {
1064                 error("no remembered resolution for '%s'", path);
1065                 goto fail_exit;
1066         }
1067
1068         filename = rerere_path(id, "postimage");
1069         if (unlink(filename)) {
1070                 if (errno == ENOENT)
1071                         error("no remembered resolution for %s", path);
1072                 else
1073                         error_errno("cannot unlink %s", filename);
1074                 goto fail_exit;
1075         }
1076
1077         /*
1078          * Update the preimage so that the user can resolve the
1079          * conflict in the working tree, run us again to record
1080          * the postimage.
1081          */
1082         handle_cache(path, sha1, rerere_path(id, "preimage"));
1083         fprintf(stderr, "Updated preimage for '%s'\n", path);
1084
1085         /*
1086          * And remember that we can record resolution for this
1087          * conflict when the user is done.
1088          */
1089         item = string_list_insert(rr, path);
1090         free_rerere_id(item);
1091         item->util = id;
1092         fprintf(stderr, "Forgot resolution for %s\n", path);
1093         return 0;
1094
1095 fail_exit:
1096         free(id);
1097         return -1;
1098 }
1099
1100 int rerere_forget(struct pathspec *pathspec)
1101 {
1102         int i, fd;
1103         struct string_list conflict = STRING_LIST_INIT_DUP;
1104         struct string_list merge_rr = STRING_LIST_INIT_DUP;
1105
1106         if (read_cache() < 0)
1107                 return error("Could not read index");
1108
1109         fd = setup_rerere(&merge_rr, RERERE_NOAUTOUPDATE);
1110         if (fd < 0)
1111                 return 0;
1112
1113         /*
1114          * The paths may have been resolved (incorrectly);
1115          * recover the original conflicted state and then
1116          * find the conflicted paths.
1117          */
1118         unmerge_cache(pathspec);
1119         find_conflict(&conflict);
1120         for (i = 0; i < conflict.nr; i++) {
1121                 struct string_list_item *it = &conflict.items[i];
1122                 if (!match_pathspec(pathspec, it->string,
1123                                     strlen(it->string), 0, NULL, 0))
1124                         continue;
1125                 rerere_forget_one_path(it->string, &merge_rr);
1126         }
1127         return write_rr(&merge_rr, fd);
1128 }
1129
1130 /*
1131  * Garbage collection support
1132  */
1133
1134 static timestamp_t rerere_created_at(struct rerere_id *id)
1135 {
1136         struct stat st;
1137
1138         return stat(rerere_path(id, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
1139 }
1140
1141 static timestamp_t rerere_last_used_at(struct rerere_id *id)
1142 {
1143         struct stat st;
1144
1145         return stat(rerere_path(id, "postimage"), &st) ? (time_t) 0 : st.st_mtime;
1146 }
1147
1148 /*
1149  * Remove the recorded resolution for a given conflict ID
1150  */
1151 static void unlink_rr_item(struct rerere_id *id)
1152 {
1153         unlink_or_warn(rerere_path(id, "thisimage"));
1154         remove_variant(id);
1155         id->collection->status[id->variant] = 0;
1156 }
1157
1158 static void prune_one(struct rerere_id *id,
1159                       timestamp_t cutoff_resolve, timestamp_t cutoff_noresolve)
1160 {
1161         timestamp_t then;
1162         timestamp_t cutoff;
1163
1164         then = rerere_last_used_at(id);
1165         if (then)
1166                 cutoff = cutoff_resolve;
1167         else {
1168                 then = rerere_created_at(id);
1169                 if (!then)
1170                         return;
1171                 cutoff = cutoff_noresolve;
1172         }
1173         if (then < cutoff)
1174                 unlink_rr_item(id);
1175 }
1176
1177 void rerere_gc(struct string_list *rr)
1178 {
1179         struct string_list to_remove = STRING_LIST_INIT_DUP;
1180         DIR *dir;
1181         struct dirent *e;
1182         int i;
1183         timestamp_t now = time(NULL);
1184         timestamp_t cutoff_noresolve = now - 15 * 86400;
1185         timestamp_t cutoff_resolve = now - 60 * 86400;
1186
1187         if (setup_rerere(rr, 0) < 0)
1188                 return;
1189
1190         git_config_get_expiry_in_days("gc.rerereresolved", &cutoff_resolve, now);
1191         git_config_get_expiry_in_days("gc.rerereunresolved", &cutoff_noresolve, now);
1192         git_config(git_default_config, NULL);
1193         dir = opendir(git_path("rr-cache"));
1194         if (!dir)
1195                 die_errno("unable to open rr-cache directory");
1196         /* Collect stale conflict IDs ... */
1197         while ((e = readdir(dir))) {
1198                 struct rerere_dir *rr_dir;
1199                 struct rerere_id id;
1200                 int now_empty;
1201
1202                 if (is_dot_or_dotdot(e->d_name))
1203                         continue;
1204                 rr_dir = find_rerere_dir(e->d_name);
1205                 if (!rr_dir)
1206                         continue; /* or should we remove e->d_name? */
1207
1208                 now_empty = 1;
1209                 for (id.variant = 0, id.collection = rr_dir;
1210                      id.variant < id.collection->status_nr;
1211                      id.variant++) {
1212                         prune_one(&id, cutoff_resolve, cutoff_noresolve);
1213                         if (id.collection->status[id.variant])
1214                                 now_empty = 0;
1215                 }
1216                 if (now_empty)
1217                         string_list_append(&to_remove, e->d_name);
1218         }
1219         closedir(dir);
1220
1221         /* ... and then remove the empty directories */
1222         for (i = 0; i < to_remove.nr; i++)
1223                 rmdir(git_path("rr-cache/%s", to_remove.items[i].string));
1224         string_list_clear(&to_remove, 0);
1225         rollback_lock_file(&write_lock);
1226 }
1227
1228 /*
1229  * During a conflict resolution, after "rerere" recorded the
1230  * preimages, abandon them if the user did not resolve them or
1231  * record their resolutions.  And drop $GIT_DIR/MERGE_RR.
1232  *
1233  * NEEDSWORK: shouldn't we be calling this from "reset --hard"?
1234  */
1235 void rerere_clear(struct string_list *merge_rr)
1236 {
1237         int i;
1238
1239         if (setup_rerere(merge_rr, 0) < 0)
1240                 return;
1241
1242         for (i = 0; i < merge_rr->nr; i++) {
1243                 struct rerere_id *id = merge_rr->items[i].util;
1244                 if (!has_rerere_resolution(id)) {
1245                         unlink_rr_item(id);
1246                         rmdir(rerere_path(id, NULL));
1247                 }
1248         }
1249         unlink_or_warn(git_path_merge_rr());
1250         rollback_lock_file(&write_lock);
1251 }