Merge branch 'jc/cache-unmerge'
[git/dscho.git] / rerere.c
blob70d0f7afffe983e6c70ad8494b095c1292393745
1 #include "cache.h"
2 #include "string-list.h"
3 #include "rerere.h"
4 #include "xdiff/xdiff.h"
5 #include "xdiff-interface.h"
6 #include "dir.h"
7 #include "resolve-undo.h"
8 #include "ll-merge.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) && (buf[i] = fgetc(in)); i++)
46 ; /* do nothing */
47 if (i == sizeof(buf))
48 die("filename too long");
49 string_list_insert(buf, rr)->util = name;
51 fclose(in);
54 static struct lock_file write_lock;
56 static int write_rr(struct string_list *rr, int out_fd)
58 int i;
59 for (i = 0; i < rr->nr; i++) {
60 const char *path;
61 int length;
62 if (!rr->items[i].util)
63 continue;
64 path = rr->items[i].string;
65 length = strlen(path) + 1;
66 if (write_in_full(out_fd, rr->items[i].util, 40) != 40 ||
67 write_str_in_full(out_fd, "\t") != 1 ||
68 write_in_full(out_fd, path, length) != length)
69 die("unable to write rerere record");
71 if (commit_lock_file(&write_lock) != 0)
72 die("unable to write rerere record");
73 return 0;
76 static void ferr_write(const void *p, size_t count, FILE *fp, int *err)
78 if (!count || *err)
79 return;
80 if (fwrite(p, count, 1, fp) != 1)
81 *err = errno;
84 static inline void ferr_puts(const char *s, FILE *fp, int *err)
86 ferr_write(s, strlen(s), fp, err);
89 struct rerere_io {
90 int (*getline)(struct strbuf *, struct rerere_io *);
91 FILE *output;
92 int wrerror;
93 /* some more stuff */
96 static void rerere_io_putstr(const char *str, struct rerere_io *io)
98 if (io->output)
99 ferr_puts(str, io->output, &io->wrerror);
102 static void rerere_io_putmem(const char *mem, size_t sz, struct rerere_io *io)
104 if (io->output)
105 ferr_write(mem, sz, io->output, &io->wrerror);
108 struct rerere_io_file {
109 struct rerere_io io;
110 FILE *input;
113 static int rerere_file_getline(struct strbuf *sb, struct rerere_io *io_)
115 struct rerere_io_file *io = (struct rerere_io_file *)io_;
116 return strbuf_getwholeline(sb, io->input, '\n');
119 static int handle_path(unsigned char *sha1, struct rerere_io *io)
121 git_SHA_CTX ctx;
122 int hunk_no = 0;
123 enum {
124 RR_CONTEXT = 0, RR_SIDE_1, RR_SIDE_2, RR_ORIGINAL,
125 } hunk = RR_CONTEXT;
126 struct strbuf one = STRBUF_INIT, two = STRBUF_INIT;
127 struct strbuf buf = STRBUF_INIT;
129 if (sha1)
130 git_SHA1_Init(&ctx);
132 while (!io->getline(&buf, io)) {
133 if (!prefixcmp(buf.buf, "<<<<<<< ")) {
134 if (hunk != RR_CONTEXT)
135 goto bad;
136 hunk = RR_SIDE_1;
137 } else if (!prefixcmp(buf.buf, "|||||||") && isspace(buf.buf[7])) {
138 if (hunk != RR_SIDE_1)
139 goto bad;
140 hunk = RR_ORIGINAL;
141 } else if (!prefixcmp(buf.buf, "=======") && isspace(buf.buf[7])) {
142 if (hunk != RR_SIDE_1 && hunk != RR_ORIGINAL)
143 goto bad;
144 hunk = RR_SIDE_2;
145 } else if (!prefixcmp(buf.buf, ">>>>>>> ")) {
146 if (hunk != RR_SIDE_2)
147 goto bad;
148 if (strbuf_cmp(&one, &two) > 0)
149 strbuf_swap(&one, &two);
150 hunk_no++;
151 hunk = RR_CONTEXT;
152 rerere_io_putstr("<<<<<<<\n", io);
153 rerere_io_putmem(one.buf, one.len, io);
154 rerere_io_putstr("=======\n", io);
155 rerere_io_putmem(two.buf, two.len, io);
156 rerere_io_putstr(">>>>>>>\n", io);
157 if (sha1) {
158 git_SHA1_Update(&ctx, one.buf ? one.buf : "",
159 one.len + 1);
160 git_SHA1_Update(&ctx, two.buf ? two.buf : "",
161 two.len + 1);
163 strbuf_reset(&one);
164 strbuf_reset(&two);
165 } else if (hunk == RR_SIDE_1)
166 strbuf_addstr(&one, buf.buf);
167 else if (hunk == RR_ORIGINAL)
168 ; /* discard */
169 else if (hunk == RR_SIDE_2)
170 strbuf_addstr(&two, buf.buf);
171 else
172 rerere_io_putstr(buf.buf, io);
173 continue;
174 bad:
175 hunk = 99; /* force error exit */
176 break;
178 strbuf_release(&one);
179 strbuf_release(&two);
180 strbuf_release(&buf);
182 if (sha1)
183 git_SHA1_Final(sha1, &ctx);
184 if (hunk != RR_CONTEXT)
185 return -1;
186 return hunk_no;
189 static int handle_file(const char *path, unsigned char *sha1, const char *output)
191 int hunk_no = 0;
192 struct rerere_io_file io;
194 memset(&io, 0, sizeof(io));
195 io.io.getline = rerere_file_getline;
196 io.input = fopen(path, "r");
197 io.io.wrerror = 0;
198 if (!io.input)
199 return error("Could not open %s", path);
201 if (output) {
202 io.io.output = fopen(output, "w");
203 if (!io.io.output) {
204 fclose(io.input);
205 return error("Could not write %s", output);
209 hunk_no = handle_path(sha1, (struct rerere_io *)&io);
211 fclose(io.input);
212 if (io.io.wrerror)
213 error("There were errors while writing %s (%s)",
214 path, strerror(io.io.wrerror));
215 if (io.io.output && fclose(io.io.output))
216 io.io.wrerror = error("Failed to flush %s: %s",
217 path, strerror(errno));
219 if (hunk_no < 0) {
220 if (output)
221 unlink_or_warn(output);
222 return error("Could not parse conflict hunks in %s", path);
224 if (io.io.wrerror)
225 return -1;
226 return hunk_no;
229 struct rerere_io_mem {
230 struct rerere_io io;
231 struct strbuf input;
234 static int rerere_mem_getline(struct strbuf *sb, struct rerere_io *io_)
236 struct rerere_io_mem *io = (struct rerere_io_mem *)io_;
237 char *ep;
238 size_t len;
240 strbuf_release(sb);
241 if (!io->input.len)
242 return -1;
243 ep = strchrnul(io->input.buf, '\n');
244 if (*ep == '\n')
245 ep++;
246 len = ep - io->input.buf;
247 strbuf_add(sb, io->input.buf, len);
248 strbuf_remove(&io->input, 0, len);
249 return 0;
252 static int handle_cache(const char *path, unsigned char *sha1, const char *output)
254 mmfile_t mmfile[3];
255 mmbuffer_t result = {NULL, 0};
256 struct cache_entry *ce;
257 int pos, len, i, hunk_no;
258 struct rerere_io_mem io;
261 * Reproduce the conflicted merge in-core
263 len = strlen(path);
264 pos = cache_name_pos(path, len);
265 if (0 <= pos)
266 return -1;
267 pos = -pos - 1;
269 for (i = 0; i < 3; i++) {
270 enum object_type type;
271 unsigned long size;
273 mmfile[i].size = 0;
274 mmfile[i].ptr = NULL;
275 if (active_nr <= pos)
276 break;
277 ce = active_cache[pos++];
278 if (ce_namelen(ce) != len || memcmp(ce->name, path, len)
279 || ce_stage(ce) != i + 1)
280 break;
281 mmfile[i].ptr = read_sha1_file(ce->sha1, &type, &size);
282 mmfile[i].size = size;
284 for (i = 0; i < 3; i++) {
285 if (!mmfile[i].ptr && !mmfile[i].size)
286 mmfile[i].ptr = xstrdup("");
288 ll_merge(&result, path, &mmfile[0],
289 &mmfile[1], "ours",
290 &mmfile[2], "theirs", 0);
291 for (i = 0; i < 3; i++)
292 free(mmfile[i].ptr);
294 memset(&io, 0, sizeof(&io));
295 io.io.getline = rerere_mem_getline;
296 if (output)
297 io.io.output = fopen(output, "w");
298 else
299 io.io.output = NULL;
300 strbuf_init(&io.input, 0);
301 strbuf_attach(&io.input, result.ptr, result.size, result.size);
303 hunk_no = handle_path(sha1, (struct rerere_io *)&io);
304 strbuf_release(&io.input);
305 if (io.io.output)
306 fclose(io.io.output);
307 return hunk_no;
310 static int find_conflict(struct string_list *conflict)
312 int i;
313 if (read_cache() < 0)
314 return error("Could not read index");
315 for (i = 0; i+1 < active_nr; i++) {
316 struct cache_entry *e2 = active_cache[i];
317 struct cache_entry *e3 = active_cache[i+1];
318 if (ce_stage(e2) == 2 &&
319 ce_stage(e3) == 3 &&
320 ce_same_name(e2, e3) &&
321 S_ISREG(e2->ce_mode) &&
322 S_ISREG(e3->ce_mode)) {
323 string_list_insert((const char *)e2->name, conflict);
324 i++; /* skip over both #2 and #3 */
327 return 0;
330 static int merge(const char *name, const char *path)
332 int ret;
333 mmfile_t cur, base, other;
334 mmbuffer_t result = {NULL, 0};
335 xpparam_t xpp = {XDF_NEED_MINIMAL};
337 if (handle_file(path, NULL, rerere_path(name, "thisimage")) < 0)
338 return 1;
340 if (read_mmfile(&cur, rerere_path(name, "thisimage")) ||
341 read_mmfile(&base, rerere_path(name, "preimage")) ||
342 read_mmfile(&other, rerere_path(name, "postimage")))
343 return 1;
344 ret = xdl_merge(&base, &cur, "", &other, "",
345 &xpp, XDL_MERGE_ZEALOUS, &result);
346 if (!ret) {
347 FILE *f = fopen(path, "w");
348 if (!f)
349 return error("Could not open %s: %s", path,
350 strerror(errno));
351 if (fwrite(result.ptr, result.size, 1, f) != 1)
352 error("Could not write %s: %s", path, strerror(errno));
353 if (fclose(f))
354 return error("Writing %s failed: %s", path,
355 strerror(errno));
358 free(cur.ptr);
359 free(base.ptr);
360 free(other.ptr);
361 free(result.ptr);
363 return ret;
366 static struct lock_file index_lock;
368 static int update_paths(struct string_list *update)
370 int i;
371 int fd = hold_locked_index(&index_lock, 0);
372 int status = 0;
374 if (fd < 0)
375 return -1;
377 for (i = 0; i < update->nr; i++) {
378 struct string_list_item *item = &update->items[i];
379 if (add_file_to_cache(item->string, ADD_CACHE_IGNORE_ERRORS))
380 status = -1;
383 if (!status && active_cache_changed) {
384 if (write_cache(fd, active_cache, active_nr) ||
385 commit_locked_index(&index_lock))
386 die("Unable to write new index file");
387 } else if (fd >= 0)
388 rollback_lock_file(&index_lock);
389 return status;
392 static int do_plain_rerere(struct string_list *rr, int fd)
394 struct string_list conflict = { NULL, 0, 0, 1 };
395 struct string_list update = { NULL, 0, 0, 1 };
396 int i;
398 find_conflict(&conflict);
401 * MERGE_RR records paths with conflicts immediately after merge
402 * failed. Some of the conflicted paths might have been hand resolved
403 * in the working tree since then, but the initial run would catch all
404 * and register their preimages.
407 for (i = 0; i < conflict.nr; i++) {
408 const char *path = conflict.items[i].string;
409 if (!string_list_has_string(rr, path)) {
410 unsigned char sha1[20];
411 char *hex;
412 int ret;
413 ret = handle_file(path, sha1, NULL);
414 if (ret < 1)
415 continue;
416 hex = xstrdup(sha1_to_hex(sha1));
417 string_list_insert(path, rr)->util = hex;
418 if (mkdir(git_path("rr-cache/%s", hex), 0755))
419 continue;
420 handle_file(path, NULL, rerere_path(hex, "preimage"));
421 fprintf(stderr, "Recorded preimage for '%s'\n", path);
426 * Now some of the paths that had conflicts earlier might have been
427 * hand resolved. Others may be similar to a conflict already that
428 * was resolved before.
431 for (i = 0; i < rr->nr; i++) {
432 int ret;
433 const char *path = rr->items[i].string;
434 const char *name = (const char *)rr->items[i].util;
436 if (has_rerere_resolution(name)) {
437 if (!merge(name, path)) {
438 if (rerere_autoupdate)
439 string_list_insert(path, &update);
440 fprintf(stderr,
441 "%s '%s' using previous resolution.\n",
442 rerere_autoupdate
443 ? "Staged" : "Resolved",
444 path);
445 goto mark_resolved;
449 /* Let's see if we have resolved it. */
450 ret = handle_file(path, NULL, NULL);
451 if (ret)
452 continue;
454 fprintf(stderr, "Recorded resolution for '%s'.\n", path);
455 copy_file(rerere_path(name, "postimage"), path, 0666);
456 mark_resolved:
457 rr->items[i].util = NULL;
460 if (update.nr)
461 update_paths(&update);
463 return write_rr(rr, fd);
466 static int git_rerere_config(const char *var, const char *value, void *cb)
468 if (!strcmp(var, "rerere.enabled"))
469 rerere_enabled = git_config_bool(var, value);
470 else if (!strcmp(var, "rerere.autoupdate"))
471 rerere_autoupdate = git_config_bool(var, value);
472 else
473 return git_default_config(var, value, cb);
474 return 0;
477 static int is_rerere_enabled(void)
479 const char *rr_cache;
480 int rr_cache_exists;
482 if (!rerere_enabled)
483 return 0;
485 rr_cache = git_path("rr-cache");
486 rr_cache_exists = is_directory(rr_cache);
487 if (rerere_enabled < 0)
488 return rr_cache_exists;
490 if (!rr_cache_exists &&
491 (mkdir(rr_cache, 0777) || adjust_shared_perm(rr_cache)))
492 die("Could not create directory %s", rr_cache);
493 return 1;
496 int setup_rerere(struct string_list *merge_rr, int flags)
498 int fd;
500 git_config(git_rerere_config, NULL);
501 if (!is_rerere_enabled())
502 return -1;
504 if (flags & (RERERE_AUTOUPDATE|RERERE_NOAUTOUPDATE))
505 rerere_autoupdate = !!(flags & RERERE_AUTOUPDATE);
506 merge_rr_path = git_pathdup("MERGE_RR");
507 fd = hold_lock_file_for_update(&write_lock, merge_rr_path,
508 LOCK_DIE_ON_ERROR);
509 read_rr(merge_rr);
510 return fd;
513 int rerere(int flags)
515 struct string_list merge_rr = { NULL, 0, 0, 1 };
516 int fd;
518 fd = setup_rerere(&merge_rr, flags);
519 if (fd < 0)
520 return 0;
521 return do_plain_rerere(&merge_rr, fd);
524 static int rerere_forget_one_path(const char *path, struct string_list *rr)
526 const char *filename;
527 char *hex;
528 unsigned char sha1[20];
529 int ret;
531 ret = handle_cache(path, sha1, NULL);
532 if (ret < 1)
533 return error("Could not parse conflict hunks in '%s'", path);
534 hex = xstrdup(sha1_to_hex(sha1));
535 filename = rerere_path(hex, "postimage");
536 if (unlink(filename))
537 return (errno == ENOENT
538 ? error("no remembered resolution for %s", path)
539 : error("cannot unlink %s: %s", filename, strerror(errno)));
541 handle_cache(path, sha1, rerere_path(hex, "preimage"));
542 fprintf(stderr, "Updated preimage for '%s'\n", path);
545 string_list_insert(path, rr)->util = hex;
546 fprintf(stderr, "Forgot resolution for %s\n", path);
547 return 0;
550 int rerere_forget(const char **pathspec)
552 int i, fd;
553 struct string_list conflict = { NULL, 0, 0, 1 };
554 struct string_list merge_rr = { NULL, 0, 0, 1 };
556 if (read_cache() < 0)
557 return error("Could not read index");
559 fd = setup_rerere(&merge_rr, RERERE_NOAUTOUPDATE);
561 unmerge_cache(pathspec);
562 find_conflict(&conflict);
563 for (i = 0; i < conflict.nr; i++) {
564 struct string_list_item *it = &conflict.items[i];
565 if (!match_pathspec(pathspec, it->string, strlen(it->string),
566 0, NULL))
567 continue;
568 rerere_forget_one_path(it->string, &merge_rr);
570 return write_rr(&merge_rr, fd);