rerere: explain the rerere I/O abstraction
[git/git.git] / rerere.c
CommitLineData
5b2fd956 1#include "cache.h"
697cc8ef 2#include "lockfile.h"
c455c87c 3#include "string-list.h"
5b2fd956 4#include "rerere.h"
5b2fd956 5#include "xdiff-interface.h"
dea4562b
JH
6#include "dir.h"
7#include "resolve-undo.h"
8#include "ll-merge.h"
8588567c 9#include "attr.h"
01a10b0a 10#include "pathspec.h"
5b2fd956 11
ac49f5ca
MZ
12#define RESOLVED 0
13#define PUNTED 1
14#define THREE_STAGED 2
15void *RERERE_RESOLVED = &RERERE_RESOLVED;
16
5b2fd956
SB
17/* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
18static int rerere_enabled = -1;
19
20/* automatically update cleanly resolved paths to the index */
21static int rerere_autoupdate;
22
23static char *merge_rr_path;
24
90056966 25const char *rerere_path(const char *hex, const char *file)
5b2fd956 26{
90056966 27 return git_path("rr-cache/%s/%s", hex, file);
5b2fd956
SB
28}
29
7e0d4ab5 30static int has_rerere_resolution(const char *hex)
5b2fd956
SB
31{
32 struct stat st;
90056966 33 return !stat(rerere_path(hex, "postimage"), &st);
5b2fd956
SB
34}
35
c455c87c 36static void read_rr(struct string_list *rr)
5b2fd956 37{
f5800f6a 38 struct strbuf buf = STRBUF_INIT;
5b2fd956 39 FILE *in = fopen(merge_rr_path, "r");
f5800f6a 40
5b2fd956
SB
41 if (!in)
42 return;
f5800f6a
JH
43 while (!strbuf_getwholeline(&buf, in, '\0')) {
44 char *path;
45 unsigned char sha1[20];
46
47 /* There has to be the hash, tab, path and then NUL */
48 if (buf.len < 42 || get_sha1_hex(buf.buf, sha1))
5b2fd956 49 die("corrupt MERGE_RR");
f5800f6a
JH
50
51 if (buf.buf[40] != '\t')
5b2fd956 52 die("corrupt MERGE_RR");
f5800f6a
JH
53 buf.buf[40] = '\0';
54 path = buf.buf + 41;
55
56 string_list_insert(rr, path)->util = xstrdup(buf.buf);
5b2fd956 57 }
f5800f6a 58 strbuf_release(&buf);
5b2fd956
SB
59 fclose(in);
60}
61
62static struct lock_file write_lock;
63
c455c87c 64static int write_rr(struct string_list *rr, int out_fd)
5b2fd956
SB
65{
66 int i;
67 for (i = 0; i < rr->nr; i++) {
e2cb6a95
JH
68 struct strbuf buf = STRBUF_INIT;
69
70 assert(rr->items[i].util != RERERE_RESOLVED);
5b2fd956
SB
71 if (!rr->items[i].util)
72 continue;
e2cb6a95
JH
73 strbuf_addf(&buf, "%s\t%s%c",
74 (char *)rr->items[i].util,
75 rr->items[i].string, 0);
76 if (write_in_full(out_fd, buf.buf, buf.len) != buf.len)
5b2fd956 77 die("unable to write rerere record");
e2cb6a95
JH
78
79 strbuf_release(&buf);
5b2fd956
SB
80 }
81 if (commit_lock_file(&write_lock) != 0)
82 die("unable to write rerere record");
83 return 0;
84}
85
a96847cc
JH
86/*
87 * "rerere" interacts with conflicted file contents using this I/O
88 * abstraction. It reads a conflicted contents from one place via
89 * "getline()" method, and optionally can write it out after
90 * normalizing the conflicted hunks to the "output". Subclasses of
91 * rerere_io embed this structure at the beginning of their own
92 * rerere_io object.
93 */
94struct rerere_io {
95 int (*getline)(struct strbuf *, struct rerere_io *);
96 FILE *output;
97 int wrerror;
98 /* some more stuff */
99};
100
47d32af2
AR
101static void ferr_write(const void *p, size_t count, FILE *fp, int *err)
102{
103 if (!count || *err)
104 return;
105 if (fwrite(p, count, 1, fp) != 1)
106 *err = errno;
107}
108
109static inline void ferr_puts(const char *s, FILE *fp, int *err)
110{
111 ferr_write(s, strlen(s), fp, err);
112}
113
27d6b085
JH
114static void rerere_io_putstr(const char *str, struct rerere_io *io)
115{
116 if (io->output)
117 ferr_puts(str, io->output, &io->wrerror);
118}
119
a96847cc
JH
120/*
121 * Write a conflict marker to io->output (if defined).
122 */
191f2417
JH
123static void rerere_io_putconflict(int ch, int size, struct rerere_io *io)
124{
125 char buf[64];
126
127 while (size) {
128 if (size < sizeof(buf) - 2) {
129 memset(buf, ch, size);
130 buf[size] = '\n';
131 buf[size + 1] = '\0';
132 size = 0;
133 } else {
134 int sz = sizeof(buf) - 1;
135 if (size <= sz)
136 sz -= (sz - size) + 1;
137 memset(buf, ch, sz);
138 buf[sz] = '\0';
139 size -= sz;
140 }
141 rerere_io_putstr(buf, io);
142 }
143}
144
27d6b085
JH
145static void rerere_io_putmem(const char *mem, size_t sz, struct rerere_io *io)
146{
147 if (io->output)
148 ferr_write(mem, sz, io->output, &io->wrerror);
149}
150
a96847cc
JH
151/*
152 * Subclass of rerere_io that reads from an on-disk file
153 */
27d6b085
JH
154struct rerere_io_file {
155 struct rerere_io io;
156 FILE *input;
157};
158
a96847cc
JH
159/*
160 * ... and its getline() method implementation
161 */
27d6b085
JH
162static int rerere_file_getline(struct strbuf *sb, struct rerere_io *io_)
163{
164 struct rerere_io_file *io = (struct rerere_io_file *)io_;
165 return strbuf_getwholeline(sb, io->input, '\n');
166}
167
67711cdc
JH
168/*
169 * Require the exact number of conflict marker letters, no more, no
170 * less, followed by SP or any whitespace
171 * (including LF).
172 */
173static int is_cmarker(char *buf, int marker_char, int marker_size)
191f2417 174{
67711cdc
JH
175 int want_sp;
176
177 /*
178 * The beginning of our version and the end of their version
179 * always are labeled like "<<<<< ours" or ">>>>> theirs",
180 * hence we set want_sp for them. Note that the version from
181 * the common ancestor in diff3-style output is not always
182 * labelled (e.g. "||||| common" is often seen but "|||||"
183 * alone is also valid), so we do not set want_sp.
184 */
185 want_sp = (marker_char == '<') || (marker_char == '>');
186
191f2417
JH
187 while (marker_size--)
188 if (*buf++ != marker_char)
189 return 0;
190 if (want_sp && *buf != ' ')
191 return 0;
192 return isspace(*buf);
193}
194
195static int handle_path(unsigned char *sha1, struct rerere_io *io, int marker_size)
5b2fd956 196{
9126f009 197 git_SHA_CTX ctx;
cc58d7df
JH
198 int hunk_no = 0;
199 enum {
4b05548f 200 RR_CONTEXT = 0, RR_SIDE_1, RR_SIDE_2, RR_ORIGINAL
cc58d7df 201 } hunk = RR_CONTEXT;
f285a2d7 202 struct strbuf one = STRBUF_INIT, two = STRBUF_INIT;
d58ee6db 203 struct strbuf buf = STRBUF_INIT;
5b2fd956
SB
204
205 if (sha1)
9126f009 206 git_SHA1_Init(&ctx);
5b2fd956 207
27d6b085 208 while (!io->getline(&buf, io)) {
67711cdc 209 if (is_cmarker(buf.buf, '<', marker_size)) {
cc58d7df 210 if (hunk != RR_CONTEXT)
5b2fd956 211 goto bad;
cc58d7df 212 hunk = RR_SIDE_1;
67711cdc 213 } else if (is_cmarker(buf.buf, '|', marker_size)) {
cc58d7df 214 if (hunk != RR_SIDE_1)
5b2fd956 215 goto bad;
387c9d49 216 hunk = RR_ORIGINAL;
67711cdc 217 } else if (is_cmarker(buf.buf, '=', marker_size)) {
387c9d49 218 if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL)
5b2fd956 219 goto bad;
cc58d7df 220 hunk = RR_SIDE_2;
67711cdc 221 } else if (is_cmarker(buf.buf, '>', marker_size)) {
cc58d7df 222 if (hunk != RR_SIDE_2)
5b2fd956
SB
223 goto bad;
224 if (strbuf_cmp(&one, &two) > 0)
225 strbuf_swap(&one, &two);
226 hunk_no++;
cc58d7df 227 hunk = RR_CONTEXT;
191f2417 228 rerere_io_putconflict('<', marker_size, io);
27d6b085 229 rerere_io_putmem(one.buf, one.len, io);
191f2417 230 rerere_io_putconflict('=', marker_size, io);
27d6b085 231 rerere_io_putmem(two.buf, two.len, io);
191f2417 232 rerere_io_putconflict('>', marker_size, io);
5b2fd956 233 if (sha1) {
9126f009 234 git_SHA1_Update(&ctx, one.buf ? one.buf : "",
5b2fd956 235 one.len + 1);
9126f009 236 git_SHA1_Update(&ctx, two.buf ? two.buf : "",
5b2fd956
SB
237 two.len + 1);
238 }
239 strbuf_reset(&one);
240 strbuf_reset(&two);
cc58d7df 241 } else if (hunk == RR_SIDE_1)
e992d1eb 242 strbuf_addbuf(&one, &buf);
387c9d49
JH
243 else if (hunk == RR_ORIGINAL)
244 ; /* discard */
cc58d7df 245 else if (hunk == RR_SIDE_2)
e992d1eb 246 strbuf_addbuf(&two, &buf);
27d6b085
JH
247 else
248 rerere_io_putstr(buf.buf, io);
5b2fd956
SB
249 continue;
250 bad:
251 hunk = 99; /* force error exit */
252 break;
253 }
254 strbuf_release(&one);
255 strbuf_release(&two);
d58ee6db 256 strbuf_release(&buf);
5b2fd956 257
5b2fd956 258 if (sha1)
9126f009 259 git_SHA1_Final(sha1, &ctx);
27d6b085
JH
260 if (hunk != RR_CONTEXT)
261 return -1;
262 return hunk_no;
263}
264
265static int handle_file(const char *path, unsigned char *sha1, const char *output)
266{
267 int hunk_no = 0;
268 struct rerere_io_file io;
8588567c 269 int marker_size = ll_merge_marker_size(path);
27d6b085
JH
270
271 memset(&io, 0, sizeof(io));
272 io.io.getline = rerere_file_getline;
273 io.input = fopen(path, "r");
274 io.io.wrerror = 0;
275 if (!io.input)
276 return error("Could not open %s", path);
277
278 if (output) {
279 io.io.output = fopen(output, "w");
280 if (!io.io.output) {
281 fclose(io.input);
282 return error("Could not write %s", output);
283 }
284 }
285
191f2417 286 hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size);
27d6b085
JH
287
288 fclose(io.input);
289 if (io.io.wrerror)
290 error("There were errors while writing %s (%s)",
291 path, strerror(io.io.wrerror));
292 if (io.io.output && fclose(io.io.output))
293 io.io.wrerror = error("Failed to flush %s: %s",
294 path, strerror(errno));
295
296 if (hunk_no < 0) {
5b2fd956 297 if (output)
691f1a28 298 unlink_or_warn(output);
5b2fd956
SB
299 return error("Could not parse conflict hunks in %s", path);
300 }
27d6b085 301 if (io.io.wrerror)
47d32af2 302 return -1;
5b2fd956
SB
303 return hunk_no;
304}
305
a96847cc
JH
306/*
307 * Subclass of rerere_io that reads from an in-core buffer that is a
308 * strbuf
309 */
dea4562b
JH
310struct rerere_io_mem {
311 struct rerere_io io;
312 struct strbuf input;
313};
314
a96847cc
JH
315/*
316 * ... and its getline() method implementation
317 */
dea4562b
JH
318static int rerere_mem_getline(struct strbuf *sb, struct rerere_io *io_)
319{
320 struct rerere_io_mem *io = (struct rerere_io_mem *)io_;
321 char *ep;
322 size_t len;
323
324 strbuf_release(sb);
325 if (!io->input.len)
326 return -1;
53d8afaf
JS
327 ep = memchr(io->input.buf, '\n', io->input.len);
328 if (!ep)
329 ep = io->input.buf + io->input.len;
330 else if (*ep == '\n')
dea4562b
JH
331 ep++;
332 len = ep - io->input.buf;
333 strbuf_add(sb, io->input.buf, len);
334 strbuf_remove(&io->input, 0, len);
335 return 0;
336}
337
338static int handle_cache(const char *path, unsigned char *sha1, const char *output)
339{
b9e31f59 340 mmfile_t mmfile[3] = {{NULL}};
dea4562b 341 mmbuffer_t result = {NULL, 0};
9c5e6c80 342 const struct cache_entry *ce;
dea4562b
JH
343 int pos, len, i, hunk_no;
344 struct rerere_io_mem io;
8588567c 345 int marker_size = ll_merge_marker_size(path);
dea4562b
JH
346
347 /*
348 * Reproduce the conflicted merge in-core
349 */
350 len = strlen(path);
351 pos = cache_name_pos(path, len);
352 if (0 <= pos)
47d32af2 353 return -1;
dea4562b
JH
354 pos = -pos - 1;
355
74444d4e 356 while (pos < active_nr) {
dea4562b
JH
357 enum object_type type;
358 unsigned long size;
359
dea4562b 360 ce = active_cache[pos++];
b9e31f59 361 if (ce_namelen(ce) != len || memcmp(ce->name, path, len))
74444d4e
JH
362 break;
363 i = ce_stage(ce) - 1;
7d4053b6
JH
364 if (!mmfile[i].ptr) {
365 mmfile[i].ptr = read_sha1_file(ce->sha1, &type, &size);
366 mmfile[i].size = size;
367 }
dea4562b 368 }
74444d4e 369 for (i = 0; i < 3; i++)
dea4562b
JH
370 if (!mmfile[i].ptr && !mmfile[i].size)
371 mmfile[i].ptr = xstrdup("");
74444d4e 372
18b037a5
JN
373 /*
374 * NEEDSWORK: handle conflicts from merges with
375 * merge.renormalize set, too
376 */
f01de62e 377 ll_merge(&result, path, &mmfile[0], NULL,
dea4562b 378 &mmfile[1], "ours",
712516bc 379 &mmfile[2], "theirs", NULL);
dea4562b
JH
380 for (i = 0; i < 3; i++)
381 free(mmfile[i].ptr);
382
af86debc 383 memset(&io, 0, sizeof(io));
dea4562b
JH
384 io.io.getline = rerere_mem_getline;
385 if (output)
386 io.io.output = fopen(output, "w");
387 else
388 io.io.output = NULL;
389 strbuf_init(&io.input, 0);
390 strbuf_attach(&io.input, result.ptr, result.size, result.size);
391
191f2417 392 hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size);
dea4562b
JH
393 strbuf_release(&io.input);
394 if (io.io.output)
395 fclose(io.io.output);
5b2fd956
SB
396 return hunk_no;
397}
398
ac49f5ca 399static int check_one_conflict(int i, int *type)
5b2fd956 400{
9c5e6c80 401 const struct cache_entry *e = active_cache[i];
ac49f5ca
MZ
402
403 if (!ce_stage(e)) {
404 *type = RESOLVED;
405 return i + 1;
406 }
407
408 *type = PUNTED;
5eda906b 409 while (ce_stage(active_cache[i]) == 1)
fb70a06d 410 i++;
ac49f5ca
MZ
411
412 /* Only handle regular files with both stages #2 and #3 */
413 if (i + 1 < active_nr) {
9c5e6c80
NTND
414 const struct cache_entry *e2 = active_cache[i];
415 const struct cache_entry *e3 = active_cache[i + 1];
5b2fd956
SB
416 if (ce_stage(e2) == 2 &&
417 ce_stage(e3) == 3 &&
ac49f5ca 418 ce_same_name(e, e3) &&
5b2fd956 419 S_ISREG(e2->ce_mode) &&
ac49f5ca
MZ
420 S_ISREG(e3->ce_mode))
421 *type = THREE_STAGED;
422 }
423
424 /* Skip the entries with the same name */
425 while (i < active_nr && ce_same_name(e, active_cache[i]))
426 i++;
427 return i;
428}
429
430static int find_conflict(struct string_list *conflict)
431{
432 int i;
433 if (read_cache() < 0)
434 return error("Could not read index");
435
436 for (i = 0; i < active_nr;) {
437 int conflict_type;
9c5e6c80 438 const struct cache_entry *e = active_cache[i];
ac49f5ca
MZ
439 i = check_one_conflict(i, &conflict_type);
440 if (conflict_type == THREE_STAGED)
441 string_list_insert(conflict, (const char *)e->name);
442 }
443 return 0;
444}
445
446int rerere_remaining(struct string_list *merge_rr)
447{
448 int i;
449 if (read_cache() < 0)
450 return error("Could not read index");
451
452 for (i = 0; i < active_nr;) {
453 int conflict_type;
9c5e6c80 454 const struct cache_entry *e = active_cache[i];
ac49f5ca
MZ
455 i = check_one_conflict(i, &conflict_type);
456 if (conflict_type == PUNTED)
457 string_list_insert(merge_rr, (const char *)e->name);
458 else if (conflict_type == RESOLVED) {
459 struct string_list_item *it;
460 it = string_list_lookup(merge_rr, (const char *)e->name);
461 if (it != NULL) {
462 free(it->util);
463 it->util = RERERE_RESOLVED;
464 }
5b2fd956
SB
465 }
466 }
467 return 0;
468}
469
470static int merge(const char *name, const char *path)
471{
472 int ret;
689b8c29 473 mmfile_t cur = {NULL, 0}, base = {NULL, 0}, other = {NULL, 0};
5b2fd956 474 mmbuffer_t result = {NULL, 0};
5b2fd956 475
90056966 476 if (handle_file(path, NULL, rerere_path(name, "thisimage")) < 0)
5b2fd956
SB
477 return 1;
478
90056966
SG
479 if (read_mmfile(&cur, rerere_path(name, "thisimage")) ||
480 read_mmfile(&base, rerere_path(name, "preimage")) ||
689b8c29
BW
481 read_mmfile(&other, rerere_path(name, "postimage"))) {
482 ret = 1;
483 goto out;
484 }
1e4cd68c 485 ret = ll_merge(&result, path, &base, NULL, &cur, "", &other, "", NULL);
5b2fd956 486 if (!ret) {
7d7ff15b
SG
487 FILE *f;
488
489 if (utime(rerere_path(name, "postimage"), NULL) < 0)
490 warning("failed utime() on %s: %s",
491 rerere_path(name, "postimage"),
492 strerror(errno));
493 f = fopen(path, "w");
5b2fd956 494 if (!f)
47d32af2
AR
495 return error("Could not open %s: %s", path,
496 strerror(errno));
497 if (fwrite(result.ptr, result.size, 1, f) != 1)
498 error("Could not write %s: %s", path, strerror(errno));
499 if (fclose(f))
500 return error("Writing %s failed: %s", path,
501 strerror(errno));
5b2fd956
SB
502 }
503
689b8c29 504out:
5b2fd956
SB
505 free(cur.ptr);
506 free(base.ptr);
507 free(other.ptr);
508 free(result.ptr);
509
510 return ret;
511}
512
513static struct lock_file index_lock;
514
89ea9035 515static void update_paths(struct string_list *update)
5b2fd956
SB
516{
517 int i;
5b2fd956 518
89ea9035 519 hold_locked_index(&index_lock, 1);
5b2fd956
SB
520
521 for (i = 0; i < update->nr; i++) {
c455c87c 522 struct string_list_item *item = &update->items[i];
89ea9035
JN
523 if (add_file_to_cache(item->string, 0))
524 exit(128);
a14c7ab8
JH
525 fprintf(stderr, "Staged '%s' using previous resolution.\n",
526 item->string);
5b2fd956
SB
527 }
528
89ea9035 529 if (active_cache_changed) {
03b86647 530 if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK))
5b2fd956 531 die("Unable to write new index file");
89ea9035 532 } else
5b2fd956 533 rollback_lock_file(&index_lock);
5b2fd956
SB
534}
535
c455c87c 536static int do_plain_rerere(struct string_list *rr, int fd)
5b2fd956 537{
183113a5
TF
538 struct string_list conflict = STRING_LIST_INIT_DUP;
539 struct string_list update = STRING_LIST_INIT_DUP;
5b2fd956
SB
540 int i;
541
542 find_conflict(&conflict);
543
544 /*
545 * MERGE_RR records paths with conflicts immediately after merge
546 * failed. Some of the conflicted paths might have been hand resolved
547 * in the working tree since then, but the initial run would catch all
548 * and register their preimages.
549 */
550
551 for (i = 0; i < conflict.nr; i++) {
c455c87c
JS
552 const char *path = conflict.items[i].string;
553 if (!string_list_has_string(rr, path)) {
5b2fd956
SB
554 unsigned char sha1[20];
555 char *hex;
556 int ret;
557 ret = handle_file(path, sha1, NULL);
558 if (ret < 1)
559 continue;
560 hex = xstrdup(sha1_to_hex(sha1));
78a395d3 561 string_list_insert(rr, path)->util = hex;
fd956338 562 if (mkdir_in_gitdir(git_path("rr-cache/%s", hex)))
ba19a808 563 continue;
90056966 564 handle_file(path, NULL, rerere_path(hex, "preimage"));
5b2fd956
SB
565 fprintf(stderr, "Recorded preimage for '%s'\n", path);
566 }
567 }
568
569 /*
570 * Now some of the paths that had conflicts earlier might have been
571 * hand resolved. Others may be similar to a conflict already that
572 * was resolved before.
573 */
574
575 for (i = 0; i < rr->nr; i++) {
576 int ret;
c455c87c 577 const char *path = rr->items[i].string;
5b2fd956
SB
578 const char *name = (const char *)rr->items[i].util;
579
90056966 580 if (has_rerere_resolution(name)) {
a14c7ab8
JH
581 if (merge(name, path))
582 continue;
583
584 if (rerere_autoupdate)
585 string_list_insert(&update, path);
586 else
587 fprintf(stderr,
588 "Resolved '%s' using previous resolution.\n",
589 path);
590 goto mark_resolved;
5b2fd956
SB
591 }
592
593 /* Let's see if we have resolved it. */
594 ret = handle_file(path, NULL, NULL);
595 if (ret)
596 continue;
597
598 fprintf(stderr, "Recorded resolution for '%s'.\n", path);
90056966 599 copy_file(rerere_path(name, "postimage"), path, 0666);
5b2fd956 600 mark_resolved:
8d9b5a4a 601 free(rr->items[i].util);
5b2fd956
SB
602 rr->items[i].util = NULL;
603 }
604
605 if (update.nr)
606 update_paths(&update);
607
608 return write_rr(rr, fd);
609}
610
633e5ad3 611static void git_rerere_config(void)
5b2fd956 612{
633e5ad3
TA
613 git_config_get_bool("rerere.enabled", &rerere_enabled);
614 git_config_get_bool("rerere.autoupdate", &rerere_autoupdate);
615 git_config(git_default_config, NULL);
5b2fd956
SB
616}
617
618static int is_rerere_enabled(void)
619{
5b2fd956
SB
620 const char *rr_cache;
621 int rr_cache_exists;
622
623 if (!rerere_enabled)
624 return 0;
625
626 rr_cache = git_path("rr-cache");
90b4a71c 627 rr_cache_exists = is_directory(rr_cache);
5b2fd956
SB
628 if (rerere_enabled < 0)
629 return rr_cache_exists;
630
90a6464b 631 if (!rr_cache_exists && mkdir_in_gitdir(rr_cache))
5b2fd956
SB
632 die("Could not create directory %s", rr_cache);
633 return 1;
634}
635
cb6020bb 636int setup_rerere(struct string_list *merge_rr, int flags)
5b2fd956
SB
637{
638 int fd;
639
633e5ad3 640 git_rerere_config();
5b2fd956
SB
641 if (!is_rerere_enabled())
642 return -1;
643
cb6020bb
JH
644 if (flags & (RERERE_AUTOUPDATE|RERERE_NOAUTOUPDATE))
645 rerere_autoupdate = !!(flags & RERERE_AUTOUPDATE);
a4f34cbb 646 merge_rr_path = git_pathdup("MERGE_RR");
acd3b9ec
JH
647 fd = hold_lock_file_for_update(&write_lock, merge_rr_path,
648 LOCK_DIE_ON_ERROR);
5b2fd956
SB
649 read_rr(merge_rr);
650 return fd;
651}
652
cb6020bb 653int rerere(int flags)
5b2fd956 654{
183113a5 655 struct string_list merge_rr = STRING_LIST_INIT_DUP;
5b2fd956
SB
656 int fd;
657
cb6020bb 658 fd = setup_rerere(&merge_rr, flags);
5b2fd956
SB
659 if (fd < 0)
660 return 0;
661 return do_plain_rerere(&merge_rr, fd);
662}
dea4562b
JH
663
664static int rerere_forget_one_path(const char *path, struct string_list *rr)
665{
666 const char *filename;
667 char *hex;
668 unsigned char sha1[20];
669 int ret;
8d9b5a4a 670 struct string_list_item *item;
dea4562b
JH
671
672 ret = handle_cache(path, sha1, NULL);
673 if (ret < 1)
674 return error("Could not parse conflict hunks in '%s'", path);
675 hex = xstrdup(sha1_to_hex(sha1));
676 filename = rerere_path(hex, "postimage");
677 if (unlink(filename))
678 return (errno == ENOENT
679 ? error("no remembered resolution for %s", path)
680 : error("cannot unlink %s: %s", filename, strerror(errno)));
681
682 handle_cache(path, sha1, rerere_path(hex, "preimage"));
683 fprintf(stderr, "Updated preimage for '%s'\n", path);
684
8d9b5a4a
JH
685 item = string_list_insert(rr, path);
686 free(item->util);
687 item->util = hex;
dea4562b
JH
688 fprintf(stderr, "Forgot resolution for %s\n", path);
689 return 0;
690}
691
01a10b0a 692int rerere_forget(struct pathspec *pathspec)
dea4562b
JH
693{
694 int i, fd;
183113a5
TF
695 struct string_list conflict = STRING_LIST_INIT_DUP;
696 struct string_list merge_rr = STRING_LIST_INIT_DUP;
dea4562b
JH
697
698 if (read_cache() < 0)
699 return error("Could not read index");
700
6751e047 701 fd = setup_rerere(&merge_rr, RERERE_NOAUTOUPDATE);
dea4562b
JH
702
703 unmerge_cache(pathspec);
704 find_conflict(&conflict);
705 for (i = 0; i < conflict.nr; i++) {
706 struct string_list_item *it = &conflict.items[i];
854b0959 707 if (!match_pathspec(pathspec, it->string,
ae8d0824 708 strlen(it->string), 0, NULL, 0))
dea4562b
JH
709 continue;
710 rerere_forget_one_path(it->string, &merge_rr);
711 }
712 return write_rr(&merge_rr, fd);
713}
0f891e7d
JH
714
715static time_t rerere_created_at(const char *name)
716{
717 struct stat st;
718 return stat(rerere_path(name, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
719}
720
721static time_t rerere_last_used_at(const char *name)
722{
723 struct stat st;
724 return stat(rerere_path(name, "postimage"), &st) ? (time_t) 0 : st.st_mtime;
725}
726
727static void unlink_rr_item(const char *name)
728{
729 unlink(rerere_path(name, "thisimage"));
730 unlink(rerere_path(name, "preimage"));
731 unlink(rerere_path(name, "postimage"));
732 rmdir(git_path("rr-cache/%s", name));
733}
734
0f891e7d
JH
735void rerere_gc(struct string_list *rr)
736{
737 struct string_list to_remove = STRING_LIST_INIT_DUP;
738 DIR *dir;
739 struct dirent *e;
740 int i, cutoff;
741 time_t now = time(NULL), then;
633e5ad3
TA
742 int cutoff_noresolve = 15;
743 int cutoff_resolve = 60;
0f891e7d 744
633e5ad3
TA
745 git_config_get_int("gc.rerereresolved", &cutoff_resolve);
746 git_config_get_int("gc.rerereunresolved", &cutoff_noresolve);
747 git_config(git_default_config, NULL);
0f891e7d
JH
748 dir = opendir(git_path("rr-cache"));
749 if (!dir)
750 die_errno("unable to open rr-cache directory");
751 while ((e = readdir(dir))) {
752 if (is_dot_or_dotdot(e->d_name))
753 continue;
754
755 then = rerere_last_used_at(e->d_name);
756 if (then) {
633e5ad3 757 cutoff = cutoff_resolve;
0f891e7d
JH
758 } else {
759 then = rerere_created_at(e->d_name);
760 if (!then)
761 continue;
633e5ad3 762 cutoff = cutoff_noresolve;
0f891e7d
JH
763 }
764 if (then < now - cutoff * 86400)
765 string_list_append(&to_remove, e->d_name);
766 }
a9930e35 767 closedir(dir);
0f891e7d
JH
768 for (i = 0; i < to_remove.nr; i++)
769 unlink_rr_item(to_remove.items[i].string);
770 string_list_clear(&to_remove, 0);
771}
772
773void rerere_clear(struct string_list *merge_rr)
774{
775 int i;
776
777 for (i = 0; i < merge_rr->nr; i++) {
778 const char *name = (const char *)merge_rr->items[i].util;
779 if (!has_rerere_resolution(name))
780 unlink_rr_item(name);
781 }
782 unlink_or_warn(git_path("MERGE_RR"));
783}