Move MERGE_RR from .git/rr-cache/ into .git/
[git/kirr.git] / builtin-rerere.c
blob1db2e0c8fbc90c81de7a7a7fc5beca7316c5b2d7
1 #include "builtin.h"
2 #include "cache.h"
3 #include "path-list.h"
4 #include "xdiff/xdiff.h"
5 #include "xdiff-interface.h"
7 #include <time.h>
9 static const char git_rerere_usage[] =
10 "git-rerere [clear | status | diff | gc]";
12 /* these values are days */
13 static int cutoff_noresolve = 15;
14 static int cutoff_resolve = 60;
16 /* if rerere_enabled == -1, fall back to detection of .git/rr-cache */
17 static int rerere_enabled = -1;
19 /* automatically update cleanly resolved paths to the index */
20 static int rerere_autoupdate;
22 static char *merge_rr_path;
24 static const char *rr_path(const char *name, const char *file)
26 return git_path("rr-cache/%s/%s", name, file);
29 static time_t rerere_created_at(const char *name)
31 struct stat st;
32 return stat(rr_path(name, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
35 static int has_resolution(const char *name)
37 struct stat st;
38 return !stat(rr_path(name, "postimage"), &st);
41 static void read_rr(struct path_list *rr)
43 unsigned char sha1[20];
44 char buf[PATH_MAX];
45 FILE *in = fopen(merge_rr_path, "r");
46 if (!in)
47 return;
48 while (fread(buf, 40, 1, in) == 1) {
49 int i;
50 char *name;
51 if (get_sha1_hex(buf, sha1))
52 die("corrupt MERGE_RR");
53 buf[40] = '\0';
54 name = xstrdup(buf);
55 if (fgetc(in) != '\t')
56 die("corrupt MERGE_RR");
57 for (i = 0; i < sizeof(buf) && (buf[i] = fgetc(in)); i++)
58 ; /* do nothing */
59 if (i == sizeof(buf))
60 die("filename too long");
61 path_list_insert(buf, rr)->util = name;
63 fclose(in);
66 static struct lock_file write_lock;
68 static int write_rr(struct path_list *rr, int out_fd)
70 int i;
71 for (i = 0; i < rr->nr; i++) {
72 const char *path;
73 int length;
74 if (!rr->items[i].util)
75 continue;
76 path = rr->items[i].path;
77 length = strlen(path) + 1;
78 if (write_in_full(out_fd, rr->items[i].util, 40) != 40 ||
79 write_in_full(out_fd, "\t", 1) != 1 ||
80 write_in_full(out_fd, path, length) != length)
81 die("unable to write rerere record");
83 if (commit_lock_file(&write_lock) != 0)
84 die("unable to write rerere record");
85 return 0;
88 static int handle_file(const char *path,
89 unsigned char *sha1, const char *output)
91 SHA_CTX ctx;
92 char buf[1024];
93 int hunk = 0, hunk_no = 0;
94 struct strbuf one, two;
95 FILE *f = fopen(path, "r");
96 FILE *out = NULL;
98 if (!f)
99 return error("Could not open %s", path);
101 if (output) {
102 out = fopen(output, "w");
103 if (!out) {
104 fclose(f);
105 return error("Could not write %s", output);
109 if (sha1)
110 SHA1_Init(&ctx);
112 strbuf_init(&one, 0);
113 strbuf_init(&two, 0);
114 while (fgets(buf, sizeof(buf), f)) {
115 if (!prefixcmp(buf, "<<<<<<< ")) {
116 if (hunk)
117 goto bad;
118 hunk = 1;
119 } else if (!prefixcmp(buf, "=======") && isspace(buf[7])) {
120 if (hunk != 1)
121 goto bad;
122 hunk = 2;
123 } else if (!prefixcmp(buf, ">>>>>>> ")) {
124 if (hunk != 2)
125 goto bad;
126 if (strbuf_cmp(&one, &two) > 0)
127 strbuf_swap(&one, &two);
128 hunk_no++;
129 hunk = 0;
130 if (out) {
131 fputs("<<<<<<<\n", out);
132 fwrite(one.buf, one.len, 1, out);
133 fputs("=======\n", out);
134 fwrite(two.buf, two.len, 1, out);
135 fputs(">>>>>>>\n", out);
137 if (sha1) {
138 SHA1_Update(&ctx, one.buf ? one.buf : "",
139 one.len + 1);
140 SHA1_Update(&ctx, two.buf ? two.buf : "",
141 two.len + 1);
143 strbuf_reset(&one);
144 strbuf_reset(&two);
145 } else if (hunk == 1)
146 strbuf_addstr(&one, buf);
147 else if (hunk == 2)
148 strbuf_addstr(&two, buf);
149 else if (out)
150 fputs(buf, out);
151 continue;
152 bad:
153 hunk = 99; /* force error exit */
154 break;
156 strbuf_release(&one);
157 strbuf_release(&two);
159 fclose(f);
160 if (out)
161 fclose(out);
162 if (sha1)
163 SHA1_Final(sha1, &ctx);
164 if (hunk) {
165 if (output)
166 unlink(output);
167 return error("Could not parse conflict hunks in %s", path);
169 return hunk_no;
172 static int find_conflict(struct path_list *conflict)
174 int i;
175 if (read_cache() < 0)
176 return error("Could not read index");
177 for (i = 0; i+1 < active_nr; i++) {
178 struct cache_entry *e2 = active_cache[i];
179 struct cache_entry *e3 = active_cache[i+1];
180 if (ce_stage(e2) == 2 &&
181 ce_stage(e3) == 3 &&
182 ce_same_name(e2, e3) &&
183 S_ISREG(e2->ce_mode) &&
184 S_ISREG(e3->ce_mode)) {
185 path_list_insert((const char *)e2->name, conflict);
186 i++; /* skip over both #2 and #3 */
189 return 0;
192 static int merge(const char *name, const char *path)
194 int ret;
195 mmfile_t cur, base, other;
196 mmbuffer_t result = {NULL, 0};
197 xpparam_t xpp = {XDF_NEED_MINIMAL};
199 if (handle_file(path, NULL, rr_path(name, "thisimage")) < 0)
200 return 1;
202 if (read_mmfile(&cur, rr_path(name, "thisimage")) ||
203 read_mmfile(&base, rr_path(name, "preimage")) ||
204 read_mmfile(&other, rr_path(name, "postimage")))
205 return 1;
206 ret = xdl_merge(&base, &cur, "", &other, "",
207 &xpp, XDL_MERGE_ZEALOUS, &result);
208 if (!ret) {
209 FILE *f = fopen(path, "w");
210 if (!f)
211 return error("Could not write to %s", path);
212 fwrite(result.ptr, result.size, 1, f);
213 fclose(f);
216 free(cur.ptr);
217 free(base.ptr);
218 free(other.ptr);
219 free(result.ptr);
221 return ret;
224 static void unlink_rr_item(const char *name)
226 unlink(rr_path(name, "thisimage"));
227 unlink(rr_path(name, "preimage"));
228 unlink(rr_path(name, "postimage"));
229 rmdir(git_path("rr-cache/%s", name));
232 static void garbage_collect(struct path_list *rr)
234 struct path_list to_remove = { NULL, 0, 0, 1 };
235 DIR *dir;
236 struct dirent *e;
237 int i, cutoff;
238 time_t now = time(NULL), then;
240 dir = opendir(git_path("rr-cache"));
241 while ((e = readdir(dir))) {
242 const char *name = e->d_name;
243 if (name[0] == '.' &&
244 (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')))
245 continue;
246 then = rerere_created_at(name);
247 if (!then)
248 continue;
249 cutoff = (has_resolution(name)
250 ? cutoff_resolve : cutoff_noresolve);
251 if (then < now - cutoff * 86400)
252 path_list_append(name, &to_remove);
254 for (i = 0; i < to_remove.nr; i++)
255 unlink_rr_item(to_remove.items[i].path);
256 path_list_clear(&to_remove, 0);
259 static int outf(void *dummy, mmbuffer_t *ptr, int nbuf)
261 int i;
262 for (i = 0; i < nbuf; i++)
263 if (write_in_full(1, ptr[i].ptr, ptr[i].size) != ptr[i].size)
264 return -1;
265 return 0;
268 static int diff_two(const char *file1, const char *label1,
269 const char *file2, const char *label2)
271 xpparam_t xpp;
272 xdemitconf_t xecfg;
273 xdemitcb_t ecb;
274 mmfile_t minus, plus;
276 if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2))
277 return 1;
279 printf("--- a/%s\n+++ b/%s\n", label1, label2);
280 fflush(stdout);
281 xpp.flags = XDF_NEED_MINIMAL;
282 memset(&xecfg, 0, sizeof(xecfg));
283 xecfg.ctxlen = 3;
284 ecb.outf = outf;
285 xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb);
287 free(minus.ptr);
288 free(plus.ptr);
289 return 0;
292 static struct lock_file index_lock;
294 static int update_paths(struct path_list *update)
296 int i;
297 int fd = hold_locked_index(&index_lock, 0);
298 int status = 0;
300 if (fd < 0)
301 return -1;
303 for (i = 0; i < update->nr; i++) {
304 struct path_list_item *item = &update->items[i];
305 if (add_file_to_cache(item->path, ADD_CACHE_IGNORE_ERRORS))
306 status = -1;
309 if (!status && active_cache_changed) {
310 if (write_cache(fd, active_cache, active_nr) ||
311 commit_locked_index(&index_lock))
312 die("Unable to write new index file");
313 } else if (fd >= 0)
314 rollback_lock_file(&index_lock);
315 return status;
318 static int do_plain_rerere(struct path_list *rr, int fd)
320 struct path_list conflict = { NULL, 0, 0, 1 };
321 struct path_list update = { NULL, 0, 0, 1 };
322 int i;
324 find_conflict(&conflict);
327 * MERGE_RR records paths with conflicts immediately after merge
328 * failed. Some of the conflicted paths might have been hand resolved
329 * in the working tree since then, but the initial run would catch all
330 * and register their preimages.
333 for (i = 0; i < conflict.nr; i++) {
334 const char *path = conflict.items[i].path;
335 if (!path_list_has_path(rr, path)) {
336 unsigned char sha1[20];
337 char *hex;
338 int ret;
339 ret = handle_file(path, sha1, NULL);
340 if (ret < 1)
341 continue;
342 hex = xstrdup(sha1_to_hex(sha1));
343 path_list_insert(path, rr)->util = hex;
344 if (mkdir(git_path("rr-cache/%s", hex), 0755))
345 continue;;
346 handle_file(path, NULL, rr_path(hex, "preimage"));
347 fprintf(stderr, "Recorded preimage for '%s'\n", path);
352 * Now some of the paths that had conflicts earlier might have been
353 * hand resolved. Others may be similar to a conflict already that
354 * was resolved before.
357 for (i = 0; i < rr->nr; i++) {
358 int ret;
359 const char *path = rr->items[i].path;
360 const char *name = (const char *)rr->items[i].util;
362 if (has_resolution(name)) {
363 if (!merge(name, path)) {
364 fprintf(stderr, "Resolved '%s' using "
365 "previous resolution.\n", path);
366 if (rerere_autoupdate)
367 path_list_insert(path, &update);
368 goto mark_resolved;
372 /* Let's see if we have resolved it. */
373 ret = handle_file(path, NULL, NULL);
374 if (ret)
375 continue;
377 fprintf(stderr, "Recorded resolution for '%s'.\n", path);
378 copy_file(rr_path(name, "postimage"), path, 0666);
379 mark_resolved:
380 rr->items[i].util = NULL;
383 if (update.nr)
384 update_paths(&update);
386 return write_rr(rr, fd);
389 static int git_rerere_config(const char *var, const char *value, void *cb)
391 if (!strcmp(var, "gc.rerereresolved"))
392 cutoff_resolve = git_config_int(var, value);
393 else if (!strcmp(var, "gc.rerereunresolved"))
394 cutoff_noresolve = git_config_int(var, value);
395 else if (!strcmp(var, "rerere.enabled"))
396 rerere_enabled = git_config_bool(var, value);
397 else if (!strcmp(var, "rerere.autoupdate"))
398 rerere_autoupdate = git_config_bool(var, value);
399 else
400 return git_default_config(var, value, cb);
401 return 0;
404 static int is_rerere_enabled(void)
406 struct stat st;
407 const char *rr_cache;
408 int rr_cache_exists;
410 if (!rerere_enabled)
411 return 0;
413 rr_cache = git_path("rr-cache");
414 rr_cache_exists = !stat(rr_cache, &st) && S_ISDIR(st.st_mode);
415 if (rerere_enabled < 0)
416 return rr_cache_exists;
418 if (!rr_cache_exists &&
419 (mkdir(rr_cache, 0777) || adjust_shared_perm(rr_cache)))
420 die("Could not create directory %s", rr_cache);
421 return 1;
424 static int setup_rerere(struct path_list *merge_rr)
426 int fd;
428 git_config(git_rerere_config, NULL);
429 if (!is_rerere_enabled())
430 return -1;
432 merge_rr_path = xstrdup(git_path("MERGE_RR"));
433 fd = hold_lock_file_for_update(&write_lock, merge_rr_path, 1);
434 read_rr(merge_rr);
435 return fd;
438 int rerere(void)
440 struct path_list merge_rr = { NULL, 0, 0, 1 };
441 int fd;
443 fd = setup_rerere(&merge_rr);
444 if (fd < 0)
445 return 0;
446 return do_plain_rerere(&merge_rr, fd);
449 int cmd_rerere(int argc, const char **argv, const char *prefix)
451 struct path_list merge_rr = { NULL, 0, 0, 1 };
452 int i, fd;
454 fd = setup_rerere(&merge_rr);
455 if (fd < 0)
456 return 0;
458 if (argc < 2)
459 return do_plain_rerere(&merge_rr, fd);
460 else if (!strcmp(argv[1], "clear")) {
461 for (i = 0; i < merge_rr.nr; i++) {
462 const char *name = (const char *)merge_rr.items[i].util;
463 if (!has_resolution(name))
464 unlink_rr_item(name);
466 unlink(merge_rr_path);
467 } else if (!strcmp(argv[1], "gc"))
468 garbage_collect(&merge_rr);
469 else if (!strcmp(argv[1], "status"))
470 for (i = 0; i < merge_rr.nr; i++)
471 printf("%s\n", merge_rr.items[i].path);
472 else if (!strcmp(argv[1], "diff"))
473 for (i = 0; i < merge_rr.nr; i++) {
474 const char *path = merge_rr.items[i].path;
475 const char *name = (const char *)merge_rr.items[i].util;
476 diff_two(rr_path(name, "preimage"), path, path, path);
478 else
479 usage(git_rerere_usage);
481 path_list_clear(&merge_rr, 1);
482 return 0;