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