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