]> git.ipfire.org Git - thirdparty/git.git/blame - rerere.c
Merge branch 'jc/cache-unmerge'
[thirdparty/git.git] / rerere.c
CommitLineData
5b2fd956 1#include "cache.h"
c455c87c 2#include "string-list.h"
5b2fd956
SB
3#include "rerere.h"
4#include "xdiff/xdiff.h"
5#include "xdiff-interface.h"
dea4562b
JH
6#include "dir.h"
7#include "resolve-undo.h"
8#include "ll-merge.h"
5b2fd956
SB
9
10/* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
11static int rerere_enabled = -1;
12
13/* automatically update cleanly resolved paths to the index */
14static int rerere_autoupdate;
15
16static char *merge_rr_path;
17
90056966 18const char *rerere_path(const char *hex, const char *file)
5b2fd956 19{
90056966 20 return git_path("rr-cache/%s/%s", hex, file);
5b2fd956
SB
21}
22
90056966 23int has_rerere_resolution(const char *hex)
5b2fd956
SB
24{
25 struct stat st;
90056966 26 return !stat(rerere_path(hex, "postimage"), &st);
5b2fd956
SB
27}
28
c455c87c 29static void read_rr(struct string_list *rr)
5b2fd956
SB
30{
31 unsigned char sha1[20];
32 char buf[PATH_MAX];
33 FILE *in = fopen(merge_rr_path, "r");
34 if (!in)
35 return;
36 while (fread(buf, 40, 1, in) == 1) {
37 int i;
38 char *name;
39 if (get_sha1_hex(buf, sha1))
40 die("corrupt MERGE_RR");
41 buf[40] = '\0';
42 name = xstrdup(buf);
43 if (fgetc(in) != '\t')
44 die("corrupt MERGE_RR");
45 for (i = 0; i < sizeof(buf) && (buf[i] = fgetc(in)); i++)
46 ; /* do nothing */
47 if (i == sizeof(buf))
48 die("filename too long");
c455c87c 49 string_list_insert(buf, rr)->util = name;
5b2fd956
SB
50 }
51 fclose(in);
52}
53
54static struct lock_file write_lock;
55
c455c87c 56static int write_rr(struct string_list *rr, int out_fd)
5b2fd956
SB
57{
58 int i;
59 for (i = 0; i < rr->nr; i++) {
60 const char *path;
61 int length;
62 if (!rr->items[i].util)
63 continue;
c455c87c 64 path = rr->items[i].string;
5b2fd956
SB
65 length = strlen(path) + 1;
66 if (write_in_full(out_fd, rr->items[i].util, 40) != 40 ||
2b7ca830 67 write_str_in_full(out_fd, "\t") != 1 ||
5b2fd956
SB
68 write_in_full(out_fd, path, length) != length)
69 die("unable to write rerere record");
70 }
71 if (commit_lock_file(&write_lock) != 0)
72 die("unable to write rerere record");
73 return 0;
74}
75
47d32af2
AR
76static void ferr_write(const void *p, size_t count, FILE *fp, int *err)
77{
78 if (!count || *err)
79 return;
80 if (fwrite(p, count, 1, fp) != 1)
81 *err = errno;
82}
83
84static inline void ferr_puts(const char *s, FILE *fp, int *err)
85{
86 ferr_write(s, strlen(s), fp, err);
87}
88
27d6b085
JH
89struct rerere_io {
90 int (*getline)(struct strbuf *, struct rerere_io *);
91 FILE *output;
92 int wrerror;
93 /* some more stuff */
94};
95
96static void rerere_io_putstr(const char *str, struct rerere_io *io)
97{
98 if (io->output)
99 ferr_puts(str, io->output, &io->wrerror);
100}
101
102static void rerere_io_putmem(const char *mem, size_t sz, struct rerere_io *io)
103{
104 if (io->output)
105 ferr_write(mem, sz, io->output, &io->wrerror);
106}
107
108struct rerere_io_file {
109 struct rerere_io io;
110 FILE *input;
111};
112
113static int rerere_file_getline(struct strbuf *sb, struct rerere_io *io_)
114{
115 struct rerere_io_file *io = (struct rerere_io_file *)io_;
116 return strbuf_getwholeline(sb, io->input, '\n');
117}
118
119static int handle_path(unsigned char *sha1, struct rerere_io *io)
5b2fd956 120{
9126f009 121 git_SHA_CTX ctx;
cc58d7df
JH
122 int hunk_no = 0;
123 enum {
387c9d49 124 RR_CONTEXT = 0, RR_SIDE_1, RR_SIDE_2, RR_ORIGINAL,
cc58d7df 125 } hunk = RR_CONTEXT;
f285a2d7 126 struct strbuf one = STRBUF_INIT, two = STRBUF_INIT;
d58ee6db 127 struct strbuf buf = STRBUF_INIT;
5b2fd956
SB
128
129 if (sha1)
9126f009 130 git_SHA1_Init(&ctx);
5b2fd956 131
27d6b085 132 while (!io->getline(&buf, io)) {
d58ee6db 133 if (!prefixcmp(buf.buf, "<<<<<<< ")) {
cc58d7df 134 if (hunk != RR_CONTEXT)
5b2fd956 135 goto bad;
cc58d7df 136 hunk = RR_SIDE_1;
d58ee6db 137 } else if (!prefixcmp(buf.buf, "|||||||") && isspace(buf.buf[7])) {
cc58d7df 138 if (hunk != RR_SIDE_1)
5b2fd956 139 goto bad;
387c9d49 140 hunk = RR_ORIGINAL;
d58ee6db 141 } else if (!prefixcmp(buf.buf, "=======") && isspace(buf.buf[7])) {
387c9d49 142 if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL)
5b2fd956 143 goto bad;
cc58d7df 144 hunk = RR_SIDE_2;
d58ee6db 145 } else if (!prefixcmp(buf.buf, ">>>>>>> ")) {
cc58d7df 146 if (hunk != RR_SIDE_2)
5b2fd956
SB
147 goto bad;
148 if (strbuf_cmp(&one, &two) > 0)
149 strbuf_swap(&one, &two);
150 hunk_no++;
cc58d7df 151 hunk = RR_CONTEXT;
27d6b085
JH
152 rerere_io_putstr("<<<<<<<\n", io);
153 rerere_io_putmem(one.buf, one.len, io);
154 rerere_io_putstr("=======\n", io);
155 rerere_io_putmem(two.buf, two.len, io);
156 rerere_io_putstr(">>>>>>>\n", io);
5b2fd956 157 if (sha1) {
9126f009 158 git_SHA1_Update(&ctx, one.buf ? one.buf : "",
5b2fd956 159 one.len + 1);
9126f009 160 git_SHA1_Update(&ctx, two.buf ? two.buf : "",
5b2fd956
SB
161 two.len + 1);
162 }
163 strbuf_reset(&one);
164 strbuf_reset(&two);
cc58d7df 165 } else if (hunk == RR_SIDE_1)
d58ee6db 166 strbuf_addstr(&one, buf.buf);
387c9d49
JH
167 else if (hunk == RR_ORIGINAL)
168 ; /* discard */
cc58d7df 169 else if (hunk == RR_SIDE_2)
d58ee6db 170 strbuf_addstr(&two, buf.buf);
27d6b085
JH
171 else
172 rerere_io_putstr(buf.buf, io);
5b2fd956
SB
173 continue;
174 bad:
175 hunk = 99; /* force error exit */
176 break;
177 }
178 strbuf_release(&one);
179 strbuf_release(&two);
d58ee6db 180 strbuf_release(&buf);
5b2fd956 181
5b2fd956 182 if (sha1)
9126f009 183 git_SHA1_Final(sha1, &ctx);
27d6b085
JH
184 if (hunk != RR_CONTEXT)
185 return -1;
186 return hunk_no;
187}
188
189static int handle_file(const char *path, unsigned char *sha1, const char *output)
190{
191 int hunk_no = 0;
192 struct rerere_io_file io;
193
194 memset(&io, 0, sizeof(io));
195 io.io.getline = rerere_file_getline;
196 io.input = fopen(path, "r");
197 io.io.wrerror = 0;
198 if (!io.input)
199 return error("Could not open %s", path);
200
201 if (output) {
202 io.io.output = fopen(output, "w");
203 if (!io.io.output) {
204 fclose(io.input);
205 return error("Could not write %s", output);
206 }
207 }
208
209 hunk_no = handle_path(sha1, (struct rerere_io *)&io);
210
211 fclose(io.input);
212 if (io.io.wrerror)
213 error("There were errors while writing %s (%s)",
214 path, strerror(io.io.wrerror));
215 if (io.io.output && fclose(io.io.output))
216 io.io.wrerror = error("Failed to flush %s: %s",
217 path, strerror(errno));
218
219 if (hunk_no < 0) {
5b2fd956 220 if (output)
691f1a28 221 unlink_or_warn(output);
5b2fd956
SB
222 return error("Could not parse conflict hunks in %s", path);
223 }
27d6b085 224 if (io.io.wrerror)
47d32af2 225 return -1;
5b2fd956
SB
226 return hunk_no;
227}
228
dea4562b
JH
229struct rerere_io_mem {
230 struct rerere_io io;
231 struct strbuf input;
232};
233
234static int rerere_mem_getline(struct strbuf *sb, struct rerere_io *io_)
235{
236 struct rerere_io_mem *io = (struct rerere_io_mem *)io_;
237 char *ep;
238 size_t len;
239
240 strbuf_release(sb);
241 if (!io->input.len)
242 return -1;
243 ep = strchrnul(io->input.buf, '\n');
244 if (*ep == '\n')
245 ep++;
246 len = ep - io->input.buf;
247 strbuf_add(sb, io->input.buf, len);
248 strbuf_remove(&io->input, 0, len);
249 return 0;
250}
251
252static int handle_cache(const char *path, unsigned char *sha1, const char *output)
253{
254 mmfile_t mmfile[3];
255 mmbuffer_t result = {NULL, 0};
256 struct cache_entry *ce;
257 int pos, len, i, hunk_no;
258 struct rerere_io_mem io;
259
260 /*
261 * Reproduce the conflicted merge in-core
262 */
263 len = strlen(path);
264 pos = cache_name_pos(path, len);
265 if (0 <= pos)
47d32af2 266 return -1;
dea4562b
JH
267 pos = -pos - 1;
268
269 for (i = 0; i < 3; i++) {
270 enum object_type type;
271 unsigned long size;
272
273 mmfile[i].size = 0;
274 mmfile[i].ptr = NULL;
275 if (active_nr <= pos)
276 break;
277 ce = active_cache[pos++];
278 if (ce_namelen(ce) != len || memcmp(ce->name, path, len)
279 || ce_stage(ce) != i + 1)
280 break;
281 mmfile[i].ptr = read_sha1_file(ce->sha1, &type, &size);
282 mmfile[i].size = size;
283 }
284 for (i = 0; i < 3; i++) {
285 if (!mmfile[i].ptr && !mmfile[i].size)
286 mmfile[i].ptr = xstrdup("");
287 }
288 ll_merge(&result, path, &mmfile[0],
289 &mmfile[1], "ours",
290 &mmfile[2], "theirs", 0);
291 for (i = 0; i < 3; i++)
292 free(mmfile[i].ptr);
293
294 memset(&io, 0, sizeof(&io));
295 io.io.getline = rerere_mem_getline;
296 if (output)
297 io.io.output = fopen(output, "w");
298 else
299 io.io.output = NULL;
300 strbuf_init(&io.input, 0);
301 strbuf_attach(&io.input, result.ptr, result.size, result.size);
302
303 hunk_no = handle_path(sha1, (struct rerere_io *)&io);
304 strbuf_release(&io.input);
305 if (io.io.output)
306 fclose(io.io.output);
5b2fd956
SB
307 return hunk_no;
308}
309
c455c87c 310static int find_conflict(struct string_list *conflict)
5b2fd956
SB
311{
312 int i;
313 if (read_cache() < 0)
314 return error("Could not read index");
315 for (i = 0; i+1 < active_nr; i++) {
316 struct cache_entry *e2 = active_cache[i];
317 struct cache_entry *e3 = active_cache[i+1];
318 if (ce_stage(e2) == 2 &&
319 ce_stage(e3) == 3 &&
320 ce_same_name(e2, e3) &&
321 S_ISREG(e2->ce_mode) &&
322 S_ISREG(e3->ce_mode)) {
c455c87c 323 string_list_insert((const char *)e2->name, conflict);
5b2fd956
SB
324 i++; /* skip over both #2 and #3 */
325 }
326 }
327 return 0;
328}
329
330static int merge(const char *name, const char *path)
331{
332 int ret;
333 mmfile_t cur, base, other;
334 mmbuffer_t result = {NULL, 0};
335 xpparam_t xpp = {XDF_NEED_MINIMAL};
336
90056966 337 if (handle_file(path, NULL, rerere_path(name, "thisimage")) < 0)
5b2fd956
SB
338 return 1;
339
90056966
SG
340 if (read_mmfile(&cur, rerere_path(name, "thisimage")) ||
341 read_mmfile(&base, rerere_path(name, "preimage")) ||
342 read_mmfile(&other, rerere_path(name, "postimage")))
5b2fd956
SB
343 return 1;
344 ret = xdl_merge(&base, &cur, "", &other, "",
345 &xpp, XDL_MERGE_ZEALOUS, &result);
346 if (!ret) {
347 FILE *f = fopen(path, "w");
348 if (!f)
47d32af2
AR
349 return error("Could not open %s: %s", path,
350 strerror(errno));
351 if (fwrite(result.ptr, result.size, 1, f) != 1)
352 error("Could not write %s: %s", path, strerror(errno));
353 if (fclose(f))
354 return error("Writing %s failed: %s", path,
355 strerror(errno));
5b2fd956
SB
356 }
357
358 free(cur.ptr);
359 free(base.ptr);
360 free(other.ptr);
361 free(result.ptr);
362
363 return ret;
364}
365
366static struct lock_file index_lock;
367
c455c87c 368static int update_paths(struct string_list *update)
5b2fd956
SB
369{
370 int i;
371 int fd = hold_locked_index(&index_lock, 0);
372 int status = 0;
373
374 if (fd < 0)
375 return -1;
376
377 for (i = 0; i < update->nr; i++) {
c455c87c
JS
378 struct string_list_item *item = &update->items[i];
379 if (add_file_to_cache(item->string, ADD_CACHE_IGNORE_ERRORS))
5b2fd956
SB
380 status = -1;
381 }
382
383 if (!status && active_cache_changed) {
384 if (write_cache(fd, active_cache, active_nr) ||
385 commit_locked_index(&index_lock))
386 die("Unable to write new index file");
387 } else if (fd >= 0)
388 rollback_lock_file(&index_lock);
389 return status;
390}
391
c455c87c 392static int do_plain_rerere(struct string_list *rr, int fd)
5b2fd956 393{
c455c87c
JS
394 struct string_list conflict = { NULL, 0, 0, 1 };
395 struct string_list update = { NULL, 0, 0, 1 };
5b2fd956
SB
396 int i;
397
398 find_conflict(&conflict);
399
400 /*
401 * MERGE_RR records paths with conflicts immediately after merge
402 * failed. Some of the conflicted paths might have been hand resolved
403 * in the working tree since then, but the initial run would catch all
404 * and register their preimages.
405 */
406
407 for (i = 0; i < conflict.nr; i++) {
c455c87c
JS
408 const char *path = conflict.items[i].string;
409 if (!string_list_has_string(rr, path)) {
5b2fd956
SB
410 unsigned char sha1[20];
411 char *hex;
412 int ret;
413 ret = handle_file(path, sha1, NULL);
414 if (ret < 1)
415 continue;
416 hex = xstrdup(sha1_to_hex(sha1));
c455c87c 417 string_list_insert(path, rr)->util = hex;
5b2fd956 418 if (mkdir(git_path("rr-cache/%s", hex), 0755))
ba19a808 419 continue;
90056966 420 handle_file(path, NULL, rerere_path(hex, "preimage"));
5b2fd956
SB
421 fprintf(stderr, "Recorded preimage for '%s'\n", path);
422 }
423 }
424
425 /*
426 * Now some of the paths that had conflicts earlier might have been
427 * hand resolved. Others may be similar to a conflict already that
428 * was resolved before.
429 */
430
431 for (i = 0; i < rr->nr; i++) {
432 int ret;
c455c87c 433 const char *path = rr->items[i].string;
5b2fd956
SB
434 const char *name = (const char *)rr->items[i].util;
435
90056966 436 if (has_rerere_resolution(name)) {
5b2fd956 437 if (!merge(name, path)) {
5b2fd956 438 if (rerere_autoupdate)
c455c87c 439 string_list_insert(path, &update);
9196e825
JH
440 fprintf(stderr,
441 "%s '%s' using previous resolution.\n",
442 rerere_autoupdate
443 ? "Staged" : "Resolved",
444 path);
5b2fd956
SB
445 goto mark_resolved;
446 }
447 }
448
449 /* Let's see if we have resolved it. */
450 ret = handle_file(path, NULL, NULL);
451 if (ret)
452 continue;
453
454 fprintf(stderr, "Recorded resolution for '%s'.\n", path);
90056966 455 copy_file(rerere_path(name, "postimage"), path, 0666);
5b2fd956
SB
456 mark_resolved:
457 rr->items[i].util = NULL;
458 }
459
460 if (update.nr)
461 update_paths(&update);
462
463 return write_rr(rr, fd);
464}
465
466static int git_rerere_config(const char *var, const char *value, void *cb)
467{
468 if (!strcmp(var, "rerere.enabled"))
469 rerere_enabled = git_config_bool(var, value);
470 else if (!strcmp(var, "rerere.autoupdate"))
471 rerere_autoupdate = git_config_bool(var, value);
472 else
473 return git_default_config(var, value, cb);
474 return 0;
475}
476
477static int is_rerere_enabled(void)
478{
5b2fd956
SB
479 const char *rr_cache;
480 int rr_cache_exists;
481
482 if (!rerere_enabled)
483 return 0;
484
485 rr_cache = git_path("rr-cache");
90b4a71c 486 rr_cache_exists = is_directory(rr_cache);
5b2fd956
SB
487 if (rerere_enabled < 0)
488 return rr_cache_exists;
489
490 if (!rr_cache_exists &&
491 (mkdir(rr_cache, 0777) || adjust_shared_perm(rr_cache)))
492 die("Could not create directory %s", rr_cache);
493 return 1;
494}
495
cb6020bb 496int setup_rerere(struct string_list *merge_rr, int flags)
5b2fd956
SB
497{
498 int fd;
499
500 git_config(git_rerere_config, NULL);
501 if (!is_rerere_enabled())
502 return -1;
503
cb6020bb
JH
504 if (flags & (RERERE_AUTOUPDATE|RERERE_NOAUTOUPDATE))
505 rerere_autoupdate = !!(flags & RERERE_AUTOUPDATE);
a4f34cbb 506 merge_rr_path = git_pathdup("MERGE_RR");
acd3b9ec
JH
507 fd = hold_lock_file_for_update(&write_lock, merge_rr_path,
508 LOCK_DIE_ON_ERROR);
5b2fd956
SB
509 read_rr(merge_rr);
510 return fd;
511}
512
cb6020bb 513int rerere(int flags)
5b2fd956 514{
c455c87c 515 struct string_list merge_rr = { NULL, 0, 0, 1 };
5b2fd956
SB
516 int fd;
517
cb6020bb 518 fd = setup_rerere(&merge_rr, flags);
5b2fd956
SB
519 if (fd < 0)
520 return 0;
521 return do_plain_rerere(&merge_rr, fd);
522}
dea4562b
JH
523
524static int rerere_forget_one_path(const char *path, struct string_list *rr)
525{
526 const char *filename;
527 char *hex;
528 unsigned char sha1[20];
529 int ret;
530
531 ret = handle_cache(path, sha1, NULL);
532 if (ret < 1)
533 return error("Could not parse conflict hunks in '%s'", path);
534 hex = xstrdup(sha1_to_hex(sha1));
535 filename = rerere_path(hex, "postimage");
536 if (unlink(filename))
537 return (errno == ENOENT
538 ? error("no remembered resolution for %s", path)
539 : error("cannot unlink %s: %s", filename, strerror(errno)));
540
541 handle_cache(path, sha1, rerere_path(hex, "preimage"));
542 fprintf(stderr, "Updated preimage for '%s'\n", path);
543
544
545 string_list_insert(path, rr)->util = hex;
546 fprintf(stderr, "Forgot resolution for %s\n", path);
547 return 0;
548}
549
550int rerere_forget(const char **pathspec)
551{
552 int i, fd;
553 struct string_list conflict = { NULL, 0, 0, 1 };
554 struct string_list merge_rr = { NULL, 0, 0, 1 };
555
556 if (read_cache() < 0)
557 return error("Could not read index");
558
6751e047 559 fd = setup_rerere(&merge_rr, RERERE_NOAUTOUPDATE);
dea4562b
JH
560
561 unmerge_cache(pathspec);
562 find_conflict(&conflict);
563 for (i = 0; i < conflict.nr; i++) {
564 struct string_list_item *it = &conflict.items[i];
565 if (!match_pathspec(pathspec, it->string, strlen(it->string),
566 0, NULL))
567 continue;
568 rerere_forget_one_path(it->string, &merge_rr);
569 }
570 return write_rr(&merge_rr, fd);
571}