blob: ca7e77ba68c1ad4aa49f032906ba419098be0d04 [file] [log] [blame]
Elijah Newrenbc5c5ec2023-05-16 06:33:57 +00001#include "git-compat-util.h"
Elijah Newren0b027f62023-03-21 06:25:58 +00002#include "abspath.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07003#include "config.h"
Elijah Newrend5fff462023-04-22 20:17:12 +00004#include "copy.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00005#include "gettext.h"
Elijah Newren41771fa2023-02-24 00:09:27 +00006#include "hex.h"
Michael Haggerty697cc8e2014-10-01 12:28:42 +02007#include "lockfile.h"
Johannes Schindelinc455c872008-07-21 19:03:49 +01008#include "string-list.h"
Elijah Newren08c46a42023-05-16 06:33:56 +00009#include "read-cache-ll.h"
Stephan Beyer5b2fd952008-07-09 14:58:57 +020010#include "rerere.h"
Stephan Beyer5b2fd952008-07-09 14:58:57 +020011#include "xdiff-interface.h"
Junio C Hamanodea45622009-12-25 15:51:32 -080012#include "dir.h"
13#include "resolve-undo.h"
Elijah Newren67238992023-05-16 06:34:04 +000014#include "merge-ll.h"
Elijah Newrenc3399322023-05-16 06:33:59 +000015#include "path.h"
Nguyễn Thái Ngọc Duy01a10b02013-07-14 15:35:40 +070016#include "pathspec.h"
Elijah Newren87bed172023-04-11 00:41:53 -070017#include "object-file.h"
Elijah Newrena034e912023-05-16 06:34:06 +000018#include "object-store-ll.h"
Jeff King680ff912021-01-28 01:34:31 -050019#include "strmap.h"
Stephan Beyer5b2fd952008-07-09 14:58:57 +020020
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -050021#define RESOLVED 0
22#define PUNTED 1
23#define THREE_STAGED 2
24void *RERERE_RESOLVED = &RERERE_RESOLVED;
25
Stephan Beyer5b2fd952008-07-09 14:58:57 +020026/* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
27static int rerere_enabled = -1;
28
29/* automatically update cleanly resolved paths to the index */
30static int rerere_autoupdate;
31
Junio C Hamano2c7929b2015-07-16 15:47:13 -070032#define RR_HAS_POSTIMAGE 1
33#define RR_HAS_PREIMAGE 2
Jeff King680ff912021-01-28 01:34:31 -050034struct rerere_dir {
Junio C Hamanoa13d1372015-07-23 14:23:24 -070035 int status_alloc, status_nr;
36 unsigned char *status;
Jeff King680ff912021-01-28 01:34:31 -050037 char name[FLEX_ARRAY];
38};
39
40static struct strmap rerere_dirs = STRMAP_INIT;
Junio C Hamano1869bbe2015-07-16 14:50:05 -070041
42static void free_rerere_dirs(void)
43{
Jeff King680ff912021-01-28 01:34:31 -050044 struct hashmap_iter iter;
45 struct strmap_entry *ent;
46
47 strmap_for_each_entry(&rerere_dirs, &iter, ent) {
48 struct rerere_dir *rr_dir = ent->value;
49 free(rr_dir->status);
50 free(rr_dir);
Junio C Hamanoa13d1372015-07-23 14:23:24 -070051 }
Jeff King680ff912021-01-28 01:34:31 -050052 strmap_clear(&rerere_dirs, 0);
Junio C Hamano1869bbe2015-07-16 14:50:05 -070053}
54
Junio C Hamano1d51ece2015-07-04 17:38:34 -070055static void free_rerere_id(struct string_list_item *item)
Stephan Beyer5b2fd952008-07-09 14:58:57 +020056{
Junio C Hamano1d51ece2015-07-04 17:38:34 -070057 free(item->util);
Stephan Beyer5b2fd952008-07-09 14:58:57 +020058}
59
Junio C Hamano1d51ece2015-07-04 17:38:34 -070060static const char *rerere_id_hex(const struct rerere_id *id)
61{
Jeff King680ff912021-01-28 01:34:31 -050062 return id->collection->name;
Junio C Hamano1d51ece2015-07-04 17:38:34 -070063}
64
Junio C Hamanoa13d1372015-07-23 14:23:24 -070065static void fit_variant(struct rerere_dir *rr_dir, int variant)
66{
67 variant++;
68 ALLOC_GROW(rr_dir->status, variant, rr_dir->status_alloc);
69 if (rr_dir->status_nr < variant) {
70 memset(rr_dir->status + rr_dir->status_nr,
71 '\0', variant - rr_dir->status_nr);
72 rr_dir->status_nr = variant;
73 }
74}
75
76static void assign_variant(struct rerere_id *id)
77{
78 int variant;
79 struct rerere_dir *rr_dir = id->collection;
80
81 variant = id->variant;
82 if (variant < 0) {
Junio C Hamano629716d2015-07-30 15:49:18 -070083 for (variant = 0; variant < rr_dir->status_nr; variant++)
84 if (!rr_dir->status[variant])
85 break;
Junio C Hamanoa13d1372015-07-23 14:23:24 -070086 }
87 fit_variant(rr_dir, variant);
88 id->variant = variant;
Junio C Hamano1d51ece2015-07-04 17:38:34 -070089}
90
91const char *rerere_path(const struct rerere_id *id, const char *file)
92{
93 if (!file)
94 return git_path("rr-cache/%s", rerere_id_hex(id));
95
Junio C Hamanoa13d1372015-07-23 14:23:24 -070096 if (id->variant <= 0)
97 return git_path("rr-cache/%s/%s", rerere_id_hex(id), file);
98
99 return git_path("rr-cache/%s/%s.%d",
100 rerere_id_hex(id), file, id->variant);
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700101}
102
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700103static int is_rr_file(const char *name, const char *filename, int *variant)
Junio C Hamano2c7929b2015-07-16 15:47:13 -0700104{
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700105 const char *suffix;
106 char *ep;
107
108 if (!strcmp(name, filename)) {
109 *variant = 0;
110 return 1;
111 }
112 if (!skip_prefix(name, filename, &suffix) || *suffix != '.')
113 return 0;
114
115 errno = 0;
116 *variant = strtol(suffix + 1, &ep, 10);
117 if (errno || *ep)
118 return 0;
119 return 1;
Junio C Hamano2c7929b2015-07-16 15:47:13 -0700120}
121
122static void scan_rerere_dir(struct rerere_dir *rr_dir)
123{
124 struct dirent *de;
Jeff King680ff912021-01-28 01:34:31 -0500125 DIR *dir = opendir(git_path("rr-cache/%s", rr_dir->name));
Junio C Hamano2c7929b2015-07-16 15:47:13 -0700126
127 if (!dir)
128 return;
129 while ((de = readdir(dir)) != NULL) {
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700130 int variant;
131
132 if (is_rr_file(de->d_name, "postimage", &variant)) {
133 fit_variant(rr_dir, variant);
134 rr_dir->status[variant] |= RR_HAS_POSTIMAGE;
135 } else if (is_rr_file(de->d_name, "preimage", &variant)) {
136 fit_variant(rr_dir, variant);
137 rr_dir->status[variant] |= RR_HAS_PREIMAGE;
138 }
Junio C Hamano2c7929b2015-07-16 15:47:13 -0700139 }
140 closedir(dir);
141}
142
Junio C Hamano1869bbe2015-07-16 14:50:05 -0700143static struct rerere_dir *find_rerere_dir(const char *hex)
144{
Junio C Hamano1869bbe2015-07-16 14:50:05 -0700145 struct rerere_dir *rr_dir;
Junio C Hamano1869bbe2015-07-16 14:50:05 -0700146
Jeff King680ff912021-01-28 01:34:31 -0500147 rr_dir = strmap_get(&rerere_dirs, hex);
148 if (!rr_dir) {
149 FLEX_ALLOC_STR(rr_dir, name, hex);
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700150 rr_dir->status = NULL;
151 rr_dir->status_nr = 0;
152 rr_dir->status_alloc = 0;
Jeff King680ff912021-01-28 01:34:31 -0500153 strmap_put(&rerere_dirs, hex, rr_dir);
Junio C Hamano1869bbe2015-07-16 14:50:05 -0700154
Junio C Hamano2c7929b2015-07-16 15:47:13 -0700155 scan_rerere_dir(rr_dir);
Junio C Hamano1869bbe2015-07-16 14:50:05 -0700156 }
Jeff King680ff912021-01-28 01:34:31 -0500157 return rr_dir;
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700158}
159
160static int has_rerere_resolution(const struct rerere_id *id)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200161{
Junio C Hamano05dd9f12015-07-17 13:28:31 -0700162 const int both = RR_HAS_POSTIMAGE|RR_HAS_PREIMAGE;
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700163 int variant = id->variant;
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700164
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700165 if (variant < 0)
166 return 0;
167 return ((id->collection->status[variant] & both) == both);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200168}
169
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700170static struct rerere_id *new_rerere_id_hex(char *hex)
171{
172 struct rerere_id *id = xmalloc(sizeof(*id));
Junio C Hamano1869bbe2015-07-16 14:50:05 -0700173 id->collection = find_rerere_dir(hex);
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700174 id->variant = -1; /* not known yet */
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700175 return id;
176}
177
brian m. carlson3f34d702019-08-18 20:04:25 +0000178static struct rerere_id *new_rerere_id(unsigned char *hash)
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700179{
brian m. carlson3f34d702019-08-18 20:04:25 +0000180 return new_rerere_id_hex(hash_to_hex(hash));
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700181}
182
Junio C Hamano4b68c2a2015-06-30 22:36:35 -0700183/*
184 * $GIT_DIR/MERGE_RR file is a collection of records, each of which is
185 * "conflict ID", a HT and pathname, terminated with a NUL, and is
186 * used to keep track of the set of paths that "rerere" may need to
187 * work on (i.e. what is left by the previous invocation of "git
188 * rerere" during the current conflict resolution session).
189 */
Nguyễn Thái Ngọc Duy55e6b352018-11-10 06:49:09 +0100190static void read_rr(struct repository *r, struct string_list *rr)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200191{
Junio C Hamanof5800f62015-06-28 15:51:59 -0700192 struct strbuf buf = STRBUF_INIT;
Nguyễn Thái Ngọc Duy55e6b352018-11-10 06:49:09 +0100193 FILE *in = fopen_or_warn(git_path_merge_rr(r), "r");
Junio C Hamanof5800f62015-06-28 15:51:59 -0700194
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200195 if (!in)
196 return;
Junio C Hamanof5800f62015-06-28 15:51:59 -0700197 while (!strbuf_getwholeline(&buf, in, '\0')) {
198 char *path;
brian m. carlson0d7c4192018-10-15 00:02:02 +0000199 unsigned char hash[GIT_MAX_RAWSZ];
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700200 struct rerere_id *id;
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700201 int variant;
brian m. carlson0d7c4192018-10-15 00:02:02 +0000202 const unsigned hexsz = the_hash_algo->hexsz;
Junio C Hamanof5800f62015-06-28 15:51:59 -0700203
204 /* There has to be the hash, tab, path and then NUL */
Junio C Hamano08e5fb12023-07-24 16:11:03 -0700205 if (buf.len < hexsz + 2 || get_hash_hex(buf.buf, hash))
Thomas Gummerer2373b652018-08-05 18:20:30 +0100206 die(_("corrupt MERGE_RR"));
Junio C Hamanof5800f62015-06-28 15:51:59 -0700207
brian m. carlson0d7c4192018-10-15 00:02:02 +0000208 if (buf.buf[hexsz] != '.') {
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700209 variant = 0;
brian m. carlson0d7c4192018-10-15 00:02:02 +0000210 path = buf.buf + hexsz;
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700211 } else {
212 errno = 0;
brian m. carlson0d7c4192018-10-15 00:02:02 +0000213 variant = strtol(buf.buf + hexsz + 1, &path, 10);
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700214 if (errno)
Thomas Gummerer2373b652018-08-05 18:20:30 +0100215 die(_("corrupt MERGE_RR"));
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700216 }
217 if (*(path++) != '\t')
Thomas Gummerer2373b652018-08-05 18:20:30 +0100218 die(_("corrupt MERGE_RR"));
brian m. carlson0d7c4192018-10-15 00:02:02 +0000219 buf.buf[hexsz] = '\0';
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700220 id = new_rerere_id_hex(buf.buf);
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700221 id->variant = variant;
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700222 string_list_insert(rr, path)->util = id;
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200223 }
Junio C Hamanof5800f62015-06-28 15:51:59 -0700224 strbuf_release(&buf);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200225 fclose(in);
226}
227
228static struct lock_file write_lock;
229
Johannes Schindelinc455c872008-07-21 19:03:49 +0100230static int write_rr(struct string_list *rr, int out_fd)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200231{
232 int i;
233 for (i = 0; i < rr->nr; i++) {
Junio C Hamanoe2cb6a92015-06-28 16:28:00 -0700234 struct strbuf buf = STRBUF_INIT;
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700235 struct rerere_id *id;
Junio C Hamanoe2cb6a92015-06-28 16:28:00 -0700236
237 assert(rr->items[i].util != RERERE_RESOLVED);
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700238
239 id = rr->items[i].util;
240 if (!id)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200241 continue;
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700242 assert(id->variant >= 0);
243 if (0 < id->variant)
244 strbuf_addf(&buf, "%s.%d\t%s%c",
245 rerere_id_hex(id), id->variant,
246 rr->items[i].string, 0);
247 else
248 strbuf_addf(&buf, "%s\t%s%c",
249 rerere_id_hex(id),
250 rr->items[i].string, 0);
251
Jeff King06f46f22017-09-13 13:16:03 -0400252 if (write_in_full(out_fd, buf.buf, buf.len) < 0)
Thomas Gummerer2373b652018-08-05 18:20:30 +0100253 die(_("unable to write rerere record"));
Junio C Hamanoe2cb6a92015-06-28 16:28:00 -0700254
255 strbuf_release(&buf);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200256 }
257 if (commit_lock_file(&write_lock) != 0)
Thomas Gummerer2373b652018-08-05 18:20:30 +0100258 die(_("unable to write rerere record"));
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200259 return 0;
260}
261
Junio C Hamanoa96847c2015-06-30 22:33:19 -0700262/*
263 * "rerere" interacts with conflicted file contents using this I/O
264 * abstraction. It reads a conflicted contents from one place via
265 * "getline()" method, and optionally can write it out after
266 * normalizing the conflicted hunks to the "output". Subclasses of
267 * rerere_io embed this structure at the beginning of their own
268 * rerere_io object.
269 */
270struct rerere_io {
271 int (*getline)(struct strbuf *, struct rerere_io *);
272 FILE *output;
273 int wrerror;
274 /* some more stuff */
275};
276
Alex Riesen47d32af2008-12-05 01:35:48 +0100277static void ferr_write(const void *p, size_t count, FILE *fp, int *err)
278{
279 if (!count || *err)
280 return;
281 if (fwrite(p, count, 1, fp) != 1)
282 *err = errno;
283}
284
285static inline void ferr_puts(const char *s, FILE *fp, int *err)
286{
287 ferr_write(s, strlen(s), fp, err);
288}
289
Junio C Hamano27d6b082009-12-25 14:34:53 -0800290static void rerere_io_putstr(const char *str, struct rerere_io *io)
291{
292 if (io->output)
293 ferr_puts(str, io->output, &io->wrerror);
294}
295
296static void rerere_io_putmem(const char *mem, size_t sz, struct rerere_io *io)
297{
298 if (io->output)
299 ferr_write(mem, sz, io->output, &io->wrerror);
300}
301
Junio C Hamanoa96847c2015-06-30 22:33:19 -0700302/*
303 * Subclass of rerere_io that reads from an on-disk file
304 */
Junio C Hamano27d6b082009-12-25 14:34:53 -0800305struct rerere_io_file {
306 struct rerere_io io;
307 FILE *input;
308};
309
Junio C Hamanoa96847c2015-06-30 22:33:19 -0700310/*
311 * ... and its getline() method implementation
312 */
Junio C Hamano27d6b082009-12-25 14:34:53 -0800313static int rerere_file_getline(struct strbuf *sb, struct rerere_io *io_)
314{
315 struct rerere_io_file *io = (struct rerere_io_file *)io_;
316 return strbuf_getwholeline(sb, io->input, '\n');
317}
318
Junio C Hamano67711cd2015-06-29 15:05:24 -0700319/*
320 * Require the exact number of conflict marker letters, no more, no
321 * less, followed by SP or any whitespace
322 * (including LF).
323 */
324static int is_cmarker(char *buf, int marker_char, int marker_size)
Junio C Hamano191f24172010-01-16 23:06:45 -0800325{
Junio C Hamano67711cd2015-06-29 15:05:24 -0700326 int want_sp;
327
328 /*
329 * The beginning of our version and the end of their version
330 * always are labeled like "<<<<< ours" or ">>>>> theirs",
331 * hence we set want_sp for them. Note that the version from
332 * the common ancestor in diff3-style output is not always
333 * labelled (e.g. "||||| common" is often seen but "|||||"
334 * alone is also valid), so we do not set want_sp.
335 */
336 want_sp = (marker_char == '<') || (marker_char == '>');
337
Junio C Hamano191f24172010-01-16 23:06:45 -0800338 while (marker_size--)
339 if (*buf++ != marker_char)
340 return 0;
341 if (want_sp && *buf != ' ')
342 return 0;
343 return isspace(*buf);
344}
345
Thomas Gummerer5ebbdad2018-08-05 18:20:35 +0100346static void rerere_strbuf_putconflict(struct strbuf *buf, int ch, size_t size)
347{
348 strbuf_addchars(buf, ch, size);
349 strbuf_addch(buf, '\n');
350}
351
352static int handle_conflict(struct strbuf *out, struct rerere_io *io,
brian m. carlson0d7c4192018-10-15 00:02:02 +0000353 int marker_size, git_hash_ctx *ctx)
Thomas Gummererc0f16f82018-08-05 18:20:34 +0100354{
355 enum {
356 RR_SIDE_1 = 0, RR_SIDE_2, RR_ORIGINAL
357 } hunk = RR_SIDE_1;
358 struct strbuf one = STRBUF_INIT, two = STRBUF_INIT;
Thomas Gummerer4af32202018-08-05 18:20:36 +0100359 struct strbuf buf = STRBUF_INIT, conflict = STRBUF_INIT;
Thomas Gummererc0f16f82018-08-05 18:20:34 +0100360 int has_conflicts = -1;
361
362 while (!io->getline(&buf, io)) {
363 if (is_cmarker(buf.buf, '<', marker_size)) {
Thomas Gummerer4af32202018-08-05 18:20:36 +0100364 if (handle_conflict(&conflict, io, marker_size, NULL) < 0)
365 break;
366 if (hunk == RR_SIDE_1)
367 strbuf_addbuf(&one, &conflict);
368 else
369 strbuf_addbuf(&two, &conflict);
370 strbuf_release(&conflict);
Thomas Gummererc0f16f82018-08-05 18:20:34 +0100371 } else if (is_cmarker(buf.buf, '|', marker_size)) {
372 if (hunk != RR_SIDE_1)
373 break;
374 hunk = RR_ORIGINAL;
375 } else if (is_cmarker(buf.buf, '=', marker_size)) {
376 if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL)
377 break;
378 hunk = RR_SIDE_2;
379 } else if (is_cmarker(buf.buf, '>', marker_size)) {
380 if (hunk != RR_SIDE_2)
381 break;
382 if (strbuf_cmp(&one, &two) > 0)
383 strbuf_swap(&one, &two);
384 has_conflicts = 1;
Thomas Gummerer5ebbdad2018-08-05 18:20:35 +0100385 rerere_strbuf_putconflict(out, '<', marker_size);
386 strbuf_addbuf(out, &one);
387 rerere_strbuf_putconflict(out, '=', marker_size);
388 strbuf_addbuf(out, &two);
389 rerere_strbuf_putconflict(out, '>', marker_size);
Thomas Gummererc0f16f82018-08-05 18:20:34 +0100390 if (ctx) {
brian m. carlson0d7c4192018-10-15 00:02:02 +0000391 the_hash_algo->update_fn(ctx, one.buf ?
392 one.buf : "",
393 one.len + 1);
394 the_hash_algo->update_fn(ctx, two.buf ?
395 two.buf : "",
396 two.len + 1);
Thomas Gummererc0f16f82018-08-05 18:20:34 +0100397 }
398 break;
399 } else if (hunk == RR_SIDE_1)
400 strbuf_addbuf(&one, &buf);
401 else if (hunk == RR_ORIGINAL)
402 ; /* discard */
403 else if (hunk == RR_SIDE_2)
404 strbuf_addbuf(&two, &buf);
405 }
406 strbuf_release(&one);
407 strbuf_release(&two);
408 strbuf_release(&buf);
409
410 return has_conflicts;
411}
412
Junio C Hamanocc899ec2015-06-30 22:40:35 -0700413/*
414 * Read contents a file with conflicts, normalize the conflicts
415 * by (1) discarding the common ancestor version in diff3-style,
416 * (2) reordering our side and their side so that whichever sorts
417 * alphabetically earlier comes before the other one, while
418 * computing the "conflict ID", which is just an SHA-1 hash of
419 * one side of the conflict, NUL, the other side of the conflict,
420 * and NUL concatenated together.
421 *
Thomas Gummerer221444f2018-08-05 18:20:33 +0100422 * Return 1 if conflict hunks are found, 0 if there are no conflict
Elijah Newren15beaaa2019-11-05 17:07:23 +0000423 * hunks and -1 if an error occurred.
Junio C Hamanocc899ec2015-06-30 22:40:35 -0700424 */
brian m. carlson0d7c4192018-10-15 00:02:02 +0000425static int handle_path(unsigned char *hash, struct rerere_io *io, int marker_size)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200426{
brian m. carlson0d7c4192018-10-15 00:02:02 +0000427 git_hash_ctx ctx;
Thomas Gummerer5ebbdad2018-08-05 18:20:35 +0100428 struct strbuf buf = STRBUF_INIT, out = STRBUF_INIT;
Thomas Gummererc0f16f82018-08-05 18:20:34 +0100429 int has_conflicts = 0;
brian m. carlson0d7c4192018-10-15 00:02:02 +0000430 if (hash)
431 the_hash_algo->init_fn(&ctx);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200432
Junio C Hamano27d6b082009-12-25 14:34:53 -0800433 while (!io->getline(&buf, io)) {
Junio C Hamano67711cd2015-06-29 15:05:24 -0700434 if (is_cmarker(buf.buf, '<', marker_size)) {
Thomas Gummerer5ebbdad2018-08-05 18:20:35 +0100435 has_conflicts = handle_conflict(&out, io, marker_size,
brian m. carlson0d7c4192018-10-15 00:02:02 +0000436 hash ? &ctx : NULL);
Thomas Gummererc0f16f82018-08-05 18:20:34 +0100437 if (has_conflicts < 0)
438 break;
Thomas Gummerer5ebbdad2018-08-05 18:20:35 +0100439 rerere_io_putmem(out.buf, out.len, io);
440 strbuf_reset(&out);
Thomas Gummererc0f16f82018-08-05 18:20:34 +0100441 } else
Junio C Hamano27d6b082009-12-25 14:34:53 -0800442 rerere_io_putstr(buf.buf, io);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200443 }
Junio C Hamanod58ee6d2009-12-25 13:55:29 -0800444 strbuf_release(&buf);
Thomas Gummerer5ebbdad2018-08-05 18:20:35 +0100445 strbuf_release(&out);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200446
brian m. carlson0d7c4192018-10-15 00:02:02 +0000447 if (hash)
448 the_hash_algo->final_fn(hash, &ctx);
Thomas Gummererc0f16f82018-08-05 18:20:34 +0100449
Thomas Gummerer221444f2018-08-05 18:20:33 +0100450 return has_conflicts;
Junio C Hamano27d6b082009-12-25 14:34:53 -0800451}
452
Junio C Hamanocc899ec2015-06-30 22:40:35 -0700453/*
454 * Scan the path for conflicts, do the "handle_path()" thing above, and
455 * return the number of conflict hunks found.
456 */
Junio C Hamanod829d492018-10-30 15:43:42 +0900457static int handle_file(struct index_state *istate,
458 const char *path, unsigned char *hash, const char *output)
Junio C Hamano27d6b082009-12-25 14:34:53 -0800459{
Thomas Gummerer221444f2018-08-05 18:20:33 +0100460 int has_conflicts = 0;
Junio C Hamano27d6b082009-12-25 14:34:53 -0800461 struct rerere_io_file io;
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200462 int marker_size = ll_merge_marker_size(istate, path);
Junio C Hamano27d6b082009-12-25 14:34:53 -0800463
464 memset(&io, 0, sizeof(io));
465 io.io.getline = rerere_file_getline;
466 io.input = fopen(path, "r");
467 io.io.wrerror = 0;
468 if (!io.input)
Thomas Gummerer2373b652018-08-05 18:20:30 +0100469 return error_errno(_("could not open '%s'"), path);
Junio C Hamano27d6b082009-12-25 14:34:53 -0800470
471 if (output) {
472 io.io.output = fopen(output, "w");
473 if (!io.io.output) {
Thomas Gummerer2373b652018-08-05 18:20:30 +0100474 error_errno(_("could not write '%s'"), output);
Junio C Hamano27d6b082009-12-25 14:34:53 -0800475 fclose(io.input);
Nguyễn Thái Ngọc Duyf7566f02017-05-09 17:11:33 +0700476 return -1;
Junio C Hamano27d6b082009-12-25 14:34:53 -0800477 }
478 }
479
brian m. carlson0d7c4192018-10-15 00:02:02 +0000480 has_conflicts = handle_path(hash, (struct rerere_io *)&io, marker_size);
Junio C Hamano27d6b082009-12-25 14:34:53 -0800481
482 fclose(io.input);
483 if (io.io.wrerror)
Thomas Gummerer2373b652018-08-05 18:20:30 +0100484 error(_("there were errors while writing '%s' (%s)"),
Junio C Hamano27d6b082009-12-25 14:34:53 -0800485 path, strerror(io.io.wrerror));
486 if (io.io.output && fclose(io.io.output))
Thomas Gummerer2373b652018-08-05 18:20:30 +0100487 io.io.wrerror = error_errno(_("failed to flush '%s'"), path);
Junio C Hamano27d6b082009-12-25 14:34:53 -0800488
Thomas Gummerer221444f2018-08-05 18:20:33 +0100489 if (has_conflicts < 0) {
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200490 if (output)
Alex Riesen691f1a22009-04-29 23:22:56 +0200491 unlink_or_warn(output);
Thomas Gummerer2373b652018-08-05 18:20:30 +0100492 return error(_("could not parse conflict hunks in '%s'"), path);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200493 }
Junio C Hamano27d6b082009-12-25 14:34:53 -0800494 if (io.io.wrerror)
Alex Riesen47d32af2008-12-05 01:35:48 +0100495 return -1;
Thomas Gummerer221444f2018-08-05 18:20:33 +0100496 return has_conflicts;
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200497}
498
Junio C Hamanoa96847c2015-06-30 22:33:19 -0700499/*
Junio C Hamano4b68c2a2015-06-30 22:36:35 -0700500 * Look at a cache entry at "i" and see if it is not conflicting,
501 * conflicting and we are willing to handle, or conflicting and
502 * we are unable to handle, and return the determination in *type.
503 * Return the cache index to be looked at next, by skipping the
504 * stages we have already looked at in this invocation of this
505 * function.
506 */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200507static int check_one_conflict(struct index_state *istate, int i, int *type)
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500508{
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200509 const struct cache_entry *e = istate->cache[i];
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500510
511 if (!ce_stage(e)) {
512 *type = RESOLVED;
513 return i + 1;
514 }
515
516 *type = PUNTED;
Junio C Hamano11877b92018-10-19 13:34:02 +0900517 while (i < istate->cache_nr && ce_stage(istate->cache[i]) == 1)
Junio C Hamanofb70a062015-06-28 14:35:13 -0700518 i++;
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500519
520 /* Only handle regular files with both stages #2 and #3 */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200521 if (i + 1 < istate->cache_nr) {
522 const struct cache_entry *e2 = istate->cache[i];
523 const struct cache_entry *e3 = istate->cache[i + 1];
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500524 if (ce_stage(e2) == 2 &&
525 ce_stage(e3) == 3 &&
526 ce_same_name(e, e3) &&
527 S_ISREG(e2->ce_mode) &&
528 S_ISREG(e3->ce_mode))
529 *type = THREE_STAGED;
530 }
531
532 /* Skip the entries with the same name */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200533 while (i < istate->cache_nr && ce_same_name(e, istate->cache[i]))
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500534 i++;
535 return i;
536}
537
Junio C Hamano4b68c2a2015-06-30 22:36:35 -0700538/*
539 * Scan the index and find paths that have conflicts that rerere can
540 * handle, i.e. the ones that has both stages #2 and #3.
541 *
542 * NEEDSWORK: we do not record or replay a previous "resolve by
543 * deletion" for a delete-modify conflict, as that is inherently risky
544 * without knowing what modification is being discarded. The only
545 * safe case, i.e. both side doing the deletion and modification that
546 * are identical to the previous round, might want to be handled,
547 * though.
548 */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200549static int find_conflict(struct repository *r, struct string_list *conflict)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200550{
551 int i;
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200552
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 09:13:26 +0700553 if (repo_read_index(r) < 0)
Thomas Gummerer2373b652018-08-05 18:20:30 +0100554 return error(_("index file corrupt"));
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500555
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200556 for (i = 0; i < r->index->cache_nr;) {
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500557 int conflict_type;
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200558 const struct cache_entry *e = r->index->cache[i];
559 i = check_one_conflict(r->index, i, &conflict_type);
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500560 if (conflict_type == THREE_STAGED)
561 string_list_insert(conflict, (const char *)e->name);
562 }
563 return 0;
564}
565
Junio C Hamano4b68c2a2015-06-30 22:36:35 -0700566/*
567 * The merge_rr list is meant to hold outstanding conflicted paths
568 * that rerere could handle. Abuse the list by adding other types of
569 * entries to allow the caller to show "rerere remaining".
570 *
571 * - Conflicted paths that rerere does not handle are added
572 * - Conflicted paths that have been resolved are marked as such
573 * by storing RERERE_RESOLVED to .util field (where conflict ID
574 * is expected to be stored).
575 *
576 * Do *not* write MERGE_RR file out after calling this function.
577 *
578 * NEEDSWORK: we may want to fix the caller that implements "rerere
579 * remaining" to do this without abusing merge_rr.
580 */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200581int rerere_remaining(struct repository *r, struct string_list *merge_rr)
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500582{
583 int i;
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200584
Nguyễn Thái Ngọc Duy55e6b352018-11-10 06:49:09 +0100585 if (setup_rerere(r, merge_rr, RERERE_READONLY))
Jeff King9dd330e2015-09-01 18:14:09 -0400586 return 0;
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 09:13:26 +0700587 if (repo_read_index(r) < 0)
Thomas Gummerer2373b652018-08-05 18:20:30 +0100588 return error(_("index file corrupt"));
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500589
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200590 for (i = 0; i < r->index->cache_nr;) {
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500591 int conflict_type;
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200592 const struct cache_entry *e = r->index->cache[i];
593 i = check_one_conflict(r->index, i, &conflict_type);
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500594 if (conflict_type == PUNTED)
595 string_list_insert(merge_rr, (const char *)e->name);
596 else if (conflict_type == RESOLVED) {
597 struct string_list_item *it;
598 it = string_list_lookup(merge_rr, (const char *)e->name);
Junio C Hamanoafe8a902022-05-02 09:50:37 -0700599 if (it) {
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700600 free_rerere_id(it);
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500601 it->util = RERERE_RESOLVED;
602 }
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200603 }
604 }
605 return 0;
606}
607
Junio C Hamanocc899ec2015-06-30 22:40:35 -0700608/*
Junio C Hamano0ce02b32016-03-11 14:53:05 -0800609 * Try using the given conflict resolution "ID" to see
610 * if that recorded conflict resolves cleanly what we
611 * got in the "cur".
612 */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200613static int try_merge(struct index_state *istate,
614 const struct rerere_id *id, const char *path,
Junio C Hamano0ce02b32016-03-11 14:53:05 -0800615 mmfile_t *cur, mmbuffer_t *result)
616{
Elijah Newren35f69672022-02-02 02:37:30 +0000617 enum ll_merge_result ret;
Junio C Hamano0ce02b32016-03-11 14:53:05 -0800618 mmfile_t base = {NULL, 0}, other = {NULL, 0};
619
620 if (read_mmfile(&base, rerere_path(id, "preimage")) ||
Elijah Newren35f69672022-02-02 02:37:30 +0000621 read_mmfile(&other, rerere_path(id, "postimage"))) {
622 ret = LL_MERGE_CONFLICT;
623 } else {
Junio C Hamano0ce02b32016-03-11 14:53:05 -0800624 /*
625 * A three-way merge. Note that this honors user-customizable
626 * low-level merge driver settings.
627 */
Nguyễn Thái Ngọc Duy32eaa462018-09-21 17:57:27 +0200628 ret = ll_merge(result, path, &base, NULL, cur, "", &other, "",
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200629 istate, NULL);
Elijah Newren35f69672022-02-02 02:37:30 +0000630 }
Junio C Hamano0ce02b32016-03-11 14:53:05 -0800631
632 free(base.ptr);
633 free(other.ptr);
634
635 return ret;
636}
637
638/*
Junio C Hamano18bb9932015-07-06 14:45:55 -0700639 * Find the conflict identified by "id"; the change between its
Junio C Hamanocc899ec2015-06-30 22:40:35 -0700640 * "preimage" (i.e. a previous contents with conflict markers) and its
641 * "postimage" (i.e. the corresponding contents with conflicts
642 * resolved) may apply cleanly to the contents stored in "path", i.e.
643 * the conflict this time around.
644 *
645 * Returns 0 for successful replay of recorded resolution, or non-zero
646 * for failure.
647 */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200648static int merge(struct index_state *istate, const struct rerere_id *id, const char *path)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200649{
Junio C Hamano15ed07d2015-07-06 15:32:53 -0700650 FILE *f;
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200651 int ret;
Junio C Hamano0ce02b32016-03-11 14:53:05 -0800652 mmfile_t cur = {NULL, 0};
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200653 mmbuffer_t result = {NULL, 0};
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200654
Junio C Hamanocc899ec2015-06-30 22:40:35 -0700655 /*
656 * Normalize the conflicts in path and write it out to
657 * "thisimage" temporary file.
658 */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200659 if ((handle_file(istate, path, NULL, rerere_path(id, "thisimage")) < 0) ||
Junio C Hamano0ce02b32016-03-11 14:53:05 -0800660 read_mmfile(&cur, rerere_path(id, "thisimage"))) {
Bert Wesarg689b8c22010-02-23 21:11:53 +0100661 ret = 1;
662 goto out;
663 }
SZEDER Gábor7d7ff152010-07-13 01:42:04 +0200664
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200665 ret = try_merge(istate, id, path, &cur, &result);
Junio C Hamano15ed07d2015-07-06 15:32:53 -0700666 if (ret)
667 goto out;
SZEDER Gábor7d7ff152010-07-13 01:42:04 +0200668
Junio C Hamano15ed07d2015-07-06 15:32:53 -0700669 /*
670 * A successful replay of recorded resolution.
671 * Mark that "postimage" was used to help gc.
672 */
673 if (utime(rerere_path(id, "postimage"), NULL) < 0)
Thomas Gummerer2373b652018-08-05 18:20:30 +0100674 warning_errno(_("failed utime() on '%s'"),
Nguyễn Thái Ngọc Duy033e0112016-05-08 16:47:52 +0700675 rerere_path(id, "postimage"));
Junio C Hamanocc899ec2015-06-30 22:40:35 -0700676
Junio C Hamano15ed07d2015-07-06 15:32:53 -0700677 /* Update "path" with the resolution */
678 f = fopen(path, "w");
679 if (!f)
Thomas Gummerer2373b652018-08-05 18:20:30 +0100680 return error_errno(_("could not open '%s'"), path);
Junio C Hamano15ed07d2015-07-06 15:32:53 -0700681 if (fwrite(result.ptr, result.size, 1, f) != 1)
Thomas Gummerer2373b652018-08-05 18:20:30 +0100682 error_errno(_("could not write '%s'"), path);
Junio C Hamano15ed07d2015-07-06 15:32:53 -0700683 if (fclose(f))
Thomas Gummerer2373b652018-08-05 18:20:30 +0100684 return error_errno(_("writing '%s' failed"), path);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200685
Bert Wesarg689b8c22010-02-23 21:11:53 +0100686out:
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200687 free(cur.ptr);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200688 free(result.ptr);
689
690 return ret;
691}
692
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200693static void update_paths(struct repository *r, struct string_list *update)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200694{
Martin Ågren0fa5a2e2018-05-09 22:55:39 +0200695 struct lock_file index_lock = LOCK_INIT;
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200696 int i;
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200697
Nguyễn Thái Ngọc Duy3a95f312019-01-12 09:13:24 +0700698 repo_hold_locked_index(r, &index_lock, LOCK_DIE_ON_ERROR);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200699
700 for (i = 0; i < update->nr; i++) {
Johannes Schindelinc455c872008-07-21 19:03:49 +0100701 struct string_list_item *item = &update->items[i];
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200702 if (add_file_to_index(r->index, item->string, 0))
Jonathan Nieder89ea9032014-12-02 20:20:49 -0800703 exit(128);
Thomas Gummerer2373b652018-08-05 18:20:30 +0100704 fprintf_ln(stderr, _("Staged '%s' using previous resolution."),
Junio C Hamanoa14c7ab2015-06-28 21:13:24 -0700705 item->string);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200706 }
707
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200708 if (write_locked_index(r->index, &index_lock,
Martin Ågren61000812018-03-01 21:40:20 +0100709 COMMIT_LOCK | SKIP_IF_UNCHANGED))
Thomas Gummerer2373b652018-08-05 18:20:30 +0100710 die(_("unable to write new index file"));
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200711}
712
Junio C Hamano629716d2015-07-30 15:49:18 -0700713static void remove_variant(struct rerere_id *id)
714{
715 unlink_or_warn(rerere_path(id, "postimage"));
716 unlink_or_warn(rerere_path(id, "preimage"));
717 id->collection->status[id->variant] = 0;
718}
719
Junio C Hamano8e7768b2015-06-30 19:36:24 -0700720/*
721 * The path indicated by rr_item may still have conflict for which we
722 * have a recorded resolution, in which case replay it and optionally
723 * update it. Or it may have been resolved by the user and we may
724 * only have the preimage for that conflict, in which case the result
725 * needs to be recorded as a resolution in a postimage file.
726 */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200727static void do_rerere_one_path(struct index_state *istate,
728 struct string_list_item *rr_item,
Junio C Hamano8e7768b2015-06-30 19:36:24 -0700729 struct string_list *update)
730{
731 const char *path = rr_item->string;
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700732 struct rerere_id *id = rr_item->util;
Junio C Hamano629716d2015-07-30 15:49:18 -0700733 struct rerere_dir *rr_dir = id->collection;
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700734 int variant;
Junio C Hamano8e7768b2015-06-30 19:36:24 -0700735
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700736 variant = id->variant;
Junio C Hamano8e7768b2015-06-30 19:36:24 -0700737
Junio C Hamano629716d2015-07-30 15:49:18 -0700738 /* Has the user resolved it already? */
739 if (variant >= 0) {
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200740 if (!handle_file(istate, path, NULL, NULL)) {
Junio C Hamano629716d2015-07-30 15:49:18 -0700741 copy_file(rerere_path(id, "postimage"), path, 0666);
742 id->collection->status[variant] |= RR_HAS_POSTIMAGE;
Thomas Gummerer2373b652018-08-05 18:20:30 +0100743 fprintf_ln(stderr, _("Recorded resolution for '%s'."), path);
Junio C Hamano629716d2015-07-30 15:49:18 -0700744 free_rerere_id(rr_item);
745 rr_item->util = NULL;
746 return;
Junio C Hamanoc0a54232015-07-20 15:19:44 -0700747 }
Junio C Hamano629716d2015-07-30 15:49:18 -0700748 /*
749 * There may be other variants that can cleanly
750 * replay. Try them and update the variant number for
751 * this one.
752 */
753 }
754
755 /* Does any existing resolution apply cleanly? */
756 for (variant = 0; variant < rr_dir->status_nr; variant++) {
757 const int both = RR_HAS_PREIMAGE | RR_HAS_POSTIMAGE;
758 struct rerere_id vid = *id;
759
760 if ((rr_dir->status[variant] & both) != both)
761 continue;
762
763 vid.variant = variant;
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200764 if (merge(istate, &vid, path))
Junio C Hamano629716d2015-07-30 15:49:18 -0700765 continue; /* failed to replay */
766
767 /*
768 * If there already is a different variant that applies
769 * cleanly, there is no point maintaining our own variant.
770 */
771 if (0 <= id->variant && id->variant != variant)
772 remove_variant(id);
Junio C Hamano8e7768b2015-06-30 19:36:24 -0700773
774 if (rerere_autoupdate)
775 string_list_insert(update, path);
776 else
Thomas Gummerer2373b652018-08-05 18:20:30 +0100777 fprintf_ln(stderr,
778 _("Resolved '%s' using previous resolution."),
779 path);
Junio C Hamano629716d2015-07-30 15:49:18 -0700780 free_rerere_id(rr_item);
781 rr_item->util = NULL;
Junio C Hamano925d73c2015-07-06 14:18:09 -0700782 return;
Junio C Hamano8e7768b2015-06-30 19:36:24 -0700783 }
Junio C Hamano629716d2015-07-30 15:49:18 -0700784
785 /* None of the existing one applies; we need a new variant */
786 assign_variant(id);
787
788 variant = id->variant;
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200789 handle_file(istate, path, NULL, rerere_path(id, "preimage"));
Junio C Hamano629716d2015-07-30 15:49:18 -0700790 if (id->collection->status[variant] & RR_HAS_POSTIMAGE) {
791 const char *path = rerere_path(id, "postimage");
792 if (unlink(path))
Thomas Gummerer2373b652018-08-05 18:20:30 +0100793 die_errno(_("cannot unlink stray '%s'"), path);
Junio C Hamano629716d2015-07-30 15:49:18 -0700794 id->collection->status[variant] &= ~RR_HAS_POSTIMAGE;
795 }
796 id->collection->status[variant] |= RR_HAS_PREIMAGE;
Thomas Gummerer2373b652018-08-05 18:20:30 +0100797 fprintf_ln(stderr, _("Recorded preimage for '%s'"), path);
Junio C Hamano8e7768b2015-06-30 19:36:24 -0700798}
799
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200800static int do_plain_rerere(struct repository *r,
801 struct string_list *rr, int fd)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200802{
Thiago Farina183113a2010-07-04 16:46:19 -0300803 struct string_list conflict = STRING_LIST_INIT_DUP;
804 struct string_list update = STRING_LIST_INIT_DUP;
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200805 int i;
806
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200807 find_conflict(r, &conflict);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200808
809 /*
Junio C Hamanocc899ec2015-06-30 22:40:35 -0700810 * MERGE_RR records paths with conflicts immediately after
811 * merge failed. Some of the conflicted paths might have been
812 * hand resolved in the working tree since then, but the
813 * initial run would catch all and register their preimages.
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200814 */
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200815 for (i = 0; i < conflict.nr; i++) {
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700816 struct rerere_id *id;
brian m. carlson0d7c4192018-10-15 00:02:02 +0000817 unsigned char hash[GIT_MAX_RAWSZ];
Johannes Schindelinc455c872008-07-21 19:03:49 +0100818 const char *path = conflict.items[i].string;
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200819 int ret;
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200820
Junio C Hamanoc7a25d32015-07-04 17:17:38 -0700821 /*
822 * Ask handle_file() to scan and assign a
823 * conflict ID. No need to write anything out
824 * yet.
825 */
Junio C Hamanod829d492018-10-30 15:43:42 +0900826 ret = handle_file(r->index, path, hash, NULL);
Thomas Gummererbd7dfa52018-08-05 18:20:37 +0100827 if (ret != 0 && string_list_has_string(rr, path)) {
Thomas Gummerer93406a22018-08-05 18:20:32 +0100828 remove_variant(string_list_lookup(rr, path)->util);
829 string_list_remove(rr, path, 1);
830 }
Junio C Hamanoc7a25d32015-07-04 17:17:38 -0700831 if (ret < 1)
832 continue;
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700833
brian m. carlson0d7c4192018-10-15 00:02:02 +0000834 id = new_rerere_id(hash);
Junio C Hamano18bb9932015-07-06 14:45:55 -0700835 string_list_insert(rr, path)->util = id;
Junio C Hamanocc899ec2015-06-30 22:40:35 -0700836
Junio C Hamanoc0a54232015-07-20 15:19:44 -0700837 /* Ensure that the directory exists. */
838 mkdir_in_gitdir(rerere_path(id, NULL));
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200839 }
840
Junio C Hamano8e7768b2015-06-30 19:36:24 -0700841 for (i = 0; i < rr->nr; i++)
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200842 do_rerere_one_path(r->index, &rr->items[i], &update);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200843
844 if (update.nr)
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200845 update_paths(r, &update);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200846
847 return write_rr(rr, fd);
848}
849
Tanay Abhra633e5ad2014-08-07 09:21:21 -0700850static void git_rerere_config(void)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200851{
Tanay Abhra633e5ad2014-08-07 09:21:21 -0700852 git_config_get_bool("rerere.enabled", &rerere_enabled);
853 git_config_get_bool("rerere.autoupdate", &rerere_autoupdate);
854 git_config(git_default_config, NULL);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200855}
856
Jeff Kingf9327292015-08-10 05:38:57 -0400857static GIT_PATH_FUNC(git_path_rr_cache, "rr-cache")
858
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200859static int is_rerere_enabled(void)
860{
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200861 int rr_cache_exists;
862
863 if (!rerere_enabled)
864 return 0;
865
Jeff Kingf9327292015-08-10 05:38:57 -0400866 rr_cache_exists = is_directory(git_path_rr_cache());
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200867 if (rerere_enabled < 0)
868 return rr_cache_exists;
869
Jeff Kingf9327292015-08-10 05:38:57 -0400870 if (!rr_cache_exists && mkdir_in_gitdir(git_path_rr_cache()))
Thomas Gummerer2373b652018-08-05 18:20:30 +0100871 die(_("could not create directory '%s'"), git_path_rr_cache());
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200872 return 1;
873}
874
Nguyễn Thái Ngọc Duy55e6b352018-11-10 06:49:09 +0100875int setup_rerere(struct repository *r, struct string_list *merge_rr, int flags)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200876{
877 int fd;
878
Tanay Abhra633e5ad2014-08-07 09:21:21 -0700879 git_rerere_config();
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200880 if (!is_rerere_enabled())
881 return -1;
882
Junio C Hamanocb6020b2009-12-04 00:20:48 -0800883 if (flags & (RERERE_AUTOUPDATE|RERERE_NOAUTOUPDATE))
884 rerere_autoupdate = !!(flags & RERERE_AUTOUPDATE);
Jeff King9dd330e2015-09-01 18:14:09 -0400885 if (flags & RERERE_READONLY)
886 fd = 0;
887 else
Stefan Beller102de882018-05-17 15:51:51 -0700888 fd = hold_lock_file_for_update(&write_lock,
Nguyễn Thái Ngọc Duy55e6b352018-11-10 06:49:09 +0100889 git_path_merge_rr(r),
Jeff King9dd330e2015-09-01 18:14:09 -0400890 LOCK_DIE_ON_ERROR);
Nguyễn Thái Ngọc Duy55e6b352018-11-10 06:49:09 +0100891 read_rr(r, merge_rr);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200892 return fd;
893}
894
Junio C Hamanocc899ec2015-06-30 22:40:35 -0700895/*
896 * The main entry point that is called internally from codepaths that
897 * perform mergy operations, possibly leaving conflicted index entries
898 * and working tree files.
899 */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200900int repo_rerere(struct repository *r, int flags)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200901{
Thiago Farina183113a2010-07-04 16:46:19 -0300902 struct string_list merge_rr = STRING_LIST_INIT_DUP;
Junio C Hamano1869bbe2015-07-16 14:50:05 -0700903 int fd, status;
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200904
Nguyễn Thái Ngọc Duy55e6b352018-11-10 06:49:09 +0100905 fd = setup_rerere(r, &merge_rr, flags);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200906 if (fd < 0)
907 return 0;
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200908 status = do_plain_rerere(r, &merge_rr, fd);
Junio C Hamano1869bbe2015-07-16 14:50:05 -0700909 free_rerere_dirs();
910 return status;
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200911}
Junio C Hamanodea45622009-12-25 15:51:32 -0800912
Junio C Hamano3d730ed2016-03-14 15:10:39 -0700913/*
914 * Subclass of rerere_io that reads from an in-core buffer that is a
915 * strbuf
916 */
917struct rerere_io_mem {
918 struct rerere_io io;
919 struct strbuf input;
920};
921
922/*
923 * ... and its getline() method implementation
924 */
925static int rerere_mem_getline(struct strbuf *sb, struct rerere_io *io_)
926{
927 struct rerere_io_mem *io = (struct rerere_io_mem *)io_;
928 char *ep;
929 size_t len;
930
931 strbuf_release(sb);
932 if (!io->input.len)
933 return -1;
934 ep = memchr(io->input.buf, '\n', io->input.len);
935 if (!ep)
936 ep = io->input.buf + io->input.len;
937 else if (*ep == '\n')
938 ep++;
939 len = ep - io->input.buf;
940 strbuf_add(sb, io->input.buf, len);
941 strbuf_remove(&io->input, 0, len);
942 return 0;
943}
944
Junio C Hamanod829d492018-10-30 15:43:42 +0900945static int handle_cache(struct index_state *istate,
946 const char *path, unsigned char *hash, const char *output)
Junio C Hamano3d730ed2016-03-14 15:10:39 -0700947{
948 mmfile_t mmfile[3] = {{NULL}};
949 mmbuffer_t result = {NULL, 0};
950 const struct cache_entry *ce;
Thomas Gummerer221444f2018-08-05 18:20:33 +0100951 int pos, len, i, has_conflicts;
Junio C Hamano3d730ed2016-03-14 15:10:39 -0700952 struct rerere_io_mem io;
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200953 int marker_size = ll_merge_marker_size(istate, path);
Junio C Hamano3d730ed2016-03-14 15:10:39 -0700954
955 /*
956 * Reproduce the conflicted merge in-core
957 */
958 len = strlen(path);
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200959 pos = index_name_pos(istate, path, len);
Junio C Hamano3d730ed2016-03-14 15:10:39 -0700960 if (0 <= pos)
961 return -1;
962 pos = -pos - 1;
963
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200964 while (pos < istate->cache_nr) {
Junio C Hamano3d730ed2016-03-14 15:10:39 -0700965 enum object_type type;
966 unsigned long size;
967
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200968 ce = istate->cache[pos++];
Junio C Hamano3d730ed2016-03-14 15:10:39 -0700969 if (ce_namelen(ce) != len || memcmp(ce->name, path, len))
970 break;
971 i = ce_stage(ce) - 1;
972 if (!mmfile[i].ptr) {
Ævar Arnfjörð Bjarmasonbc726bd2023-03-28 15:58:50 +0200973 mmfile[i].ptr = repo_read_object_file(the_repository,
974 &ce->oid, &type,
975 &size);
Junio C Hamano3d730ed2016-03-14 15:10:39 -0700976 mmfile[i].size = size;
977 }
978 }
979 for (i = 0; i < 3; i++)
980 if (!mmfile[i].ptr && !mmfile[i].size)
981 mmfile[i].ptr = xstrdup("");
982
983 /*
984 * NEEDSWORK: handle conflicts from merges with
985 * merge.renormalize set, too?
986 */
987 ll_merge(&result, path, &mmfile[0], NULL,
988 &mmfile[1], "ours",
Nguyễn Thái Ngọc Duy32eaa462018-09-21 17:57:27 +0200989 &mmfile[2], "theirs",
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200990 istate, NULL);
Junio C Hamano3d730ed2016-03-14 15:10:39 -0700991 for (i = 0; i < 3; i++)
992 free(mmfile[i].ptr);
993
994 memset(&io, 0, sizeof(io));
995 io.io.getline = rerere_mem_getline;
996 if (output)
997 io.io.output = fopen(output, "w");
998 else
999 io.io.output = NULL;
1000 strbuf_init(&io.input, 0);
1001 strbuf_attach(&io.input, result.ptr, result.size, result.size);
1002
1003 /*
1004 * Grab the conflict ID and optionally write the original
1005 * contents with conflict markers out.
1006 */
brian m. carlson0d7c4192018-10-15 00:02:02 +00001007 has_conflicts = handle_path(hash, (struct rerere_io *)&io, marker_size);
Junio C Hamano3d730ed2016-03-14 15:10:39 -07001008 strbuf_release(&io.input);
1009 if (io.io.output)
1010 fclose(io.io.output);
Thomas Gummerer221444f2018-08-05 18:20:33 +01001011 return has_conflicts;
Stephan Beyer5b2fd952008-07-09 14:58:57 +02001012}
Junio C Hamanodea45622009-12-25 15:51:32 -08001013
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +02001014static int rerere_forget_one_path(struct index_state *istate,
1015 const char *path,
1016 struct string_list *rr)
Junio C Hamanodea45622009-12-25 15:51:32 -08001017{
1018 const char *filename;
Junio C Hamano1d51ece2015-07-04 17:38:34 -07001019 struct rerere_id *id;
brian m. carlson0d7c4192018-10-15 00:02:02 +00001020 unsigned char hash[GIT_MAX_RAWSZ];
Junio C Hamanodea45622009-12-25 15:51:32 -08001021 int ret;
Junio C Hamano8d9b5a42015-06-30 13:03:36 -07001022 struct string_list_item *item;
Junio C Hamanodea45622009-12-25 15:51:32 -08001023
Junio C Hamano963ec002015-06-30 22:42:34 -07001024 /*
1025 * Recreate the original conflict from the stages in the
1026 * index and compute the conflict ID
1027 */
Junio C Hamanod829d492018-10-30 15:43:42 +09001028 ret = handle_cache(istate, path, hash, NULL);
Junio C Hamanodea45622009-12-25 15:51:32 -08001029 if (ret < 1)
Thomas Gummerer2373b652018-08-05 18:20:30 +01001030 return error(_("could not parse conflict hunks in '%s'"), path);
Junio C Hamano963ec002015-06-30 22:42:34 -07001031
1032 /* Nuke the recorded resolution for the conflict */
brian m. carlson0d7c4192018-10-15 00:02:02 +00001033 id = new_rerere_id(hash);
Junio C Hamano890fca82016-03-28 14:48:13 -07001034
1035 for (id->variant = 0;
1036 id->variant < id->collection->status_nr;
1037 id->variant++) {
1038 mmfile_t cur = { NULL, 0 };
1039 mmbuffer_t result = {NULL, 0};
1040 int cleanly_resolved;
1041
1042 if (!has_rerere_resolution(id))
1043 continue;
1044
Junio C Hamanod829d492018-10-30 15:43:42 +09001045 handle_cache(istate, path, hash, rerere_path(id, "thisimage"));
Junio C Hamano890fca82016-03-28 14:48:13 -07001046 if (read_mmfile(&cur, rerere_path(id, "thisimage"))) {
1047 free(cur.ptr);
Thomas Gummerer2373b652018-08-05 18:20:30 +01001048 error(_("failed to update conflicted state in '%s'"), path);
Junio C Hamano8f449612016-05-11 16:19:17 -07001049 goto fail_exit;
Junio C Hamano890fca82016-03-28 14:48:13 -07001050 }
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +02001051 cleanly_resolved = !try_merge(istate, id, path, &cur, &result);
Junio C Hamano890fca82016-03-28 14:48:13 -07001052 free(result.ptr);
1053 free(cur.ptr);
1054 if (cleanly_resolved)
1055 break;
1056 }
1057
Junio C Hamano8f449612016-05-11 16:19:17 -07001058 if (id->collection->status_nr <= id->variant) {
Thomas Gummerer2373b652018-08-05 18:20:30 +01001059 error(_("no remembered resolution for '%s'"), path);
Junio C Hamano8f449612016-05-11 16:19:17 -07001060 goto fail_exit;
1061 }
Junio C Hamano890fca82016-03-28 14:48:13 -07001062
Junio C Hamano18bb9932015-07-06 14:45:55 -07001063 filename = rerere_path(id, "postimage");
Junio C Hamano8f449612016-05-11 16:19:17 -07001064 if (unlink(filename)) {
1065 if (errno == ENOENT)
Thomas Gummerer2373b652018-08-05 18:20:30 +01001066 error(_("no remembered resolution for '%s'"), path);
Junio C Hamano8f449612016-05-11 16:19:17 -07001067 else
Thomas Gummerer2373b652018-08-05 18:20:30 +01001068 error_errno(_("cannot unlink '%s'"), filename);
Junio C Hamano8f449612016-05-11 16:19:17 -07001069 goto fail_exit;
Junio C Hamanod9d501b2016-05-19 12:51:22 -07001070 }
Junio C Hamanodea45622009-12-25 15:51:32 -08001071
Junio C Hamano963ec002015-06-30 22:42:34 -07001072 /*
1073 * Update the preimage so that the user can resolve the
1074 * conflict in the working tree, run us again to record
1075 * the postimage.
1076 */
Junio C Hamanod829d492018-10-30 15:43:42 +09001077 handle_cache(istate, path, hash, rerere_path(id, "preimage"));
Thomas Gummerer2373b652018-08-05 18:20:30 +01001078 fprintf_ln(stderr, _("Updated preimage for '%s'"), path);
Junio C Hamanodea45622009-12-25 15:51:32 -08001079
Junio C Hamano963ec002015-06-30 22:42:34 -07001080 /*
1081 * And remember that we can record resolution for this
1082 * conflict when the user is done.
1083 */
Junio C Hamano8d9b5a42015-06-30 13:03:36 -07001084 item = string_list_insert(rr, path);
Junio C Hamano1d51ece2015-07-04 17:38:34 -07001085 free_rerere_id(item);
Junio C Hamano18bb9932015-07-06 14:45:55 -07001086 item->util = id;
Thomas Gummerer2373b652018-08-05 18:20:30 +01001087 fprintf(stderr, _("Forgot resolution for '%s'\n"), path);
Junio C Hamanodea45622009-12-25 15:51:32 -08001088 return 0;
Junio C Hamano8f449612016-05-11 16:19:17 -07001089
1090fail_exit:
1091 free(id);
1092 return -1;
Junio C Hamanodea45622009-12-25 15:51:32 -08001093}
1094
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +02001095int rerere_forget(struct repository *r, struct pathspec *pathspec)
Junio C Hamanodea45622009-12-25 15:51:32 -08001096{
1097 int i, fd;
Thiago Farina183113a2010-07-04 16:46:19 -03001098 struct string_list conflict = STRING_LIST_INIT_DUP;
1099 struct string_list merge_rr = STRING_LIST_INIT_DUP;
Junio C Hamanodea45622009-12-25 15:51:32 -08001100
Nguyễn Thái Ngọc Duye1ff0a32019-01-12 09:13:26 +07001101 if (repo_read_index(r) < 0)
Thomas Gummerer2373b652018-08-05 18:20:30 +01001102 return error(_("index file corrupt"));
Junio C Hamanodea45622009-12-25 15:51:32 -08001103
Nguyễn Thái Ngọc Duy55e6b352018-11-10 06:49:09 +01001104 fd = setup_rerere(r, &merge_rr, RERERE_NOAUTOUPDATE);
Jeff King05445742015-05-14 15:20:52 -04001105 if (fd < 0)
1106 return 0;
Junio C Hamanodea45622009-12-25 15:51:32 -08001107
Junio C Hamano963ec002015-06-30 22:42:34 -07001108 /*
1109 * The paths may have been resolved (incorrectly);
1110 * recover the original conflicted state and then
1111 * find the conflicted paths.
1112 */
Junio C Hamano5bdedac2023-07-31 15:44:09 -07001113 unmerge_index(r->index, pathspec, 0);
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +02001114 find_conflict(r, &conflict);
Junio C Hamanodea45622009-12-25 15:51:32 -08001115 for (i = 0; i < conflict.nr; i++) {
1116 struct string_list_item *it = &conflict.items[i];
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +02001117 if (!match_pathspec(r->index, pathspec, it->string,
Nguyễn Thái Ngọc Duyae8d0822014-01-24 20:40:33 +07001118 strlen(it->string), 0, NULL, 0))
Junio C Hamanodea45622009-12-25 15:51:32 -08001119 continue;
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +02001120 rerere_forget_one_path(r->index, it->string, &merge_rr);
Junio C Hamanodea45622009-12-25 15:51:32 -08001121 }
1122 return write_rr(&merge_rr, fd);
1123}
Junio C Hamano0f891e72011-05-08 12:55:34 -07001124
Junio C Hamanoe828de82015-06-30 22:43:37 -07001125/*
1126 * Garbage collection support
1127 */
Junio C Hamano1d51ece2015-07-04 17:38:34 -07001128
Junio C Hamano5ea82272017-08-19 11:16:01 -07001129static timestamp_t rerere_created_at(struct rerere_id *id)
Junio C Hamano0f891e72011-05-08 12:55:34 -07001130{
1131 struct stat st;
Junio C Hamano1d51ece2015-07-04 17:38:34 -07001132
Junio C Hamano18bb9932015-07-06 14:45:55 -07001133 return stat(rerere_path(id, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
Junio C Hamano0f891e72011-05-08 12:55:34 -07001134}
1135
Junio C Hamano5ea82272017-08-19 11:16:01 -07001136static timestamp_t rerere_last_used_at(struct rerere_id *id)
Junio C Hamano0f891e72011-05-08 12:55:34 -07001137{
Junio C Hamano0f891e72011-05-08 12:55:34 -07001138 struct stat st;
Junio C Hamano1d51ece2015-07-04 17:38:34 -07001139
Junio C Hamano18bb9932015-07-06 14:45:55 -07001140 return stat(rerere_path(id, "postimage"), &st) ? (time_t) 0 : st.st_mtime;
Junio C Hamano0f891e72011-05-08 12:55:34 -07001141}
1142
Junio C Hamanoe828de82015-06-30 22:43:37 -07001143/*
1144 * Remove the recorded resolution for a given conflict ID
1145 */
Junio C Hamano1d51ece2015-07-04 17:38:34 -07001146static void unlink_rr_item(struct rerere_id *id)
Junio C Hamano0f891e72011-05-08 12:55:34 -07001147{
Junio C Hamano1be1e852016-03-08 12:11:00 -08001148 unlink_or_warn(rerere_path(id, "thisimage"));
1149 remove_variant(id);
1150 id->collection->status[id->variant] = 0;
1151}
1152
Junio C Hamano5ea82272017-08-19 11:16:01 -07001153static void prune_one(struct rerere_id *id,
1154 timestamp_t cutoff_resolve, timestamp_t cutoff_noresolve)
Junio C Hamano1be1e852016-03-08 12:11:00 -08001155{
Junio C Hamano5ea82272017-08-19 11:16:01 -07001156 timestamp_t then;
1157 timestamp_t cutoff;
Junio C Hamano1be1e852016-03-08 12:11:00 -08001158
1159 then = rerere_last_used_at(id);
1160 if (then)
1161 cutoff = cutoff_resolve;
1162 else {
1163 then = rerere_created_at(id);
1164 if (!then)
1165 return;
1166 cutoff = cutoff_noresolve;
1167 }
Junio C Hamano5ea82272017-08-19 11:16:01 -07001168 if (then < cutoff)
Junio C Hamano1be1e852016-03-08 12:11:00 -08001169 unlink_rr_item(id);
Junio C Hamano0f891e72011-05-08 12:55:34 -07001170}
1171
Jeff King2bc1a872021-01-28 01:14:11 -05001172/* Does the basename in "path" look plausibly like an rr-cache entry? */
1173static int is_rr_cache_dirname(const char *path)
1174{
Jeff King098c1732021-01-28 01:16:50 -05001175 struct object_id oid;
1176 const char *end;
1177 return !parse_oid_hex(path, &oid, &end) && !*end;
Jeff King2bc1a872021-01-28 01:14:11 -05001178}
1179
Nguyễn Thái Ngọc Duy55e6b352018-11-10 06:49:09 +01001180void rerere_gc(struct repository *r, struct string_list *rr)
Junio C Hamano0f891e72011-05-08 12:55:34 -07001181{
1182 struct string_list to_remove = STRING_LIST_INIT_DUP;
1183 DIR *dir;
1184 struct dirent *e;
Junio C Hamano1be1e852016-03-08 12:11:00 -08001185 int i;
Junio C Hamano5ea82272017-08-19 11:16:01 -07001186 timestamp_t now = time(NULL);
1187 timestamp_t cutoff_noresolve = now - 15 * 86400;
1188 timestamp_t cutoff_resolve = now - 60 * 86400;
Junio C Hamano0f891e72011-05-08 12:55:34 -07001189
Nguyễn Thái Ngọc Duy55e6b352018-11-10 06:49:09 +01001190 if (setup_rerere(r, rr, 0) < 0)
Jeff King9dd330e2015-09-01 18:14:09 -04001191 return;
1192
Junio C Hamano6e96cb52017-08-19 11:43:39 -07001193 git_config_get_expiry_in_days("gc.rerereresolved", &cutoff_resolve, now);
1194 git_config_get_expiry_in_days("gc.rerereunresolved", &cutoff_noresolve, now);
Tanay Abhra633e5ad2014-08-07 09:21:21 -07001195 git_config(git_default_config, NULL);
Junio C Hamano0f891e72011-05-08 12:55:34 -07001196 dir = opendir(git_path("rr-cache"));
1197 if (!dir)
Thomas Gummerer2373b652018-08-05 18:20:30 +01001198 die_errno(_("unable to open rr-cache directory"));
Junio C Hamanoe828de82015-06-30 22:43:37 -07001199 /* Collect stale conflict IDs ... */
Elijah Newrenb548f0f2021-05-12 17:28:22 +00001200 while ((e = readdir_skip_dot_and_dotdot(dir))) {
Junio C Hamano1be1e852016-03-08 12:11:00 -08001201 struct rerere_dir *rr_dir;
1202 struct rerere_id id;
1203 int now_empty;
1204
Jeff King2bc1a872021-01-28 01:14:11 -05001205 if (!is_rr_cache_dirname(e->d_name))
Junio C Hamano1be1e852016-03-08 12:11:00 -08001206 continue; /* or should we remove e->d_name? */
Junio C Hamano0f891e72011-05-08 12:55:34 -07001207
Jeff King2bc1a872021-01-28 01:14:11 -05001208 rr_dir = find_rerere_dir(e->d_name);
1209
Junio C Hamano1be1e852016-03-08 12:11:00 -08001210 now_empty = 1;
1211 for (id.variant = 0, id.collection = rr_dir;
1212 id.variant < id.collection->status_nr;
1213 id.variant++) {
Junio C Hamano5ea82272017-08-19 11:16:01 -07001214 prune_one(&id, cutoff_resolve, cutoff_noresolve);
Junio C Hamano1be1e852016-03-08 12:11:00 -08001215 if (id.collection->status[id.variant])
1216 now_empty = 0;
Junio C Hamano0f891e72011-05-08 12:55:34 -07001217 }
Junio C Hamano1be1e852016-03-08 12:11:00 -08001218 if (now_empty)
Junio C Hamano0f891e72011-05-08 12:55:34 -07001219 string_list_append(&to_remove, e->d_name);
1220 }
Jim Meyeringa9930e32011-05-26 15:55:50 +02001221 closedir(dir);
Junio C Hamano1be1e852016-03-08 12:11:00 -08001222
1223 /* ... and then remove the empty directories */
Junio C Hamano0f891e72011-05-08 12:55:34 -07001224 for (i = 0; i < to_remove.nr; i++)
Junio C Hamano1be1e852016-03-08 12:11:00 -08001225 rmdir(git_path("rr-cache/%s", to_remove.items[i].string));
Junio C Hamano0f891e72011-05-08 12:55:34 -07001226 string_list_clear(&to_remove, 0);
Jeff King9dd330e2015-09-01 18:14:09 -04001227 rollback_lock_file(&write_lock);
Junio C Hamano0f891e72011-05-08 12:55:34 -07001228}
1229
Junio C Hamanoe828de82015-06-30 22:43:37 -07001230/*
1231 * During a conflict resolution, after "rerere" recorded the
1232 * preimages, abandon them if the user did not resolve them or
1233 * record their resolutions. And drop $GIT_DIR/MERGE_RR.
1234 *
1235 * NEEDSWORK: shouldn't we be calling this from "reset --hard"?
1236 */
Nguyễn Thái Ngọc Duy55e6b352018-11-10 06:49:09 +01001237void rerere_clear(struct repository *r, struct string_list *merge_rr)
Junio C Hamano0f891e72011-05-08 12:55:34 -07001238{
1239 int i;
1240
Nguyễn Thái Ngọc Duy55e6b352018-11-10 06:49:09 +01001241 if (setup_rerere(r, merge_rr, 0) < 0)
Jeff King9dd330e2015-09-01 18:14:09 -04001242 return;
1243
Junio C Hamano0f891e72011-05-08 12:55:34 -07001244 for (i = 0; i < merge_rr->nr; i++) {
Junio C Hamano1d51ece2015-07-04 17:38:34 -07001245 struct rerere_id *id = merge_rr->items[i].util;
Junio C Hamano1be1e852016-03-08 12:11:00 -08001246 if (!has_rerere_resolution(id)) {
Junio C Hamano18bb9932015-07-06 14:45:55 -07001247 unlink_rr_item(id);
Junio C Hamano1be1e852016-03-08 12:11:00 -08001248 rmdir(rerere_path(id, NULL));
1249 }
Junio C Hamano0f891e72011-05-08 12:55:34 -07001250 }
Nguyễn Thái Ngọc Duy55e6b352018-11-10 06:49:09 +01001251 unlink_or_warn(git_path_merge_rr(r));
Jeff King9dd330e2015-09-01 18:14:09 -04001252 rollback_lock_file(&write_lock);
Junio C Hamano0f891e72011-05-08 12:55:34 -07001253}