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