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