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