Merge branch 'jk/fix-alias-pager-config-key-warnings'
[git/git-svn.git] / rerere.c
blob6a517aa383b75512ba77592616e084d74647b491
1 #include "cache.h"
2 #include "lockfile.h"
3 #include "string-list.h"
4 #include "rerere.h"
5 #include "xdiff-interface.h"
6 #include "dir.h"
7 #include "resolve-undo.h"
8 #include "ll-merge.h"
9 #include "attr.h"
10 #include "pathspec.h"
12 #define RESOLVED 0
13 #define PUNTED 1
14 #define THREE_STAGED 2
15 void *RERERE_RESOLVED = &RERERE_RESOLVED;
17 /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
18 static int rerere_enabled = -1;
20 /* automatically update cleanly resolved paths to the index */
21 static int rerere_autoupdate;
23 const char *rerere_path(const char *hex, const char *file)
25 return git_path("rr-cache/%s/%s", hex, file);
28 static int has_rerere_resolution(const char *hex)
30 struct stat st;
31 return !stat(rerere_path(hex, "postimage"), &st);
34 static void read_rr(struct string_list *rr)
36 unsigned char sha1[20];
37 char buf[PATH_MAX];
38 FILE *in = fopen(git_path_merge_rr(), "r");
39 if (!in)
40 return;
41 while (fread(buf, 40, 1, in) == 1) {
42 int i;
43 char *name;
44 if (get_sha1_hex(buf, sha1))
45 die("corrupt MERGE_RR");
46 buf[40] = '\0';
47 name = xstrdup(buf);
48 if (fgetc(in) != '\t')
49 die("corrupt MERGE_RR");
50 for (i = 0; i < sizeof(buf); i++) {
51 int c = fgetc(in);
52 if (c < 0)
53 die("corrupt MERGE_RR");
54 buf[i] = c;
55 if (c == 0)
56 break;
58 if (i == sizeof(buf))
59 die("filename too long");
60 string_list_insert(rr, buf)->util = name;
62 fclose(in);
65 static struct lock_file write_lock;
67 static int write_rr(struct string_list *rr, int out_fd)
69 int i;
70 for (i = 0; i < rr->nr; i++) {
71 const char *path;
72 int length;
73 if (!rr->items[i].util)
74 continue;
75 path = rr->items[i].string;
76 length = strlen(path) + 1;
77 if (write_in_full(out_fd, rr->items[i].util, 40) != 40 ||
78 write_str_in_full(out_fd, "\t") != 1 ||
79 write_in_full(out_fd, path, length) != length)
80 die("unable to write rerere record");
82 if (commit_lock_file(&write_lock) != 0)
83 die("unable to write rerere record");
84 return 0;
87 static void ferr_write(const void *p, size_t count, FILE *fp, int *err)
89 if (!count || *err)
90 return;
91 if (fwrite(p, count, 1, fp) != 1)
92 *err = errno;
95 static inline void ferr_puts(const char *s, FILE *fp, int *err)
97 ferr_write(s, strlen(s), fp, err);
100 struct rerere_io {
101 int (*getline)(struct strbuf *, struct rerere_io *);
102 FILE *output;
103 int wrerror;
104 /* some more stuff */
107 static void rerere_io_putstr(const char *str, struct rerere_io *io)
109 if (io->output)
110 ferr_puts(str, io->output, &io->wrerror);
113 static void rerere_io_putconflict(int ch, int size, struct rerere_io *io)
115 char buf[64];
117 while (size) {
118 if (size < sizeof(buf) - 2) {
119 memset(buf, ch, size);
120 buf[size] = '\n';
121 buf[size + 1] = '\0';
122 size = 0;
123 } else {
124 int sz = sizeof(buf) - 1;
125 if (size <= sz)
126 sz -= (sz - size) + 1;
127 memset(buf, ch, sz);
128 buf[sz] = '\0';
129 size -= sz;
131 rerere_io_putstr(buf, io);
135 static void rerere_io_putmem(const char *mem, size_t sz, struct rerere_io *io)
137 if (io->output)
138 ferr_write(mem, sz, io->output, &io->wrerror);
141 struct rerere_io_file {
142 struct rerere_io io;
143 FILE *input;
146 static int rerere_file_getline(struct strbuf *sb, struct rerere_io *io_)
148 struct rerere_io_file *io = (struct rerere_io_file *)io_;
149 return strbuf_getwholeline(sb, io->input, '\n');
152 static int is_cmarker(char *buf, int marker_char, int marker_size, int want_sp)
154 while (marker_size--)
155 if (*buf++ != marker_char)
156 return 0;
157 if (want_sp && *buf != ' ')
158 return 0;
159 return isspace(*buf);
162 static int handle_path(unsigned char *sha1, struct rerere_io *io, int marker_size)
164 git_SHA_CTX ctx;
165 int hunk_no = 0;
166 enum {
167 RR_CONTEXT = 0, RR_SIDE_1, RR_SIDE_2, RR_ORIGINAL
168 } hunk = RR_CONTEXT;
169 struct strbuf one = STRBUF_INIT, two = STRBUF_INIT;
170 struct strbuf buf = STRBUF_INIT;
172 if (sha1)
173 git_SHA1_Init(&ctx);
175 while (!io->getline(&buf, io)) {
176 if (is_cmarker(buf.buf, '<', marker_size, 1)) {
177 if (hunk != RR_CONTEXT)
178 goto bad;
179 hunk = RR_SIDE_1;
180 } else if (is_cmarker(buf.buf, '|', marker_size, 0)) {
181 if (hunk != RR_SIDE_1)
182 goto bad;
183 hunk = RR_ORIGINAL;
184 } else if (is_cmarker(buf.buf, '=', marker_size, 0)) {
185 if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL)
186 goto bad;
187 hunk = RR_SIDE_2;
188 } else if (is_cmarker(buf.buf, '>', marker_size, 1)) {
189 if (hunk != RR_SIDE_2)
190 goto bad;
191 if (strbuf_cmp(&one, &two) > 0)
192 strbuf_swap(&one, &two);
193 hunk_no++;
194 hunk = RR_CONTEXT;
195 rerere_io_putconflict('<', marker_size, io);
196 rerere_io_putmem(one.buf, one.len, io);
197 rerere_io_putconflict('=', marker_size, io);
198 rerere_io_putmem(two.buf, two.len, io);
199 rerere_io_putconflict('>', marker_size, io);
200 if (sha1) {
201 git_SHA1_Update(&ctx, one.buf ? one.buf : "",
202 one.len + 1);
203 git_SHA1_Update(&ctx, two.buf ? two.buf : "",
204 two.len + 1);
206 strbuf_reset(&one);
207 strbuf_reset(&two);
208 } else if (hunk == RR_SIDE_1)
209 strbuf_addbuf(&one, &buf);
210 else if (hunk == RR_ORIGINAL)
211 ; /* discard */
212 else if (hunk == RR_SIDE_2)
213 strbuf_addbuf(&two, &buf);
214 else
215 rerere_io_putstr(buf.buf, io);
216 continue;
217 bad:
218 hunk = 99; /* force error exit */
219 break;
221 strbuf_release(&one);
222 strbuf_release(&two);
223 strbuf_release(&buf);
225 if (sha1)
226 git_SHA1_Final(sha1, &ctx);
227 if (hunk != RR_CONTEXT)
228 return -1;
229 return hunk_no;
232 static int handle_file(const char *path, unsigned char *sha1, const char *output)
234 int hunk_no = 0;
235 struct rerere_io_file io;
236 int marker_size = ll_merge_marker_size(path);
238 memset(&io, 0, sizeof(io));
239 io.io.getline = rerere_file_getline;
240 io.input = fopen(path, "r");
241 io.io.wrerror = 0;
242 if (!io.input)
243 return error("Could not open %s", path);
245 if (output) {
246 io.io.output = fopen(output, "w");
247 if (!io.io.output) {
248 fclose(io.input);
249 return error("Could not write %s", output);
253 hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size);
255 fclose(io.input);
256 if (io.io.wrerror)
257 error("There were errors while writing %s (%s)",
258 path, strerror(io.io.wrerror));
259 if (io.io.output && fclose(io.io.output))
260 io.io.wrerror = error("Failed to flush %s: %s",
261 path, strerror(errno));
263 if (hunk_no < 0) {
264 if (output)
265 unlink_or_warn(output);
266 return error("Could not parse conflict hunks in %s", path);
268 if (io.io.wrerror)
269 return -1;
270 return hunk_no;
273 struct rerere_io_mem {
274 struct rerere_io io;
275 struct strbuf input;
278 static int rerere_mem_getline(struct strbuf *sb, struct rerere_io *io_)
280 struct rerere_io_mem *io = (struct rerere_io_mem *)io_;
281 char *ep;
282 size_t len;
284 strbuf_release(sb);
285 if (!io->input.len)
286 return -1;
287 ep = memchr(io->input.buf, '\n', io->input.len);
288 if (!ep)
289 ep = io->input.buf + io->input.len;
290 else if (*ep == '\n')
291 ep++;
292 len = ep - io->input.buf;
293 strbuf_add(sb, io->input.buf, len);
294 strbuf_remove(&io->input, 0, len);
295 return 0;
298 static int handle_cache(const char *path, unsigned char *sha1, const char *output)
300 mmfile_t mmfile[3] = {{NULL}};
301 mmbuffer_t result = {NULL, 0};
302 const struct cache_entry *ce;
303 int pos, len, i, hunk_no;
304 struct rerere_io_mem io;
305 int marker_size = ll_merge_marker_size(path);
308 * Reproduce the conflicted merge in-core
310 len = strlen(path);
311 pos = cache_name_pos(path, len);
312 if (0 <= pos)
313 return -1;
314 pos = -pos - 1;
316 for (i = 0; i < 3; i++) {
317 enum object_type type;
318 unsigned long size;
319 int j;
321 if (active_nr <= pos)
322 break;
323 ce = active_cache[pos++];
324 if (ce_namelen(ce) != len || memcmp(ce->name, path, len))
325 continue;
326 j = ce_stage(ce) - 1;
327 mmfile[j].ptr = read_sha1_file(ce->sha1, &type, &size);
328 mmfile[j].size = size;
330 for (i = 0; i < 3; i++) {
331 if (!mmfile[i].ptr && !mmfile[i].size)
332 mmfile[i].ptr = xstrdup("");
335 * NEEDSWORK: handle conflicts from merges with
336 * merge.renormalize set, too
338 ll_merge(&result, path, &mmfile[0], NULL,
339 &mmfile[1], "ours",
340 &mmfile[2], "theirs", NULL);
341 for (i = 0; i < 3; i++)
342 free(mmfile[i].ptr);
344 memset(&io, 0, sizeof(io));
345 io.io.getline = rerere_mem_getline;
346 if (output)
347 io.io.output = fopen(output, "w");
348 else
349 io.io.output = NULL;
350 strbuf_init(&io.input, 0);
351 strbuf_attach(&io.input, result.ptr, result.size, result.size);
353 hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size);
354 strbuf_release(&io.input);
355 if (io.io.output)
356 fclose(io.io.output);
357 return hunk_no;
360 static int check_one_conflict(int i, int *type)
362 const struct cache_entry *e = active_cache[i];
364 if (!ce_stage(e)) {
365 *type = RESOLVED;
366 return i + 1;
369 *type = PUNTED;
370 if (ce_stage(e) == 1) {
371 if (active_nr <= ++i)
372 return i + 1;
375 /* Only handle regular files with both stages #2 and #3 */
376 if (i + 1 < active_nr) {
377 const struct cache_entry *e2 = active_cache[i];
378 const struct cache_entry *e3 = active_cache[i + 1];
379 if (ce_stage(e2) == 2 &&
380 ce_stage(e3) == 3 &&
381 ce_same_name(e, e3) &&
382 S_ISREG(e2->ce_mode) &&
383 S_ISREG(e3->ce_mode))
384 *type = THREE_STAGED;
387 /* Skip the entries with the same name */
388 while (i < active_nr && ce_same_name(e, active_cache[i]))
389 i++;
390 return i;
393 static int find_conflict(struct string_list *conflict)
395 int i;
396 if (read_cache() < 0)
397 return error("Could not read index");
399 for (i = 0; i < active_nr;) {
400 int conflict_type;
401 const struct cache_entry *e = active_cache[i];
402 i = check_one_conflict(i, &conflict_type);
403 if (conflict_type == THREE_STAGED)
404 string_list_insert(conflict, (const char *)e->name);
406 return 0;
409 int rerere_remaining(struct string_list *merge_rr)
411 int i;
412 if (read_cache() < 0)
413 return error("Could not read index");
415 for (i = 0; i < active_nr;) {
416 int conflict_type;
417 const struct cache_entry *e = active_cache[i];
418 i = check_one_conflict(i, &conflict_type);
419 if (conflict_type == PUNTED)
420 string_list_insert(merge_rr, (const char *)e->name);
421 else if (conflict_type == RESOLVED) {
422 struct string_list_item *it;
423 it = string_list_lookup(merge_rr, (const char *)e->name);
424 if (it != NULL) {
425 free(it->util);
426 it->util = RERERE_RESOLVED;
430 return 0;
433 static int merge(const char *name, const char *path)
435 int ret;
436 mmfile_t cur = {NULL, 0}, base = {NULL, 0}, other = {NULL, 0};
437 mmbuffer_t result = {NULL, 0};
439 if (handle_file(path, NULL, rerere_path(name, "thisimage")) < 0)
440 return 1;
442 if (read_mmfile(&cur, rerere_path(name, "thisimage")) ||
443 read_mmfile(&base, rerere_path(name, "preimage")) ||
444 read_mmfile(&other, rerere_path(name, "postimage"))) {
445 ret = 1;
446 goto out;
448 ret = ll_merge(&result, path, &base, NULL, &cur, "", &other, "", NULL);
449 if (!ret) {
450 FILE *f;
452 if (utime(rerere_path(name, "postimage"), NULL) < 0)
453 warning("failed utime() on %s: %s",
454 rerere_path(name, "postimage"),
455 strerror(errno));
456 f = fopen(path, "w");
457 if (!f)
458 return error("Could not open %s: %s", path,
459 strerror(errno));
460 if (fwrite(result.ptr, result.size, 1, f) != 1)
461 error("Could not write %s: %s", path, strerror(errno));
462 if (fclose(f))
463 return error("Writing %s failed: %s", path,
464 strerror(errno));
467 out:
468 free(cur.ptr);
469 free(base.ptr);
470 free(other.ptr);
471 free(result.ptr);
473 return ret;
476 static struct lock_file index_lock;
478 static void update_paths(struct string_list *update)
480 int i;
482 hold_locked_index(&index_lock, 1);
484 for (i = 0; i < update->nr; i++) {
485 struct string_list_item *item = &update->items[i];
486 if (add_file_to_cache(item->string, 0))
487 exit(128);
490 if (active_cache_changed) {
491 if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK))
492 die("Unable to write new index file");
493 } else
494 rollback_lock_file(&index_lock);
497 static int do_plain_rerere(struct string_list *rr, int fd)
499 struct string_list conflict = STRING_LIST_INIT_DUP;
500 struct string_list update = STRING_LIST_INIT_DUP;
501 int i;
503 find_conflict(&conflict);
506 * MERGE_RR records paths with conflicts immediately after merge
507 * failed. Some of the conflicted paths might have been hand resolved
508 * in the working tree since then, but the initial run would catch all
509 * and register their preimages.
512 for (i = 0; i < conflict.nr; i++) {
513 const char *path = conflict.items[i].string;
514 if (!string_list_has_string(rr, path)) {
515 unsigned char sha1[20];
516 char *hex;
517 int ret;
518 ret = handle_file(path, sha1, NULL);
519 if (ret < 1)
520 continue;
521 hex = xstrdup(sha1_to_hex(sha1));
522 string_list_insert(rr, path)->util = hex;
523 if (mkdir_in_gitdir(git_path("rr-cache/%s", hex)))
524 continue;
525 handle_file(path, NULL, rerere_path(hex, "preimage"));
526 fprintf(stderr, "Recorded preimage for '%s'\n", path);
531 * Now some of the paths that had conflicts earlier might have been
532 * hand resolved. Others may be similar to a conflict already that
533 * was resolved before.
536 for (i = 0; i < rr->nr; i++) {
537 int ret;
538 const char *path = rr->items[i].string;
539 const char *name = (const char *)rr->items[i].util;
541 if (has_rerere_resolution(name)) {
542 if (!merge(name, path)) {
543 const char *msg;
544 if (rerere_autoupdate) {
545 string_list_insert(&update, path);
546 msg = "Staged '%s' using previous resolution.\n";
547 } else
548 msg = "Resolved '%s' using previous resolution.\n";
549 fprintf(stderr, msg, path);
550 goto mark_resolved;
554 /* Let's see if we have resolved it. */
555 ret = handle_file(path, NULL, NULL);
556 if (ret)
557 continue;
559 fprintf(stderr, "Recorded resolution for '%s'.\n", path);
560 copy_file(rerere_path(name, "postimage"), path, 0666);
561 mark_resolved:
562 rr->items[i].util = NULL;
565 if (update.nr)
566 update_paths(&update);
568 return write_rr(rr, fd);
571 static void git_rerere_config(void)
573 git_config_get_bool("rerere.enabled", &rerere_enabled);
574 git_config_get_bool("rerere.autoupdate", &rerere_autoupdate);
575 git_config(git_default_config, NULL);
578 static GIT_PATH_FUNC(git_path_rr_cache, "rr-cache")
580 static int is_rerere_enabled(void)
582 int rr_cache_exists;
584 if (!rerere_enabled)
585 return 0;
587 rr_cache_exists = is_directory(git_path_rr_cache());
588 if (rerere_enabled < 0)
589 return rr_cache_exists;
591 if (!rr_cache_exists && mkdir_in_gitdir(git_path_rr_cache()))
592 die("Could not create directory %s", git_path_rr_cache());
593 return 1;
596 int setup_rerere(struct string_list *merge_rr, int flags)
598 int fd;
600 git_rerere_config();
601 if (!is_rerere_enabled())
602 return -1;
604 if (flags & (RERERE_AUTOUPDATE|RERERE_NOAUTOUPDATE))
605 rerere_autoupdate = !!(flags & RERERE_AUTOUPDATE);
606 fd = hold_lock_file_for_update(&write_lock, git_path_merge_rr(),
607 LOCK_DIE_ON_ERROR);
608 read_rr(merge_rr);
609 return fd;
612 int rerere(int flags)
614 struct string_list merge_rr = STRING_LIST_INIT_DUP;
615 int fd;
617 fd = setup_rerere(&merge_rr, flags);
618 if (fd < 0)
619 return 0;
620 return do_plain_rerere(&merge_rr, fd);
623 static int rerere_forget_one_path(const char *path, struct string_list *rr)
625 const char *filename;
626 char *hex;
627 unsigned char sha1[20];
628 int ret;
630 ret = handle_cache(path, sha1, NULL);
631 if (ret < 1)
632 return error("Could not parse conflict hunks in '%s'", path);
633 hex = xstrdup(sha1_to_hex(sha1));
634 filename = rerere_path(hex, "postimage");
635 if (unlink(filename))
636 return (errno == ENOENT
637 ? error("no remembered resolution for %s", path)
638 : error("cannot unlink %s: %s", filename, strerror(errno)));
640 handle_cache(path, sha1, rerere_path(hex, "preimage"));
641 fprintf(stderr, "Updated preimage for '%s'\n", path);
644 string_list_insert(rr, path)->util = hex;
645 fprintf(stderr, "Forgot resolution for %s\n", path);
646 return 0;
649 int rerere_forget(struct pathspec *pathspec)
651 int i, fd;
652 struct string_list conflict = STRING_LIST_INIT_DUP;
653 struct string_list merge_rr = STRING_LIST_INIT_DUP;
655 if (read_cache() < 0)
656 return error("Could not read index");
658 fd = setup_rerere(&merge_rr, RERERE_NOAUTOUPDATE);
659 if (fd < 0)
660 return 0;
662 unmerge_cache(pathspec);
663 find_conflict(&conflict);
664 for (i = 0; i < conflict.nr; i++) {
665 struct string_list_item *it = &conflict.items[i];
666 if (!match_pathspec(pathspec, it->string,
667 strlen(it->string), 0, NULL, 0))
668 continue;
669 rerere_forget_one_path(it->string, &merge_rr);
671 return write_rr(&merge_rr, fd);
674 static time_t rerere_created_at(const char *name)
676 struct stat st;
677 return stat(rerere_path(name, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
680 static time_t rerere_last_used_at(const char *name)
682 struct stat st;
683 return stat(rerere_path(name, "postimage"), &st) ? (time_t) 0 : st.st_mtime;
686 static void unlink_rr_item(const char *name)
688 unlink(rerere_path(name, "thisimage"));
689 unlink(rerere_path(name, "preimage"));
690 unlink(rerere_path(name, "postimage"));
691 rmdir(git_path("rr-cache/%s", name));
694 void rerere_gc(struct string_list *rr)
696 struct string_list to_remove = STRING_LIST_INIT_DUP;
697 DIR *dir;
698 struct dirent *e;
699 int i, cutoff;
700 time_t now = time(NULL), then;
701 int cutoff_noresolve = 15;
702 int cutoff_resolve = 60;
704 git_config_get_int("gc.rerereresolved", &cutoff_resolve);
705 git_config_get_int("gc.rerereunresolved", &cutoff_noresolve);
706 git_config(git_default_config, NULL);
707 dir = opendir(git_path("rr-cache"));
708 if (!dir)
709 die_errno("unable to open rr-cache directory");
710 while ((e = readdir(dir))) {
711 if (is_dot_or_dotdot(e->d_name))
712 continue;
714 then = rerere_last_used_at(e->d_name);
715 if (then) {
716 cutoff = cutoff_resolve;
717 } else {
718 then = rerere_created_at(e->d_name);
719 if (!then)
720 continue;
721 cutoff = cutoff_noresolve;
723 if (then < now - cutoff * 86400)
724 string_list_append(&to_remove, e->d_name);
726 closedir(dir);
727 for (i = 0; i < to_remove.nr; i++)
728 unlink_rr_item(to_remove.items[i].string);
729 string_list_clear(&to_remove, 0);
732 void rerere_clear(struct string_list *merge_rr)
734 int i;
736 for (i = 0; i < merge_rr->nr; i++) {
737 const char *name = (const char *)merge_rr->items[i].util;
738 if (!has_rerere_resolution(name))
739 unlink_rr_item(name);
741 unlink_or_warn(git_path_merge_rr());