rerere: prepare for customizable conflict marker length
[git/gitweb.git] / rerere.c
blob5f332cb25c240d9a26ec48bd3daa36ae1ced9d7c
1 #include "cache.h"
2 #include "string-list.h"
3 #include "rerere.h"
4 #include "xdiff-interface.h"
5 #include "dir.h"
6 #include "resolve-undo.h"
7 #include "ll-merge.h"
9 /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
10 static int rerere_enabled = -1;
12 /* automatically update cleanly resolved paths to the index */
13 static int rerere_autoupdate;
15 static char *merge_rr_path;
17 const char *rerere_path(const char *hex, const char *file)
19 return git_path("rr-cache/%s/%s", hex, file);
22 int has_rerere_resolution(const char *hex)
24 struct stat st;
25 return !stat(rerere_path(hex, "postimage"), &st);
28 static void read_rr(struct string_list *rr)
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");
48 string_list_insert(buf, rr)->util = name;
50 fclose(in);
53 static struct lock_file write_lock;
55 static int write_rr(struct string_list *rr, int out_fd)
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;
63 path = rr->items[i].string;
64 length = strlen(path) + 1;
65 if (write_in_full(out_fd, rr->items[i].util, 40) != 40 ||
66 write_str_in_full(out_fd, "\t") != 1 ||
67 write_in_full(out_fd, path, length) != length)
68 die("unable to write rerere record");
70 if (commit_lock_file(&write_lock) != 0)
71 die("unable to write rerere record");
72 return 0;
75 static void ferr_write(const void *p, size_t count, FILE *fp, int *err)
77 if (!count || *err)
78 return;
79 if (fwrite(p, count, 1, fp) != 1)
80 *err = errno;
83 static inline void ferr_puts(const char *s, FILE *fp, int *err)
85 ferr_write(s, strlen(s), fp, err);
88 struct rerere_io {
89 int (*getline)(struct strbuf *, struct rerere_io *);
90 FILE *output;
91 int wrerror;
92 /* some more stuff */
95 static void rerere_io_putstr(const char *str, struct rerere_io *io)
97 if (io->output)
98 ferr_puts(str, io->output, &io->wrerror);
101 static void rerere_io_putconflict(int ch, int size, struct rerere_io *io)
103 char buf[64];
105 while (size) {
106 if (size < sizeof(buf) - 2) {
107 memset(buf, ch, size);
108 buf[size] = '\n';
109 buf[size + 1] = '\0';
110 size = 0;
111 } else {
112 int sz = sizeof(buf) - 1;
113 if (size <= sz)
114 sz -= (sz - size) + 1;
115 memset(buf, ch, sz);
116 buf[sz] = '\0';
117 size -= sz;
119 rerere_io_putstr(buf, io);
123 static void rerere_io_putmem(const char *mem, size_t sz, struct rerere_io *io)
125 if (io->output)
126 ferr_write(mem, sz, io->output, &io->wrerror);
129 struct rerere_io_file {
130 struct rerere_io io;
131 FILE *input;
134 static int rerere_file_getline(struct strbuf *sb, struct rerere_io *io_)
136 struct rerere_io_file *io = (struct rerere_io_file *)io_;
137 return strbuf_getwholeline(sb, io->input, '\n');
140 static int is_cmarker(char *buf, int marker_char, int marker_size, int want_sp)
142 while (marker_size--)
143 if (*buf++ != marker_char)
144 return 0;
145 if (want_sp && *buf != ' ')
146 return 0;
147 return isspace(*buf);
150 static int handle_path(unsigned char *sha1, struct rerere_io *io, int marker_size)
152 git_SHA_CTX ctx;
153 int hunk_no = 0;
154 enum {
155 RR_CONTEXT = 0, RR_SIDE_1, RR_SIDE_2, RR_ORIGINAL,
156 } hunk = RR_CONTEXT;
157 struct strbuf one = STRBUF_INIT, two = STRBUF_INIT;
158 struct strbuf buf = STRBUF_INIT;
160 if (sha1)
161 git_SHA1_Init(&ctx);
163 while (!io->getline(&buf, io)) {
164 if (is_cmarker(buf.buf, '<', marker_size, 1)) {
165 if (hunk != RR_CONTEXT)
166 goto bad;
167 hunk = RR_SIDE_1;
168 } else if (is_cmarker(buf.buf, '|', marker_size, 0)) {
169 if (hunk != RR_SIDE_1)
170 goto bad;
171 hunk = RR_ORIGINAL;
172 } else if (is_cmarker(buf.buf, '=', marker_size, 0)) {
173 if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL)
174 goto bad;
175 hunk = RR_SIDE_2;
176 } else if (is_cmarker(buf.buf, '>', marker_size, 1)) {
177 if (hunk != RR_SIDE_2)
178 goto bad;
179 if (strbuf_cmp(&one, &two) > 0)
180 strbuf_swap(&one, &two);
181 hunk_no++;
182 hunk = RR_CONTEXT;
183 rerere_io_putconflict('<', marker_size, io);
184 rerere_io_putmem(one.buf, one.len, io);
185 rerere_io_putconflict('=', marker_size, io);
186 rerere_io_putmem(two.buf, two.len, io);
187 rerere_io_putconflict('>', marker_size, io);
188 if (sha1) {
189 git_SHA1_Update(&ctx, one.buf ? one.buf : "",
190 one.len + 1);
191 git_SHA1_Update(&ctx, two.buf ? two.buf : "",
192 two.len + 1);
194 strbuf_reset(&one);
195 strbuf_reset(&two);
196 } else if (hunk == RR_SIDE_1)
197 strbuf_addstr(&one, buf.buf);
198 else if (hunk == RR_ORIGINAL)
199 ; /* discard */
200 else if (hunk == RR_SIDE_2)
201 strbuf_addstr(&two, buf.buf);
202 else
203 rerere_io_putstr(buf.buf, io);
204 continue;
205 bad:
206 hunk = 99; /* force error exit */
207 break;
209 strbuf_release(&one);
210 strbuf_release(&two);
211 strbuf_release(&buf);
213 if (sha1)
214 git_SHA1_Final(sha1, &ctx);
215 if (hunk != RR_CONTEXT)
216 return -1;
217 return hunk_no;
220 static int handle_file(const char *path, unsigned char *sha1, const char *output)
222 int hunk_no = 0;
223 struct rerere_io_file io;
224 int marker_size = 7;
226 memset(&io, 0, sizeof(io));
227 io.io.getline = rerere_file_getline;
228 io.input = fopen(path, "r");
229 io.io.wrerror = 0;
230 if (!io.input)
231 return error("Could not open %s", path);
233 if (output) {
234 io.io.output = fopen(output, "w");
235 if (!io.io.output) {
236 fclose(io.input);
237 return error("Could not write %s", output);
241 hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size);
243 fclose(io.input);
244 if (io.io.wrerror)
245 error("There were errors while writing %s (%s)",
246 path, strerror(io.io.wrerror));
247 if (io.io.output && fclose(io.io.output))
248 io.io.wrerror = error("Failed to flush %s: %s",
249 path, strerror(errno));
251 if (hunk_no < 0) {
252 if (output)
253 unlink_or_warn(output);
254 return error("Could not parse conflict hunks in %s", path);
256 if (io.io.wrerror)
257 return -1;
258 return hunk_no;
261 struct rerere_io_mem {
262 struct rerere_io io;
263 struct strbuf input;
266 static int rerere_mem_getline(struct strbuf *sb, struct rerere_io *io_)
268 struct rerere_io_mem *io = (struct rerere_io_mem *)io_;
269 char *ep;
270 size_t len;
272 strbuf_release(sb);
273 if (!io->input.len)
274 return -1;
275 ep = strchrnul(io->input.buf, '\n');
276 if (*ep == '\n')
277 ep++;
278 len = ep - io->input.buf;
279 strbuf_add(sb, io->input.buf, len);
280 strbuf_remove(&io->input, 0, len);
281 return 0;
284 static int handle_cache(const char *path, unsigned char *sha1, const char *output)
286 mmfile_t mmfile[3];
287 mmbuffer_t result = {NULL, 0};
288 struct cache_entry *ce;
289 int pos, len, i, hunk_no;
290 struct rerere_io_mem io;
291 int marker_size = 7;
294 * Reproduce the conflicted merge in-core
296 len = strlen(path);
297 pos = cache_name_pos(path, len);
298 if (0 <= pos)
299 return -1;
300 pos = -pos - 1;
302 for (i = 0; i < 3; i++) {
303 enum object_type type;
304 unsigned long size;
306 mmfile[i].size = 0;
307 mmfile[i].ptr = NULL;
308 if (active_nr <= pos)
309 break;
310 ce = active_cache[pos++];
311 if (ce_namelen(ce) != len || memcmp(ce->name, path, len)
312 || ce_stage(ce) != i + 1)
313 break;
314 mmfile[i].ptr = read_sha1_file(ce->sha1, &type, &size);
315 mmfile[i].size = size;
317 for (i = 0; i < 3; i++) {
318 if (!mmfile[i].ptr && !mmfile[i].size)
319 mmfile[i].ptr = xstrdup("");
321 ll_merge(&result, path, &mmfile[0],
322 &mmfile[1], "ours",
323 &mmfile[2], "theirs", 0);
324 for (i = 0; i < 3; i++)
325 free(mmfile[i].ptr);
327 memset(&io, 0, sizeof(&io));
328 io.io.getline = rerere_mem_getline;
329 if (output)
330 io.io.output = fopen(output, "w");
331 else
332 io.io.output = NULL;
333 strbuf_init(&io.input, 0);
334 strbuf_attach(&io.input, result.ptr, result.size, result.size);
336 hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size);
337 strbuf_release(&io.input);
338 if (io.io.output)
339 fclose(io.io.output);
340 return hunk_no;
343 static int find_conflict(struct string_list *conflict)
345 int i;
346 if (read_cache() < 0)
347 return error("Could not read index");
348 for (i = 0; i+1 < active_nr; i++) {
349 struct cache_entry *e2 = active_cache[i];
350 struct cache_entry *e3 = active_cache[i+1];
351 if (ce_stage(e2) == 2 &&
352 ce_stage(e3) == 3 &&
353 ce_same_name(e2, e3) &&
354 S_ISREG(e2->ce_mode) &&
355 S_ISREG(e3->ce_mode)) {
356 string_list_insert((const char *)e2->name, conflict);
357 i++; /* skip over both #2 and #3 */
360 return 0;
363 static int merge(const char *name, const char *path)
365 int ret;
366 mmfile_t cur, base, other;
367 mmbuffer_t result = {NULL, 0};
369 if (handle_file(path, NULL, rerere_path(name, "thisimage")) < 0)
370 return 1;
372 if (read_mmfile(&cur, rerere_path(name, "thisimage")) ||
373 read_mmfile(&base, rerere_path(name, "preimage")) ||
374 read_mmfile(&other, rerere_path(name, "postimage")))
375 return 1;
376 ret = ll_merge(&result, path, &base, &cur, "", &other, "", 0);
377 if (!ret) {
378 FILE *f = fopen(path, "w");
379 if (!f)
380 return error("Could not open %s: %s", path,
381 strerror(errno));
382 if (fwrite(result.ptr, result.size, 1, f) != 1)
383 error("Could not write %s: %s", path, strerror(errno));
384 if (fclose(f))
385 return error("Writing %s failed: %s", path,
386 strerror(errno));
389 free(cur.ptr);
390 free(base.ptr);
391 free(other.ptr);
392 free(result.ptr);
394 return ret;
397 static struct lock_file index_lock;
399 static int update_paths(struct string_list *update)
401 int i;
402 int fd = hold_locked_index(&index_lock, 0);
403 int status = 0;
405 if (fd < 0)
406 return -1;
408 for (i = 0; i < update->nr; i++) {
409 struct string_list_item *item = &update->items[i];
410 if (add_file_to_cache(item->string, ADD_CACHE_IGNORE_ERRORS))
411 status = -1;
414 if (!status && active_cache_changed) {
415 if (write_cache(fd, active_cache, active_nr) ||
416 commit_locked_index(&index_lock))
417 die("Unable to write new index file");
418 } else if (fd >= 0)
419 rollback_lock_file(&index_lock);
420 return status;
423 static int do_plain_rerere(struct string_list *rr, int fd)
425 struct string_list conflict = { NULL, 0, 0, 1 };
426 struct string_list update = { NULL, 0, 0, 1 };
427 int i;
429 find_conflict(&conflict);
432 * MERGE_RR records paths with conflicts immediately after merge
433 * failed. Some of the conflicted paths might have been hand resolved
434 * in the working tree since then, but the initial run would catch all
435 * and register their preimages.
438 for (i = 0; i < conflict.nr; i++) {
439 const char *path = conflict.items[i].string;
440 if (!string_list_has_string(rr, path)) {
441 unsigned char sha1[20];
442 char *hex;
443 int ret;
444 ret = handle_file(path, sha1, NULL);
445 if (ret < 1)
446 continue;
447 hex = xstrdup(sha1_to_hex(sha1));
448 string_list_insert(path, rr)->util = hex;
449 if (mkdir(git_path("rr-cache/%s", hex), 0755))
450 continue;
451 handle_file(path, NULL, rerere_path(hex, "preimage"));
452 fprintf(stderr, "Recorded preimage for '%s'\n", path);
457 * Now some of the paths that had conflicts earlier might have been
458 * hand resolved. Others may be similar to a conflict already that
459 * was resolved before.
462 for (i = 0; i < rr->nr; i++) {
463 int ret;
464 const char *path = rr->items[i].string;
465 const char *name = (const char *)rr->items[i].util;
467 if (has_rerere_resolution(name)) {
468 if (!merge(name, path)) {
469 if (rerere_autoupdate)
470 string_list_insert(path, &update);
471 fprintf(stderr,
472 "%s '%s' using previous resolution.\n",
473 rerere_autoupdate
474 ? "Staged" : "Resolved",
475 path);
476 goto mark_resolved;
480 /* Let's see if we have resolved it. */
481 ret = handle_file(path, NULL, NULL);
482 if (ret)
483 continue;
485 fprintf(stderr, "Recorded resolution for '%s'.\n", path);
486 copy_file(rerere_path(name, "postimage"), path, 0666);
487 mark_resolved:
488 rr->items[i].util = NULL;
491 if (update.nr)
492 update_paths(&update);
494 return write_rr(rr, fd);
497 static int git_rerere_config(const char *var, const char *value, void *cb)
499 if (!strcmp(var, "rerere.enabled"))
500 rerere_enabled = git_config_bool(var, value);
501 else if (!strcmp(var, "rerere.autoupdate"))
502 rerere_autoupdate = git_config_bool(var, value);
503 else
504 return git_default_config(var, value, cb);
505 return 0;
508 static int is_rerere_enabled(void)
510 const char *rr_cache;
511 int rr_cache_exists;
513 if (!rerere_enabled)
514 return 0;
516 rr_cache = git_path("rr-cache");
517 rr_cache_exists = is_directory(rr_cache);
518 if (rerere_enabled < 0)
519 return rr_cache_exists;
521 if (!rr_cache_exists &&
522 (mkdir(rr_cache, 0777) || adjust_shared_perm(rr_cache)))
523 die("Could not create directory %s", rr_cache);
524 return 1;
527 int setup_rerere(struct string_list *merge_rr)
529 int fd;
531 git_config(git_rerere_config, NULL);
532 if (!is_rerere_enabled())
533 return -1;
535 merge_rr_path = git_pathdup("MERGE_RR");
536 fd = hold_lock_file_for_update(&write_lock, merge_rr_path,
537 LOCK_DIE_ON_ERROR);
538 read_rr(merge_rr);
539 return fd;
542 int rerere(void)
544 struct string_list merge_rr = { NULL, 0, 0, 1 };
545 int fd;
547 fd = setup_rerere(&merge_rr);
548 if (fd < 0)
549 return 0;
550 return do_plain_rerere(&merge_rr, fd);
553 static int rerere_forget_one_path(const char *path, struct string_list *rr)
555 const char *filename;
556 char *hex;
557 unsigned char sha1[20];
558 int ret;
560 ret = handle_cache(path, sha1, NULL);
561 if (ret < 1)
562 return error("Could not parse conflict hunks in '%s'", path);
563 hex = xstrdup(sha1_to_hex(sha1));
564 filename = rerere_path(hex, "postimage");
565 if (unlink(filename))
566 return (errno == ENOENT
567 ? error("no remembered resolution for %s", path)
568 : error("cannot unlink %s: %s", filename, strerror(errno)));
570 handle_cache(path, sha1, rerere_path(hex, "preimage"));
571 fprintf(stderr, "Updated preimage for '%s'\n", path);
574 string_list_insert(path, rr)->util = hex;
575 fprintf(stderr, "Forgot resolution for %s\n", path);
576 return 0;
579 int rerere_forget(const char **pathspec)
581 int i, fd;
582 struct string_list conflict = { NULL, 0, 0, 1 };
583 struct string_list merge_rr = { NULL, 0, 0, 1 };
585 if (read_cache() < 0)
586 return error("Could not read index");
588 fd = setup_rerere(&merge_rr);
590 unmerge_cache(pathspec);
591 find_conflict(&conflict);
592 for (i = 0; i < conflict.nr; i++) {
593 struct string_list_item *it = &conflict.items[i];
594 if (!match_pathspec(pathspec, it->string, strlen(it->string),
595 0, NULL))
596 continue;
597 rerere_forget_one_path(it->string, &merge_rr);
599 return write_rr(&merge_rr, fd);