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