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