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