blob: b5b2357411f145867eb1918cbc79f77c6192db1a [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"
Junio C Hamano1869bbe2015-07-16 14:50:05 -070013#include "sha1-lookup.h"
Stephan Beyer5b2fd952008-07-09 14:58:57 +020014
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -050015#define RESOLVED 0
16#define PUNTED 1
17#define THREE_STAGED 2
18void *RERERE_RESOLVED = &RERERE_RESOLVED;
19
Stephan Beyer5b2fd952008-07-09 14:58:57 +020020/* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
21static int rerere_enabled = -1;
22
23/* automatically update cleanly resolved paths to the index */
24static int rerere_autoupdate;
25
Junio C Hamano1869bbe2015-07-16 14:50:05 -070026static int rerere_dir_nr;
27static int rerere_dir_alloc;
28
Junio C Hamano2c7929b2015-07-16 15:47:13 -070029#define RR_HAS_POSTIMAGE 1
30#define RR_HAS_PREIMAGE 2
Junio C Hamano1869bbe2015-07-16 14:50:05 -070031static struct rerere_dir {
brian m. carlson0d7c4192018-10-15 00:02:02 +000032 unsigned char hash[GIT_MAX_HEXSZ];
Junio C Hamanoa13d1372015-07-23 14:23:24 -070033 int status_alloc, status_nr;
34 unsigned char *status;
Junio C Hamano1869bbe2015-07-16 14:50:05 -070035} **rerere_dir;
36
37static void free_rerere_dirs(void)
38{
39 int i;
Junio C Hamanoa13d1372015-07-23 14:23:24 -070040 for (i = 0; i < rerere_dir_nr; i++) {
41 free(rerere_dir[i]->status);
Junio C Hamano1869bbe2015-07-16 14:50:05 -070042 free(rerere_dir[i]);
Junio C Hamanoa13d1372015-07-23 14:23:24 -070043 }
Ævar Arnfjörð Bjarmason88ce3ef2017-06-15 23:15:49 +000044 FREE_AND_NULL(rerere_dir);
Junio C Hamano1869bbe2015-07-16 14:50:05 -070045 rerere_dir_nr = rerere_dir_alloc = 0;
Junio C Hamano1869bbe2015-07-16 14:50:05 -070046}
47
Junio C Hamano1d51ece2015-07-04 17:38:34 -070048static void free_rerere_id(struct string_list_item *item)
Stephan Beyer5b2fd952008-07-09 14:58:57 +020049{
Junio C Hamano1d51ece2015-07-04 17:38:34 -070050 free(item->util);
Stephan Beyer5b2fd952008-07-09 14:58:57 +020051}
52
Junio C Hamano1d51ece2015-07-04 17:38:34 -070053static const char *rerere_id_hex(const struct rerere_id *id)
54{
brian m. carlson0d7c4192018-10-15 00:02:02 +000055 return sha1_to_hex(id->collection->hash);
Junio C Hamano1d51ece2015-07-04 17:38:34 -070056}
57
Junio C Hamanoa13d1372015-07-23 14:23:24 -070058static void fit_variant(struct rerere_dir *rr_dir, int variant)
59{
60 variant++;
61 ALLOC_GROW(rr_dir->status, variant, rr_dir->status_alloc);
62 if (rr_dir->status_nr < variant) {
63 memset(rr_dir->status + rr_dir->status_nr,
64 '\0', variant - rr_dir->status_nr);
65 rr_dir->status_nr = variant;
66 }
67}
68
69static void assign_variant(struct rerere_id *id)
70{
71 int variant;
72 struct rerere_dir *rr_dir = id->collection;
73
74 variant = id->variant;
75 if (variant < 0) {
Junio C Hamano629716d2015-07-30 15:49:18 -070076 for (variant = 0; variant < rr_dir->status_nr; variant++)
77 if (!rr_dir->status[variant])
78 break;
Junio C Hamanoa13d1372015-07-23 14:23:24 -070079 }
80 fit_variant(rr_dir, variant);
81 id->variant = variant;
Junio C Hamano1d51ece2015-07-04 17:38:34 -070082}
83
84const char *rerere_path(const struct rerere_id *id, const char *file)
85{
86 if (!file)
87 return git_path("rr-cache/%s", rerere_id_hex(id));
88
Junio C Hamanoa13d1372015-07-23 14:23:24 -070089 if (id->variant <= 0)
90 return git_path("rr-cache/%s/%s", rerere_id_hex(id), file);
91
92 return git_path("rr-cache/%s/%s.%d",
93 rerere_id_hex(id), file, id->variant);
Junio C Hamano1d51ece2015-07-04 17:38:34 -070094}
95
Junio C Hamanoa13d1372015-07-23 14:23:24 -070096static int is_rr_file(const char *name, const char *filename, int *variant)
Junio C Hamano2c7929b2015-07-16 15:47:13 -070097{
Junio C Hamanoa13d1372015-07-23 14:23:24 -070098 const char *suffix;
99 char *ep;
100
101 if (!strcmp(name, filename)) {
102 *variant = 0;
103 return 1;
104 }
105 if (!skip_prefix(name, filename, &suffix) || *suffix != '.')
106 return 0;
107
108 errno = 0;
109 *variant = strtol(suffix + 1, &ep, 10);
110 if (errno || *ep)
111 return 0;
112 return 1;
Junio C Hamano2c7929b2015-07-16 15:47:13 -0700113}
114
115static void scan_rerere_dir(struct rerere_dir *rr_dir)
116{
117 struct dirent *de;
brian m. carlson0d7c4192018-10-15 00:02:02 +0000118 DIR *dir = opendir(git_path("rr-cache/%s", sha1_to_hex(rr_dir->hash)));
Junio C Hamano2c7929b2015-07-16 15:47:13 -0700119
120 if (!dir)
121 return;
122 while ((de = readdir(dir)) != NULL) {
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700123 int variant;
124
125 if (is_rr_file(de->d_name, "postimage", &variant)) {
126 fit_variant(rr_dir, variant);
127 rr_dir->status[variant] |= RR_HAS_POSTIMAGE;
128 } else if (is_rr_file(de->d_name, "preimage", &variant)) {
129 fit_variant(rr_dir, variant);
130 rr_dir->status[variant] |= RR_HAS_PREIMAGE;
131 }
Junio C Hamano2c7929b2015-07-16 15:47:13 -0700132 }
133 closedir(dir);
134}
135
brian m. carlson0d7c4192018-10-15 00:02:02 +0000136static const unsigned char *rerere_dir_hash(size_t i, void *table)
Junio C Hamano1869bbe2015-07-16 14:50:05 -0700137{
138 struct rerere_dir **rr_dir = table;
brian m. carlson0d7c4192018-10-15 00:02:02 +0000139 return rr_dir[i]->hash;
Junio C Hamano1869bbe2015-07-16 14:50:05 -0700140}
141
142static struct rerere_dir *find_rerere_dir(const char *hex)
143{
brian m. carlson0d7c4192018-10-15 00:02:02 +0000144 unsigned char hash[GIT_MAX_RAWSZ];
Junio C Hamano1869bbe2015-07-16 14:50:05 -0700145 struct rerere_dir *rr_dir;
146 int pos;
147
brian m. carlson0d7c4192018-10-15 00:02:02 +0000148 if (get_sha1_hex(hex, hash))
Junio C Hamano1869bbe2015-07-16 14:50:05 -0700149 return NULL; /* BUG */
brian m. carlson0d7c4192018-10-15 00:02:02 +0000150 pos = sha1_pos(hash, rerere_dir, rerere_dir_nr, rerere_dir_hash);
Junio C Hamano1869bbe2015-07-16 14:50:05 -0700151 if (pos < 0) {
152 rr_dir = xmalloc(sizeof(*rr_dir));
brian m. carlson0d7c4192018-10-15 00:02:02 +0000153 hashcpy(rr_dir->hash, hash);
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700154 rr_dir->status = NULL;
155 rr_dir->status_nr = 0;
156 rr_dir->status_alloc = 0;
Junio C Hamano1869bbe2015-07-16 14:50:05 -0700157 pos = -1 - pos;
158
159 /* Make sure the array is big enough ... */
160 ALLOC_GROW(rerere_dir, rerere_dir_nr + 1, rerere_dir_alloc);
161 /* ... and add it in. */
162 rerere_dir_nr++;
SZEDER Gáborf919ffe2018-01-22 18:50:09 +0100163 MOVE_ARRAY(rerere_dir + pos + 1, rerere_dir + pos,
164 rerere_dir_nr - pos - 1);
Junio C Hamano1869bbe2015-07-16 14:50:05 -0700165 rerere_dir[pos] = rr_dir;
Junio C Hamano2c7929b2015-07-16 15:47:13 -0700166 scan_rerere_dir(rr_dir);
Junio C Hamano1869bbe2015-07-16 14:50:05 -0700167 }
168 return rerere_dir[pos];
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700169}
170
171static int has_rerere_resolution(const struct rerere_id *id)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200172{
Junio C Hamano05dd9f12015-07-17 13:28:31 -0700173 const int both = RR_HAS_POSTIMAGE|RR_HAS_PREIMAGE;
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700174 int variant = id->variant;
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700175
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700176 if (variant < 0)
177 return 0;
178 return ((id->collection->status[variant] & both) == both);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200179}
180
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700181static struct rerere_id *new_rerere_id_hex(char *hex)
182{
183 struct rerere_id *id = xmalloc(sizeof(*id));
Junio C Hamano1869bbe2015-07-16 14:50:05 -0700184 id->collection = find_rerere_dir(hex);
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700185 id->variant = -1; /* not known yet */
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700186 return id;
187}
188
189static struct rerere_id *new_rerere_id(unsigned char *sha1)
190{
191 return new_rerere_id_hex(sha1_to_hex(sha1));
192}
193
Junio C Hamano4b68c2a2015-06-30 22:36:35 -0700194/*
195 * $GIT_DIR/MERGE_RR file is a collection of records, each of which is
196 * "conflict ID", a HT and pathname, terminated with a NUL, and is
197 * used to keep track of the set of paths that "rerere" may need to
198 * work on (i.e. what is left by the previous invocation of "git
199 * rerere" during the current conflict resolution session).
200 */
Johannes Schindelinc455c872008-07-21 19:03:49 +0100201static void read_rr(struct string_list *rr)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200202{
Junio C Hamanof5800f62015-06-28 15:51:59 -0700203 struct strbuf buf = STRBUF_INIT;
Stefan Beller102de882018-05-17 15:51:51 -0700204 FILE *in = fopen_or_warn(git_path_merge_rr(the_repository), "r");
Junio C Hamanof5800f62015-06-28 15:51:59 -0700205
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200206 if (!in)
207 return;
Junio C Hamanof5800f62015-06-28 15:51:59 -0700208 while (!strbuf_getwholeline(&buf, in, '\0')) {
209 char *path;
brian m. carlson0d7c4192018-10-15 00:02:02 +0000210 unsigned char hash[GIT_MAX_RAWSZ];
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700211 struct rerere_id *id;
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700212 int variant;
brian m. carlson0d7c4192018-10-15 00:02:02 +0000213 const unsigned hexsz = the_hash_algo->hexsz;
Junio C Hamanof5800f62015-06-28 15:51:59 -0700214
215 /* There has to be the hash, tab, path and then NUL */
brian m. carlson0d7c4192018-10-15 00:02:02 +0000216 if (buf.len < hexsz + 2 || get_sha1_hex(buf.buf, hash))
Thomas Gummerer2373b652018-08-05 18:20:30 +0100217 die(_("corrupt MERGE_RR"));
Junio C Hamanof5800f62015-06-28 15:51:59 -0700218
brian m. carlson0d7c4192018-10-15 00:02:02 +0000219 if (buf.buf[hexsz] != '.') {
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700220 variant = 0;
brian m. carlson0d7c4192018-10-15 00:02:02 +0000221 path = buf.buf + hexsz;
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700222 } else {
223 errno = 0;
brian m. carlson0d7c4192018-10-15 00:02:02 +0000224 variant = strtol(buf.buf + hexsz + 1, &path, 10);
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700225 if (errno)
Thomas Gummerer2373b652018-08-05 18:20:30 +0100226 die(_("corrupt MERGE_RR"));
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700227 }
228 if (*(path++) != '\t')
Thomas Gummerer2373b652018-08-05 18:20:30 +0100229 die(_("corrupt MERGE_RR"));
brian m. carlson0d7c4192018-10-15 00:02:02 +0000230 buf.buf[hexsz] = '\0';
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700231 id = new_rerere_id_hex(buf.buf);
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700232 id->variant = variant;
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700233 string_list_insert(rr, path)->util = id;
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200234 }
Junio C Hamanof5800f62015-06-28 15:51:59 -0700235 strbuf_release(&buf);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200236 fclose(in);
237}
238
239static struct lock_file write_lock;
240
Johannes Schindelinc455c872008-07-21 19:03:49 +0100241static int write_rr(struct string_list *rr, int out_fd)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200242{
243 int i;
244 for (i = 0; i < rr->nr; i++) {
Junio C Hamanoe2cb6a92015-06-28 16:28:00 -0700245 struct strbuf buf = STRBUF_INIT;
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700246 struct rerere_id *id;
Junio C Hamanoe2cb6a92015-06-28 16:28:00 -0700247
248 assert(rr->items[i].util != RERERE_RESOLVED);
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700249
250 id = rr->items[i].util;
251 if (!id)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200252 continue;
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700253 assert(id->variant >= 0);
254 if (0 < id->variant)
255 strbuf_addf(&buf, "%s.%d\t%s%c",
256 rerere_id_hex(id), id->variant,
257 rr->items[i].string, 0);
258 else
259 strbuf_addf(&buf, "%s\t%s%c",
260 rerere_id_hex(id),
261 rr->items[i].string, 0);
262
Jeff King06f46f22017-09-13 13:16:03 -0400263 if (write_in_full(out_fd, buf.buf, buf.len) < 0)
Thomas Gummerer2373b652018-08-05 18:20:30 +0100264 die(_("unable to write rerere record"));
Junio C Hamanoe2cb6a92015-06-28 16:28:00 -0700265
266 strbuf_release(&buf);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200267 }
268 if (commit_lock_file(&write_lock) != 0)
Thomas Gummerer2373b652018-08-05 18:20:30 +0100269 die(_("unable to write rerere record"));
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200270 return 0;
271}
272
Junio C Hamanoa96847c2015-06-30 22:33:19 -0700273/*
274 * "rerere" interacts with conflicted file contents using this I/O
275 * abstraction. It reads a conflicted contents from one place via
276 * "getline()" method, and optionally can write it out after
277 * normalizing the conflicted hunks to the "output". Subclasses of
278 * rerere_io embed this structure at the beginning of their own
279 * rerere_io object.
280 */
281struct rerere_io {
282 int (*getline)(struct strbuf *, struct rerere_io *);
283 FILE *output;
284 int wrerror;
285 /* some more stuff */
286};
287
Alex Riesen47d32af2008-12-05 01:35:48 +0100288static void ferr_write(const void *p, size_t count, FILE *fp, int *err)
289{
290 if (!count || *err)
291 return;
292 if (fwrite(p, count, 1, fp) != 1)
293 *err = errno;
294}
295
296static inline void ferr_puts(const char *s, FILE *fp, int *err)
297{
298 ferr_write(s, strlen(s), fp, err);
299}
300
Junio C Hamano27d6b082009-12-25 14:34:53 -0800301static void rerere_io_putstr(const char *str, struct rerere_io *io)
302{
303 if (io->output)
304 ferr_puts(str, io->output, &io->wrerror);
305}
306
307static void rerere_io_putmem(const char *mem, size_t sz, struct rerere_io *io)
308{
309 if (io->output)
310 ferr_write(mem, sz, io->output, &io->wrerror);
311}
312
Junio C Hamanoa96847c2015-06-30 22:33:19 -0700313/*
314 * Subclass of rerere_io that reads from an on-disk file
315 */
Junio C Hamano27d6b082009-12-25 14:34:53 -0800316struct rerere_io_file {
317 struct rerere_io io;
318 FILE *input;
319};
320
Junio C Hamanoa96847c2015-06-30 22:33:19 -0700321/*
322 * ... and its getline() method implementation
323 */
Junio C Hamano27d6b082009-12-25 14:34:53 -0800324static int rerere_file_getline(struct strbuf *sb, struct rerere_io *io_)
325{
326 struct rerere_io_file *io = (struct rerere_io_file *)io_;
327 return strbuf_getwholeline(sb, io->input, '\n');
328}
329
Junio C Hamano67711cd2015-06-29 15:05:24 -0700330/*
331 * Require the exact number of conflict marker letters, no more, no
332 * less, followed by SP or any whitespace
333 * (including LF).
334 */
335static int is_cmarker(char *buf, int marker_char, int marker_size)
Junio C Hamano191f24172010-01-16 23:06:45 -0800336{
Junio C Hamano67711cd2015-06-29 15:05:24 -0700337 int want_sp;
338
339 /*
340 * The beginning of our version and the end of their version
341 * always are labeled like "<<<<< ours" or ">>>>> theirs",
342 * hence we set want_sp for them. Note that the version from
343 * the common ancestor in diff3-style output is not always
344 * labelled (e.g. "||||| common" is often seen but "|||||"
345 * alone is also valid), so we do not set want_sp.
346 */
347 want_sp = (marker_char == '<') || (marker_char == '>');
348
Junio C Hamano191f24172010-01-16 23:06:45 -0800349 while (marker_size--)
350 if (*buf++ != marker_char)
351 return 0;
352 if (want_sp && *buf != ' ')
353 return 0;
354 return isspace(*buf);
355}
356
Thomas Gummerer5ebbdad2018-08-05 18:20:35 +0100357static void rerere_strbuf_putconflict(struct strbuf *buf, int ch, size_t size)
358{
359 strbuf_addchars(buf, ch, size);
360 strbuf_addch(buf, '\n');
361}
362
363static int handle_conflict(struct strbuf *out, struct rerere_io *io,
brian m. carlson0d7c4192018-10-15 00:02:02 +0000364 int marker_size, git_hash_ctx *ctx)
Thomas Gummererc0f16f82018-08-05 18:20:34 +0100365{
366 enum {
367 RR_SIDE_1 = 0, RR_SIDE_2, RR_ORIGINAL
368 } hunk = RR_SIDE_1;
369 struct strbuf one = STRBUF_INIT, two = STRBUF_INIT;
Thomas Gummerer4af32202018-08-05 18:20:36 +0100370 struct strbuf buf = STRBUF_INIT, conflict = STRBUF_INIT;
Thomas Gummererc0f16f82018-08-05 18:20:34 +0100371 int has_conflicts = -1;
372
373 while (!io->getline(&buf, io)) {
374 if (is_cmarker(buf.buf, '<', marker_size)) {
Thomas Gummerer4af32202018-08-05 18:20:36 +0100375 if (handle_conflict(&conflict, io, marker_size, NULL) < 0)
376 break;
377 if (hunk == RR_SIDE_1)
378 strbuf_addbuf(&one, &conflict);
379 else
380 strbuf_addbuf(&two, &conflict);
381 strbuf_release(&conflict);
Thomas Gummererc0f16f82018-08-05 18:20:34 +0100382 } else if (is_cmarker(buf.buf, '|', marker_size)) {
383 if (hunk != RR_SIDE_1)
384 break;
385 hunk = RR_ORIGINAL;
386 } else if (is_cmarker(buf.buf, '=', marker_size)) {
387 if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL)
388 break;
389 hunk = RR_SIDE_2;
390 } else if (is_cmarker(buf.buf, '>', marker_size)) {
391 if (hunk != RR_SIDE_2)
392 break;
393 if (strbuf_cmp(&one, &two) > 0)
394 strbuf_swap(&one, &two);
395 has_conflicts = 1;
Thomas Gummerer5ebbdad2018-08-05 18:20:35 +0100396 rerere_strbuf_putconflict(out, '<', marker_size);
397 strbuf_addbuf(out, &one);
398 rerere_strbuf_putconflict(out, '=', marker_size);
399 strbuf_addbuf(out, &two);
400 rerere_strbuf_putconflict(out, '>', marker_size);
Thomas Gummererc0f16f82018-08-05 18:20:34 +0100401 if (ctx) {
brian m. carlson0d7c4192018-10-15 00:02:02 +0000402 the_hash_algo->update_fn(ctx, one.buf ?
403 one.buf : "",
404 one.len + 1);
405 the_hash_algo->update_fn(ctx, two.buf ?
406 two.buf : "",
407 two.len + 1);
Thomas Gummererc0f16f82018-08-05 18:20:34 +0100408 }
409 break;
410 } else if (hunk == RR_SIDE_1)
411 strbuf_addbuf(&one, &buf);
412 else if (hunk == RR_ORIGINAL)
413 ; /* discard */
414 else if (hunk == RR_SIDE_2)
415 strbuf_addbuf(&two, &buf);
416 }
417 strbuf_release(&one);
418 strbuf_release(&two);
419 strbuf_release(&buf);
420
421 return has_conflicts;
422}
423
Junio C Hamanocc899ec2015-06-30 22:40:35 -0700424/*
425 * Read contents a file with conflicts, normalize the conflicts
426 * by (1) discarding the common ancestor version in diff3-style,
427 * (2) reordering our side and their side so that whichever sorts
428 * alphabetically earlier comes before the other one, while
429 * computing the "conflict ID", which is just an SHA-1 hash of
430 * one side of the conflict, NUL, the other side of the conflict,
431 * and NUL concatenated together.
432 *
Thomas Gummerer221444f2018-08-05 18:20:33 +0100433 * Return 1 if conflict hunks are found, 0 if there are no conflict
434 * hunks and -1 if an error occured.
Junio C Hamanocc899ec2015-06-30 22:40:35 -0700435 */
brian m. carlson0d7c4192018-10-15 00:02:02 +0000436static int handle_path(unsigned char *hash, struct rerere_io *io, int marker_size)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200437{
brian m. carlson0d7c4192018-10-15 00:02:02 +0000438 git_hash_ctx ctx;
Thomas Gummerer5ebbdad2018-08-05 18:20:35 +0100439 struct strbuf buf = STRBUF_INIT, out = STRBUF_INIT;
Thomas Gummererc0f16f82018-08-05 18:20:34 +0100440 int has_conflicts = 0;
brian m. carlson0d7c4192018-10-15 00:02:02 +0000441 if (hash)
442 the_hash_algo->init_fn(&ctx);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200443
Junio C Hamano27d6b082009-12-25 14:34:53 -0800444 while (!io->getline(&buf, io)) {
Junio C Hamano67711cd2015-06-29 15:05:24 -0700445 if (is_cmarker(buf.buf, '<', marker_size)) {
Thomas Gummerer5ebbdad2018-08-05 18:20:35 +0100446 has_conflicts = handle_conflict(&out, io, marker_size,
brian m. carlson0d7c4192018-10-15 00:02:02 +0000447 hash ? &ctx : NULL);
Thomas Gummererc0f16f82018-08-05 18:20:34 +0100448 if (has_conflicts < 0)
449 break;
Thomas Gummerer5ebbdad2018-08-05 18:20:35 +0100450 rerere_io_putmem(out.buf, out.len, io);
451 strbuf_reset(&out);
Thomas Gummererc0f16f82018-08-05 18:20:34 +0100452 } else
Junio C Hamano27d6b082009-12-25 14:34:53 -0800453 rerere_io_putstr(buf.buf, io);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200454 }
Junio C Hamanod58ee6d2009-12-25 13:55:29 -0800455 strbuf_release(&buf);
Thomas Gummerer5ebbdad2018-08-05 18:20:35 +0100456 strbuf_release(&out);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200457
brian m. carlson0d7c4192018-10-15 00:02:02 +0000458 if (hash)
459 the_hash_algo->final_fn(hash, &ctx);
Thomas Gummererc0f16f82018-08-05 18:20:34 +0100460
Thomas Gummerer221444f2018-08-05 18:20:33 +0100461 return has_conflicts;
Junio C Hamano27d6b082009-12-25 14:34:53 -0800462}
463
Junio C Hamanocc899ec2015-06-30 22:40:35 -0700464/*
465 * Scan the path for conflicts, do the "handle_path()" thing above, and
466 * return the number of conflict hunks found.
467 */
Junio C Hamanod829d492018-10-30 15:43:42 +0900468static int handle_file(struct index_state *istate,
469 const char *path, unsigned char *hash, const char *output)
Junio C Hamano27d6b082009-12-25 14:34:53 -0800470{
Thomas Gummerer221444f2018-08-05 18:20:33 +0100471 int has_conflicts = 0;
Junio C Hamano27d6b082009-12-25 14:34:53 -0800472 struct rerere_io_file io;
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200473 int marker_size = ll_merge_marker_size(istate, path);
Junio C Hamano27d6b082009-12-25 14:34:53 -0800474
475 memset(&io, 0, sizeof(io));
476 io.io.getline = rerere_file_getline;
477 io.input = fopen(path, "r");
478 io.io.wrerror = 0;
479 if (!io.input)
Thomas Gummerer2373b652018-08-05 18:20:30 +0100480 return error_errno(_("could not open '%s'"), path);
Junio C Hamano27d6b082009-12-25 14:34:53 -0800481
482 if (output) {
483 io.io.output = fopen(output, "w");
484 if (!io.io.output) {
Thomas Gummerer2373b652018-08-05 18:20:30 +0100485 error_errno(_("could not write '%s'"), output);
Junio C Hamano27d6b082009-12-25 14:34:53 -0800486 fclose(io.input);
Nguyễn Thái Ngọc Duyf7566f02017-05-09 17:11:33 +0700487 return -1;
Junio C Hamano27d6b082009-12-25 14:34:53 -0800488 }
489 }
490
brian m. carlson0d7c4192018-10-15 00:02:02 +0000491 has_conflicts = handle_path(hash, (struct rerere_io *)&io, marker_size);
Junio C Hamano27d6b082009-12-25 14:34:53 -0800492
493 fclose(io.input);
494 if (io.io.wrerror)
Thomas Gummerer2373b652018-08-05 18:20:30 +0100495 error(_("there were errors while writing '%s' (%s)"),
Junio C Hamano27d6b082009-12-25 14:34:53 -0800496 path, strerror(io.io.wrerror));
497 if (io.io.output && fclose(io.io.output))
Thomas Gummerer2373b652018-08-05 18:20:30 +0100498 io.io.wrerror = error_errno(_("failed to flush '%s'"), path);
Junio C Hamano27d6b082009-12-25 14:34:53 -0800499
Thomas Gummerer221444f2018-08-05 18:20:33 +0100500 if (has_conflicts < 0) {
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200501 if (output)
Alex Riesen691f1a22009-04-29 23:22:56 +0200502 unlink_or_warn(output);
Thomas Gummerer2373b652018-08-05 18:20:30 +0100503 return error(_("could not parse conflict hunks in '%s'"), path);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200504 }
Junio C Hamano27d6b082009-12-25 14:34:53 -0800505 if (io.io.wrerror)
Alex Riesen47d32af2008-12-05 01:35:48 +0100506 return -1;
Thomas Gummerer221444f2018-08-05 18:20:33 +0100507 return has_conflicts;
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200508}
509
Junio C Hamanoa96847c2015-06-30 22:33:19 -0700510/*
Junio C Hamano4b68c2a2015-06-30 22:36:35 -0700511 * Look at a cache entry at "i" and see if it is not conflicting,
512 * conflicting and we are willing to handle, or conflicting and
513 * we are unable to handle, and return the determination in *type.
514 * Return the cache index to be looked at next, by skipping the
515 * stages we have already looked at in this invocation of this
516 * function.
517 */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200518static int check_one_conflict(struct index_state *istate, int i, int *type)
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500519{
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200520 const struct cache_entry *e = istate->cache[i];
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500521
522 if (!ce_stage(e)) {
523 *type = RESOLVED;
524 return i + 1;
525 }
526
527 *type = PUNTED;
Junio C Hamano11877b92018-10-19 13:34:02 +0900528 while (i < istate->cache_nr && ce_stage(istate->cache[i]) == 1)
Junio C Hamanofb70a062015-06-28 14:35:13 -0700529 i++;
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500530
531 /* Only handle regular files with both stages #2 and #3 */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200532 if (i + 1 < istate->cache_nr) {
533 const struct cache_entry *e2 = istate->cache[i];
534 const struct cache_entry *e3 = istate->cache[i + 1];
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500535 if (ce_stage(e2) == 2 &&
536 ce_stage(e3) == 3 &&
537 ce_same_name(e, e3) &&
538 S_ISREG(e2->ce_mode) &&
539 S_ISREG(e3->ce_mode))
540 *type = THREE_STAGED;
541 }
542
543 /* Skip the entries with the same name */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200544 while (i < istate->cache_nr && ce_same_name(e, istate->cache[i]))
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500545 i++;
546 return i;
547}
548
Junio C Hamano4b68c2a2015-06-30 22:36:35 -0700549/*
550 * Scan the index and find paths that have conflicts that rerere can
551 * handle, i.e. the ones that has both stages #2 and #3.
552 *
553 * NEEDSWORK: we do not record or replay a previous "resolve by
554 * deletion" for a delete-modify conflict, as that is inherently risky
555 * without knowing what modification is being discarded. The only
556 * safe case, i.e. both side doing the deletion and modification that
557 * are identical to the previous round, might want to be handled,
558 * though.
559 */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200560static int find_conflict(struct repository *r, struct string_list *conflict)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200561{
562 int i;
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200563
564 if (read_index(r->index) < 0)
Thomas Gummerer2373b652018-08-05 18:20:30 +0100565 return error(_("index file corrupt"));
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500566
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200567 for (i = 0; i < r->index->cache_nr;) {
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500568 int conflict_type;
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200569 const struct cache_entry *e = r->index->cache[i];
570 i = check_one_conflict(r->index, i, &conflict_type);
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500571 if (conflict_type == THREE_STAGED)
572 string_list_insert(conflict, (const char *)e->name);
573 }
574 return 0;
575}
576
Junio C Hamano4b68c2a2015-06-30 22:36:35 -0700577/*
578 * The merge_rr list is meant to hold outstanding conflicted paths
579 * that rerere could handle. Abuse the list by adding other types of
580 * entries to allow the caller to show "rerere remaining".
581 *
582 * - Conflicted paths that rerere does not handle are added
583 * - Conflicted paths that have been resolved are marked as such
584 * by storing RERERE_RESOLVED to .util field (where conflict ID
585 * is expected to be stored).
586 *
587 * Do *not* write MERGE_RR file out after calling this function.
588 *
589 * NEEDSWORK: we may want to fix the caller that implements "rerere
590 * remaining" to do this without abusing merge_rr.
591 */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200592int rerere_remaining(struct repository *r, struct string_list *merge_rr)
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500593{
594 int i;
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200595
Jeff King9dd330e2015-09-01 18:14:09 -0400596 if (setup_rerere(merge_rr, RERERE_READONLY))
597 return 0;
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200598 if (read_index(r->index) < 0)
Thomas Gummerer2373b652018-08-05 18:20:30 +0100599 return error(_("index file corrupt"));
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500600
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200601 for (i = 0; i < r->index->cache_nr;) {
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500602 int conflict_type;
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200603 const struct cache_entry *e = r->index->cache[i];
604 i = check_one_conflict(r->index, i, &conflict_type);
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500605 if (conflict_type == PUNTED)
606 string_list_insert(merge_rr, (const char *)e->name);
607 else if (conflict_type == RESOLVED) {
608 struct string_list_item *it;
609 it = string_list_lookup(merge_rr, (const char *)e->name);
610 if (it != NULL) {
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700611 free_rerere_id(it);
Martin von Zweigbergkac49f5c2011-02-16 05:47:44 -0500612 it->util = RERERE_RESOLVED;
613 }
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200614 }
615 }
616 return 0;
617}
618
Junio C Hamanocc899ec2015-06-30 22:40:35 -0700619/*
Junio C Hamano0ce02b32016-03-11 14:53:05 -0800620 * Try using the given conflict resolution "ID" to see
621 * if that recorded conflict resolves cleanly what we
622 * got in the "cur".
623 */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200624static int try_merge(struct index_state *istate,
625 const struct rerere_id *id, const char *path,
Junio C Hamano0ce02b32016-03-11 14:53:05 -0800626 mmfile_t *cur, mmbuffer_t *result)
627{
628 int ret;
629 mmfile_t base = {NULL, 0}, other = {NULL, 0};
630
631 if (read_mmfile(&base, rerere_path(id, "preimage")) ||
632 read_mmfile(&other, rerere_path(id, "postimage")))
633 ret = 1;
634 else
635 /*
636 * A three-way merge. Note that this honors user-customizable
637 * low-level merge driver settings.
638 */
Nguyễn Thái Ngọc Duy32eaa462018-09-21 17:57:27 +0200639 ret = ll_merge(result, path, &base, NULL, cur, "", &other, "",
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200640 istate, NULL);
Junio C Hamano0ce02b32016-03-11 14:53:05 -0800641
642 free(base.ptr);
643 free(other.ptr);
644
645 return ret;
646}
647
648/*
Junio C Hamano18bb9932015-07-06 14:45:55 -0700649 * Find the conflict identified by "id"; the change between its
Junio C Hamanocc899ec2015-06-30 22:40:35 -0700650 * "preimage" (i.e. a previous contents with conflict markers) and its
651 * "postimage" (i.e. the corresponding contents with conflicts
652 * resolved) may apply cleanly to the contents stored in "path", i.e.
653 * the conflict this time around.
654 *
655 * Returns 0 for successful replay of recorded resolution, or non-zero
656 * for failure.
657 */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200658static int merge(struct index_state *istate, const struct rerere_id *id, const char *path)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200659{
Junio C Hamano15ed07d2015-07-06 15:32:53 -0700660 FILE *f;
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200661 int ret;
Junio C Hamano0ce02b32016-03-11 14:53:05 -0800662 mmfile_t cur = {NULL, 0};
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200663 mmbuffer_t result = {NULL, 0};
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200664
Junio C Hamanocc899ec2015-06-30 22:40:35 -0700665 /*
666 * Normalize the conflicts in path and write it out to
667 * "thisimage" temporary file.
668 */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200669 if ((handle_file(istate, path, NULL, rerere_path(id, "thisimage")) < 0) ||
Junio C Hamano0ce02b32016-03-11 14:53:05 -0800670 read_mmfile(&cur, rerere_path(id, "thisimage"))) {
Bert Wesarg689b8c22010-02-23 21:11:53 +0100671 ret = 1;
672 goto out;
673 }
SZEDER Gábor7d7ff152010-07-13 01:42:04 +0200674
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200675 ret = try_merge(istate, id, path, &cur, &result);
Junio C Hamano15ed07d2015-07-06 15:32:53 -0700676 if (ret)
677 goto out;
SZEDER Gábor7d7ff152010-07-13 01:42:04 +0200678
Junio C Hamano15ed07d2015-07-06 15:32:53 -0700679 /*
680 * A successful replay of recorded resolution.
681 * Mark that "postimage" was used to help gc.
682 */
683 if (utime(rerere_path(id, "postimage"), NULL) < 0)
Thomas Gummerer2373b652018-08-05 18:20:30 +0100684 warning_errno(_("failed utime() on '%s'"),
Nguyễn Thái Ngọc Duy033e0112016-05-08 16:47:52 +0700685 rerere_path(id, "postimage"));
Junio C Hamanocc899ec2015-06-30 22:40:35 -0700686
Junio C Hamano15ed07d2015-07-06 15:32:53 -0700687 /* Update "path" with the resolution */
688 f = fopen(path, "w");
689 if (!f)
Thomas Gummerer2373b652018-08-05 18:20:30 +0100690 return error_errno(_("could not open '%s'"), path);
Junio C Hamano15ed07d2015-07-06 15:32:53 -0700691 if (fwrite(result.ptr, result.size, 1, f) != 1)
Thomas Gummerer2373b652018-08-05 18:20:30 +0100692 error_errno(_("could not write '%s'"), path);
Junio C Hamano15ed07d2015-07-06 15:32:53 -0700693 if (fclose(f))
Thomas Gummerer2373b652018-08-05 18:20:30 +0100694 return error_errno(_("writing '%s' failed"), path);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200695
Bert Wesarg689b8c22010-02-23 21:11:53 +0100696out:
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200697 free(cur.ptr);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200698 free(result.ptr);
699
700 return ret;
701}
702
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200703static void update_paths(struct repository *r, struct string_list *update)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200704{
Martin Ågren0fa5a2e2018-05-09 22:55:39 +0200705 struct lock_file index_lock = LOCK_INIT;
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200706 int i;
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200707
Junio C Hamanob3e83cc2016-12-07 10:33:54 -0800708 hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200709
710 for (i = 0; i < update->nr; i++) {
Johannes Schindelinc455c872008-07-21 19:03:49 +0100711 struct string_list_item *item = &update->items[i];
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200712 if (add_file_to_index(r->index, item->string, 0))
Jonathan Nieder89ea9032014-12-02 20:20:49 -0800713 exit(128);
Thomas Gummerer2373b652018-08-05 18:20:30 +0100714 fprintf_ln(stderr, _("Staged '%s' using previous resolution."),
Junio C Hamanoa14c7ab2015-06-28 21:13:24 -0700715 item->string);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200716 }
717
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200718 if (write_locked_index(r->index, &index_lock,
Martin Ågren61000812018-03-01 21:40:20 +0100719 COMMIT_LOCK | SKIP_IF_UNCHANGED))
Thomas Gummerer2373b652018-08-05 18:20:30 +0100720 die(_("unable to write new index file"));
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200721}
722
Junio C Hamano629716d2015-07-30 15:49:18 -0700723static void remove_variant(struct rerere_id *id)
724{
725 unlink_or_warn(rerere_path(id, "postimage"));
726 unlink_or_warn(rerere_path(id, "preimage"));
727 id->collection->status[id->variant] = 0;
728}
729
Junio C Hamano8e7768b2015-06-30 19:36:24 -0700730/*
731 * The path indicated by rr_item may still have conflict for which we
732 * have a recorded resolution, in which case replay it and optionally
733 * update it. Or it may have been resolved by the user and we may
734 * only have the preimage for that conflict, in which case the result
735 * needs to be recorded as a resolution in a postimage file.
736 */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200737static void do_rerere_one_path(struct index_state *istate,
738 struct string_list_item *rr_item,
Junio C Hamano8e7768b2015-06-30 19:36:24 -0700739 struct string_list *update)
740{
741 const char *path = rr_item->string;
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700742 struct rerere_id *id = rr_item->util;
Junio C Hamano629716d2015-07-30 15:49:18 -0700743 struct rerere_dir *rr_dir = id->collection;
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700744 int variant;
Junio C Hamano8e7768b2015-06-30 19:36:24 -0700745
Junio C Hamanoa13d1372015-07-23 14:23:24 -0700746 variant = id->variant;
Junio C Hamano8e7768b2015-06-30 19:36:24 -0700747
Junio C Hamano629716d2015-07-30 15:49:18 -0700748 /* Has the user resolved it already? */
749 if (variant >= 0) {
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200750 if (!handle_file(istate, path, NULL, NULL)) {
Junio C Hamano629716d2015-07-30 15:49:18 -0700751 copy_file(rerere_path(id, "postimage"), path, 0666);
752 id->collection->status[variant] |= RR_HAS_POSTIMAGE;
Thomas Gummerer2373b652018-08-05 18:20:30 +0100753 fprintf_ln(stderr, _("Recorded resolution for '%s'."), path);
Junio C Hamano629716d2015-07-30 15:49:18 -0700754 free_rerere_id(rr_item);
755 rr_item->util = NULL;
756 return;
Junio C Hamanoc0a54232015-07-20 15:19:44 -0700757 }
Junio C Hamano629716d2015-07-30 15:49:18 -0700758 /*
759 * There may be other variants that can cleanly
760 * replay. Try them and update the variant number for
761 * this one.
762 */
763 }
764
765 /* Does any existing resolution apply cleanly? */
766 for (variant = 0; variant < rr_dir->status_nr; variant++) {
767 const int both = RR_HAS_PREIMAGE | RR_HAS_POSTIMAGE;
768 struct rerere_id vid = *id;
769
770 if ((rr_dir->status[variant] & both) != both)
771 continue;
772
773 vid.variant = variant;
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200774 if (merge(istate, &vid, path))
Junio C Hamano629716d2015-07-30 15:49:18 -0700775 continue; /* failed to replay */
776
777 /*
778 * If there already is a different variant that applies
779 * cleanly, there is no point maintaining our own variant.
780 */
781 if (0 <= id->variant && id->variant != variant)
782 remove_variant(id);
Junio C Hamano8e7768b2015-06-30 19:36:24 -0700783
784 if (rerere_autoupdate)
785 string_list_insert(update, path);
786 else
Thomas Gummerer2373b652018-08-05 18:20:30 +0100787 fprintf_ln(stderr,
788 _("Resolved '%s' using previous resolution."),
789 path);
Junio C Hamano629716d2015-07-30 15:49:18 -0700790 free_rerere_id(rr_item);
791 rr_item->util = NULL;
Junio C Hamano925d73c2015-07-06 14:18:09 -0700792 return;
Junio C Hamano8e7768b2015-06-30 19:36:24 -0700793 }
Junio C Hamano629716d2015-07-30 15:49:18 -0700794
795 /* None of the existing one applies; we need a new variant */
796 assign_variant(id);
797
798 variant = id->variant;
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200799 handle_file(istate, path, NULL, rerere_path(id, "preimage"));
Junio C Hamano629716d2015-07-30 15:49:18 -0700800 if (id->collection->status[variant] & RR_HAS_POSTIMAGE) {
801 const char *path = rerere_path(id, "postimage");
802 if (unlink(path))
Thomas Gummerer2373b652018-08-05 18:20:30 +0100803 die_errno(_("cannot unlink stray '%s'"), path);
Junio C Hamano629716d2015-07-30 15:49:18 -0700804 id->collection->status[variant] &= ~RR_HAS_POSTIMAGE;
805 }
806 id->collection->status[variant] |= RR_HAS_PREIMAGE;
Thomas Gummerer2373b652018-08-05 18:20:30 +0100807 fprintf_ln(stderr, _("Recorded preimage for '%s'"), path);
Junio C Hamano8e7768b2015-06-30 19:36:24 -0700808}
809
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200810static int do_plain_rerere(struct repository *r,
811 struct string_list *rr, int fd)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200812{
Thiago Farina183113a2010-07-04 16:46:19 -0300813 struct string_list conflict = STRING_LIST_INIT_DUP;
814 struct string_list update = STRING_LIST_INIT_DUP;
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200815 int i;
816
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200817 find_conflict(r, &conflict);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200818
819 /*
Junio C Hamanocc899ec2015-06-30 22:40:35 -0700820 * MERGE_RR records paths with conflicts immediately after
821 * merge failed. Some of the conflicted paths might have been
822 * hand resolved in the working tree since then, but the
823 * initial run would catch all and register their preimages.
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200824 */
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200825 for (i = 0; i < conflict.nr; i++) {
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700826 struct rerere_id *id;
brian m. carlson0d7c4192018-10-15 00:02:02 +0000827 unsigned char hash[GIT_MAX_RAWSZ];
Johannes Schindelinc455c872008-07-21 19:03:49 +0100828 const char *path = conflict.items[i].string;
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200829 int ret;
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200830
Junio C Hamanoc7a25d32015-07-04 17:17:38 -0700831 /*
832 * Ask handle_file() to scan and assign a
833 * conflict ID. No need to write anything out
834 * yet.
835 */
Junio C Hamanod829d492018-10-30 15:43:42 +0900836 ret = handle_file(r->index, path, hash, NULL);
Thomas Gummererbd7dfa52018-08-05 18:20:37 +0100837 if (ret != 0 && string_list_has_string(rr, path)) {
Thomas Gummerer93406a22018-08-05 18:20:32 +0100838 remove_variant(string_list_lookup(rr, path)->util);
839 string_list_remove(rr, path, 1);
840 }
Junio C Hamanoc7a25d32015-07-04 17:17:38 -0700841 if (ret < 1)
842 continue;
Junio C Hamano1d51ece2015-07-04 17:38:34 -0700843
brian m. carlson0d7c4192018-10-15 00:02:02 +0000844 id = new_rerere_id(hash);
Junio C Hamano18bb9932015-07-06 14:45:55 -0700845 string_list_insert(rr, path)->util = id;
Junio C Hamanocc899ec2015-06-30 22:40:35 -0700846
Junio C Hamanoc0a54232015-07-20 15:19:44 -0700847 /* Ensure that the directory exists. */
848 mkdir_in_gitdir(rerere_path(id, NULL));
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200849 }
850
Junio C Hamano8e7768b2015-06-30 19:36:24 -0700851 for (i = 0; i < rr->nr; i++)
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200852 do_rerere_one_path(r->index, &rr->items[i], &update);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200853
854 if (update.nr)
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200855 update_paths(r, &update);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200856
857 return write_rr(rr, fd);
858}
859
Tanay Abhra633e5ad2014-08-07 09:21:21 -0700860static void git_rerere_config(void)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200861{
Tanay Abhra633e5ad2014-08-07 09:21:21 -0700862 git_config_get_bool("rerere.enabled", &rerere_enabled);
863 git_config_get_bool("rerere.autoupdate", &rerere_autoupdate);
864 git_config(git_default_config, NULL);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200865}
866
Jeff Kingf9327292015-08-10 05:38:57 -0400867static GIT_PATH_FUNC(git_path_rr_cache, "rr-cache")
868
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200869static int is_rerere_enabled(void)
870{
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200871 int rr_cache_exists;
872
873 if (!rerere_enabled)
874 return 0;
875
Jeff Kingf9327292015-08-10 05:38:57 -0400876 rr_cache_exists = is_directory(git_path_rr_cache());
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200877 if (rerere_enabled < 0)
878 return rr_cache_exists;
879
Jeff Kingf9327292015-08-10 05:38:57 -0400880 if (!rr_cache_exists && mkdir_in_gitdir(git_path_rr_cache()))
Thomas Gummerer2373b652018-08-05 18:20:30 +0100881 die(_("could not create directory '%s'"), git_path_rr_cache());
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200882 return 1;
883}
884
Junio C Hamanocb6020b2009-12-04 00:20:48 -0800885int setup_rerere(struct string_list *merge_rr, int flags)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200886{
887 int fd;
888
Tanay Abhra633e5ad2014-08-07 09:21:21 -0700889 git_rerere_config();
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200890 if (!is_rerere_enabled())
891 return -1;
892
Junio C Hamanocb6020b2009-12-04 00:20:48 -0800893 if (flags & (RERERE_AUTOUPDATE|RERERE_NOAUTOUPDATE))
894 rerere_autoupdate = !!(flags & RERERE_AUTOUPDATE);
Jeff King9dd330e2015-09-01 18:14:09 -0400895 if (flags & RERERE_READONLY)
896 fd = 0;
897 else
Stefan Beller102de882018-05-17 15:51:51 -0700898 fd = hold_lock_file_for_update(&write_lock,
899 git_path_merge_rr(the_repository),
Jeff King9dd330e2015-09-01 18:14:09 -0400900 LOCK_DIE_ON_ERROR);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200901 read_rr(merge_rr);
902 return fd;
903}
904
Junio C Hamanocc899ec2015-06-30 22:40:35 -0700905/*
906 * The main entry point that is called internally from codepaths that
907 * perform mergy operations, possibly leaving conflicted index entries
908 * and working tree files.
909 */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200910int repo_rerere(struct repository *r, int flags)
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200911{
Thiago Farina183113a2010-07-04 16:46:19 -0300912 struct string_list merge_rr = STRING_LIST_INIT_DUP;
Junio C Hamano1869bbe2015-07-16 14:50:05 -0700913 int fd, status;
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200914
Junio C Hamanocb6020b2009-12-04 00:20:48 -0800915 fd = setup_rerere(&merge_rr, flags);
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200916 if (fd < 0)
917 return 0;
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200918 status = do_plain_rerere(r, &merge_rr, fd);
Junio C Hamano1869bbe2015-07-16 14:50:05 -0700919 free_rerere_dirs();
920 return status;
Stephan Beyer5b2fd952008-07-09 14:58:57 +0200921}
Junio C Hamanodea45622009-12-25 15:51:32 -0800922
Junio C Hamano3d730ed2016-03-14 15:10:39 -0700923/*
924 * Subclass of rerere_io that reads from an in-core buffer that is a
925 * strbuf
926 */
927struct rerere_io_mem {
928 struct rerere_io io;
929 struct strbuf input;
930};
931
932/*
933 * ... and its getline() method implementation
934 */
935static int rerere_mem_getline(struct strbuf *sb, struct rerere_io *io_)
936{
937 struct rerere_io_mem *io = (struct rerere_io_mem *)io_;
938 char *ep;
939 size_t len;
940
941 strbuf_release(sb);
942 if (!io->input.len)
943 return -1;
944 ep = memchr(io->input.buf, '\n', io->input.len);
945 if (!ep)
946 ep = io->input.buf + io->input.len;
947 else if (*ep == '\n')
948 ep++;
949 len = ep - io->input.buf;
950 strbuf_add(sb, io->input.buf, len);
951 strbuf_remove(&io->input, 0, len);
952 return 0;
953}
954
Junio C Hamanod829d492018-10-30 15:43:42 +0900955static int handle_cache(struct index_state *istate,
956 const char *path, unsigned char *hash, const char *output)
Junio C Hamano3d730ed2016-03-14 15:10:39 -0700957{
958 mmfile_t mmfile[3] = {{NULL}};
959 mmbuffer_t result = {NULL, 0};
960 const struct cache_entry *ce;
Thomas Gummerer221444f2018-08-05 18:20:33 +0100961 int pos, len, i, has_conflicts;
Junio C Hamano3d730ed2016-03-14 15:10:39 -0700962 struct rerere_io_mem io;
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200963 int marker_size = ll_merge_marker_size(istate, path);
Junio C Hamano3d730ed2016-03-14 15:10:39 -0700964
965 /*
966 * Reproduce the conflicted merge in-core
967 */
968 len = strlen(path);
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200969 pos = index_name_pos(istate, path, len);
Junio C Hamano3d730ed2016-03-14 15:10:39 -0700970 if (0 <= pos)
971 return -1;
972 pos = -pos - 1;
973
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200974 while (pos < istate->cache_nr) {
Junio C Hamano3d730ed2016-03-14 15:10:39 -0700975 enum object_type type;
976 unsigned long size;
977
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200978 ce = istate->cache[pos++];
Junio C Hamano3d730ed2016-03-14 15:10:39 -0700979 if (ce_namelen(ce) != len || memcmp(ce->name, path, len))
980 break;
981 i = ce_stage(ce) - 1;
982 if (!mmfile[i].ptr) {
brian m. carlsonb4f5aca2018-03-12 02:27:53 +0000983 mmfile[i].ptr = read_object_file(&ce->oid, &type,
984 &size);
Junio C Hamano3d730ed2016-03-14 15:10:39 -0700985 mmfile[i].size = size;
986 }
987 }
988 for (i = 0; i < 3; i++)
989 if (!mmfile[i].ptr && !mmfile[i].size)
990 mmfile[i].ptr = xstrdup("");
991
992 /*
993 * NEEDSWORK: handle conflicts from merges with
994 * merge.renormalize set, too?
995 */
996 ll_merge(&result, path, &mmfile[0], NULL,
997 &mmfile[1], "ours",
Nguyễn Thái Ngọc Duy32eaa462018-09-21 17:57:27 +0200998 &mmfile[2], "theirs",
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +0200999 istate, NULL);
Junio C Hamano3d730ed2016-03-14 15:10:39 -07001000 for (i = 0; i < 3; i++)
1001 free(mmfile[i].ptr);
1002
1003 memset(&io, 0, sizeof(io));
1004 io.io.getline = rerere_mem_getline;
1005 if (output)
1006 io.io.output = fopen(output, "w");
1007 else
1008 io.io.output = NULL;
1009 strbuf_init(&io.input, 0);
1010 strbuf_attach(&io.input, result.ptr, result.size, result.size);
1011
1012 /*
1013 * Grab the conflict ID and optionally write the original
1014 * contents with conflict markers out.
1015 */
brian m. carlson0d7c4192018-10-15 00:02:02 +00001016 has_conflicts = handle_path(hash, (struct rerere_io *)&io, marker_size);
Junio C Hamano3d730ed2016-03-14 15:10:39 -07001017 strbuf_release(&io.input);
1018 if (io.io.output)
1019 fclose(io.io.output);
Thomas Gummerer221444f2018-08-05 18:20:33 +01001020 return has_conflicts;
Stephan Beyer5b2fd952008-07-09 14:58:57 +02001021}
Junio C Hamanodea45622009-12-25 15:51:32 -08001022
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +02001023static int rerere_forget_one_path(struct index_state *istate,
1024 const char *path,
1025 struct string_list *rr)
Junio C Hamanodea45622009-12-25 15:51:32 -08001026{
1027 const char *filename;
Junio C Hamano1d51ece2015-07-04 17:38:34 -07001028 struct rerere_id *id;
brian m. carlson0d7c4192018-10-15 00:02:02 +00001029 unsigned char hash[GIT_MAX_RAWSZ];
Junio C Hamanodea45622009-12-25 15:51:32 -08001030 int ret;
Junio C Hamano8d9b5a42015-06-30 13:03:36 -07001031 struct string_list_item *item;
Junio C Hamanodea45622009-12-25 15:51:32 -08001032
Junio C Hamano963ec002015-06-30 22:42:34 -07001033 /*
1034 * Recreate the original conflict from the stages in the
1035 * index and compute the conflict ID
1036 */
Junio C Hamanod829d492018-10-30 15:43:42 +09001037 ret = handle_cache(istate, path, hash, NULL);
Junio C Hamanodea45622009-12-25 15:51:32 -08001038 if (ret < 1)
Thomas Gummerer2373b652018-08-05 18:20:30 +01001039 return error(_("could not parse conflict hunks in '%s'"), path);
Junio C Hamano963ec002015-06-30 22:42:34 -07001040
1041 /* Nuke the recorded resolution for the conflict */
brian m. carlson0d7c4192018-10-15 00:02:02 +00001042 id = new_rerere_id(hash);
Junio C Hamano890fca82016-03-28 14:48:13 -07001043
1044 for (id->variant = 0;
1045 id->variant < id->collection->status_nr;
1046 id->variant++) {
1047 mmfile_t cur = { NULL, 0 };
1048 mmbuffer_t result = {NULL, 0};
1049 int cleanly_resolved;
1050
1051 if (!has_rerere_resolution(id))
1052 continue;
1053
Junio C Hamanod829d492018-10-30 15:43:42 +09001054 handle_cache(istate, path, hash, rerere_path(id, "thisimage"));
Junio C Hamano890fca82016-03-28 14:48:13 -07001055 if (read_mmfile(&cur, rerere_path(id, "thisimage"))) {
1056 free(cur.ptr);
Thomas Gummerer2373b652018-08-05 18:20:30 +01001057 error(_("failed to update conflicted state in '%s'"), path);
Junio C Hamano8f449612016-05-11 16:19:17 -07001058 goto fail_exit;
Junio C Hamano890fca82016-03-28 14:48:13 -07001059 }
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +02001060 cleanly_resolved = !try_merge(istate, id, path, &cur, &result);
Junio C Hamano890fca82016-03-28 14:48:13 -07001061 free(result.ptr);
1062 free(cur.ptr);
1063 if (cleanly_resolved)
1064 break;
1065 }
1066
Junio C Hamano8f449612016-05-11 16:19:17 -07001067 if (id->collection->status_nr <= id->variant) {
Thomas Gummerer2373b652018-08-05 18:20:30 +01001068 error(_("no remembered resolution for '%s'"), path);
Junio C Hamano8f449612016-05-11 16:19:17 -07001069 goto fail_exit;
1070 }
Junio C Hamano890fca82016-03-28 14:48:13 -07001071
Junio C Hamano18bb9932015-07-06 14:45:55 -07001072 filename = rerere_path(id, "postimage");
Junio C Hamano8f449612016-05-11 16:19:17 -07001073 if (unlink(filename)) {
1074 if (errno == ENOENT)
Thomas Gummerer2373b652018-08-05 18:20:30 +01001075 error(_("no remembered resolution for '%s'"), path);
Junio C Hamano8f449612016-05-11 16:19:17 -07001076 else
Thomas Gummerer2373b652018-08-05 18:20:30 +01001077 error_errno(_("cannot unlink '%s'"), filename);
Junio C Hamano8f449612016-05-11 16:19:17 -07001078 goto fail_exit;
Junio C Hamanod9d501b2016-05-19 12:51:22 -07001079 }
Junio C Hamanodea45622009-12-25 15:51:32 -08001080
Junio C Hamano963ec002015-06-30 22:42:34 -07001081 /*
1082 * Update the preimage so that the user can resolve the
1083 * conflict in the working tree, run us again to record
1084 * the postimage.
1085 */
Junio C Hamanod829d492018-10-30 15:43:42 +09001086 handle_cache(istate, path, hash, rerere_path(id, "preimage"));
Thomas Gummerer2373b652018-08-05 18:20:30 +01001087 fprintf_ln(stderr, _("Updated preimage for '%s'"), path);
Junio C Hamanodea45622009-12-25 15:51:32 -08001088
Junio C Hamano963ec002015-06-30 22:42:34 -07001089 /*
1090 * And remember that we can record resolution for this
1091 * conflict when the user is done.
1092 */
Junio C Hamano8d9b5a42015-06-30 13:03:36 -07001093 item = string_list_insert(rr, path);
Junio C Hamano1d51ece2015-07-04 17:38:34 -07001094 free_rerere_id(item);
Junio C Hamano18bb9932015-07-06 14:45:55 -07001095 item->util = id;
Thomas Gummerer2373b652018-08-05 18:20:30 +01001096 fprintf(stderr, _("Forgot resolution for '%s'\n"), path);
Junio C Hamanodea45622009-12-25 15:51:32 -08001097 return 0;
Junio C Hamano8f449612016-05-11 16:19:17 -07001098
1099fail_exit:
1100 free(id);
1101 return -1;
Junio C Hamanodea45622009-12-25 15:51:32 -08001102}
1103
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +02001104int rerere_forget(struct repository *r, struct pathspec *pathspec)
Junio C Hamanodea45622009-12-25 15:51:32 -08001105{
1106 int i, fd;
Thiago Farina183113a2010-07-04 16:46:19 -03001107 struct string_list conflict = STRING_LIST_INIT_DUP;
1108 struct string_list merge_rr = STRING_LIST_INIT_DUP;
Junio C Hamanodea45622009-12-25 15:51:32 -08001109
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +02001110 if (read_index(r->index) < 0)
Thomas Gummerer2373b652018-08-05 18:20:30 +01001111 return error(_("index file corrupt"));
Junio C Hamanodea45622009-12-25 15:51:32 -08001112
Junio C Hamano6751e042010-01-20 14:44:31 -08001113 fd = setup_rerere(&merge_rr, RERERE_NOAUTOUPDATE);
Jeff King05445742015-05-14 15:20:52 -04001114 if (fd < 0)
1115 return 0;
Junio C Hamanodea45622009-12-25 15:51:32 -08001116
Junio C Hamano963ec002015-06-30 22:42:34 -07001117 /*
1118 * The paths may have been resolved (incorrectly);
1119 * recover the original conflicted state and then
1120 * find the conflicted paths.
1121 */
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +02001122 unmerge_index(r->index, pathspec);
1123 find_conflict(r, &conflict);
Junio C Hamanodea45622009-12-25 15:51:32 -08001124 for (i = 0; i < conflict.nr; i++) {
1125 struct string_list_item *it = &conflict.items[i];
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +02001126 if (!match_pathspec(r->index, pathspec, it->string,
Nguyễn Thái Ngọc Duyae8d0822014-01-24 20:40:33 +07001127 strlen(it->string), 0, NULL, 0))
Junio C Hamanodea45622009-12-25 15:51:32 -08001128 continue;
Nguyễn Thái Ngọc Duy35843b12018-09-21 17:57:32 +02001129 rerere_forget_one_path(r->index, it->string, &merge_rr);
Junio C Hamanodea45622009-12-25 15:51:32 -08001130 }
1131 return write_rr(&merge_rr, fd);
1132}
Junio C Hamano0f891e72011-05-08 12:55:34 -07001133
Junio C Hamanoe828de82015-06-30 22:43:37 -07001134/*
1135 * Garbage collection support
1136 */
Junio C Hamano1d51ece2015-07-04 17:38:34 -07001137
Junio C Hamano5ea82272017-08-19 11:16:01 -07001138static timestamp_t rerere_created_at(struct rerere_id *id)
Junio C Hamano0f891e72011-05-08 12:55:34 -07001139{
1140 struct stat st;
Junio C Hamano1d51ece2015-07-04 17:38:34 -07001141
Junio C Hamano18bb9932015-07-06 14:45:55 -07001142 return stat(rerere_path(id, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
Junio C Hamano0f891e72011-05-08 12:55:34 -07001143}
1144
Junio C Hamano5ea82272017-08-19 11:16:01 -07001145static timestamp_t rerere_last_used_at(struct rerere_id *id)
Junio C Hamano0f891e72011-05-08 12:55:34 -07001146{
Junio C Hamano0f891e72011-05-08 12:55:34 -07001147 struct stat st;
Junio C Hamano1d51ece2015-07-04 17:38:34 -07001148
Junio C Hamano18bb9932015-07-06 14:45:55 -07001149 return stat(rerere_path(id, "postimage"), &st) ? (time_t) 0 : st.st_mtime;
Junio C Hamano0f891e72011-05-08 12:55:34 -07001150}
1151
Junio C Hamanoe828de82015-06-30 22:43:37 -07001152/*
1153 * Remove the recorded resolution for a given conflict ID
1154 */
Junio C Hamano1d51ece2015-07-04 17:38:34 -07001155static void unlink_rr_item(struct rerere_id *id)
Junio C Hamano0f891e72011-05-08 12:55:34 -07001156{
Junio C Hamano1be1e852016-03-08 12:11:00 -08001157 unlink_or_warn(rerere_path(id, "thisimage"));
1158 remove_variant(id);
1159 id->collection->status[id->variant] = 0;
1160}
1161
Junio C Hamano5ea82272017-08-19 11:16:01 -07001162static void prune_one(struct rerere_id *id,
1163 timestamp_t cutoff_resolve, timestamp_t cutoff_noresolve)
Junio C Hamano1be1e852016-03-08 12:11:00 -08001164{
Junio C Hamano5ea82272017-08-19 11:16:01 -07001165 timestamp_t then;
1166 timestamp_t cutoff;
Junio C Hamano1be1e852016-03-08 12:11:00 -08001167
1168 then = rerere_last_used_at(id);
1169 if (then)
1170 cutoff = cutoff_resolve;
1171 else {
1172 then = rerere_created_at(id);
1173 if (!then)
1174 return;
1175 cutoff = cutoff_noresolve;
1176 }
Junio C Hamano5ea82272017-08-19 11:16:01 -07001177 if (then < cutoff)
Junio C Hamano1be1e852016-03-08 12:11:00 -08001178 unlink_rr_item(id);
Junio C Hamano0f891e72011-05-08 12:55:34 -07001179}
1180
Junio C Hamano0f891e72011-05-08 12:55:34 -07001181void rerere_gc(struct string_list *rr)
1182{
1183 struct string_list to_remove = STRING_LIST_INIT_DUP;
1184 DIR *dir;
1185 struct dirent *e;
Junio C Hamano1be1e852016-03-08 12:11:00 -08001186 int i;
Junio C Hamano5ea82272017-08-19 11:16:01 -07001187 timestamp_t now = time(NULL);
1188 timestamp_t cutoff_noresolve = now - 15 * 86400;
1189 timestamp_t cutoff_resolve = now - 60 * 86400;
Junio C Hamano0f891e72011-05-08 12:55:34 -07001190
Jeff King9dd330e2015-09-01 18:14:09 -04001191 if (setup_rerere(rr, 0) < 0)
1192 return;
1193
Junio C Hamano6e96cb52017-08-19 11:43:39 -07001194 git_config_get_expiry_in_days("gc.rerereresolved", &cutoff_resolve, now);
1195 git_config_get_expiry_in_days("gc.rerereunresolved", &cutoff_noresolve, now);
Tanay Abhra633e5ad2014-08-07 09:21:21 -07001196 git_config(git_default_config, NULL);
Junio C Hamano0f891e72011-05-08 12:55:34 -07001197 dir = opendir(git_path("rr-cache"));
1198 if (!dir)
Thomas Gummerer2373b652018-08-05 18:20:30 +01001199 die_errno(_("unable to open rr-cache directory"));
Junio C Hamanoe828de82015-06-30 22:43:37 -07001200 /* Collect stale conflict IDs ... */
Junio C Hamano0f891e72011-05-08 12:55:34 -07001201 while ((e = readdir(dir))) {
Junio C Hamano1be1e852016-03-08 12:11:00 -08001202 struct rerere_dir *rr_dir;
1203 struct rerere_id id;
1204 int now_empty;
1205
Junio C Hamano0f891e72011-05-08 12:55:34 -07001206 if (is_dot_or_dotdot(e->d_name))
1207 continue;
Junio C Hamano1be1e852016-03-08 12:11:00 -08001208 rr_dir = find_rerere_dir(e->d_name);
1209 if (!rr_dir)
1210 continue; /* or should we remove e->d_name? */
Junio C Hamano0f891e72011-05-08 12:55:34 -07001211
Junio C Hamano1be1e852016-03-08 12:11:00 -08001212 now_empty = 1;
1213 for (id.variant = 0, id.collection = rr_dir;
1214 id.variant < id.collection->status_nr;
1215 id.variant++) {
Junio C Hamano5ea82272017-08-19 11:16:01 -07001216 prune_one(&id, cutoff_resolve, cutoff_noresolve);
Junio C Hamano1be1e852016-03-08 12:11:00 -08001217 if (id.collection->status[id.variant])
1218 now_empty = 0;
Junio C Hamano0f891e72011-05-08 12:55:34 -07001219 }
Junio C Hamano1be1e852016-03-08 12:11:00 -08001220 if (now_empty)
Junio C Hamano0f891e72011-05-08 12:55:34 -07001221 string_list_append(&to_remove, e->d_name);
1222 }
Jim Meyeringa9930e32011-05-26 15:55:50 +02001223 closedir(dir);
Junio C Hamano1be1e852016-03-08 12:11:00 -08001224
1225 /* ... and then remove the empty directories */
Junio C Hamano0f891e72011-05-08 12:55:34 -07001226 for (i = 0; i < to_remove.nr; i++)
Junio C Hamano1be1e852016-03-08 12:11:00 -08001227 rmdir(git_path("rr-cache/%s", to_remove.items[i].string));
Junio C Hamano0f891e72011-05-08 12:55:34 -07001228 string_list_clear(&to_remove, 0);
Jeff King9dd330e2015-09-01 18:14:09 -04001229 rollback_lock_file(&write_lock);
Junio C Hamano0f891e72011-05-08 12:55:34 -07001230}
1231
Junio C Hamanoe828de82015-06-30 22:43:37 -07001232/*
1233 * During a conflict resolution, after "rerere" recorded the
1234 * preimages, abandon them if the user did not resolve them or
1235 * record their resolutions. And drop $GIT_DIR/MERGE_RR.
1236 *
1237 * NEEDSWORK: shouldn't we be calling this from "reset --hard"?
1238 */
Junio C Hamano0f891e72011-05-08 12:55:34 -07001239void rerere_clear(struct string_list *merge_rr)
1240{
1241 int i;
1242
Jeff King9dd330e2015-09-01 18:14:09 -04001243 if (setup_rerere(merge_rr, 0) < 0)
1244 return;
1245
Junio C Hamano0f891e72011-05-08 12:55:34 -07001246 for (i = 0; i < merge_rr->nr; i++) {
Junio C Hamano1d51ece2015-07-04 17:38:34 -07001247 struct rerere_id *id = merge_rr->items[i].util;
Junio C Hamano1be1e852016-03-08 12:11:00 -08001248 if (!has_rerere_resolution(id)) {
Junio C Hamano18bb9932015-07-06 14:45:55 -07001249 unlink_rr_item(id);
Junio C Hamano1be1e852016-03-08 12:11:00 -08001250 rmdir(rerere_path(id, NULL));
1251 }
Junio C Hamano0f891e72011-05-08 12:55:34 -07001252 }
Stefan Beller102de882018-05-17 15:51:51 -07001253 unlink_or_warn(git_path_merge_rr(the_repository));
Jeff King9dd330e2015-09-01 18:14:09 -04001254 rollback_lock_file(&write_lock);
Junio C Hamano0f891e72011-05-08 12:55:34 -07001255}