Code

i18n: git-bisect [Y/n] messages
[git.git] / rerere.c
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 #define RESOLVED 0
11 #define PUNTED 1
12 #define THREE_STAGED 2
13 void *RERERE_RESOLVED = &RERERE_RESOLVED;
15 /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
16 static int rerere_enabled = -1;
18 /* automatically update cleanly resolved paths to the index */
19 static int rerere_autoupdate;
21 static char *merge_rr_path;
23 const char *rerere_path(const char *hex, const char *file)
24 {
25         return git_path("rr-cache/%s/%s", hex, file);
26 }
28 int has_rerere_resolution(const char *hex)
29 {
30         struct stat st;
31         return !stat(rerere_path(hex, "postimage"), &st);
32 }
34 static void read_rr(struct string_list *rr)
35 {
36         unsigned char sha1[20];
37         char buf[PATH_MAX];
38         FILE *in = fopen(merge_rr_path, "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) && (buf[i] = fgetc(in)); i++)
51                         ; /* do nothing */
52                 if (i == sizeof(buf))
53                         die("filename too long");
54                 string_list_insert(rr, buf)->util = name;
55         }
56         fclose(in);
57 }
59 static struct lock_file write_lock;
61 static int write_rr(struct string_list *rr, int out_fd)
62 {
63         int i;
64         for (i = 0; i < rr->nr; i++) {
65                 const char *path;
66                 int length;
67                 if (!rr->items[i].util)
68                         continue;
69                 path = rr->items[i].string;
70                 length = strlen(path) + 1;
71                 if (write_in_full(out_fd, rr->items[i].util, 40) != 40 ||
72                     write_str_in_full(out_fd, "\t") != 1 ||
73                     write_in_full(out_fd, path, length) != length)
74                         die("unable to write rerere record");
75         }
76         if (commit_lock_file(&write_lock) != 0)
77                 die("unable to write rerere record");
78         return 0;
79 }
81 static void ferr_write(const void *p, size_t count, FILE *fp, int *err)
82 {
83         if (!count || *err)
84                 return;
85         if (fwrite(p, count, 1, fp) != 1)
86                 *err = errno;
87 }
89 static inline void ferr_puts(const char *s, FILE *fp, int *err)
90 {
91         ferr_write(s, strlen(s), fp, err);
92 }
94 struct rerere_io {
95         int (*getline)(struct strbuf *, struct rerere_io *);
96         FILE *output;
97         int wrerror;
98         /* some more stuff */
99 };
101 static void rerere_io_putstr(const char *str, struct rerere_io *io)
103         if (io->output)
104                 ferr_puts(str, io->output, &io->wrerror);
107 static void rerere_io_putconflict(int ch, int size, struct rerere_io *io)
109         char buf[64];
111         while (size) {
112                 if (size < sizeof(buf) - 2) {
113                         memset(buf, ch, size);
114                         buf[size] = '\n';
115                         buf[size + 1] = '\0';
116                         size = 0;
117                 } else {
118                         int sz = sizeof(buf) - 1;
119                         if (size <= sz)
120                                 sz -= (sz - size) + 1;
121                         memset(buf, ch, sz);
122                         buf[sz] = '\0';
123                         size -= sz;
124                 }
125                 rerere_io_putstr(buf, io);
126         }
129 static void rerere_io_putmem(const char *mem, size_t sz, struct rerere_io *io)
131         if (io->output)
132                 ferr_write(mem, sz, io->output, &io->wrerror);
135 struct rerere_io_file {
136         struct rerere_io io;
137         FILE *input;
138 };
140 static int rerere_file_getline(struct strbuf *sb, struct rerere_io *io_)
142         struct rerere_io_file *io = (struct rerere_io_file *)io_;
143         return strbuf_getwholeline(sb, io->input, '\n');
146 static int is_cmarker(char *buf, int marker_char, int marker_size, int want_sp)
148         while (marker_size--)
149                 if (*buf++ != marker_char)
150                         return 0;
151         if (want_sp && *buf != ' ')
152                 return 0;
153         return isspace(*buf);
156 static int handle_path(unsigned char *sha1, struct rerere_io *io, int marker_size)
158         git_SHA_CTX ctx;
159         int hunk_no = 0;
160         enum {
161                 RR_CONTEXT = 0, RR_SIDE_1, RR_SIDE_2, RR_ORIGINAL
162         } hunk = RR_CONTEXT;
163         struct strbuf one = STRBUF_INIT, two = STRBUF_INIT;
164         struct strbuf buf = STRBUF_INIT;
166         if (sha1)
167                 git_SHA1_Init(&ctx);
169         while (!io->getline(&buf, io)) {
170                 if (is_cmarker(buf.buf, '<', marker_size, 1)) {
171                         if (hunk != RR_CONTEXT)
172                                 goto bad;
173                         hunk = RR_SIDE_1;
174                 } else if (is_cmarker(buf.buf, '|', marker_size, 0)) {
175                         if (hunk != RR_SIDE_1)
176                                 goto bad;
177                         hunk = RR_ORIGINAL;
178                 } else if (is_cmarker(buf.buf, '=', marker_size, 0)) {
179                         if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL)
180                                 goto bad;
181                         hunk = RR_SIDE_2;
182                 } else if (is_cmarker(buf.buf, '>', marker_size, 1)) {
183                         if (hunk != RR_SIDE_2)
184                                 goto bad;
185                         if (strbuf_cmp(&one, &two) > 0)
186                                 strbuf_swap(&one, &two);
187                         hunk_no++;
188                         hunk = RR_CONTEXT;
189                         rerere_io_putconflict('<', marker_size, io);
190                         rerere_io_putmem(one.buf, one.len, io);
191                         rerere_io_putconflict('=', marker_size, io);
192                         rerere_io_putmem(two.buf, two.len, io);
193                         rerere_io_putconflict('>', marker_size, io);
194                         if (sha1) {
195                                 git_SHA1_Update(&ctx, one.buf ? one.buf : "",
196                                             one.len + 1);
197                                 git_SHA1_Update(&ctx, two.buf ? two.buf : "",
198                                             two.len + 1);
199                         }
200                         strbuf_reset(&one);
201                         strbuf_reset(&two);
202                 } else if (hunk == RR_SIDE_1)
203                         strbuf_addstr(&one, buf.buf);
204                 else if (hunk == RR_ORIGINAL)
205                         ; /* discard */
206                 else if (hunk == RR_SIDE_2)
207                         strbuf_addstr(&two, buf.buf);
208                 else
209                         rerere_io_putstr(buf.buf, io);
210                 continue;
211         bad:
212                 hunk = 99; /* force error exit */
213                 break;
214         }
215         strbuf_release(&one);
216         strbuf_release(&two);
217         strbuf_release(&buf);
219         if (sha1)
220                 git_SHA1_Final(sha1, &ctx);
221         if (hunk != RR_CONTEXT)
222                 return -1;
223         return hunk_no;
226 static int handle_file(const char *path, unsigned char *sha1, const char *output)
228         int hunk_no = 0;
229         struct rerere_io_file io;
230         int marker_size = ll_merge_marker_size(path);
232         memset(&io, 0, sizeof(io));
233         io.io.getline = rerere_file_getline;
234         io.input = fopen(path, "r");
235         io.io.wrerror = 0;
236         if (!io.input)
237                 return error("Could not open %s", path);
239         if (output) {
240                 io.io.output = fopen(output, "w");
241                 if (!io.io.output) {
242                         fclose(io.input);
243                         return error("Could not write %s", output);
244                 }
245         }
247         hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size);
249         fclose(io.input);
250         if (io.io.wrerror)
251                 error("There were errors while writing %s (%s)",
252                       path, strerror(io.io.wrerror));
253         if (io.io.output && fclose(io.io.output))
254                 io.io.wrerror = error("Failed to flush %s: %s",
255                                       path, strerror(errno));
257         if (hunk_no < 0) {
258                 if (output)
259                         unlink_or_warn(output);
260                 return error("Could not parse conflict hunks in %s", path);
261         }
262         if (io.io.wrerror)
263                 return -1;
264         return hunk_no;
267 struct rerere_io_mem {
268         struct rerere_io io;
269         struct strbuf input;
270 };
272 static int rerere_mem_getline(struct strbuf *sb, struct rerere_io *io_)
274         struct rerere_io_mem *io = (struct rerere_io_mem *)io_;
275         char *ep;
276         size_t len;
278         strbuf_release(sb);
279         if (!io->input.len)
280                 return -1;
281         ep = strchrnul(io->input.buf, '\n');
282         if (*ep == '\n')
283                 ep++;
284         len = ep - io->input.buf;
285         strbuf_add(sb, io->input.buf, len);
286         strbuf_remove(&io->input, 0, len);
287         return 0;
290 static int handle_cache(const char *path, unsigned char *sha1, const char *output)
292         mmfile_t mmfile[3];
293         mmbuffer_t result = {NULL, 0};
294         struct cache_entry *ce;
295         int pos, len, i, hunk_no;
296         struct rerere_io_mem io;
297         int marker_size = ll_merge_marker_size(path);
299         /*
300          * Reproduce the conflicted merge in-core
301          */
302         len = strlen(path);
303         pos = cache_name_pos(path, len);
304         if (0 <= pos)
305                 return -1;
306         pos = -pos - 1;
308         for (i = 0; i < 3; i++) {
309                 enum object_type type;
310                 unsigned long size;
312                 mmfile[i].size = 0;
313                 mmfile[i].ptr = NULL;
314                 if (active_nr <= pos)
315                         break;
316                 ce = active_cache[pos++];
317                 if (ce_namelen(ce) != len || memcmp(ce->name, path, len)
318                     || ce_stage(ce) != i + 1)
319                         break;
320                 mmfile[i].ptr = read_sha1_file(ce->sha1, &type, &size);
321                 mmfile[i].size = size;
322         }
323         for (i = 0; i < 3; i++) {
324                 if (!mmfile[i].ptr && !mmfile[i].size)
325                         mmfile[i].ptr = xstrdup("");
326         }
327         /*
328          * NEEDSWORK: handle conflicts from merges with
329          * merge.renormalize set, too
330          */
331         ll_merge(&result, path, &mmfile[0], NULL,
332                  &mmfile[1], "ours",
333                  &mmfile[2], "theirs", NULL);
334         for (i = 0; i < 3; i++)
335                 free(mmfile[i].ptr);
337         memset(&io, 0, sizeof(io));
338         io.io.getline = rerere_mem_getline;
339         if (output)
340                 io.io.output = fopen(output, "w");
341         else
342                 io.io.output = NULL;
343         strbuf_init(&io.input, 0);
344         strbuf_attach(&io.input, result.ptr, result.size, result.size);
346         hunk_no = handle_path(sha1, (struct rerere_io *)&io, marker_size);
347         strbuf_release(&io.input);
348         if (io.io.output)
349                 fclose(io.io.output);
350         return hunk_no;
353 static int check_one_conflict(int i, int *type)
355         struct cache_entry *e = active_cache[i];
357         if (!ce_stage(e)) {
358                 *type = RESOLVED;
359                 return i + 1;
360         }
362         *type = PUNTED;
363         if (ce_stage(e) == 1) {
364                 if (active_nr <= ++i)
365                         return i + 1;
366         }
368         /* Only handle regular files with both stages #2 and #3 */
369         if (i + 1 < active_nr) {
370                 struct cache_entry *e2 = active_cache[i];
371                 struct cache_entry *e3 = active_cache[i + 1];
372                 if (ce_stage(e2) == 2 &&
373                     ce_stage(e3) == 3 &&
374                     ce_same_name(e, e3) &&
375                     S_ISREG(e2->ce_mode) &&
376                     S_ISREG(e3->ce_mode))
377                         *type = THREE_STAGED;
378         }
380         /* Skip the entries with the same name */
381         while (i < active_nr && ce_same_name(e, active_cache[i]))
382                 i++;
383         return i;
386 static int find_conflict(struct string_list *conflict)
388         int i;
389         if (read_cache() < 0)
390                 return error("Could not read index");
392         for (i = 0; i < active_nr;) {
393                 int conflict_type;
394                 struct cache_entry *e = active_cache[i];
395                 i = check_one_conflict(i, &conflict_type);
396                 if (conflict_type == THREE_STAGED)
397                         string_list_insert(conflict, (const char *)e->name);
398         }
399         return 0;
402 int rerere_remaining(struct string_list *merge_rr)
404         int i;
405         if (read_cache() < 0)
406                 return error("Could not read index");
408         for (i = 0; i < active_nr;) {
409                 int conflict_type;
410                 struct cache_entry *e = active_cache[i];
411                 i = check_one_conflict(i, &conflict_type);
412                 if (conflict_type == PUNTED)
413                         string_list_insert(merge_rr, (const char *)e->name);
414                 else if (conflict_type == RESOLVED) {
415                         struct string_list_item *it;
416                         it = string_list_lookup(merge_rr, (const char *)e->name);
417                         if (it != NULL) {
418                                 free(it->util);
419                                 it->util = RERERE_RESOLVED;
420                         }
421                 }
422         }
423         return 0;
426 static int merge(const char *name, const char *path)
428         int ret;
429         mmfile_t cur = {NULL, 0}, base = {NULL, 0}, other = {NULL, 0};
430         mmbuffer_t result = {NULL, 0};
432         if (handle_file(path, NULL, rerere_path(name, "thisimage")) < 0)
433                 return 1;
435         if (read_mmfile(&cur, rerere_path(name, "thisimage")) ||
436                         read_mmfile(&base, rerere_path(name, "preimage")) ||
437                         read_mmfile(&other, rerere_path(name, "postimage"))) {
438                 ret = 1;
439                 goto out;
440         }
441         ret = ll_merge(&result, path, &base, NULL, &cur, "", &other, "", NULL);
442         if (!ret) {
443                 FILE *f;
445                 if (utime(rerere_path(name, "postimage"), NULL) < 0)
446                         warning("failed utime() on %s: %s",
447                                         rerere_path(name, "postimage"),
448                                         strerror(errno));
449                 f = fopen(path, "w");
450                 if (!f)
451                         return error("Could not open %s: %s", path,
452                                      strerror(errno));
453                 if (fwrite(result.ptr, result.size, 1, f) != 1)
454                         error("Could not write %s: %s", path, strerror(errno));
455                 if (fclose(f))
456                         return error("Writing %s failed: %s", path,
457                                      strerror(errno));
458         }
460 out:
461         free(cur.ptr);
462         free(base.ptr);
463         free(other.ptr);
464         free(result.ptr);
466         return ret;
469 static struct lock_file index_lock;
471 static int update_paths(struct string_list *update)
473         int i;
474         int fd = hold_locked_index(&index_lock, 0);
475         int status = 0;
477         if (fd < 0)
478                 return -1;
480         for (i = 0; i < update->nr; i++) {
481                 struct string_list_item *item = &update->items[i];
482                 if (add_file_to_cache(item->string, ADD_CACHE_IGNORE_ERRORS))
483                         status = -1;
484         }
486         if (!status && active_cache_changed) {
487                 if (write_cache(fd, active_cache, active_nr) ||
488                     commit_locked_index(&index_lock))
489                         die("Unable to write new index file");
490         } else if (fd >= 0)
491                 rollback_lock_file(&index_lock);
492         return status;
495 static int do_plain_rerere(struct string_list *rr, int fd)
497         struct string_list conflict = STRING_LIST_INIT_DUP;
498         struct string_list update = STRING_LIST_INIT_DUP;
499         int i;
501         find_conflict(&conflict);
503         /*
504          * MERGE_RR records paths with conflicts immediately after merge
505          * failed.  Some of the conflicted paths might have been hand resolved
506          * in the working tree since then, but the initial run would catch all
507          * and register their preimages.
508          */
510         for (i = 0; i < conflict.nr; i++) {
511                 const char *path = conflict.items[i].string;
512                 if (!string_list_has_string(rr, path)) {
513                         unsigned char sha1[20];
514                         char *hex;
515                         int ret;
516                         ret = handle_file(path, sha1, NULL);
517                         if (ret < 1)
518                                 continue;
519                         hex = xstrdup(sha1_to_hex(sha1));
520                         string_list_insert(rr, path)->util = hex;
521                         if (mkdir(git_path("rr-cache/%s", hex), 0755))
522                                 continue;
523                         handle_file(path, NULL, rerere_path(hex, "preimage"));
524                         fprintf(stderr, "Recorded preimage for '%s'\n", path);
525                 }
526         }
528         /*
529          * Now some of the paths that had conflicts earlier might have been
530          * hand resolved.  Others may be similar to a conflict already that
531          * was resolved before.
532          */
534         for (i = 0; i < rr->nr; i++) {
535                 int ret;
536                 const char *path = rr->items[i].string;
537                 const char *name = (const char *)rr->items[i].util;
539                 if (has_rerere_resolution(name)) {
540                         if (!merge(name, path)) {
541                                 if (rerere_autoupdate)
542                                         string_list_insert(&update, path);
543                                 fprintf(stderr,
544                                         "%s '%s' using previous resolution.\n",
545                                         rerere_autoupdate
546                                         ? "Staged" : "Resolved",
547                                         path);
548                                 goto mark_resolved;
549                         }
550                 }
552                 /* Let's see if we have resolved it. */
553                 ret = handle_file(path, NULL, NULL);
554                 if (ret)
555                         continue;
557                 fprintf(stderr, "Recorded resolution for '%s'.\n", path);
558                 copy_file(rerere_path(name, "postimage"), path, 0666);
559         mark_resolved:
560                 rr->items[i].util = NULL;
561         }
563         if (update.nr)
564                 update_paths(&update);
566         return write_rr(rr, fd);
569 static int git_rerere_config(const char *var, const char *value, void *cb)
571         if (!strcmp(var, "rerere.enabled"))
572                 rerere_enabled = git_config_bool(var, value);
573         else if (!strcmp(var, "rerere.autoupdate"))
574                 rerere_autoupdate = git_config_bool(var, value);
575         else
576                 return git_default_config(var, value, cb);
577         return 0;
580 static int is_rerere_enabled(void)
582         const char *rr_cache;
583         int rr_cache_exists;
585         if (!rerere_enabled)
586                 return 0;
588         rr_cache = git_path("rr-cache");
589         rr_cache_exists = is_directory(rr_cache);
590         if (rerere_enabled < 0)
591                 return rr_cache_exists;
593         if (!rr_cache_exists && mkdir_in_gitdir(rr_cache))
594                 die("Could not create directory %s", rr_cache);
595         return 1;
598 int setup_rerere(struct string_list *merge_rr, int flags)
600         int fd;
602         git_config(git_rerere_config, NULL);
603         if (!is_rerere_enabled())
604                 return -1;
606         if (flags & (RERERE_AUTOUPDATE|RERERE_NOAUTOUPDATE))
607                 rerere_autoupdate = !!(flags & RERERE_AUTOUPDATE);
608         merge_rr_path = git_pathdup("MERGE_RR");
609         fd = hold_lock_file_for_update(&write_lock, merge_rr_path,
610                                        LOCK_DIE_ON_ERROR);
611         read_rr(merge_rr);
612         return fd;
615 int rerere(int flags)
617         struct string_list merge_rr = STRING_LIST_INIT_DUP;
618         int fd;
620         fd = setup_rerere(&merge_rr, flags);
621         if (fd < 0)
622                 return 0;
623         return do_plain_rerere(&merge_rr, fd);
626 static int rerere_forget_one_path(const char *path, struct string_list *rr)
628         const char *filename;
629         char *hex;
630         unsigned char sha1[20];
631         int ret;
633         ret = handle_cache(path, sha1, NULL);
634         if (ret < 1)
635                 return error("Could not parse conflict hunks in '%s'", path);
636         hex = xstrdup(sha1_to_hex(sha1));
637         filename = rerere_path(hex, "postimage");
638         if (unlink(filename))
639                 return (errno == ENOENT
640                         ? error("no remembered resolution for %s", path)
641                         : error("cannot unlink %s: %s", filename, strerror(errno)));
643         handle_cache(path, sha1, rerere_path(hex, "preimage"));
644         fprintf(stderr, "Updated preimage for '%s'\n", path);
647         string_list_insert(rr, path)->util = hex;
648         fprintf(stderr, "Forgot resolution for %s\n", path);
649         return 0;
652 int rerere_forget(const char **pathspec)
654         int i, fd;
655         struct string_list conflict = STRING_LIST_INIT_DUP;
656         struct string_list merge_rr = STRING_LIST_INIT_DUP;
658         if (read_cache() < 0)
659                 return error("Could not read index");
661         fd = setup_rerere(&merge_rr, RERERE_NOAUTOUPDATE);
663         unmerge_cache(pathspec);
664         find_conflict(&conflict);
665         for (i = 0; i < conflict.nr; i++) {
666                 struct string_list_item *it = &conflict.items[i];
667                 if (!match_pathspec(pathspec, it->string, strlen(it->string),
668                                     0, NULL))
669                         continue;
670                 rerere_forget_one_path(it->string, &merge_rr);
671         }
672         return write_rr(&merge_rr, fd);