read_gitfile_gently: use ssize_t to hold read result
[git.git] / rerere.c
blob283a0024b05706b98e8056ac20258bf9248b80c7
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"
8 #include "attr.h"
10 /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
11 static int rerere_enabled = -1;
13 /* automatically update cleanly resolved paths to the index */
14 static int rerere_autoupdate;
16 static char *merge_rr_path;
18 const char *rerere_path(const char *hex, const char *file)
20 return git_path("rr-cache/%s/%s", hex, file);
23 int has_rerere_resolution(const char *hex)
25 struct stat st;
26 return !stat(rerere_path(hex, "postimage"), &st);
29 static void read_rr(struct string_list *rr)
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); i++) {
46 int c = fgetc(in);
47 if (c < 0)
48 die("corrupt MERGE_RR");
49 buf[i] = c;
50 if (c == 0)
51 break;
53 if (i == sizeof(buf))
54 die("filename too long");
55 string_list_insert(rr, buf)->util = name;
57 fclose(in);
60 static struct lock_file write_lock;
62 static int write_rr(struct string_list *rr, int out_fd)
64 int i;
65 for (i = 0; i < rr->nr; i++) {
66 const char *path;
67 int length;
68 if (!rr->items[i].util)
69 continue;
70 path = rr->items[i].string;
71 length = strlen(path) + 1;
72 if (write_in_full(out_fd, rr->items[i].util, 40) != 40 ||
73 write_str_in_full(out_fd, "\t") != 1 ||
74 write_in_full(out_fd, path, length) != length)
75 die("unable to write rerere record");
77 if (commit_lock_file(&write_lock) != 0)
78 die("unable to write rerere record");
79 return 0;
82 static void ferr_write(const void *p, size_t count, FILE *fp, int *err)
84 if (!count || *err)
85 return;
86 if (fwrite(p, count, 1, fp) != 1)
87 *err = errno;
90 static inline void ferr_puts(const char *s, FILE *fp, int *err)
92 ferr_write(s, strlen(s), fp, err);
95 struct rerere_io {
96 int (*getline)(struct strbuf *, struct rerere_io *);
97 FILE *output;
98 int wrerror;
99 /* some more stuff */
102 static void rerere_io_putstr(const char *str, struct rerere_io *io)
104 if (io->output)
105 ferr_puts(str, io->output, &io->wrerror);
108 static void rerere_io_putconflict(int ch, int size, struct rerere_io *io)
110 char buf[64];
112 while (size) {
113 if (size < sizeof(buf) - 2) {
114 memset(buf, ch, size);
115 buf[size] = '\n';
116 buf[size + 1] = '\0';
117 size = 0;
118 } else {
119 int sz = sizeof(buf) - 1;
120 if (size <= sz)
121 sz -= (sz - size) + 1;
122 memset(buf, ch, sz);
123 buf[sz] = '\0';
124 size -= sz;
126 rerere_io_putstr(buf, io);
130 static void rerere_io_putmem(const char *mem, size_t sz, struct rerere_io *io)
132 if (io->output)
133 ferr_write(mem, sz, io->output, &io->wrerror);
136 struct rerere_io_file {
137 struct rerere_io io;
138 FILE *input;
141 static int rerere_file_getline(struct strbuf *sb, struct rerere_io *io_)
143 struct rerere_io_file *io = (struct rerere_io_file *)io_;
144 return strbuf_getwholeline(sb, io->input, '\n');
147 static int is_cmarker(char *buf, int marker_char, int marker_size, int want_sp)
149 while (marker_size--)
150 if (*buf++ != marker_char)
151 return 0;
152 if (want_sp && *buf != ' ')
153 return 0;
154 return isspace(*buf);
157 static int handle_path(unsigned char *sha1, struct rerere_io *io, int marker_size)
159 git_SHA_CTX ctx;
160 int hunk_no = 0;
161 enum {
162 RR_CONTEXT = 0, RR_SIDE_1, RR_SIDE_2, RR_ORIGINAL
163 } hunk = RR_CONTEXT;
164 struct strbuf one = STRBUF_INIT, two = STRBUF_INIT;
165 struct strbuf buf = STRBUF_INIT;
167 if (sha1)
168 git_SHA1_Init(&ctx);
170 while (!io->getline(&buf, io)) {
171 if (is_cmarker(buf.buf, '<', marker_size, 1)) {
172 if (hunk != RR_CONTEXT)
173 goto bad;
174 hunk = RR_SIDE_1;
175 } else if (is_cmarker(buf.buf, '|', marker_size, 0)) {
176 if (hunk != RR_SIDE_1)
177 goto bad;
178 hunk = RR_ORIGINAL;
179 } else if (is_cmarker(buf.buf, '=', marker_size, 0)) {
180 if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL)
181 goto bad;
182 hunk = RR_SIDE_2;
183 } else if (is_cmarker(buf.buf, '>', marker_size, 1)) {
184 if (hunk != RR_SIDE_2)
185 goto bad;
186 if (strbuf_cmp(&one, &two) > 0)
187 strbuf_swap(&one, &two);
188 hunk_no++;
189 hunk = RR_CONTEXT;
190 rerere_io_putconflict('<', marker_size, io);
191 rerere_io_putmem(one.buf, one.len, io);
192 rerere_io_putconflict('=', marker_size, io);
193 rerere_io_putmem(two.buf, two.len, io);
194 rerere_io_putconflict('>', marker_size, io);
195 if (sha1) {
196 git_SHA1_Update(&ctx, one.buf ? one.buf : "",
197 one.len + 1);
198 git_SHA1_Update(&ctx, two.buf ? two.buf : "",
199 two.len + 1);
201 strbuf_reset(&one);
202 strbuf_reset(&two);
203 } else if (hunk == RR_SIDE_1)
204 strbuf_addstr(&one, buf.buf);
205 else if (hunk == RR_ORIGINAL)
206 ; /* discard */
207 else if (hunk == RR_SIDE_2)
208 strbuf_addstr(&two, buf.buf);
209 else
210 rerere_io_putstr(buf.buf, io);
211 continue;
212 bad:
213 hunk = 99; /* force error exit */
214 break;
216 strbuf_release(&one);
217 strbuf_release(&two);
218 strbuf_release(&buf);
220 if (sha1)
221 git_SHA1_Final(sha1, &ctx);
222 if (hunk != RR_CONTEXT)
223 return -1;
224 return hunk_no;
227 static int handle_file(const char *path, unsigned char *sha1, const char *output)
229 int hunk_no = 0;
230 struct rerere_io_file io;
231 int marker_size = ll_merge_marker_size(path);
233 memset(&io, 0, sizeof(io));
234 io.io.getline = rerere_file_getline;
235 io.input = fopen(path, "r");
236 io.io.wrerror = 0;
237 if (!io.input)
238 return error("Could not open %s", path);
240 if (output) {
241 io.io.output = fopen(output, "w");
242 if (!io.io.output) {
243 fclose(io.input);
244 return error("Could not write %s", output);
248 hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size);
250 fclose(io.input);
251 if (io.io.wrerror)
252 error("There were errors while writing %s (%s)",
253 path, strerror(io.io.wrerror));
254 if (io.io.output && fclose(io.io.output))
255 io.io.wrerror = error("Failed to flush %s: %s",
256 path, strerror(errno));
258 if (hunk_no < 0) {
259 if (output)
260 unlink_or_warn(output);
261 return error("Could not parse conflict hunks in %s", path);
263 if (io.io.wrerror)
264 return -1;
265 return hunk_no;
268 struct rerere_io_mem {
269 struct rerere_io io;
270 struct strbuf input;
273 static int rerere_mem_getline(struct strbuf *sb, struct rerere_io *io_)
275 struct rerere_io_mem *io = (struct rerere_io_mem *)io_;
276 char *ep;
277 size_t len;
279 strbuf_release(sb);
280 if (!io->input.len)
281 return -1;
282 ep = strchrnul(io->input.buf, '\n');
283 if (*ep == '\n')
284 ep++;
285 len = ep - io->input.buf;
286 strbuf_add(sb, io->input.buf, len);
287 strbuf_remove(&io->input, 0, len);
288 return 0;
291 static int handle_cache(const char *path, unsigned char *sha1, const char *output)
293 mmfile_t mmfile[3];
294 mmbuffer_t result = {NULL, 0};
295 struct cache_entry *ce;
296 int pos, len, i, hunk_no;
297 struct rerere_io_mem io;
298 int marker_size = ll_merge_marker_size(path);
301 * Reproduce the conflicted merge in-core
303 len = strlen(path);
304 pos = cache_name_pos(path, len);
305 if (0 <= pos)
306 return -1;
307 pos = -pos - 1;
309 for (i = 0; i < 3; i++) {
310 enum object_type type;
311 unsigned long size;
313 mmfile[i].size = 0;
314 mmfile[i].ptr = NULL;
315 if (active_nr <= pos)
316 break;
317 ce = active_cache[pos++];
318 if (ce_namelen(ce) != len || memcmp(ce->name, path, len)
319 || ce_stage(ce) != i + 1)
320 break;
321 mmfile[i].ptr = read_sha1_file(ce->sha1, &type, &size);
322 mmfile[i].size = size;
324 for (i = 0; i < 3; i++) {
325 if (!mmfile[i].ptr && !mmfile[i].size)
326 mmfile[i].ptr = xstrdup("");
329 * NEEDSWORK: handle conflicts from merges with
330 * merge.renormalize set, too
332 ll_merge(&result, path, &mmfile[0], NULL,
333 &mmfile[1], "ours",
334 &mmfile[2], "theirs", NULL);
335 for (i = 0; i < 3; i++)
336 free(mmfile[i].ptr);
338 memset(&io, 0, sizeof(io));
339 io.io.getline = rerere_mem_getline;
340 if (output)
341 io.io.output = fopen(output, "w");
342 else
343 io.io.output = NULL;
344 strbuf_init(&io.input, 0);
345 strbuf_attach(&io.input, result.ptr, result.size, result.size);
347 hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size);
348 strbuf_release(&io.input);
349 if (io.io.output)
350 fclose(io.io.output);
351 return hunk_no;
354 static int find_conflict(struct string_list *conflict)
356 int i;
357 if (read_cache() < 0)
358 return error("Could not read index");
359 for (i = 0; i+1 < active_nr; i++) {
360 struct cache_entry *e2 = active_cache[i];
361 struct cache_entry *e3 = active_cache[i+1];
362 if (ce_stage(e2) == 2 &&
363 ce_stage(e3) == 3 &&
364 ce_same_name(e2, e3) &&
365 S_ISREG(e2->ce_mode) &&
366 S_ISREG(e3->ce_mode)) {
367 string_list_insert(conflict, (const char *)e2->name);
368 i++; /* skip over both #2 and #3 */
371 return 0;
374 static int merge(const char *name, const char *path)
376 int ret;
377 mmfile_t cur = {NULL, 0}, base = {NULL, 0}, other = {NULL, 0};
378 mmbuffer_t result = {NULL, 0};
380 if (handle_file(path, NULL, rerere_path(name, "thisimage")) < 0)
381 return 1;
383 if (read_mmfile(&cur, rerere_path(name, "thisimage")) ||
384 read_mmfile(&base, rerere_path(name, "preimage")) ||
385 read_mmfile(&other, rerere_path(name, "postimage"))) {
386 ret = 1;
387 goto out;
389 ret = ll_merge(&result, path, &base, NULL, &cur, "", &other, "", 0);
390 if (!ret) {
391 FILE *f;
393 if (utime(rerere_path(name, "postimage"), NULL) < 0)
394 warning("failed utime() on %s: %s",
395 rerere_path(name, "postimage"),
396 strerror(errno));
397 f = fopen(path, "w");
398 if (!f)
399 return error("Could not open %s: %s", path,
400 strerror(errno));
401 if (fwrite(result.ptr, result.size, 1, f) != 1)
402 error("Could not write %s: %s", path, strerror(errno));
403 if (fclose(f))
404 return error("Writing %s failed: %s", path,
405 strerror(errno));
408 out:
409 free(cur.ptr);
410 free(base.ptr);
411 free(other.ptr);
412 free(result.ptr);
414 return ret;
417 static struct lock_file index_lock;
419 static int update_paths(struct string_list *update)
421 int i;
422 int fd = hold_locked_index(&index_lock, 0);
423 int status = 0;
425 if (fd < 0)
426 return -1;
428 for (i = 0; i < update->nr; i++) {
429 struct string_list_item *item = &update->items[i];
430 if (add_file_to_cache(item->string, ADD_CACHE_IGNORE_ERRORS))
431 status = -1;
434 if (!status && active_cache_changed) {
435 if (write_cache(fd, active_cache, active_nr) ||
436 commit_locked_index(&index_lock))
437 die("Unable to write new index file");
438 } else if (fd >= 0)
439 rollback_lock_file(&index_lock);
440 return status;
443 static int do_plain_rerere(struct string_list *rr, int fd)
445 struct string_list conflict = STRING_LIST_INIT_DUP;
446 struct string_list update = STRING_LIST_INIT_DUP;
447 int i;
449 find_conflict(&conflict);
452 * MERGE_RR records paths with conflicts immediately after merge
453 * failed. Some of the conflicted paths might have been hand resolved
454 * in the working tree since then, but the initial run would catch all
455 * and register their preimages.
458 for (i = 0; i < conflict.nr; i++) {
459 const char *path = conflict.items[i].string;
460 if (!string_list_has_string(rr, path)) {
461 unsigned char sha1[20];
462 char *hex;
463 int ret;
464 ret = handle_file(path, sha1, NULL);
465 if (ret < 1)
466 continue;
467 hex = xstrdup(sha1_to_hex(sha1));
468 string_list_insert(rr, path)->util = hex;
469 if (mkdir(git_path("rr-cache/%s", hex), 0755))
470 continue;
471 handle_file(path, NULL, rerere_path(hex, "preimage"));
472 fprintf(stderr, "Recorded preimage for '%s'\n", path);
477 * Now some of the paths that had conflicts earlier might have been
478 * hand resolved. Others may be similar to a conflict already that
479 * was resolved before.
482 for (i = 0; i < rr->nr; i++) {
483 int ret;
484 const char *path = rr->items[i].string;
485 const char *name = (const char *)rr->items[i].util;
487 if (has_rerere_resolution(name)) {
488 if (!merge(name, path)) {
489 if (rerere_autoupdate)
490 string_list_insert(&update, path);
491 fprintf(stderr,
492 "%s '%s' using previous resolution.\n",
493 rerere_autoupdate
494 ? "Staged" : "Resolved",
495 path);
496 goto mark_resolved;
500 /* Let's see if we have resolved it. */
501 ret = handle_file(path, NULL, NULL);
502 if (ret)
503 continue;
505 fprintf(stderr, "Recorded resolution for '%s'.\n", path);
506 copy_file(rerere_path(name, "postimage"), path, 0666);
507 mark_resolved:
508 rr->items[i].util = NULL;
511 if (update.nr)
512 update_paths(&update);
514 return write_rr(rr, fd);
517 static int git_rerere_config(const char *var, const char *value, void *cb)
519 if (!strcmp(var, "rerere.enabled"))
520 rerere_enabled = git_config_bool(var, value);
521 else if (!strcmp(var, "rerere.autoupdate"))
522 rerere_autoupdate = git_config_bool(var, value);
523 else
524 return git_default_config(var, value, cb);
525 return 0;
528 static int is_rerere_enabled(void)
530 const char *rr_cache;
531 int rr_cache_exists;
533 if (!rerere_enabled)
534 return 0;
536 rr_cache = git_path("rr-cache");
537 rr_cache_exists = is_directory(rr_cache);
538 if (rerere_enabled < 0)
539 return rr_cache_exists;
541 if (!rr_cache_exists &&
542 (mkdir(rr_cache, 0777) || adjust_shared_perm(rr_cache)))
543 die("Could not create directory %s", rr_cache);
544 return 1;
547 int setup_rerere(struct string_list *merge_rr, int flags)
549 int fd;
551 git_config(git_rerere_config, NULL);
552 if (!is_rerere_enabled())
553 return -1;
555 if (flags & (RERERE_AUTOUPDATE|RERERE_NOAUTOUPDATE))
556 rerere_autoupdate = !!(flags & RERERE_AUTOUPDATE);
557 merge_rr_path = git_pathdup("MERGE_RR");
558 fd = hold_lock_file_for_update(&write_lock, merge_rr_path,
559 LOCK_DIE_ON_ERROR);
560 read_rr(merge_rr);
561 return fd;
564 int rerere(int flags)
566 struct string_list merge_rr = STRING_LIST_INIT_DUP;
567 int fd;
569 fd = setup_rerere(&merge_rr, flags);
570 if (fd < 0)
571 return 0;
572 return do_plain_rerere(&merge_rr, fd);
575 static int rerere_forget_one_path(const char *path, struct string_list *rr)
577 const char *filename;
578 char *hex;
579 unsigned char sha1[20];
580 int ret;
582 ret = handle_cache(path, sha1, NULL);
583 if (ret < 1)
584 return error("Could not parse conflict hunks in '%s'", path);
585 hex = xstrdup(sha1_to_hex(sha1));
586 filename = rerere_path(hex, "postimage");
587 if (unlink(filename))
588 return (errno == ENOENT
589 ? error("no remembered resolution for %s", path)
590 : error("cannot unlink %s: %s", filename, strerror(errno)));
592 handle_cache(path, sha1, rerere_path(hex, "preimage"));
593 fprintf(stderr, "Updated preimage for '%s'\n", path);
596 string_list_insert(rr, path)->util = hex;
597 fprintf(stderr, "Forgot resolution for %s\n", path);
598 return 0;
601 int rerere_forget(const char **pathspec)
603 int i, fd;
604 struct string_list conflict = STRING_LIST_INIT_DUP;
605 struct string_list merge_rr = STRING_LIST_INIT_DUP;
607 if (read_cache() < 0)
608 return error("Could not read index");
610 fd = setup_rerere(&merge_rr, RERERE_NOAUTOUPDATE);
612 unmerge_cache(pathspec);
613 find_conflict(&conflict);
614 for (i = 0; i < conflict.nr; i++) {
615 struct string_list_item *it = &conflict.items[i];
616 if (!match_pathspec(pathspec, it->string, strlen(it->string),
617 0, NULL))
618 continue;
619 rerere_forget_one_path(it->string, &merge_rr);
621 return write_rr(&merge_rr, fd);