]> git.ipfire.org Git - thirdparty/git.git/blame - rerere.c
Merge branch 'js/empty-index-fixes'
[thirdparty/git.git] / rerere.c
CommitLineData
bc5c5ec0 1#include "git-compat-util.h"
0b027f6c 2#include "abspath.h"
36bf1958 3#include "alloc.h"
b2141fc1 4#include "config.h"
d5fff46f 5#include "copy.h"
f394e093 6#include "gettext.h"
41771fa4 7#include "hex.h"
697cc8ef 8#include "lockfile.h"
c455c87c 9#include "string-list.h"
08c46a49 10#include "read-cache-ll.h"
5b2fd956 11#include "rerere.h"
5b2fd956 12#include "xdiff-interface.h"
dea4562b
JH
13#include "dir.h"
14#include "resolve-undo.h"
67238999 15#include "merge-ll.h"
8588567c 16#include "attr.h"
c339932b 17#include "path.h"
01a10b0a 18#include "pathspec.h"
87bed179 19#include "object-file.h"
a034e910 20#include "object-store-ll.h"
bc626927 21#include "hash-lookup.h"
680ff910 22#include "strmap.h"
d5ebb50d 23#include "wrapper.h"
5b2fd956 24
ac49f5ca
MZ
25#define RESOLVED 0
26#define PUNTED 1
27#define THREE_STAGED 2
28void *RERERE_RESOLVED = &RERERE_RESOLVED;
29
5b2fd956
SB
30/* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
31static int rerere_enabled = -1;
32
33/* automatically update cleanly resolved paths to the index */
34static int rerere_autoupdate;
35
2c7929b1
JH
36#define RR_HAS_POSTIMAGE 1
37#define RR_HAS_PREIMAGE 2
680ff910 38struct rerere_dir {
a13d1370
JH
39 int status_alloc, status_nr;
40 unsigned char *status;
680ff910
JK
41 char name[FLEX_ARRAY];
42};
43
44static struct strmap rerere_dirs = STRMAP_INIT;
1869bbe1
JH
45
46static void free_rerere_dirs(void)
47{
680ff910
JK
48 struct hashmap_iter iter;
49 struct strmap_entry *ent;
50
51 strmap_for_each_entry(&rerere_dirs, &iter, ent) {
52 struct rerere_dir *rr_dir = ent->value;
53 free(rr_dir->status);
54 free(rr_dir);
a13d1370 55 }
680ff910 56 strmap_clear(&rerere_dirs, 0);
1869bbe1
JH
57}
58
1d51eced 59static void free_rerere_id(struct string_list_item *item)
5b2fd956 60{
1d51eced 61 free(item->util);
5b2fd956
SB
62}
63
1d51eced
JH
64static const char *rerere_id_hex(const struct rerere_id *id)
65{
680ff910 66 return id->collection->name;
1d51eced
JH
67}
68
a13d1370
JH
69static void fit_variant(struct rerere_dir *rr_dir, int variant)
70{
71 variant++;
72 ALLOC_GROW(rr_dir->status, variant, rr_dir->status_alloc);
73 if (rr_dir->status_nr < variant) {
74 memset(rr_dir->status + rr_dir->status_nr,
75 '\0', variant - rr_dir->status_nr);
76 rr_dir->status_nr = variant;
77 }
78}
79
80static void assign_variant(struct rerere_id *id)
81{
82 int variant;
83 struct rerere_dir *rr_dir = id->collection;
84
85 variant = id->variant;
86 if (variant < 0) {
629716d2
JH
87 for (variant = 0; variant < rr_dir->status_nr; variant++)
88 if (!rr_dir->status[variant])
89 break;
a13d1370
JH
90 }
91 fit_variant(rr_dir, variant);
92 id->variant = variant;
1d51eced
JH
93}
94
95const char *rerere_path(const struct rerere_id *id, const char *file)
96{
97 if (!file)
98 return git_path("rr-cache/%s", rerere_id_hex(id));
99
a13d1370
JH
100 if (id->variant <= 0)
101 return git_path("rr-cache/%s/%s", rerere_id_hex(id), file);
102
103 return git_path("rr-cache/%s/%s.%d",
104 rerere_id_hex(id), file, id->variant);
5b2fd956
SB
105}
106
a13d1370 107static int is_rr_file(const char *name, const char *filename, int *variant)
2c7929b1 108{
a13d1370
JH
109 const char *suffix;
110 char *ep;
111
112 if (!strcmp(name, filename)) {
113 *variant = 0;
114 return 1;
115 }
116 if (!skip_prefix(name, filename, &suffix) || *suffix != '.')
117 return 0;
118
119 errno = 0;
120 *variant = strtol(suffix + 1, &ep, 10);
121 if (errno || *ep)
122 return 0;
123 return 1;
2c7929b1
JH
124}
125
126static void scan_rerere_dir(struct rerere_dir *rr_dir)
127{
128 struct dirent *de;
680ff910 129 DIR *dir = opendir(git_path("rr-cache/%s", rr_dir->name));
2c7929b1
JH
130
131 if (!dir)
132 return;
133 while ((de = readdir(dir)) != NULL) {
a13d1370
JH
134 int variant;
135
136 if (is_rr_file(de->d_name, "postimage", &variant)) {
137 fit_variant(rr_dir, variant);
138 rr_dir->status[variant] |= RR_HAS_POSTIMAGE;
139 } else if (is_rr_file(de->d_name, "preimage", &variant)) {
140 fit_variant(rr_dir, variant);
141 rr_dir->status[variant] |= RR_HAS_PREIMAGE;
142 }
2c7929b1
JH
143 }
144 closedir(dir);
145}
146
1869bbe1
JH
147static struct rerere_dir *find_rerere_dir(const char *hex)
148{
1869bbe1 149 struct rerere_dir *rr_dir;
680ff910
JK
150
151 rr_dir = strmap_get(&rerere_dirs, hex);
152 if (!rr_dir) {
153 FLEX_ALLOC_STR(rr_dir, name, hex);
a13d1370
JH
154 rr_dir->status = NULL;
155 rr_dir->status_nr = 0;
156 rr_dir->status_alloc = 0;
680ff910
JK
157 strmap_put(&rerere_dirs, hex, rr_dir);
158
2c7929b1 159 scan_rerere_dir(rr_dir);
1869bbe1 160 }
680ff910 161 return rr_dir;
5b2fd956
SB
162}
163
1d51eced 164static int has_rerere_resolution(const struct rerere_id *id)
5b2fd956 165{
05dd9f13 166 const int both = RR_HAS_POSTIMAGE|RR_HAS_PREIMAGE;
a13d1370 167 int variant = id->variant;
1d51eced 168
a13d1370
JH
169 if (variant < 0)
170 return 0;
171 return ((id->collection->status[variant] & both) == both);
5b2fd956
SB
172}
173
1d51eced
JH
174static struct rerere_id *new_rerere_id_hex(char *hex)
175{
176 struct rerere_id *id = xmalloc(sizeof(*id));
1869bbe1 177 id->collection = find_rerere_dir(hex);
a13d1370 178 id->variant = -1; /* not known yet */
1d51eced
JH
179 return id;
180}
181
3f34d70d 182static struct rerere_id *new_rerere_id(unsigned char *hash)
1d51eced 183{
3f34d70d 184 return new_rerere_id_hex(hash_to_hex(hash));
5b2fd956
SB
185}
186
4b68c2a0
JH
187/*
188 * $GIT_DIR/MERGE_RR file is a collection of records, each of which is
189 * "conflict ID", a HT and pathname, terminated with a NUL, and is
190 * used to keep track of the set of paths that "rerere" may need to
191 * work on (i.e. what is left by the previous invocation of "git
192 * rerere" during the current conflict resolution session).
193 */
55e6b354 194static void read_rr(struct repository *r, struct string_list *rr)
5b2fd956 195{
f5800f6a 196 struct strbuf buf = STRBUF_INIT;
55e6b354 197 FILE *in = fopen_or_warn(git_path_merge_rr(r), "r");
f5800f6a 198
5b2fd956
SB
199 if (!in)
200 return;
f5800f6a
JH
201 while (!strbuf_getwholeline(&buf, in, '\0')) {
202 char *path;
0d7c419a 203 unsigned char hash[GIT_MAX_RAWSZ];
1d51eced 204 struct rerere_id *id;
a13d1370 205 int variant;
0d7c419a 206 const unsigned hexsz = the_hash_algo->hexsz;
f5800f6a
JH
207
208 /* There has to be the hash, tab, path and then NUL */
0d7c419a 209 if (buf.len < hexsz + 2 || get_sha1_hex(buf.buf, hash))
2373b650 210 die(_("corrupt MERGE_RR"));
f5800f6a 211
0d7c419a 212 if (buf.buf[hexsz] != '.') {
a13d1370 213 variant = 0;
0d7c419a 214 path = buf.buf + hexsz;
a13d1370
JH
215 } else {
216 errno = 0;
0d7c419a 217 variant = strtol(buf.buf + hexsz + 1, &path, 10);
a13d1370 218 if (errno)
2373b650 219 die(_("corrupt MERGE_RR"));
a13d1370
JH
220 }
221 if (*(path++) != '\t')
2373b650 222 die(_("corrupt MERGE_RR"));
0d7c419a 223 buf.buf[hexsz] = '\0';
1d51eced 224 id = new_rerere_id_hex(buf.buf);
a13d1370 225 id->variant = variant;
1d51eced 226 string_list_insert(rr, path)->util = id;
5b2fd956 227 }
f5800f6a 228 strbuf_release(&buf);
5b2fd956
SB
229 fclose(in);
230}
231
232static struct lock_file write_lock;
233
c455c87c 234static int write_rr(struct string_list *rr, int out_fd)
5b2fd956
SB
235{
236 int i;
237 for (i = 0; i < rr->nr; i++) {
e2cb6a95 238 struct strbuf buf = STRBUF_INIT;
1d51eced 239 struct rerere_id *id;
e2cb6a95
JH
240
241 assert(rr->items[i].util != RERERE_RESOLVED);
1d51eced
JH
242
243 id = rr->items[i].util;
244 if (!id)
5b2fd956 245 continue;
a13d1370
JH
246 assert(id->variant >= 0);
247 if (0 < id->variant)
248 strbuf_addf(&buf, "%s.%d\t%s%c",
249 rerere_id_hex(id), id->variant,
250 rr->items[i].string, 0);
251 else
252 strbuf_addf(&buf, "%s\t%s%c",
253 rerere_id_hex(id),
254 rr->items[i].string, 0);
255
06f46f23 256 if (write_in_full(out_fd, buf.buf, buf.len) < 0)
2373b650 257 die(_("unable to write rerere record"));
e2cb6a95
JH
258
259 strbuf_release(&buf);
5b2fd956
SB
260 }
261 if (commit_lock_file(&write_lock) != 0)
2373b650 262 die(_("unable to write rerere record"));
5b2fd956
SB
263 return 0;
264}
265
a96847cc
JH
266/*
267 * "rerere" interacts with conflicted file contents using this I/O
268 * abstraction. It reads a conflicted contents from one place via
269 * "getline()" method, and optionally can write it out after
270 * normalizing the conflicted hunks to the "output". Subclasses of
271 * rerere_io embed this structure at the beginning of their own
272 * rerere_io object.
273 */
274struct rerere_io {
275 int (*getline)(struct strbuf *, struct rerere_io *);
276 FILE *output;
277 int wrerror;
278 /* some more stuff */
279};
280
47d32af2
AR
281static void ferr_write(const void *p, size_t count, FILE *fp, int *err)
282{
283 if (!count || *err)
284 return;
285 if (fwrite(p, count, 1, fp) != 1)
286 *err = errno;
287}
288
289static inline void ferr_puts(const char *s, FILE *fp, int *err)
290{
291 ferr_write(s, strlen(s), fp, err);
292}
293
27d6b085
JH
294static void rerere_io_putstr(const char *str, struct rerere_io *io)
295{
296 if (io->output)
297 ferr_puts(str, io->output, &io->wrerror);
298}
299
300static void rerere_io_putmem(const char *mem, size_t sz, struct rerere_io *io)
301{
302 if (io->output)
303 ferr_write(mem, sz, io->output, &io->wrerror);
304}
305
a96847cc
JH
306/*
307 * Subclass of rerere_io that reads from an on-disk file
308 */
27d6b085
JH
309struct rerere_io_file {
310 struct rerere_io io;
311 FILE *input;
312};
313
a96847cc
JH
314/*
315 * ... and its getline() method implementation
316 */
27d6b085
JH
317static int rerere_file_getline(struct strbuf *sb, struct rerere_io *io_)
318{
319 struct rerere_io_file *io = (struct rerere_io_file *)io_;
320 return strbuf_getwholeline(sb, io->input, '\n');
321}
322
67711cdc
JH
323/*
324 * Require the exact number of conflict marker letters, no more, no
325 * less, followed by SP or any whitespace
326 * (including LF).
327 */
328static int is_cmarker(char *buf, int marker_char, int marker_size)
191f2417 329{
67711cdc
JH
330 int want_sp;
331
332 /*
333 * The beginning of our version and the end of their version
334 * always are labeled like "<<<<< ours" or ">>>>> theirs",
335 * hence we set want_sp for them. Note that the version from
336 * the common ancestor in diff3-style output is not always
337 * labelled (e.g. "||||| common" is often seen but "|||||"
338 * alone is also valid), so we do not set want_sp.
339 */
340 want_sp = (marker_char == '<') || (marker_char == '>');
341
191f2417
JH
342 while (marker_size--)
343 if (*buf++ != marker_char)
344 return 0;
345 if (want_sp && *buf != ' ')
346 return 0;
347 return isspace(*buf);
348}
349
5ebbdad3
TG
350static void rerere_strbuf_putconflict(struct strbuf *buf, int ch, size_t size)
351{
352 strbuf_addchars(buf, ch, size);
353 strbuf_addch(buf, '\n');
354}
355
356static int handle_conflict(struct strbuf *out, struct rerere_io *io,
0d7c419a 357 int marker_size, git_hash_ctx *ctx)
5b2fd956 358{
cc58d7df 359 enum {
c0f16f8e
TG
360 RR_SIDE_1 = 0, RR_SIDE_2, RR_ORIGINAL
361 } hunk = RR_SIDE_1;
f285a2d7 362 struct strbuf one = STRBUF_INIT, two = STRBUF_INIT;
4af32207 363 struct strbuf buf = STRBUF_INIT, conflict = STRBUF_INIT;
c0f16f8e 364 int has_conflicts = -1;
5b2fd956 365
27d6b085 366 while (!io->getline(&buf, io)) {
67711cdc 367 if (is_cmarker(buf.buf, '<', marker_size)) {
4af32207
TG
368 if (handle_conflict(&conflict, io, marker_size, NULL) < 0)
369 break;
370 if (hunk == RR_SIDE_1)
371 strbuf_addbuf(&one, &conflict);
372 else
373 strbuf_addbuf(&two, &conflict);
374 strbuf_release(&conflict);
67711cdc 375 } else if (is_cmarker(buf.buf, '|', marker_size)) {
cc58d7df 376 if (hunk != RR_SIDE_1)
c0f16f8e 377 break;
387c9d49 378 hunk = RR_ORIGINAL;
67711cdc 379 } else if (is_cmarker(buf.buf, '=', marker_size)) {
387c9d49 380 if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL)
c0f16f8e 381 break;
cc58d7df 382 hunk = RR_SIDE_2;
67711cdc 383 } else if (is_cmarker(buf.buf, '>', marker_size)) {
cc58d7df 384 if (hunk != RR_SIDE_2)
c0f16f8e 385 break;
5b2fd956
SB
386 if (strbuf_cmp(&one, &two) > 0)
387 strbuf_swap(&one, &two);
221444f5 388 has_conflicts = 1;
5ebbdad3
TG
389 rerere_strbuf_putconflict(out, '<', marker_size);
390 strbuf_addbuf(out, &one);
391 rerere_strbuf_putconflict(out, '=', marker_size);
392 strbuf_addbuf(out, &two);
393 rerere_strbuf_putconflict(out, '>', marker_size);
c0f16f8e 394 if (ctx) {
0d7c419a 395 the_hash_algo->update_fn(ctx, one.buf ?
396 one.buf : "",
397 one.len + 1);
398 the_hash_algo->update_fn(ctx, two.buf ?
399 two.buf : "",
400 two.len + 1);
5b2fd956 401 }
c0f16f8e 402 break;
cc58d7df 403 } else if (hunk == RR_SIDE_1)
e992d1eb 404 strbuf_addbuf(&one, &buf);
387c9d49
JH
405 else if (hunk == RR_ORIGINAL)
406 ; /* discard */
cc58d7df 407 else if (hunk == RR_SIDE_2)
e992d1eb 408 strbuf_addbuf(&two, &buf);
5b2fd956
SB
409 }
410 strbuf_release(&one);
411 strbuf_release(&two);
d58ee6db 412 strbuf_release(&buf);
5b2fd956 413
c0f16f8e
TG
414 return has_conflicts;
415}
416
417/*
418 * Read contents a file with conflicts, normalize the conflicts
419 * by (1) discarding the common ancestor version in diff3-style,
420 * (2) reordering our side and their side so that whichever sorts
421 * alphabetically earlier comes before the other one, while
422 * computing the "conflict ID", which is just an SHA-1 hash of
423 * one side of the conflict, NUL, the other side of the conflict,
424 * and NUL concatenated together.
425 *
426 * Return 1 if conflict hunks are found, 0 if there are no conflict
15beaaa3 427 * hunks and -1 if an error occurred.
c0f16f8e 428 */
0d7c419a 429static int handle_path(unsigned char *hash, struct rerere_io *io, int marker_size)
c0f16f8e 430{
0d7c419a 431 git_hash_ctx ctx;
5ebbdad3 432 struct strbuf buf = STRBUF_INIT, out = STRBUF_INIT;
c0f16f8e 433 int has_conflicts = 0;
0d7c419a 434 if (hash)
435 the_hash_algo->init_fn(&ctx);
c0f16f8e
TG
436
437 while (!io->getline(&buf, io)) {
438 if (is_cmarker(buf.buf, '<', marker_size)) {
5ebbdad3 439 has_conflicts = handle_conflict(&out, io, marker_size,
0d7c419a 440 hash ? &ctx : NULL);
c0f16f8e
TG
441 if (has_conflicts < 0)
442 break;
5ebbdad3
TG
443 rerere_io_putmem(out.buf, out.len, io);
444 strbuf_reset(&out);
c0f16f8e
TG
445 } else
446 rerere_io_putstr(buf.buf, io);
447 }
448 strbuf_release(&buf);
5ebbdad3 449 strbuf_release(&out);
c0f16f8e 450
0d7c419a 451 if (hash)
452 the_hash_algo->final_fn(hash, &ctx);
c0f16f8e 453
221444f5 454 return has_conflicts;
27d6b085
JH
455}
456
cc899eca
JH
457/*
458 * Scan the path for conflicts, do the "handle_path()" thing above, and
459 * return the number of conflict hunks found.
460 */
d829d491
JH
461static int handle_file(struct index_state *istate,
462 const char *path, unsigned char *hash, const char *output)
27d6b085 463{
221444f5 464 int has_conflicts = 0;
27d6b085 465 struct rerere_io_file io;
35843b11 466 int marker_size = ll_merge_marker_size(istate, path);
27d6b085
JH
467
468 memset(&io, 0, sizeof(io));
469 io.io.getline = rerere_file_getline;
470 io.input = fopen(path, "r");
471 io.io.wrerror = 0;
472 if (!io.input)
2373b650 473 return error_errno(_("could not open '%s'"), path);
27d6b085
JH
474
475 if (output) {
476 io.io.output = fopen(output, "w");
477 if (!io.io.output) {
2373b650 478 error_errno(_("could not write '%s'"), output);
27d6b085 479 fclose(io.input);
f7566f07 480 return -1;
27d6b085
JH
481 }
482 }
483
0d7c419a 484 has_conflicts = handle_path(hash, (struct rerere_io *)&io, marker_size);
27d6b085
JH
485
486 fclose(io.input);
487 if (io.io.wrerror)
2373b650 488 error(_("there were errors while writing '%s' (%s)"),
27d6b085
JH
489 path, strerror(io.io.wrerror));
490 if (io.io.output && fclose(io.io.output))
2373b650 491 io.io.wrerror = error_errno(_("failed to flush '%s'"), path);
27d6b085 492
221444f5 493 if (has_conflicts < 0) {
5b2fd956 494 if (output)
691f1a28 495 unlink_or_warn(output);
2373b650 496 return error(_("could not parse conflict hunks in '%s'"), path);
5b2fd956 497 }
27d6b085 498 if (io.io.wrerror)
47d32af2 499 return -1;
221444f5 500 return has_conflicts;
5b2fd956
SB
501}
502
4b68c2a0
JH
503/*
504 * Look at a cache entry at "i" and see if it is not conflicting,
505 * conflicting and we are willing to handle, or conflicting and
506 * we are unable to handle, and return the determination in *type.
507 * Return the cache index to be looked at next, by skipping the
508 * stages we have already looked at in this invocation of this
509 * function.
510 */
35843b11 511static int check_one_conflict(struct index_state *istate, int i, int *type)
5b2fd956 512{
35843b11 513 const struct cache_entry *e = istate->cache[i];
ac49f5ca
MZ
514
515 if (!ce_stage(e)) {
516 *type = RESOLVED;
517 return i + 1;
518 }
519
520 *type = PUNTED;
11877b9e 521 while (i < istate->cache_nr && ce_stage(istate->cache[i]) == 1)
fb70a06d 522 i++;
ac49f5ca
MZ
523
524 /* Only handle regular files with both stages #2 and #3 */
35843b11
NTND
525 if (i + 1 < istate->cache_nr) {
526 const struct cache_entry *e2 = istate->cache[i];
527 const struct cache_entry *e3 = istate->cache[i + 1];
5b2fd956
SB
528 if (ce_stage(e2) == 2 &&
529 ce_stage(e3) == 3 &&
ac49f5ca 530 ce_same_name(e, e3) &&
5b2fd956 531 S_ISREG(e2->ce_mode) &&
ac49f5ca
MZ
532 S_ISREG(e3->ce_mode))
533 *type = THREE_STAGED;
534 }
535
536 /* Skip the entries with the same name */
35843b11 537 while (i < istate->cache_nr && ce_same_name(e, istate->cache[i]))
ac49f5ca
MZ
538 i++;
539 return i;
540}
541
4b68c2a0
JH
542/*
543 * Scan the index and find paths that have conflicts that rerere can
544 * handle, i.e. the ones that has both stages #2 and #3.
545 *
546 * NEEDSWORK: we do not record or replay a previous "resolve by
547 * deletion" for a delete-modify conflict, as that is inherently risky
548 * without knowing what modification is being discarded. The only
549 * safe case, i.e. both side doing the deletion and modification that
550 * are identical to the previous round, might want to be handled,
551 * though.
552 */
35843b11 553static int find_conflict(struct repository *r, struct string_list *conflict)
ac49f5ca
MZ
554{
555 int i;
35843b11 556
e1ff0a32 557 if (repo_read_index(r) < 0)
2373b650 558 return error(_("index file corrupt"));
ac49f5ca 559
35843b11 560 for (i = 0; i < r->index->cache_nr;) {
ac49f5ca 561 int conflict_type;
35843b11
NTND
562 const struct cache_entry *e = r->index->cache[i];
563 i = check_one_conflict(r->index, i, &conflict_type);
ac49f5ca
MZ
564 if (conflict_type == THREE_STAGED)
565 string_list_insert(conflict, (const char *)e->name);
566 }
567 return 0;
568}
569
4b68c2a0
JH
570/*
571 * The merge_rr list is meant to hold outstanding conflicted paths
572 * that rerere could handle. Abuse the list by adding other types of
573 * entries to allow the caller to show "rerere remaining".
574 *
575 * - Conflicted paths that rerere does not handle are added
576 * - Conflicted paths that have been resolved are marked as such
577 * by storing RERERE_RESOLVED to .util field (where conflict ID
578 * is expected to be stored).
579 *
580 * Do *not* write MERGE_RR file out after calling this function.
581 *
582 * NEEDSWORK: we may want to fix the caller that implements "rerere
583 * remaining" to do this without abusing merge_rr.
584 */
35843b11 585int rerere_remaining(struct repository *r, struct string_list *merge_rr)
ac49f5ca
MZ
586{
587 int i;
35843b11 588
55e6b354 589 if (setup_rerere(r, merge_rr, RERERE_READONLY))
9dd330e6 590 return 0;
e1ff0a32 591 if (repo_read_index(r) < 0)
2373b650 592 return error(_("index file corrupt"));
ac49f5ca 593
35843b11 594 for (i = 0; i < r->index->cache_nr;) {
ac49f5ca 595 int conflict_type;
35843b11
NTND
596 const struct cache_entry *e = r->index->cache[i];
597 i = check_one_conflict(r->index, i, &conflict_type);
ac49f5ca
MZ
598 if (conflict_type == PUNTED)
599 string_list_insert(merge_rr, (const char *)e->name);
600 else if (conflict_type == RESOLVED) {
601 struct string_list_item *it;
602 it = string_list_lookup(merge_rr, (const char *)e->name);
afe8a907 603 if (it) {
1d51eced 604 free_rerere_id(it);
ac49f5ca
MZ
605 it->util = RERERE_RESOLVED;
606 }
5b2fd956
SB
607 }
608 }
609 return 0;
610}
611
0ce02b36
JH
612/*
613 * Try using the given conflict resolution "ID" to see
614 * if that recorded conflict resolves cleanly what we
615 * got in the "cur".
616 */
35843b11
NTND
617static int try_merge(struct index_state *istate,
618 const struct rerere_id *id, const char *path,
0ce02b36
JH
619 mmfile_t *cur, mmbuffer_t *result)
620{
35f69671 621 enum ll_merge_result ret;
0ce02b36
JH
622 mmfile_t base = {NULL, 0}, other = {NULL, 0};
623
624 if (read_mmfile(&base, rerere_path(id, "preimage")) ||
35f69671
EN
625 read_mmfile(&other, rerere_path(id, "postimage"))) {
626 ret = LL_MERGE_CONFLICT;
627 } else {
0ce02b36
JH
628 /*
629 * A three-way merge. Note that this honors user-customizable
630 * low-level merge driver settings.
631 */
32eaa468 632 ret = ll_merge(result, path, &base, NULL, cur, "", &other, "",
35843b11 633 istate, NULL);
35f69671 634 }
0ce02b36
JH
635
636 free(base.ptr);
637 free(other.ptr);
638
639 return ret;
640}
641
cc899eca 642/*
18bb9934 643 * Find the conflict identified by "id"; the change between its
cc899eca
JH
644 * "preimage" (i.e. a previous contents with conflict markers) and its
645 * "postimage" (i.e. the corresponding contents with conflicts
646 * resolved) may apply cleanly to the contents stored in "path", i.e.
647 * the conflict this time around.
648 *
649 * Returns 0 for successful replay of recorded resolution, or non-zero
650 * for failure.
651 */
35843b11 652static int merge(struct index_state *istate, const struct rerere_id *id, const char *path)
5b2fd956 653{
15ed07d5 654 FILE *f;
5b2fd956 655 int ret;
0ce02b36 656 mmfile_t cur = {NULL, 0};
5b2fd956 657 mmbuffer_t result = {NULL, 0};
5b2fd956 658
cc899eca
JH
659 /*
660 * Normalize the conflicts in path and write it out to
661 * "thisimage" temporary file.
662 */
35843b11 663 if ((handle_file(istate, path, NULL, rerere_path(id, "thisimage")) < 0) ||
0ce02b36 664 read_mmfile(&cur, rerere_path(id, "thisimage"))) {
689b8c29
BW
665 ret = 1;
666 goto out;
667 }
cc899eca 668
35843b11 669 ret = try_merge(istate, id, path, &cur, &result);
15ed07d5
JH
670 if (ret)
671 goto out;
672
673 /*
674 * A successful replay of recorded resolution.
675 * Mark that "postimage" was used to help gc.
676 */
677 if (utime(rerere_path(id, "postimage"), NULL) < 0)
2373b650 678 warning_errno(_("failed utime() on '%s'"),
033e011e 679 rerere_path(id, "postimage"));
15ed07d5
JH
680
681 /* Update "path" with the resolution */
682 f = fopen(path, "w");
683 if (!f)
2373b650 684 return error_errno(_("could not open '%s'"), path);
15ed07d5 685 if (fwrite(result.ptr, result.size, 1, f) != 1)
2373b650 686 error_errno(_("could not write '%s'"), path);
15ed07d5 687 if (fclose(f))
2373b650 688 return error_errno(_("writing '%s' failed"), path);
5b2fd956 689
689b8c29 690out:
5b2fd956 691 free(cur.ptr);
5b2fd956
SB
692 free(result.ptr);
693
694 return ret;
695}
696
35843b11 697static void update_paths(struct repository *r, struct string_list *update)
5b2fd956 698{
0fa5a2ed 699 struct lock_file index_lock = LOCK_INIT;
5b2fd956 700 int i;
5b2fd956 701
3a95f31d 702 repo_hold_locked_index(r, &index_lock, LOCK_DIE_ON_ERROR);
5b2fd956
SB
703
704 for (i = 0; i < update->nr; i++) {
c455c87c 705 struct string_list_item *item = &update->items[i];
35843b11 706 if (add_file_to_index(r->index, item->string, 0))
89ea9035 707 exit(128);
2373b650 708 fprintf_ln(stderr, _("Staged '%s' using previous resolution."),
a14c7ab8 709 item->string);
5b2fd956
SB
710 }
711
35843b11 712 if (write_locked_index(r->index, &index_lock,
61000814 713 COMMIT_LOCK | SKIP_IF_UNCHANGED))
2373b650 714 die(_("unable to write new index file"));
5b2fd956
SB
715}
716
629716d2
JH
717static void remove_variant(struct rerere_id *id)
718{
719 unlink_or_warn(rerere_path(id, "postimage"));
720 unlink_or_warn(rerere_path(id, "preimage"));
721 id->collection->status[id->variant] = 0;
722}
723
8e7768b2
JH
724/*
725 * The path indicated by rr_item may still have conflict for which we
726 * have a recorded resolution, in which case replay it and optionally
727 * update it. Or it may have been resolved by the user and we may
728 * only have the preimage for that conflict, in which case the result
729 * needs to be recorded as a resolution in a postimage file.
730 */
35843b11
NTND
731static void do_rerere_one_path(struct index_state *istate,
732 struct string_list_item *rr_item,
8e7768b2
JH
733 struct string_list *update)
734{
735 const char *path = rr_item->string;
a13d1370 736 struct rerere_id *id = rr_item->util;
629716d2 737 struct rerere_dir *rr_dir = id->collection;
a13d1370
JH
738 int variant;
739
a13d1370 740 variant = id->variant;
8e7768b2 741
629716d2
JH
742 /* Has the user resolved it already? */
743 if (variant >= 0) {
35843b11 744 if (!handle_file(istate, path, NULL, NULL)) {
629716d2
JH
745 copy_file(rerere_path(id, "postimage"), path, 0666);
746 id->collection->status[variant] |= RR_HAS_POSTIMAGE;
2373b650 747 fprintf_ln(stderr, _("Recorded resolution for '%s'."), path);
629716d2
JH
748 free_rerere_id(rr_item);
749 rr_item->util = NULL;
750 return;
751 }
c0a5423b 752 /*
629716d2
JH
753 * There may be other variants that can cleanly
754 * replay. Try them and update the variant number for
755 * this one.
c0a5423b 756 */
629716d2
JH
757 }
758
759 /* Does any existing resolution apply cleanly? */
760 for (variant = 0; variant < rr_dir->status_nr; variant++) {
761 const int both = RR_HAS_PREIMAGE | RR_HAS_POSTIMAGE;
762 struct rerere_id vid = *id;
763
764 if ((rr_dir->status[variant] & both) != both)
765 continue;
8e7768b2 766
629716d2 767 vid.variant = variant;
35843b11 768 if (merge(istate, &vid, path))
629716d2
JH
769 continue; /* failed to replay */
770
771 /*
772 * If there already is a different variant that applies
773 * cleanly, there is no point maintaining our own variant.
774 */
775 if (0 <= id->variant && id->variant != variant)
776 remove_variant(id);
8e7768b2
JH
777
778 if (rerere_autoupdate)
779 string_list_insert(update, path);
780 else
2373b650
TG
781 fprintf_ln(stderr,
782 _("Resolved '%s' using previous resolution."),
783 path);
629716d2
JH
784 free_rerere_id(rr_item);
785 rr_item->util = NULL;
925d73c4 786 return;
8e7768b2 787 }
629716d2
JH
788
789 /* None of the existing one applies; we need a new variant */
790 assign_variant(id);
791
792 variant = id->variant;
35843b11 793 handle_file(istate, path, NULL, rerere_path(id, "preimage"));
629716d2
JH
794 if (id->collection->status[variant] & RR_HAS_POSTIMAGE) {
795 const char *path = rerere_path(id, "postimage");
796 if (unlink(path))
2373b650 797 die_errno(_("cannot unlink stray '%s'"), path);
629716d2
JH
798 id->collection->status[variant] &= ~RR_HAS_POSTIMAGE;
799 }
800 id->collection->status[variant] |= RR_HAS_PREIMAGE;
2373b650 801 fprintf_ln(stderr, _("Recorded preimage for '%s'"), path);
8e7768b2
JH
802}
803
35843b11
NTND
804static int do_plain_rerere(struct repository *r,
805 struct string_list *rr, int fd)
5b2fd956 806{
183113a5
TF
807 struct string_list conflict = STRING_LIST_INIT_DUP;
808 struct string_list update = STRING_LIST_INIT_DUP;
5b2fd956
SB
809 int i;
810
35843b11 811 find_conflict(r, &conflict);
5b2fd956
SB
812
813 /*
cc899eca
JH
814 * MERGE_RR records paths with conflicts immediately after
815 * merge failed. Some of the conflicted paths might have been
816 * hand resolved in the working tree since then, but the
817 * initial run would catch all and register their preimages.
5b2fd956 818 */
5b2fd956 819 for (i = 0; i < conflict.nr; i++) {
1d51eced 820 struct rerere_id *id;
0d7c419a 821 unsigned char hash[GIT_MAX_RAWSZ];
c455c87c 822 const char *path = conflict.items[i].string;
1d51eced 823 int ret;
5b2fd956 824
c7a25d37
JH
825 /*
826 * Ask handle_file() to scan and assign a
827 * conflict ID. No need to write anything out
828 * yet.
829 */
d829d491 830 ret = handle_file(r->index, path, hash, NULL);
bd7dfa54 831 if (ret != 0 && string_list_has_string(rr, path)) {
93406a28
TG
832 remove_variant(string_list_lookup(rr, path)->util);
833 string_list_remove(rr, path, 1);
834 }
c7a25d37
JH
835 if (ret < 1)
836 continue;
5b2fd956 837
0d7c419a 838 id = new_rerere_id(hash);
18bb9934 839 string_list_insert(rr, path)->util = id;
cc899eca 840
c0a5423b
JH
841 /* Ensure that the directory exists. */
842 mkdir_in_gitdir(rerere_path(id, NULL));
5b2fd956
SB
843 }
844
8e7768b2 845 for (i = 0; i < rr->nr; i++)
35843b11 846 do_rerere_one_path(r->index, &rr->items[i], &update);
5b2fd956
SB
847
848 if (update.nr)
35843b11 849 update_paths(r, &update);
5b2fd956
SB
850
851 return write_rr(rr, fd);
852}
853
633e5ad3 854static void git_rerere_config(void)
5b2fd956 855{
633e5ad3
TA
856 git_config_get_bool("rerere.enabled", &rerere_enabled);
857 git_config_get_bool("rerere.autoupdate", &rerere_autoupdate);
858 git_config(git_default_config, NULL);
5b2fd956
SB
859}
860
f932729c
JK
861static GIT_PATH_FUNC(git_path_rr_cache, "rr-cache")
862
5b2fd956
SB
863static int is_rerere_enabled(void)
864{
5b2fd956
SB
865 int rr_cache_exists;
866
867 if (!rerere_enabled)
868 return 0;
869
f932729c 870 rr_cache_exists = is_directory(git_path_rr_cache());
5b2fd956
SB
871 if (rerere_enabled < 0)
872 return rr_cache_exists;
873
f932729c 874 if (!rr_cache_exists && mkdir_in_gitdir(git_path_rr_cache()))
2373b650 875 die(_("could not create directory '%s'"), git_path_rr_cache());
5b2fd956
SB
876 return 1;
877}
878
55e6b354 879int setup_rerere(struct repository *r, struct string_list *merge_rr, int flags)
5b2fd956
SB
880{
881 int fd;
882
633e5ad3 883 git_rerere_config();
5b2fd956
SB
884 if (!is_rerere_enabled())
885 return -1;
886
cb6020bb
JH
887 if (flags & (RERERE_AUTOUPDATE|RERERE_NOAUTOUPDATE))
888 rerere_autoupdate = !!(flags & RERERE_AUTOUPDATE);
9dd330e6
JK
889 if (flags & RERERE_READONLY)
890 fd = 0;
891 else
102de880 892 fd = hold_lock_file_for_update(&write_lock,
55e6b354 893 git_path_merge_rr(r),
9dd330e6 894 LOCK_DIE_ON_ERROR);
55e6b354 895 read_rr(r, merge_rr);
5b2fd956
SB
896 return fd;
897}
898
cc899eca
JH
899/*
900 * The main entry point that is called internally from codepaths that
901 * perform mergy operations, possibly leaving conflicted index entries
902 * and working tree files.
903 */
35843b11 904int repo_rerere(struct repository *r, int flags)
5b2fd956 905{
183113a5 906 struct string_list merge_rr = STRING_LIST_INIT_DUP;
1869bbe1 907 int fd, status;
5b2fd956 908
55e6b354 909 fd = setup_rerere(r, &merge_rr, flags);
5b2fd956
SB
910 if (fd < 0)
911 return 0;
35843b11 912 status = do_plain_rerere(r, &merge_rr, fd);
1869bbe1
JH
913 free_rerere_dirs();
914 return status;
5b2fd956 915}
dea4562b 916
3d730ed9
JH
917/*
918 * Subclass of rerere_io that reads from an in-core buffer that is a
919 * strbuf
920 */
921struct rerere_io_mem {
922 struct rerere_io io;
923 struct strbuf input;
924};
925
926/*
927 * ... and its getline() method implementation
928 */
929static int rerere_mem_getline(struct strbuf *sb, struct rerere_io *io_)
930{
931 struct rerere_io_mem *io = (struct rerere_io_mem *)io_;
932 char *ep;
933 size_t len;
934
935 strbuf_release(sb);
936 if (!io->input.len)
937 return -1;
938 ep = memchr(io->input.buf, '\n', io->input.len);
939 if (!ep)
940 ep = io->input.buf + io->input.len;
941 else if (*ep == '\n')
942 ep++;
943 len = ep - io->input.buf;
944 strbuf_add(sb, io->input.buf, len);
945 strbuf_remove(&io->input, 0, len);
946 return 0;
947}
948
d829d491
JH
949static int handle_cache(struct index_state *istate,
950 const char *path, unsigned char *hash, const char *output)
3d730ed9
JH
951{
952 mmfile_t mmfile[3] = {{NULL}};
953 mmbuffer_t result = {NULL, 0};
954 const struct cache_entry *ce;
221444f5 955 int pos, len, i, has_conflicts;
3d730ed9 956 struct rerere_io_mem io;
35843b11 957 int marker_size = ll_merge_marker_size(istate, path);
3d730ed9
JH
958
959 /*
960 * Reproduce the conflicted merge in-core
961 */
962 len = strlen(path);
35843b11 963 pos = index_name_pos(istate, path, len);
3d730ed9
JH
964 if (0 <= pos)
965 return -1;
966 pos = -pos - 1;
967
35843b11 968 while (pos < istate->cache_nr) {
3d730ed9
JH
969 enum object_type type;
970 unsigned long size;
971
35843b11 972 ce = istate->cache[pos++];
3d730ed9
JH
973 if (ce_namelen(ce) != len || memcmp(ce->name, path, len))
974 break;
975 i = ce_stage(ce) - 1;
976 if (!mmfile[i].ptr) {
bc726bd0
ÆAB
977 mmfile[i].ptr = repo_read_object_file(the_repository,
978 &ce->oid, &type,
979 &size);
3d730ed9
JH
980 mmfile[i].size = size;
981 }
982 }
983 for (i = 0; i < 3; i++)
984 if (!mmfile[i].ptr && !mmfile[i].size)
985 mmfile[i].ptr = xstrdup("");
986
987 /*
988 * NEEDSWORK: handle conflicts from merges with
989 * merge.renormalize set, too?
990 */
991 ll_merge(&result, path, &mmfile[0], NULL,
992 &mmfile[1], "ours",
32eaa468 993 &mmfile[2], "theirs",
35843b11 994 istate, NULL);
3d730ed9
JH
995 for (i = 0; i < 3; i++)
996 free(mmfile[i].ptr);
997
998 memset(&io, 0, sizeof(io));
999 io.io.getline = rerere_mem_getline;
1000 if (output)
1001 io.io.output = fopen(output, "w");
1002 else
1003 io.io.output = NULL;
1004 strbuf_init(&io.input, 0);
1005 strbuf_attach(&io.input, result.ptr, result.size, result.size);
1006
1007 /*
1008 * Grab the conflict ID and optionally write the original
1009 * contents with conflict markers out.
1010 */
0d7c419a 1011 has_conflicts = handle_path(hash, (struct rerere_io *)&io, marker_size);
3d730ed9
JH
1012 strbuf_release(&io.input);
1013 if (io.io.output)
1014 fclose(io.io.output);
221444f5 1015 return has_conflicts;
5b2fd956 1016}
dea4562b 1017
35843b11
NTND
1018static int rerere_forget_one_path(struct index_state *istate,
1019 const char *path,
1020 struct string_list *rr)
dea4562b
JH
1021{
1022 const char *filename;
1d51eced 1023 struct rerere_id *id;
0d7c419a 1024 unsigned char hash[GIT_MAX_RAWSZ];
dea4562b 1025 int ret;
8d9b5a4a 1026 struct string_list_item *item;
dea4562b 1027
963ec003
JH
1028 /*
1029 * Recreate the original conflict from the stages in the
1030 * index and compute the conflict ID
1031 */
d829d491 1032 ret = handle_cache(istate, path, hash, NULL);
dea4562b 1033 if (ret < 1)
2373b650 1034 return error(_("could not parse conflict hunks in '%s'"), path);
963ec003
JH
1035
1036 /* Nuke the recorded resolution for the conflict */
0d7c419a 1037 id = new_rerere_id(hash);
890fca84
JH
1038
1039 for (id->variant = 0;
1040 id->variant < id->collection->status_nr;
1041 id->variant++) {
1042 mmfile_t cur = { NULL, 0 };
1043 mmbuffer_t result = {NULL, 0};
1044 int cleanly_resolved;
1045
1046 if (!has_rerere_resolution(id))
1047 continue;
1048
d829d491 1049 handle_cache(istate, path, hash, rerere_path(id, "thisimage"));
890fca84
JH
1050 if (read_mmfile(&cur, rerere_path(id, "thisimage"))) {
1051 free(cur.ptr);
2373b650 1052 error(_("failed to update conflicted state in '%s'"), path);
8f449614 1053 goto fail_exit;
890fca84 1054 }
35843b11 1055 cleanly_resolved = !try_merge(istate, id, path, &cur, &result);
890fca84
JH
1056 free(result.ptr);
1057 free(cur.ptr);
1058 if (cleanly_resolved)
1059 break;
1060 }
1061
8f449614 1062 if (id->collection->status_nr <= id->variant) {
2373b650 1063 error(_("no remembered resolution for '%s'"), path);
8f449614
JH
1064 goto fail_exit;
1065 }
890fca84 1066
18bb9934 1067 filename = rerere_path(id, "postimage");
8f449614
JH
1068 if (unlink(filename)) {
1069 if (errno == ENOENT)
2373b650 1070 error(_("no remembered resolution for '%s'"), path);
8f449614 1071 else
2373b650 1072 error_errno(_("cannot unlink '%s'"), filename);
8f449614 1073 goto fail_exit;
d9d501b0 1074 }
dea4562b 1075
963ec003
JH
1076 /*
1077 * Update the preimage so that the user can resolve the
1078 * conflict in the working tree, run us again to record
1079 * the postimage.
1080 */
d829d491 1081 handle_cache(istate, path, hash, rerere_path(id, "preimage"));
2373b650 1082 fprintf_ln(stderr, _("Updated preimage for '%s'"), path);
dea4562b 1083
963ec003
JH
1084 /*
1085 * And remember that we can record resolution for this
1086 * conflict when the user is done.
1087 */
8d9b5a4a 1088 item = string_list_insert(rr, path);
1d51eced 1089 free_rerere_id(item);
18bb9934 1090 item->util = id;
2373b650 1091 fprintf(stderr, _("Forgot resolution for '%s'\n"), path);
dea4562b 1092 return 0;
8f449614
JH
1093
1094fail_exit:
1095 free(id);
1096 return -1;
dea4562b
JH
1097}
1098
35843b11 1099int rerere_forget(struct repository *r, struct pathspec *pathspec)
dea4562b
JH
1100{
1101 int i, fd;
183113a5
TF
1102 struct string_list conflict = STRING_LIST_INIT_DUP;
1103 struct string_list merge_rr = STRING_LIST_INIT_DUP;
dea4562b 1104
e1ff0a32 1105 if (repo_read_index(r) < 0)
2373b650 1106 return error(_("index file corrupt"));
dea4562b 1107
55e6b354 1108 fd = setup_rerere(r, &merge_rr, RERERE_NOAUTOUPDATE);
0544574c
JK
1109 if (fd < 0)
1110 return 0;
dea4562b 1111
963ec003
JH
1112 /*
1113 * The paths may have been resolved (incorrectly);
1114 * recover the original conflicted state and then
1115 * find the conflicted paths.
1116 */
35843b11
NTND
1117 unmerge_index(r->index, pathspec);
1118 find_conflict(r, &conflict);
dea4562b
JH
1119 for (i = 0; i < conflict.nr; i++) {
1120 struct string_list_item *it = &conflict.items[i];
35843b11 1121 if (!match_pathspec(r->index, pathspec, it->string,
ae8d0824 1122 strlen(it->string), 0, NULL, 0))
dea4562b 1123 continue;
35843b11 1124 rerere_forget_one_path(r->index, it->string, &merge_rr);
dea4562b
JH
1125 }
1126 return write_rr(&merge_rr, fd);
1127}
0f891e7d 1128
e828de82
JH
1129/*
1130 * Garbage collection support
1131 */
1d51eced 1132
5ea82279 1133static timestamp_t rerere_created_at(struct rerere_id *id)
0f891e7d
JH
1134{
1135 struct stat st;
1d51eced 1136
18bb9934 1137 return stat(rerere_path(id, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
0f891e7d
JH
1138}
1139
5ea82279 1140static timestamp_t rerere_last_used_at(struct rerere_id *id)
0f891e7d
JH
1141{
1142 struct stat st;
1d51eced 1143
18bb9934 1144 return stat(rerere_path(id, "postimage"), &st) ? (time_t) 0 : st.st_mtime;
0f891e7d
JH
1145}
1146
e828de82
JH
1147/*
1148 * Remove the recorded resolution for a given conflict ID
1149 */
1d51eced 1150static void unlink_rr_item(struct rerere_id *id)
0f891e7d 1151{
1be1e851
JH
1152 unlink_or_warn(rerere_path(id, "thisimage"));
1153 remove_variant(id);
1154 id->collection->status[id->variant] = 0;
1155}
1156
5ea82279
JH
1157static void prune_one(struct rerere_id *id,
1158 timestamp_t cutoff_resolve, timestamp_t cutoff_noresolve)
1be1e851 1159{
5ea82279
JH
1160 timestamp_t then;
1161 timestamp_t cutoff;
1be1e851
JH
1162
1163 then = rerere_last_used_at(id);
1164 if (then)
1165 cutoff = cutoff_resolve;
1166 else {
1167 then = rerere_created_at(id);
1168 if (!then)
1169 return;
1170 cutoff = cutoff_noresolve;
1171 }
5ea82279 1172 if (then < cutoff)
1be1e851 1173 unlink_rr_item(id);
0f891e7d
JH
1174}
1175
2bc1a87e
JK
1176/* Does the basename in "path" look plausibly like an rr-cache entry? */
1177static int is_rr_cache_dirname(const char *path)
1178{
098c173f
JK
1179 struct object_id oid;
1180 const char *end;
1181 return !parse_oid_hex(path, &oid, &end) && !*end;
2bc1a87e
JK
1182}
1183
55e6b354 1184void rerere_gc(struct repository *r, struct string_list *rr)
0f891e7d
JH
1185{
1186 struct string_list to_remove = STRING_LIST_INIT_DUP;
1187 DIR *dir;
1188 struct dirent *e;
1be1e851 1189 int i;
5ea82279
JH
1190 timestamp_t now = time(NULL);
1191 timestamp_t cutoff_noresolve = now - 15 * 86400;
1192 timestamp_t cutoff_resolve = now - 60 * 86400;
0f891e7d 1193
55e6b354 1194 if (setup_rerere(r, rr, 0) < 0)
9dd330e6
JK
1195 return;
1196
6e96cb52
JH
1197 git_config_get_expiry_in_days("gc.rerereresolved", &cutoff_resolve, now);
1198 git_config_get_expiry_in_days("gc.rerereunresolved", &cutoff_noresolve, now);
633e5ad3 1199 git_config(git_default_config, NULL);
0f891e7d
JH
1200 dir = opendir(git_path("rr-cache"));
1201 if (!dir)
2373b650 1202 die_errno(_("unable to open rr-cache directory"));
e828de82 1203 /* Collect stale conflict IDs ... */
b548f0f1 1204 while ((e = readdir_skip_dot_and_dotdot(dir))) {
1be1e851
JH
1205 struct rerere_dir *rr_dir;
1206 struct rerere_id id;
1207 int now_empty;
1208
2bc1a87e 1209 if (!is_rr_cache_dirname(e->d_name))
1be1e851
JH
1210 continue; /* or should we remove e->d_name? */
1211
2bc1a87e
JK
1212 rr_dir = find_rerere_dir(e->d_name);
1213
1be1e851
JH
1214 now_empty = 1;
1215 for (id.variant = 0, id.collection = rr_dir;
1216 id.variant < id.collection->status_nr;
1217 id.variant++) {
5ea82279 1218 prune_one(&id, cutoff_resolve, cutoff_noresolve);
1be1e851
JH
1219 if (id.collection->status[id.variant])
1220 now_empty = 0;
0f891e7d 1221 }
1be1e851 1222 if (now_empty)
0f891e7d
JH
1223 string_list_append(&to_remove, e->d_name);
1224 }
a9930e35 1225 closedir(dir);
1be1e851
JH
1226
1227 /* ... and then remove the empty directories */
0f891e7d 1228 for (i = 0; i < to_remove.nr; i++)
1be1e851 1229 rmdir(git_path("rr-cache/%s", to_remove.items[i].string));
0f891e7d 1230 string_list_clear(&to_remove, 0);
9dd330e6 1231 rollback_lock_file(&write_lock);
0f891e7d
JH
1232}
1233
e828de82
JH
1234/*
1235 * During a conflict resolution, after "rerere" recorded the
1236 * preimages, abandon them if the user did not resolve them or
1237 * record their resolutions. And drop $GIT_DIR/MERGE_RR.
1238 *
1239 * NEEDSWORK: shouldn't we be calling this from "reset --hard"?
1240 */
55e6b354 1241void rerere_clear(struct repository *r, struct string_list *merge_rr)
0f891e7d
JH
1242{
1243 int i;
1244
55e6b354 1245 if (setup_rerere(r, merge_rr, 0) < 0)
9dd330e6
JK
1246 return;
1247
0f891e7d 1248 for (i = 0; i < merge_rr->nr; i++) {
1d51eced 1249 struct rerere_id *id = merge_rr->items[i].util;
1be1e851 1250 if (!has_rerere_resolution(id)) {
18bb9934 1251 unlink_rr_item(id);
1be1e851
JH
1252 rmdir(rerere_path(id, NULL));
1253 }
0f891e7d 1254 }
55e6b354 1255 unlink_or_warn(git_path_merge_rr(r));
9dd330e6 1256 rollback_lock_file(&write_lock);
0f891e7d 1257}