blob: d26627c59329151ef196e26444a64cb44ad699d4 [file] [log] [blame]
Stephan Beyer5b2fd952008-07-09 14:58:57 +02001#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +02003#include "lockfile.h"
Johannes Schindelinc455c872008-07-21 19:03:49 +01004#include "string-list.h"
Stephan Beyer5b2fd952008-07-09 14:58:57 +02005#include "rerere.h"
Stephan Beyer5b2fd952008-07-09 14:58:57 +02006#include "xdiff-interface.h"
Junio C Hamanodea45622009-12-25 15:51:32 -08007#include "dir.h"
8#include "resolve-undo.h"
9#include "ll-merge.h"
Junio C Hamano85885672010-01-16 23:28:46 -080010#include "attr.h"
Nguyễn Thái Ngọc Duy01a10b02013-07-14 15:35:40 +070011#include "pathspec.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -070012#include "object-store.h"
Martin Ågrenbc626922020-12-31 12:56:23 +010013#include "hash-lookup.h"
Jeff King680ff912021-01-28 01:34:31 -050014#include "strmap.h"
Stephan Beyer5b2fd952008-07-09 14:58:57 +020015
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -050016#define RESOLVED 0
17#define PUNTED 1
18#define THREE_STAGED 2
19void *RERERE_RESOLVED = &RERERE_RESOLVED;
20
Stephan Beyer5b2fd952008-07-09 14:58:57 +020021/* 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
Junio C Hamano2c7929b2015-07-16 15:47:13 -070027#define RR_HAS_POSTIMAGE 1
28#define RR_HAS_PREIMAGE 2
Jeff King680ff912021-01-28 01:34:31 -050029struct rerere_dir {
Junio C Hamanoa13d1372015-07-23 14:23:24 -070030 int status_alloc, status_nr;
31 unsigned char *status;
Jeff King680ff912021-01-28 01:34:31 -050032 char name[FLEX_ARRAY];
33};
34
35static struct strmap rerere_dirs = STRMAP_INIT;
Junio C Hamano1869bbe2015-07-16 14:50:05 -070036
37static void free_rerere_dirs(void)
38{
Jeff King680ff912021-01-28 01:34:31 -050039 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);
Junio C Hamanoa13d1372015-07-23 14:23:24 -070046 }
Jeff King680ff912021-01-28 01:34:31 -050047 strmap_clear(&rerere_dirs, 0);
Junio C Hamano1869bbe2015-07-16 14:50:05 -070048}
49
Junio C Hamano1d51ece2015-07-04 17:38:34 -070050static void free_rerere_id(struct string_list_item *item)
Stephan Beyer5b2fd952008-07-09 14:58:57 +020051{
Junio C Hamano1d51ece2015-07-04 17:38:34 -070052 free(item->util);
Stephan Beyer5b2fd952008-07-09 14:58:57 +020053}
54
Junio C Hamano1d51ece2015-07-04 17:38:34 -070055static const char *rerere_id_hex(const struct rerere_id *id)
56{
Jeff King680ff912021-01-28 01:34:31 -050057 return id->collection->name;
Junio C Hamano1d51ece2015-07-04 17:38:34 -070058}
59
Junio C Hamanoa13d1372015-07-23 14:23:24 -070060static 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) {
Junio C Hamano629716d2015-07-30 15:49:18 -070078 for (variant = 0; variant < rr_dir->status_nr; variant++)
79 if (!rr_dir->status[variant])
80 break;
Junio C Hamanoa13d1372015-07-23 14:23:24 -070081 }
82 fit_variant(rr_dir, variant);
83 id->variant = variant;
Junio C Hamano1d51ece2015-07-04 17:38:34 -070084}
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
Junio C Hamanoa13d1372015-07-23 14:23:24 -070091 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);
Junio C Hamano1d51ece2015-07-04 17:38:34 -070096}
97
Junio C Hamanoa13d1372015-07-23 14:23:24 -070098static int is_rr_file(const char *name, const char *filename, int *variant)
Junio C Hamano2c7929b2015-07-16 15:47:13 -070099{
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700100 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;
Junio C Hamano2c7929b2015-07-16 15:47:13 -0700115}
116
117static void scan_rerere_dir(struct rerere_dir *rr_dir)
118{
119 struct dirent *de;
Jeff King680ff912021-01-28 01:34:31 -0500120 DIR *dir = opendir(git_path("rr-cache/%s", rr_dir->name));
Junio C Hamano2c7929b2015-07-16 15:47:13 -0700121
122 if (!dir)
123 return;
124 while ((de = readdir(dir)) != NULL) {
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700125 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 }
Junio C Hamano2c7929b2015-07-16 15:47:13 -0700134 }
135 closedir(dir);
136}
137
Junio C Hamano1869bbe2015-07-16 14:50:05 -0700138static struct rerere_dir *find_rerere_dir(const char *hex)
139{
Junio C Hamano1869bbe2015-07-16 14:50:05 -0700140 struct rerere_dir *rr_dir;
Junio C Hamano1869bbe2015-07-16 14:50:05 -0700141
Jeff King680ff912021-01-28 01:34:31 -0500142 rr_dir = strmap_get(&rerere_dirs, hex);
143 if (!rr_dir) {
144 FLEX_ALLOC_STR(rr_dir, name, hex);
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700145 rr_dir->status = NULL;
146 rr_dir->status_nr = 0;
147 rr_dir->status_alloc = 0;
Jeff King680ff912021-01-28 01:34:31 -0500148 strmap_put(&rerere_dirs, hex, rr_dir);
Junio C Hamano1869bbe2015-07-16 14:50:05 -0700149
Junio C Hamano2c7929b2015-07-16 15:47:13 -0700150 scan_rerere_dir(rr_dir);
Junio C Hamano1869bbe2015-07-16 14:50:05 -0700151 }
Jeff King680ff912021-01-28 01:34:31 -0500152 return rr_dir;
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700153}
154
155static int has_rerere_resolution(const struct rerere_id *id)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200156{
Junio C Hamano05dd9f12015-07-17 13:28:31 -0700157 const int both = RR_HAS_POSTIMAGE|RR_HAS_PREIMAGE;
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700158 int variant = id->variant;
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700159
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700160 if (variant < 0)
161 return 0;
162 return ((id->collection->status[variant] & both) == both);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200163}
164
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700165static struct rerere_id *new_rerere_id_hex(char *hex)
166{
167 struct rerere_id *id = xmalloc(sizeof(*id));
Junio C Hamano1869bbe2015-07-16 14:50:05 -0700168 id->collection = find_rerere_dir(hex);
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700169 id->variant = -1; /* not known yet */
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700170 return id;
171}
172
brian m. carlson3f34d702019-08-18 20:04:25 +0000173static struct rerere_id *new_rerere_id(unsigned char *hash)
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700174{
brian m. carlson3f34d702019-08-18 20:04:25 +0000175 return new_rerere_id_hex(hash_to_hex(hash));
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700176}
177
Junio C Hamano4b68c2a2015-06-30 22:36:35 -0700178/*
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 */
Nguyễn Thái Ngọc Duy55e6b352018-11-10 06:49:09 +0100185static void read_rr(struct repository *r, struct string_list *rr)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200186{
Junio C Hamanof5800f62015-06-28 15:51:59 -0700187 struct strbuf buf = STRBUF_INIT;
Nguyễn Thái Ngọc Duy55e6b352018-11-10 06:49:09 +0100188 FILE *in = fopen_or_warn(git_path_merge_rr(r), "r");
Junio C Hamanof5800f62015-06-28 15:51:59 -0700189
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200190 if (!in)
191 return;
Junio C Hamanof5800f62015-06-28 15:51:59 -0700192 while (!strbuf_getwholeline(&buf, in, '\0')) {
193 char *path;
brian m. carlson0d7c4192018-10-15 00:02:02 +0000194 unsigned char hash[GIT_MAX_RAWSZ];
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700195 struct rerere_id *id;
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700196 int variant;
brian m. carlson0d7c4192018-10-15 00:02:02 +0000197 const unsigned hexsz = the_hash_algo->hexsz;
Junio C Hamanof5800f62015-06-28 15:51:59 -0700198
199 /* There has to be the hash, tab, path and then NUL */
brian m. carlson0d7c4192018-10-15 00:02:02 +0000200 if (buf.len < hexsz + 2 || get_sha1_hex(buf.buf, hash))
Thomas Gummerer2373b652018-08-05 18:20:30 +0100201 die(_("corrupt MERGE_RR"));
Junio C Hamanof5800f62015-06-28 15:51:59 -0700202
brian m. carlson0d7c4192018-10-15 00:02:02 +0000203 if (buf.buf[hexsz] != '.') {
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700204 variant = 0;
brian m. carlson0d7c4192018-10-15 00:02:02 +0000205 path = buf.buf + hexsz;
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700206 } else {
207 errno = 0;
brian m. carlson0d7c4192018-10-15 00:02:02 +0000208 variant = strtol(buf.buf + hexsz + 1, &path, 10);
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700209 if (errno)
Thomas Gummerer2373b652018-08-05 18:20:30 +0100210 die(_("corrupt MERGE_RR"));
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700211 }
212 if (*(path++) != '\t')
Thomas Gummerer2373b652018-08-05 18:20:30 +0100213 die(_("corrupt MERGE_RR"));
brian m. carlson0d7c4192018-10-15 00:02:02 +0000214 buf.buf[hexsz] = '\0';
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700215 id = new_rerere_id_hex(buf.buf);
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700216 id->variant = variant;
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700217 string_list_insert(rr, path)->util = id;
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200218 }
Junio C Hamanof5800f62015-06-28 15:51:59 -0700219 strbuf_release(&buf);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200220 fclose(in);
221}
222
223static struct lock_file write_lock;
224
Johannes Schindelinc455c872008-07-21 19:03:49 +0100225static int write_rr(struct string_list *rr, int out_fd)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200226{
227 int i;
228 for (i = 0; i < rr->nr; i++) {
Junio C Hamanoe2cb6a92015-06-28 16:28:00 -0700229 struct strbuf buf = STRBUF_INIT;
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700230 struct rerere_id *id;
Junio C Hamanoe2cb6a92015-06-28 16:28:00 -0700231
232 assert(rr->items[i].util != RERERE_RESOLVED);
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700233
234 id = rr->items[i].util;
235 if (!id)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200236 continue;
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700237 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
Jeff King06f46f22017-09-13 13:16:03 -0400247 if (write_in_full(out_fd, buf.buf, buf.len) < 0)
Thomas Gummerer2373b652018-08-05 18:20:30 +0100248 die(_("unable to write rerere record"));
Junio C Hamanoe2cb6a92015-06-28 16:28:00 -0700249
250 strbuf_release(&buf);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200251 }
252 if (commit_lock_file(&write_lock) != 0)
Thomas Gummerer2373b652018-08-05 18:20:30 +0100253 die(_("unable to write rerere record"));
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200254 return 0;
255}
256
Junio C Hamanoa96847c2015-06-30 22:33:19 -0700257/*
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
Alex Riesen47d32af2008-12-05 01:35:48 +0100272static 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
Junio C Hamano27d6b082009-12-25 14:34:53 -0800285static 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
Junio C Hamanoa96847c2015-06-30 22:33:19 -0700297/*
298 * Subclass of rerere_io that reads from an on-disk file
299 */
Junio C Hamano27d6b082009-12-25 14:34:53 -0800300struct rerere_io_file {
301 struct rerere_io io;
302 FILE *input;
303};
304
Junio C Hamanoa96847c2015-06-30 22:33:19 -0700305/*
306 * ... and its getline() method implementation
307 */
Junio C Hamano27d6b082009-12-25 14:34:53 -0800308static 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
Junio C Hamano67711cd2015-06-29 15:05:24 -0700314/*
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)
Junio C Hamano191f24172010-01-16 23:06:45 -0800320{
Junio C Hamano67711cd2015-06-29 15:05:24 -0700321 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
Junio C Hamano191f24172010-01-16 23:06:45 -0800333 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
Thomas Gummerer5ebbdad2018-08-05 18:20:35 +0100341static 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,
brian m. carlson0d7c4192018-10-15 00:02:02 +0000348 int marker_size, git_hash_ctx *ctx)
Thomas Gummererc0f16f82018-08-05 18:20:34 +0100349{
350 enum {
351 RR_SIDE_1 = 0, RR_SIDE_2, RR_ORIGINAL
352 } hunk = RR_SIDE_1;
353 struct strbuf one = STRBUF_INIT, two = STRBUF_INIT;
Thomas Gummerer4af32202018-08-05 18:20:36 +0100354 struct strbuf buf = STRBUF_INIT, conflict = STRBUF_INIT;
Thomas Gummererc0f16f82018-08-05 18:20:34 +0100355 int has_conflicts = -1;
356
357 while (!io->getline(&buf, io)) {
358 if (is_cmarker(buf.buf, '<', marker_size)) {
Thomas Gummerer4af32202018-08-05 18:20:36 +0100359 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);
Thomas Gummererc0f16f82018-08-05 18:20:34 +0100366 } else if (is_cmarker(buf.buf, '|', marker_size)) {
367 if (hunk != RR_SIDE_1)
368 break;
369 hunk = RR_ORIGINAL;
370 } else if (is_cmarker(buf.buf, '=', marker_size)) {
371 if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL)
372 break;
373 hunk = RR_SIDE_2;
374 } else if (is_cmarker(buf.buf, '>', marker_size)) {
375 if (hunk != RR_SIDE_2)
376 break;
377 if (strbuf_cmp(&one, &two) > 0)
378 strbuf_swap(&one, &two);
379 has_conflicts = 1;
Thomas Gummerer5ebbdad2018-08-05 18:20:35 +0100380 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);
Thomas Gummererc0f16f82018-08-05 18:20:34 +0100385 if (ctx) {
brian m. carlson0d7c4192018-10-15 00:02:02 +0000386 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);
Thomas Gummererc0f16f82018-08-05 18:20:34 +0100392 }
393 break;
394 } else if (hunk == RR_SIDE_1)
395 strbuf_addbuf(&one, &buf);
396 else if (hunk == RR_ORIGINAL)
397 ; /* discard */
398 else if (hunk == RR_SIDE_2)
399 strbuf_addbuf(&two, &buf);
400 }
401 strbuf_release(&one);
402 strbuf_release(&two);
403 strbuf_release(&buf);
404
405 return has_conflicts;
406}
407
Junio C Hamanocc899ec2015-06-30 22:40:35 -0700408/*
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 *
Thomas Gummerer221444f2018-08-05 18:20:33 +0100417 * Return 1 if conflict hunks are found, 0 if there are no conflict
Elijah Newren15beaaa2019-11-05 17:07:23 +0000418 * hunks and -1 if an error occurred.
Junio C Hamanocc899ec2015-06-30 22:40:35 -0700419 */
brian m. carlson0d7c4192018-10-15 00:02:02 +0000420static int handle_path(unsigned char *hash, struct rerere_io *io, int marker_size)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200421{
brian m. carlson0d7c4192018-10-15 00:02:02 +0000422 git_hash_ctx ctx;
Thomas Gummerer5ebbdad2018-08-05 18:20:35 +0100423 struct strbuf buf = STRBUF_INIT, out = STRBUF_INIT;
Thomas Gummererc0f16f82018-08-05 18:20:34 +0100424 int has_conflicts = 0;
brian m. carlson0d7c4192018-10-15 00:02:02 +0000425 if (hash)
426 the_hash_algo->init_fn(&ctx);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200427
Junio C Hamano27d6b082009-12-25 14:34:53 -0800428 while (!io->getline(&buf, io)) {
Junio C Hamano67711cd2015-06-29 15:05:24 -0700429 if (is_cmarker(buf.buf, '<', marker_size)) {
Thomas Gummerer5ebbdad2018-08-05 18:20:35 +0100430 has_conflicts = handle_conflict(&out, io, marker_size,
brian m. carlson0d7c4192018-10-15 00:02:02 +0000431 hash ? &ctx : NULL);
Thomas Gummererc0f16f82018-08-05 18:20:34 +0100432 if (has_conflicts < 0)
433 break;
Thomas Gummerer5ebbdad2018-08-05 18:20:35 +0100434 rerere_io_putmem(out.buf, out.len, io);
435 strbuf_reset(&out);
Thomas Gummererc0f16f82018-08-05 18:20:34 +0100436 } else
Junio C Hamano27d6b082009-12-25 14:34:53 -0800437 rerere_io_putstr(buf.buf, io);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200438 }
Junio C Hamanod58ee6d2009-12-25 13:55:29 -0800439 strbuf_release(&buf);
Thomas Gummerer5ebbdad2018-08-05 18:20:35 +0100440 strbuf_release(&out);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200441
brian m. carlson0d7c4192018-10-15 00:02:02 +0000442 if (hash)
443 the_hash_algo->final_fn(hash, &ctx);
Thomas Gummererc0f16f82018-08-05 18:20:34 +0100444
Thomas Gummerer221444f2018-08-05 18:20:33 +0100445 return has_conflicts;
Junio C Hamano27d6b082009-12-25 14:34:53 -0800446}
447
Junio C Hamanocc899ec2015-06-30 22:40:35 -0700448/*
449 * Scan the path for conflicts, do the "handle_path()" thing above, and
450 * return the number of conflict hunks found.
451 */
Junio C Hamanod829d492018-10-30 15:43:42 +0900452static int handle_file(struct index_state *istate,
453 const char *path, unsigned char *hash, const char *output)
Junio C Hamano27d6b082009-12-25 14:34:53 -0800454{
Thomas Gummerer221444f2018-08-05 18:20:33 +0100455 int has_conflicts = 0;
Junio C Hamano27d6b082009-12-25 14:34:53 -0800456 struct rerere_io_file io;
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200457 int marker_size = ll_merge_marker_size(istate, path);
Junio C Hamano27d6b082009-12-25 14:34:53 -0800458
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)
Thomas Gummerer2373b652018-08-05 18:20:30 +0100464 return error_errno(_("could not open '%s'"), path);
Junio C Hamano27d6b082009-12-25 14:34:53 -0800465
466 if (output) {
467 io.io.output = fopen(output, "w");
468 if (!io.io.output) {
Thomas Gummerer2373b652018-08-05 18:20:30 +0100469 error_errno(_("could not write '%s'"), output);
Junio C Hamano27d6b082009-12-25 14:34:53 -0800470 fclose(io.input);
Nguyễn Thái Ngọc Duyf7566f02017-05-09 17:11:33 +0700471 return -1;
Junio C Hamano27d6b082009-12-25 14:34:53 -0800472 }
473 }
474
brian m. carlson0d7c4192018-10-15 00:02:02 +0000475 has_conflicts = handle_path(hash, (struct rerere_io *)&io, marker_size);
Junio C Hamano27d6b082009-12-25 14:34:53 -0800476
477 fclose(io.input);
478 if (io.io.wrerror)
Thomas Gummerer2373b652018-08-05 18:20:30 +0100479 error(_("there were errors while writing '%s' (%s)"),
Junio C Hamano27d6b082009-12-25 14:34:53 -0800480 path, strerror(io.io.wrerror));
481 if (io.io.output && fclose(io.io.output))
Thomas Gummerer2373b652018-08-05 18:20:30 +0100482 io.io.wrerror = error_errno(_("failed to flush '%s'"), path);
Junio C Hamano27d6b082009-12-25 14:34:53 -0800483
Thomas Gummerer221444f2018-08-05 18:20:33 +0100484 if (has_conflicts < 0) {
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200485 if (output)
Alex Riesen691f1a22009-04-29 23:22:56 +0200486 unlink_or_warn(output);
Thomas Gummerer2373b652018-08-05 18:20:30 +0100487 return error(_("could not parse conflict hunks in '%s'"), path);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200488 }
Junio C Hamano27d6b082009-12-25 14:34:53 -0800489 if (io.io.wrerror)
Alex Riesen47d32af2008-12-05 01:35:48 +0100490 return -1;
Thomas Gummerer221444f2018-08-05 18:20:33 +0100491 return has_conflicts;
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200492}
493
Junio C Hamanoa96847c2015-06-30 22:33:19 -0700494/*
Junio C Hamano4b68c2a2015-06-30 22:36:35 -0700495 * 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 */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200502static int check_one_conflict(struct index_state *istate, int i, int *type)
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500503{
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200504 const struct cache_entry *e = istate->cache[i];
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500505
506 if (!ce_stage(e)) {
507 *type = RESOLVED;
508 return i + 1;
509 }
510
511 *type = PUNTED;
Junio C Hamano11877b92018-10-19 13:34:02 +0900512 while (i < istate->cache_nr && ce_stage(istate->cache[i]) == 1)
Junio C Hamanofb70a062015-06-28 14:35:13 -0700513 i++;
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500514
515 /* Only handle regular files with both stages #2 and #3 */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200516 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];
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500519 if (ce_stage(e2) == 2 &&
520 ce_stage(e3) == 3 &&
521 ce_same_name(e, e3) &&
522 S_ISREG(e2->ce_mode) &&
523 S_ISREG(e3->ce_mode))
524 *type = THREE_STAGED;
525 }
526
527 /* Skip the entries with the same name */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200528 while (i < istate->cache_nr && ce_same_name(e, istate->cache[i]))
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500529 i++;
530 return i;
531}
532
Junio C Hamano4b68c2a2015-06-30 22:36:35 -0700533/*
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 */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200544static int find_conflict(struct repository *r, struct string_list *conflict)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200545{
546 int i;
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200547
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 09:13:26 +0700548 if (repo_read_index(r) < 0)
Thomas Gummerer2373b652018-08-05 18:20:30 +0100549 return error(_("index file corrupt"));
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500550
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200551 for (i = 0; i < r->index->cache_nr;) {
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500552 int conflict_type;
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200553 const struct cache_entry *e = r->index->cache[i];
554 i = check_one_conflict(r->index, i, &conflict_type);
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500555 if (conflict_type == THREE_STAGED)
556 string_list_insert(conflict, (const char *)e->name);
557 }
558 return 0;
559}
560
Junio C Hamano4b68c2a2015-06-30 22:36:35 -0700561/*
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 */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200576int rerere_remaining(struct repository *r, struct string_list *merge_rr)
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500577{
578 int i;
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200579
Nguyễn Thái Ngọc Duy55e6b352018-11-10 06:49:09 +0100580 if (setup_rerere(r, merge_rr, RERERE_READONLY))
Jeff King9dd330e2015-09-01 18:14:09 -0400581 return 0;
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 09:13:26 +0700582 if (repo_read_index(r) < 0)
Thomas Gummerer2373b652018-08-05 18:20:30 +0100583 return error(_("index file corrupt"));
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500584
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200585 for (i = 0; i < r->index->cache_nr;) {
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500586 int conflict_type;
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200587 const struct cache_entry *e = r->index->cache[i];
588 i = check_one_conflict(r->index, i, &conflict_type);
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500589 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) {
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700595 free_rerere_id(it);
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500596 it->util = RERERE_RESOLVED;
597 }
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200598 }
599 }
600 return 0;
601}
602
Junio C Hamanocc899ec2015-06-30 22:40:35 -0700603/*
Junio C Hamano0ce02b32016-03-11 14:53:05 -0800604 * Try using the given conflict resolution "ID" to see
605 * if that recorded conflict resolves cleanly what we
606 * got in the "cur".
607 */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200608static int try_merge(struct index_state *istate,
609 const struct rerere_id *id, const char *path,
Junio C Hamano0ce02b32016-03-11 14:53:05 -0800610 mmfile_t *cur, mmbuffer_t *result)
611{
Elijah Newren35f69672022-02-02 02:37:30 +0000612 enum ll_merge_result ret;
Junio C Hamano0ce02b32016-03-11 14:53:05 -0800613 mmfile_t base = {NULL, 0}, other = {NULL, 0};
614
615 if (read_mmfile(&base, rerere_path(id, "preimage")) ||
Elijah Newren35f69672022-02-02 02:37:30 +0000616 read_mmfile(&other, rerere_path(id, "postimage"))) {
617 ret = LL_MERGE_CONFLICT;
618 } else {
Junio C Hamano0ce02b32016-03-11 14:53:05 -0800619 /*
620 * A three-way merge. Note that this honors user-customizable
621 * low-level merge driver settings.
622 */
Nguyễn Thái Ngọc Duy32eaa462018-09-21 17:57:27 +0200623 ret = ll_merge(result, path, &base, NULL, cur, "", &other, "",
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200624 istate, NULL);
Elijah Newren35f69672022-02-02 02:37:30 +0000625 }
Junio C Hamano0ce02b32016-03-11 14:53:05 -0800626
627 free(base.ptr);
628 free(other.ptr);
629
630 return ret;
631}
632
633/*
Junio C Hamano18bb9932015-07-06 14:45:55 -0700634 * Find the conflict identified by "id"; the change between its
Junio C Hamanocc899ec2015-06-30 22:40:35 -0700635 * "preimage" (i.e. a previous contents with conflict markers) and its
636 * "postimage" (i.e. the corresponding contents with conflicts
637 * resolved) may apply cleanly to the contents stored in "path", i.e.
638 * the conflict this time around.
639 *
640 * Returns 0 for successful replay of recorded resolution, or non-zero
641 * for failure.
642 */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200643static int merge(struct index_state *istate, const struct rerere_id *id, const char *path)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200644{
Junio C Hamano15ed07d2015-07-06 15:32:53 -0700645 FILE *f;
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200646 int ret;
Junio C Hamano0ce02b32016-03-11 14:53:05 -0800647 mmfile_t cur = {NULL, 0};
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200648 mmbuffer_t result = {NULL, 0};
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200649
Junio C Hamanocc899ec2015-06-30 22:40:35 -0700650 /*
651 * Normalize the conflicts in path and write it out to
652 * "thisimage" temporary file.
653 */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200654 if ((handle_file(istate, path, NULL, rerere_path(id, "thisimage")) < 0) ||
Junio C Hamano0ce02b32016-03-11 14:53:05 -0800655 read_mmfile(&cur, rerere_path(id, "thisimage"))) {
Bert Wesarg689b8c22010-02-23 21:11:53 +0100656 ret = 1;
657 goto out;
658 }
SZEDER Gábor7d7ff152010-07-13 01:42:04 +0200659
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200660 ret = try_merge(istate, id, path, &cur, &result);
Junio C Hamano15ed07d2015-07-06 15:32:53 -0700661 if (ret)
662 goto out;
SZEDER Gábor7d7ff152010-07-13 01:42:04 +0200663
Junio C Hamano15ed07d2015-07-06 15:32:53 -0700664 /*
665 * A successful replay of recorded resolution.
666 * Mark that "postimage" was used to help gc.
667 */
668 if (utime(rerere_path(id, "postimage"), NULL) < 0)
Thomas Gummerer2373b652018-08-05 18:20:30 +0100669 warning_errno(_("failed utime() on '%s'"),
Nguyễn Thái Ngọc Duy033e0112016-05-08 16:47:52 +0700670 rerere_path(id, "postimage"));
Junio C Hamanocc899ec2015-06-30 22:40:35 -0700671
Junio C Hamano15ed07d2015-07-06 15:32:53 -0700672 /* Update "path" with the resolution */
673 f = fopen(path, "w");
674 if (!f)
Thomas Gummerer2373b652018-08-05 18:20:30 +0100675 return error_errno(_("could not open '%s'"), path);
Junio C Hamano15ed07d2015-07-06 15:32:53 -0700676 if (fwrite(result.ptr, result.size, 1, f) != 1)
Thomas Gummerer2373b652018-08-05 18:20:30 +0100677 error_errno(_("could not write '%s'"), path);
Junio C Hamano15ed07d2015-07-06 15:32:53 -0700678 if (fclose(f))
Thomas Gummerer2373b652018-08-05 18:20:30 +0100679 return error_errno(_("writing '%s' failed"), path);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200680
Bert Wesarg689b8c22010-02-23 21:11:53 +0100681out:
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200682 free(cur.ptr);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200683 free(result.ptr);
684
685 return ret;
686}
687
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200688static void update_paths(struct repository *r, struct string_list *update)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200689{
Martin Ågren0fa5a2e2018-05-09 22:55:39 +0200690 struct lock_file index_lock = LOCK_INIT;
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200691 int i;
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200692
Nguyễn Thái Ngọc Duy3a95f312019-01-12 09:13:24 +0700693 repo_hold_locked_index(r, &index_lock, LOCK_DIE_ON_ERROR);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200694
695 for (i = 0; i < update->nr; i++) {
Johannes Schindelinc455c872008-07-21 19:03:49 +0100696 struct string_list_item *item = &update->items[i];
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200697 if (add_file_to_index(r->index, item->string, 0))
Jonathan Nieder89ea9032014-12-02 20:20:49 -0800698 exit(128);
Thomas Gummerer2373b652018-08-05 18:20:30 +0100699 fprintf_ln(stderr, _("Staged '%s' using previous resolution."),
Junio C Hamanoa14c7ab2015-06-28 21:13:24 -0700700 item->string);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200701 }
702
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200703 if (write_locked_index(r->index, &index_lock,
Martin Ågren61000812018-03-01 21:40:20 +0100704 COMMIT_LOCK | SKIP_IF_UNCHANGED))
Thomas Gummerer2373b652018-08-05 18:20:30 +0100705 die(_("unable to write new index file"));
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200706}
707
Junio C Hamano629716d2015-07-30 15:49:18 -0700708static void remove_variant(struct rerere_id *id)
709{
710 unlink_or_warn(rerere_path(id, "postimage"));
711 unlink_or_warn(rerere_path(id, "preimage"));
712 id->collection->status[id->variant] = 0;
713}
714
Junio C Hamano8e7768b2015-06-30 19:36:24 -0700715/*
716 * The path indicated by rr_item may still have conflict for which we
717 * have a recorded resolution, in which case replay it and optionally
718 * update it. Or it may have been resolved by the user and we may
719 * only have the preimage for that conflict, in which case the result
720 * needs to be recorded as a resolution in a postimage file.
721 */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200722static void do_rerere_one_path(struct index_state *istate,
723 struct string_list_item *rr_item,
Junio C Hamano8e7768b2015-06-30 19:36:24 -0700724 struct string_list *update)
725{
726 const char *path = rr_item->string;
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700727 struct rerere_id *id = rr_item->util;
Junio C Hamano629716d2015-07-30 15:49:18 -0700728 struct rerere_dir *rr_dir = id->collection;
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700729 int variant;
Junio C Hamano8e7768b2015-06-30 19:36:24 -0700730
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700731 variant = id->variant;
Junio C Hamano8e7768b2015-06-30 19:36:24 -0700732
Junio C Hamano629716d2015-07-30 15:49:18 -0700733 /* Has the user resolved it already? */
734 if (variant >= 0) {
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200735 if (!handle_file(istate, path, NULL, NULL)) {
Junio C Hamano629716d2015-07-30 15:49:18 -0700736 copy_file(rerere_path(id, "postimage"), path, 0666);
737 id->collection->status[variant] |= RR_HAS_POSTIMAGE;
Thomas Gummerer2373b652018-08-05 18:20:30 +0100738 fprintf_ln(stderr, _("Recorded resolution for '%s'."), path);
Junio C Hamano629716d2015-07-30 15:49:18 -0700739 free_rerere_id(rr_item);
740 rr_item->util = NULL;
741 return;
Junio C Hamanoc0a54232015-07-20 15:19:44 -0700742 }
Junio C Hamano629716d2015-07-30 15:49:18 -0700743 /*
744 * There may be other variants that can cleanly
745 * replay. Try them and update the variant number for
746 * this one.
747 */
748 }
749
750 /* Does any existing resolution apply cleanly? */
751 for (variant = 0; variant < rr_dir->status_nr; variant++) {
752 const int both = RR_HAS_PREIMAGE | RR_HAS_POSTIMAGE;
753 struct rerere_id vid = *id;
754
755 if ((rr_dir->status[variant] & both) != both)
756 continue;
757
758 vid.variant = variant;
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200759 if (merge(istate, &vid, path))
Junio C Hamano629716d2015-07-30 15:49:18 -0700760 continue; /* failed to replay */
761
762 /*
763 * If there already is a different variant that applies
764 * cleanly, there is no point maintaining our own variant.
765 */
766 if (0 <= id->variant && id->variant != variant)
767 remove_variant(id);
Junio C Hamano8e7768b2015-06-30 19:36:24 -0700768
769 if (rerere_autoupdate)
770 string_list_insert(update, path);
771 else
Thomas Gummerer2373b652018-08-05 18:20:30 +0100772 fprintf_ln(stderr,
773 _("Resolved '%s' using previous resolution."),
774 path);
Junio C Hamano629716d2015-07-30 15:49:18 -0700775 free_rerere_id(rr_item);
776 rr_item->util = NULL;
Junio C Hamano925d73c2015-07-06 14:18:09 -0700777 return;
Junio C Hamano8e7768b2015-06-30 19:36:24 -0700778 }
Junio C Hamano629716d2015-07-30 15:49:18 -0700779
780 /* None of the existing one applies; we need a new variant */
781 assign_variant(id);
782
783 variant = id->variant;
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200784 handle_file(istate, path, NULL, rerere_path(id, "preimage"));
Junio C Hamano629716d2015-07-30 15:49:18 -0700785 if (id->collection->status[variant] & RR_HAS_POSTIMAGE) {
786 const char *path = rerere_path(id, "postimage");
787 if (unlink(path))
Thomas Gummerer2373b652018-08-05 18:20:30 +0100788 die_errno(_("cannot unlink stray '%s'"), path);
Junio C Hamano629716d2015-07-30 15:49:18 -0700789 id->collection->status[variant] &= ~RR_HAS_POSTIMAGE;
790 }
791 id->collection->status[variant] |= RR_HAS_PREIMAGE;
Thomas Gummerer2373b652018-08-05 18:20:30 +0100792 fprintf_ln(stderr, _("Recorded preimage for '%s'"), path);
Junio C Hamano8e7768b2015-06-30 19:36:24 -0700793}
794
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200795static int do_plain_rerere(struct repository *r,
796 struct string_list *rr, int fd)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200797{
Thiago Farina183113a2010-07-04 16:46:19 -0300798 struct string_list conflict = STRING_LIST_INIT_DUP;
799 struct string_list update = STRING_LIST_INIT_DUP;
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200800 int i;
801
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200802 find_conflict(r, &conflict);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200803
804 /*
Junio C Hamanocc899ec2015-06-30 22:40:35 -0700805 * MERGE_RR records paths with conflicts immediately after
806 * merge failed. Some of the conflicted paths might have been
807 * hand resolved in the working tree since then, but the
808 * initial run would catch all and register their preimages.
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200809 */
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200810 for (i = 0; i < conflict.nr; i++) {
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700811 struct rerere_id *id;
brian m. carlson0d7c4192018-10-15 00:02:02 +0000812 unsigned char hash[GIT_MAX_RAWSZ];
Johannes Schindelinc455c872008-07-21 19:03:49 +0100813 const char *path = conflict.items[i].string;
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200814 int ret;
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200815
Junio C Hamanoc7a25d32015-07-04 17:17:38 -0700816 /*
817 * Ask handle_file() to scan and assign a
818 * conflict ID. No need to write anything out
819 * yet.
820 */
Junio C Hamanod829d492018-10-30 15:43:42 +0900821 ret = handle_file(r->index, path, hash, NULL);
Thomas Gummererbd7dfa52018-08-05 18:20:37 +0100822 if (ret != 0 && string_list_has_string(rr, path)) {
Thomas Gummerer93406a22018-08-05 18:20:32 +0100823 remove_variant(string_list_lookup(rr, path)->util);
824 string_list_remove(rr, path, 1);
825 }
Junio C Hamanoc7a25d32015-07-04 17:17:38 -0700826 if (ret < 1)
827 continue;
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700828
brian m. carlson0d7c4192018-10-15 00:02:02 +0000829 id = new_rerere_id(hash);
Junio C Hamano18bb9932015-07-06 14:45:55 -0700830 string_list_insert(rr, path)->util = id;
Junio C Hamanocc899ec2015-06-30 22:40:35 -0700831
Junio C Hamanoc0a54232015-07-20 15:19:44 -0700832 /* Ensure that the directory exists. */
833 mkdir_in_gitdir(rerere_path(id, NULL));
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200834 }
835
Junio C Hamano8e7768b2015-06-30 19:36:24 -0700836 for (i = 0; i < rr->nr; i++)
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200837 do_rerere_one_path(r->index, &rr->items[i], &update);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200838
839 if (update.nr)
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200840 update_paths(r, &update);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200841
842 return write_rr(rr, fd);
843}
844
Tanay Abhra633e5ad2014-08-07 09:21:21 -0700845static void git_rerere_config(void)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200846{
Tanay Abhra633e5ad2014-08-07 09:21:21 -0700847 git_config_get_bool("rerere.enabled", &rerere_enabled);
848 git_config_get_bool("rerere.autoupdate", &rerere_autoupdate);
849 git_config(git_default_config, NULL);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200850}
851
Jeff Kingf9327292015-08-10 05:38:57 -0400852static GIT_PATH_FUNC(git_path_rr_cache, "rr-cache")
853
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200854static int is_rerere_enabled(void)
855{
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200856 int rr_cache_exists;
857
858 if (!rerere_enabled)
859 return 0;
860
Jeff Kingf9327292015-08-10 05:38:57 -0400861 rr_cache_exists = is_directory(git_path_rr_cache());
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200862 if (rerere_enabled < 0)
863 return rr_cache_exists;
864
Jeff Kingf9327292015-08-10 05:38:57 -0400865 if (!rr_cache_exists && mkdir_in_gitdir(git_path_rr_cache()))
Thomas Gummerer2373b652018-08-05 18:20:30 +0100866 die(_("could not create directory '%s'"), git_path_rr_cache());
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200867 return 1;
868}
869
Nguyễn Thái Ngọc Duy55e6b352018-11-10 06:49:09 +0100870int setup_rerere(struct repository *r, struct string_list *merge_rr, int flags)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200871{
872 int fd;
873
Tanay Abhra633e5ad2014-08-07 09:21:21 -0700874 git_rerere_config();
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200875 if (!is_rerere_enabled())
876 return -1;
877
Junio C Hamanocb6020b2009-12-04 00:20:48 -0800878 if (flags & (RERERE_AUTOUPDATE|RERERE_NOAUTOUPDATE))
879 rerere_autoupdate = !!(flags & RERERE_AUTOUPDATE);
Jeff King9dd330e2015-09-01 18:14:09 -0400880 if (flags & RERERE_READONLY)
881 fd = 0;
882 else
Stefan Beller102de882018-05-17 15:51:51 -0700883 fd = hold_lock_file_for_update(&write_lock,
Nguyễn Thái Ngọc Duy55e6b352018-11-10 06:49:09 +0100884 git_path_merge_rr(r),
Jeff King9dd330e2015-09-01 18:14:09 -0400885 LOCK_DIE_ON_ERROR);
Nguyễn Thái Ngọc Duy55e6b352018-11-10 06:49:09 +0100886 read_rr(r, merge_rr);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200887 return fd;
888}
889
Junio C Hamanocc899ec2015-06-30 22:40:35 -0700890/*
891 * The main entry point that is called internally from codepaths that
892 * perform mergy operations, possibly leaving conflicted index entries
893 * and working tree files.
894 */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200895int repo_rerere(struct repository *r, int flags)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200896{
Thiago Farina183113a2010-07-04 16:46:19 -0300897 struct string_list merge_rr = STRING_LIST_INIT_DUP;
Junio C Hamano1869bbe2015-07-16 14:50:05 -0700898 int fd, status;
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200899
Nguyễn Thái Ngọc Duy55e6b352018-11-10 06:49:09 +0100900 fd = setup_rerere(r, &merge_rr, flags);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200901 if (fd < 0)
902 return 0;
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200903 status = do_plain_rerere(r, &merge_rr, fd);
Junio C Hamano1869bbe2015-07-16 14:50:05 -0700904 free_rerere_dirs();
905 return status;
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200906}
Junio C Hamanodea45622009-12-25 15:51:32 -0800907
Junio C Hamano3d730ed2016-03-14 15:10:39 -0700908/*
909 * Subclass of rerere_io that reads from an in-core buffer that is a
910 * strbuf
911 */
912struct rerere_io_mem {
913 struct rerere_io io;
914 struct strbuf input;
915};
916
917/*
918 * ... and its getline() method implementation
919 */
920static int rerere_mem_getline(struct strbuf *sb, struct rerere_io *io_)
921{
922 struct rerere_io_mem *io = (struct rerere_io_mem *)io_;
923 char *ep;
924 size_t len;
925
926 strbuf_release(sb);
927 if (!io->input.len)
928 return -1;
929 ep = memchr(io->input.buf, '\n', io->input.len);
930 if (!ep)
931 ep = io->input.buf + io->input.len;
932 else if (*ep == '\n')
933 ep++;
934 len = ep - io->input.buf;
935 strbuf_add(sb, io->input.buf, len);
936 strbuf_remove(&io->input, 0, len);
937 return 0;
938}
939
Junio C Hamanod829d492018-10-30 15:43:42 +0900940static int handle_cache(struct index_state *istate,
941 const char *path, unsigned char *hash, const char *output)
Junio C Hamano3d730ed2016-03-14 15:10:39 -0700942{
943 mmfile_t mmfile[3] = {{NULL}};
944 mmbuffer_t result = {NULL, 0};
945 const struct cache_entry *ce;
Thomas Gummerer221444f2018-08-05 18:20:33 +0100946 int pos, len, i, has_conflicts;
Junio C Hamano3d730ed2016-03-14 15:10:39 -0700947 struct rerere_io_mem io;
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200948 int marker_size = ll_merge_marker_size(istate, path);
Junio C Hamano3d730ed2016-03-14 15:10:39 -0700949
950 /*
951 * Reproduce the conflicted merge in-core
952 */
953 len = strlen(path);
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200954 pos = index_name_pos(istate, path, len);
Junio C Hamano3d730ed2016-03-14 15:10:39 -0700955 if (0 <= pos)
956 return -1;
957 pos = -pos - 1;
958
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200959 while (pos < istate->cache_nr) {
Junio C Hamano3d730ed2016-03-14 15:10:39 -0700960 enum object_type type;
961 unsigned long size;
962
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200963 ce = istate->cache[pos++];
Junio C Hamano3d730ed2016-03-14 15:10:39 -0700964 if (ce_namelen(ce) != len || memcmp(ce->name, path, len))
965 break;
966 i = ce_stage(ce) - 1;
967 if (!mmfile[i].ptr) {
brian m. carlsonb4f5aca2018-03-12 02:27:53 +0000968 mmfile[i].ptr = read_object_file(&ce->oid, &type,
969 &size);
Junio C Hamano3d730ed2016-03-14 15:10:39 -0700970 mmfile[i].size = size;
971 }
972 }
973 for (i = 0; i < 3; i++)
974 if (!mmfile[i].ptr && !mmfile[i].size)
975 mmfile[i].ptr = xstrdup("");
976
977 /*
978 * NEEDSWORK: handle conflicts from merges with
979 * merge.renormalize set, too?
980 */
981 ll_merge(&result, path, &mmfile[0], NULL,
982 &mmfile[1], "ours",
Nguyễn Thái Ngọc Duy32eaa462018-09-21 17:57:27 +0200983 &mmfile[2], "theirs",
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200984 istate, NULL);
Junio C Hamano3d730ed2016-03-14 15:10:39 -0700985 for (i = 0; i < 3; i++)
986 free(mmfile[i].ptr);
987
988 memset(&io, 0, sizeof(io));
989 io.io.getline = rerere_mem_getline;
990 if (output)
991 io.io.output = fopen(output, "w");
992 else
993 io.io.output = NULL;
994 strbuf_init(&io.input, 0);
995 strbuf_attach(&io.input, result.ptr, result.size, result.size);
996
997 /*
998 * Grab the conflict ID and optionally write the original
999 * contents with conflict markers out.
1000 */
brian m. carlson0d7c4192018-10-15 00:02:02 +00001001 has_conflicts = handle_path(hash, (struct rerere_io *)&io, marker_size);
Junio C Hamano3d730ed2016-03-14 15:10:39 -07001002 strbuf_release(&io.input);
1003 if (io.io.output)
1004 fclose(io.io.output);
Thomas Gummerer221444f2018-08-05 18:20:33 +01001005 return has_conflicts;
Stephan Beyer5b2fd952008-07-09 14:58:57 +02001006}
Junio C Hamanodea45622009-12-25 15:51:32 -08001007
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +02001008static int rerere_forget_one_path(struct index_state *istate,
1009 const char *path,
1010 struct string_list *rr)
Junio C Hamanodea45622009-12-25 15:51:32 -08001011{
1012 const char *filename;
Junio C Hamano1d51ece2015-07-04 17:38:34 -07001013 struct rerere_id *id;
brian m. carlson0d7c4192018-10-15 00:02:02 +00001014 unsigned char hash[GIT_MAX_RAWSZ];
Junio C Hamanodea45622009-12-25 15:51:32 -08001015 int ret;
Junio C Hamano8d9b5a42015-06-30 13:03:36 -07001016 struct string_list_item *item;
Junio C Hamanodea45622009-12-25 15:51:32 -08001017
Junio C Hamano963ec002015-06-30 22:42:34 -07001018 /*
1019 * Recreate the original conflict from the stages in the
1020 * index and compute the conflict ID
1021 */
Junio C Hamanod829d492018-10-30 15:43:42 +09001022 ret = handle_cache(istate, path, hash, NULL);
Junio C Hamanodea45622009-12-25 15:51:32 -08001023 if (ret < 1)
Thomas Gummerer2373b652018-08-05 18:20:30 +01001024 return error(_("could not parse conflict hunks in '%s'"), path);
Junio C Hamano963ec002015-06-30 22:42:34 -07001025
1026 /* Nuke the recorded resolution for the conflict */
brian m. carlson0d7c4192018-10-15 00:02:02 +00001027 id = new_rerere_id(hash);
Junio C Hamano890fca82016-03-28 14:48:13 -07001028
1029 for (id->variant = 0;
1030 id->variant < id->collection->status_nr;
1031 id->variant++) {
1032 mmfile_t cur = { NULL, 0 };
1033 mmbuffer_t result = {NULL, 0};
1034 int cleanly_resolved;
1035
1036 if (!has_rerere_resolution(id))
1037 continue;
1038
Junio C Hamanod829d492018-10-30 15:43:42 +09001039 handle_cache(istate, path, hash, rerere_path(id, "thisimage"));
Junio C Hamano890fca82016-03-28 14:48:13 -07001040 if (read_mmfile(&cur, rerere_path(id, "thisimage"))) {
1041 free(cur.ptr);
Thomas Gummerer2373b652018-08-05 18:20:30 +01001042 error(_("failed to update conflicted state in '%s'"), path);
Junio C Hamano8f449612016-05-11 16:19:17 -07001043 goto fail_exit;
Junio C Hamano890fca82016-03-28 14:48:13 -07001044 }
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +02001045 cleanly_resolved = !try_merge(istate, id, path, &cur, &result);
Junio C Hamano890fca82016-03-28 14:48:13 -07001046 free(result.ptr);
1047 free(cur.ptr);
1048 if (cleanly_resolved)
1049 break;
1050 }
1051
Junio C Hamano8f449612016-05-11 16:19:17 -07001052 if (id->collection->status_nr <= id->variant) {
Thomas Gummerer2373b652018-08-05 18:20:30 +01001053 error(_("no remembered resolution for '%s'"), path);
Junio C Hamano8f449612016-05-11 16:19:17 -07001054 goto fail_exit;
1055 }
Junio C Hamano890fca82016-03-28 14:48:13 -07001056
Junio C Hamano18bb9932015-07-06 14:45:55 -07001057 filename = rerere_path(id, "postimage");
Junio C Hamano8f449612016-05-11 16:19:17 -07001058 if (unlink(filename)) {
1059 if (errno == ENOENT)
Thomas Gummerer2373b652018-08-05 18:20:30 +01001060 error(_("no remembered resolution for '%s'"), path);
Junio C Hamano8f449612016-05-11 16:19:17 -07001061 else
Thomas Gummerer2373b652018-08-05 18:20:30 +01001062 error_errno(_("cannot unlink '%s'"), filename);
Junio C Hamano8f449612016-05-11 16:19:17 -07001063 goto fail_exit;
Junio C Hamanod9d501b2016-05-19 12:51:22 -07001064 }
Junio C Hamanodea45622009-12-25 15:51:32 -08001065
Junio C Hamano963ec002015-06-30 22:42:34 -07001066 /*
1067 * Update the preimage so that the user can resolve the
1068 * conflict in the working tree, run us again to record
1069 * the postimage.
1070 */
Junio C Hamanod829d492018-10-30 15:43:42 +09001071 handle_cache(istate, path, hash, rerere_path(id, "preimage"));
Thomas Gummerer2373b652018-08-05 18:20:30 +01001072 fprintf_ln(stderr, _("Updated preimage for '%s'"), path);
Junio C Hamanodea45622009-12-25 15:51:32 -08001073
Junio C Hamano963ec002015-06-30 22:42:34 -07001074 /*
1075 * And remember that we can record resolution for this
1076 * conflict when the user is done.
1077 */
Junio C Hamano8d9b5a42015-06-30 13:03:36 -07001078 item = string_list_insert(rr, path);
Junio C Hamano1d51ece2015-07-04 17:38:34 -07001079 free_rerere_id(item);
Junio C Hamano18bb9932015-07-06 14:45:55 -07001080 item->util = id;
Thomas Gummerer2373b652018-08-05 18:20:30 +01001081 fprintf(stderr, _("Forgot resolution for '%s'\n"), path);
Junio C Hamanodea45622009-12-25 15:51:32 -08001082 return 0;
Junio C Hamano8f449612016-05-11 16:19:17 -07001083
1084fail_exit:
1085 free(id);
1086 return -1;
Junio C Hamanodea45622009-12-25 15:51:32 -08001087}
1088
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +02001089int rerere_forget(struct repository *r, struct pathspec *pathspec)
Junio C Hamanodea45622009-12-25 15:51:32 -08001090{
1091 int i, fd;
Thiago Farina183113a2010-07-04 16:46:19 -03001092 struct string_list conflict = STRING_LIST_INIT_DUP;
1093 struct string_list merge_rr = STRING_LIST_INIT_DUP;
Junio C Hamanodea45622009-12-25 15:51:32 -08001094
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 09:13:26 +07001095 if (repo_read_index(r) < 0)
Thomas Gummerer2373b652018-08-05 18:20:30 +01001096 return error(_("index file corrupt"));
Junio C Hamanodea45622009-12-25 15:51:32 -08001097
Nguyễn Thái Ngọc Duy55e6b352018-11-10 06:49:09 +01001098 fd = setup_rerere(r, &merge_rr, RERERE_NOAUTOUPDATE);
Jeff King05445742015-05-14 15:20:52 -04001099 if (fd < 0)
1100 return 0;
Junio C Hamanodea45622009-12-25 15:51:32 -08001101
Junio C Hamano963ec002015-06-30 22:42:34 -07001102 /*
1103 * The paths may have been resolved (incorrectly);
1104 * recover the original conflicted state and then
1105 * find the conflicted paths.
1106 */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +02001107 unmerge_index(r->index, pathspec);
1108 find_conflict(r, &conflict);
Junio C Hamanodea45622009-12-25 15:51:32 -08001109 for (i = 0; i < conflict.nr; i++) {
1110 struct string_list_item *it = &conflict.items[i];
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +02001111 if (!match_pathspec(r->index, pathspec, it->string,
Nguyễn Thái Ngọc Duyae8d0822014-01-24 20:40:33 +07001112 strlen(it->string), 0, NULL, 0))
Junio C Hamanodea45622009-12-25 15:51:32 -08001113 continue;
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +02001114 rerere_forget_one_path(r->index, it->string, &merge_rr);
Junio C Hamanodea45622009-12-25 15:51:32 -08001115 }
1116 return write_rr(&merge_rr, fd);
1117}
Junio C Hamano0f891e72011-05-08 12:55:34 -07001118
Junio C Hamanoe828de82015-06-30 22:43:37 -07001119/*
1120 * Garbage collection support
1121 */
Junio C Hamano1d51ece2015-07-04 17:38:34 -07001122
Junio C Hamano5ea82272017-08-19 11:16:01 -07001123static timestamp_t rerere_created_at(struct rerere_id *id)
Junio C Hamano0f891e72011-05-08 12:55:34 -07001124{
1125 struct stat st;
Junio C Hamano1d51ece2015-07-04 17:38:34 -07001126
Junio C Hamano18bb9932015-07-06 14:45:55 -07001127 return stat(rerere_path(id, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
Junio C Hamano0f891e72011-05-08 12:55:34 -07001128}
1129
Junio C Hamano5ea82272017-08-19 11:16:01 -07001130static timestamp_t rerere_last_used_at(struct rerere_id *id)
Junio C Hamano0f891e72011-05-08 12:55:34 -07001131{
Junio C Hamano0f891e72011-05-08 12:55:34 -07001132 struct stat st;
Junio C Hamano1d51ece2015-07-04 17:38:34 -07001133
Junio C Hamano18bb9932015-07-06 14:45:55 -07001134 return stat(rerere_path(id, "postimage"), &st) ? (time_t) 0 : st.st_mtime;
Junio C Hamano0f891e72011-05-08 12:55:34 -07001135}
1136
Junio C Hamanoe828de82015-06-30 22:43:37 -07001137/*
1138 * Remove the recorded resolution for a given conflict ID
1139 */
Junio C Hamano1d51ece2015-07-04 17:38:34 -07001140static void unlink_rr_item(struct rerere_id *id)
Junio C Hamano0f891e72011-05-08 12:55:34 -07001141{
Junio C Hamano1be1e852016-03-08 12:11:00 -08001142 unlink_or_warn(rerere_path(id, "thisimage"));
1143 remove_variant(id);
1144 id->collection->status[id->variant] = 0;
1145}
1146
Junio C Hamano5ea82272017-08-19 11:16:01 -07001147static void prune_one(struct rerere_id *id,
1148 timestamp_t cutoff_resolve, timestamp_t cutoff_noresolve)
Junio C Hamano1be1e852016-03-08 12:11:00 -08001149{
Junio C Hamano5ea82272017-08-19 11:16:01 -07001150 timestamp_t then;
1151 timestamp_t cutoff;
Junio C Hamano1be1e852016-03-08 12:11:00 -08001152
1153 then = rerere_last_used_at(id);
1154 if (then)
1155 cutoff = cutoff_resolve;
1156 else {
1157 then = rerere_created_at(id);
1158 if (!then)
1159 return;
1160 cutoff = cutoff_noresolve;
1161 }
Junio C Hamano5ea82272017-08-19 11:16:01 -07001162 if (then < cutoff)
Junio C Hamano1be1e852016-03-08 12:11:00 -08001163 unlink_rr_item(id);
Junio C Hamano0f891e72011-05-08 12:55:34 -07001164}
1165
Jeff King2bc1a872021-01-28 01:14:11 -05001166/* Does the basename in "path" look plausibly like an rr-cache entry? */
1167static int is_rr_cache_dirname(const char *path)
1168{
Jeff King098c1732021-01-28 01:16:50 -05001169 struct object_id oid;
1170 const char *end;
1171 return !parse_oid_hex(path, &oid, &end) && !*end;
Jeff King2bc1a872021-01-28 01:14:11 -05001172}
1173
Nguyễn Thái Ngọc Duy55e6b352018-11-10 06:49:09 +01001174void rerere_gc(struct repository *r, struct string_list *rr)
Junio C Hamano0f891e72011-05-08 12:55:34 -07001175{
1176 struct string_list to_remove = STRING_LIST_INIT_DUP;
1177 DIR *dir;
1178 struct dirent *e;
Junio C Hamano1be1e852016-03-08 12:11:00 -08001179 int i;
Junio C Hamano5ea82272017-08-19 11:16:01 -07001180 timestamp_t now = time(NULL);
1181 timestamp_t cutoff_noresolve = now - 15 * 86400;
1182 timestamp_t cutoff_resolve = now - 60 * 86400;
Junio C Hamano0f891e72011-05-08 12:55:34 -07001183
Nguyễn Thái Ngọc Duy55e6b352018-11-10 06:49:09 +01001184 if (setup_rerere(r, rr, 0) < 0)
Jeff King9dd330e2015-09-01 18:14:09 -04001185 return;
1186
Junio C Hamano6e96cb52017-08-19 11:43:39 -07001187 git_config_get_expiry_in_days("gc.rerereresolved", &cutoff_resolve, now);
1188 git_config_get_expiry_in_days("gc.rerereunresolved", &cutoff_noresolve, now);
Tanay Abhra633e5ad2014-08-07 09:21:21 -07001189 git_config(git_default_config, NULL);
Junio C Hamano0f891e72011-05-08 12:55:34 -07001190 dir = opendir(git_path("rr-cache"));
1191 if (!dir)
Thomas Gummerer2373b652018-08-05 18:20:30 +01001192 die_errno(_("unable to open rr-cache directory"));
Junio C Hamanoe828de82015-06-30 22:43:37 -07001193 /* Collect stale conflict IDs ... */
Elijah Newrenb548f0f2021-05-12 17:28:22 +00001194 while ((e = readdir_skip_dot_and_dotdot(dir))) {
Junio C Hamano1be1e852016-03-08 12:11:00 -08001195 struct rerere_dir *rr_dir;
1196 struct rerere_id id;
1197 int now_empty;
1198
Jeff King2bc1a872021-01-28 01:14:11 -05001199 if (!is_rr_cache_dirname(e->d_name))
Junio C Hamano1be1e852016-03-08 12:11:00 -08001200 continue; /* or should we remove e->d_name? */
Junio C Hamano0f891e72011-05-08 12:55:34 -07001201
Jeff King2bc1a872021-01-28 01:14:11 -05001202 rr_dir = find_rerere_dir(e->d_name);
1203
Junio C Hamano1be1e852016-03-08 12:11:00 -08001204 now_empty = 1;
1205 for (id.variant = 0, id.collection = rr_dir;
1206 id.variant < id.collection->status_nr;
1207 id.variant++) {
Junio C Hamano5ea82272017-08-19 11:16:01 -07001208 prune_one(&id, cutoff_resolve, cutoff_noresolve);
Junio C Hamano1be1e852016-03-08 12:11:00 -08001209 if (id.collection->status[id.variant])
1210 now_empty = 0;
Junio C Hamano0f891e72011-05-08 12:55:34 -07001211 }
Junio C Hamano1be1e852016-03-08 12:11:00 -08001212 if (now_empty)
Junio C Hamano0f891e72011-05-08 12:55:34 -07001213 string_list_append(&to_remove, e->d_name);
1214 }
Jim Meyeringa9930e32011-05-26 15:55:50 +02001215 closedir(dir);
Junio C Hamano1be1e852016-03-08 12:11:00 -08001216
1217 /* ... and then remove the empty directories */
Junio C Hamano0f891e72011-05-08 12:55:34 -07001218 for (i = 0; i < to_remove.nr; i++)
Junio C Hamano1be1e852016-03-08 12:11:00 -08001219 rmdir(git_path("rr-cache/%s", to_remove.items[i].string));
Junio C Hamano0f891e72011-05-08 12:55:34 -07001220 string_list_clear(&to_remove, 0);
Jeff King9dd330e2015-09-01 18:14:09 -04001221 rollback_lock_file(&write_lock);
Junio C Hamano0f891e72011-05-08 12:55:34 -07001222}
1223
Junio C Hamanoe828de82015-06-30 22:43:37 -07001224/*
1225 * During a conflict resolution, after "rerere" recorded the
1226 * preimages, abandon them if the user did not resolve them or
1227 * record their resolutions. And drop $GIT_DIR/MERGE_RR.
1228 *
1229 * NEEDSWORK: shouldn't we be calling this from "reset --hard"?
1230 */
Nguyễn Thái Ngọc Duy55e6b352018-11-10 06:49:09 +01001231void rerere_clear(struct repository *r, struct string_list *merge_rr)
Junio C Hamano0f891e72011-05-08 12:55:34 -07001232{
1233 int i;
1234
Nguyễn Thái Ngọc Duy55e6b352018-11-10 06:49:09 +01001235 if (setup_rerere(r, merge_rr, 0) < 0)
Jeff King9dd330e2015-09-01 18:14:09 -04001236 return;
1237
Junio C Hamano0f891e72011-05-08 12:55:34 -07001238 for (i = 0; i < merge_rr->nr; i++) {
Junio C Hamano1d51ece2015-07-04 17:38:34 -07001239 struct rerere_id *id = merge_rr->items[i].util;
Junio C Hamano1be1e852016-03-08 12:11:00 -08001240 if (!has_rerere_resolution(id)) {
Junio C Hamano18bb9932015-07-06 14:45:55 -07001241 unlink_rr_item(id);
Junio C Hamano1be1e852016-03-08 12:11:00 -08001242 rmdir(rerere_path(id, NULL));
1243 }
Junio C Hamano0f891e72011-05-08 12:55:34 -07001244 }
Nguyễn Thái Ngọc Duy55e6b352018-11-10 06:49:09 +01001245 unlink_or_warn(git_path_merge_rr(r));
Jeff King9dd330e2015-09-01 18:14:09 -04001246 rollback_lock_file(&write_lock);
Junio C Hamano0f891e72011-05-08 12:55:34 -07001247}