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