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