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