4 #include "xdiff/xdiff.h"
5 #include "xdiff-interface.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 static char *merge_rr_path;
21 static const char *rr_path(const char *name, const char *file)
23 return git_path("rr-cache/%s/%s", name, file);
26 static time_t rerere_created_at(const char *name)
29 return stat(rr_path(name, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
32 static int has_resolution(const char *name)
35 return !stat(rr_path(name, "postimage"), &st);
38 static void read_rr(struct path_list *rr)
40 unsigned char sha1[20];
42 FILE *in = fopen(merge_rr_path, "r");
45 while (fread(buf, 40, 1, in) == 1) {
48 if (get_sha1_hex(buf, sha1))
49 die("corrupt MERGE_RR");
52 if (fgetc(in) != '\t')
53 die("corrupt MERGE_RR");
54 for (i = 0; i < sizeof(buf) && (buf[i] = fgetc(in)); i++)
57 die("filename too long");
58 path_list_insert(buf, rr)->util = name;
63 static struct lock_file write_lock;
65 static int write_rr(struct path_list *rr, int out_fd)
68 for (i = 0; i < rr->nr; i++) {
69 const char *path = rr->items[i].path;
70 int length = strlen(path) + 1;
71 if (write_in_full(out_fd, rr->items[i].util, 40) != 40 ||
72 write_in_full(out_fd, "\t", 1) != 1 ||
73 write_in_full(out_fd, path, length) != length)
74 die("unable to write rerere record");
76 if (commit_lock_file(&write_lock) != 0)
77 die("unable to write rerere record");
81 static int handle_file(const char *path,
82 unsigned char *sha1, const char *output)
86 int hunk = 0, hunk_no = 0;
87 struct strbuf one, two;
88 FILE *f = fopen(path, "r");
92 return error("Could not open %s", path);
95 out = fopen(output, "w");
98 return error("Could not write %s", output);
105 strbuf_init(&one, 0);
106 strbuf_init(&two, 0);
107 while (fgets(buf, sizeof(buf), f)) {
108 if (!prefixcmp(buf, "<<<<<<< "))
110 else if (!prefixcmp(buf, "======="))
112 else if (!prefixcmp(buf, ">>>>>>> ")) {
113 if (strbuf_cmp(&one, &two) > 0)
114 strbuf_swap(&one, &two);
118 fputs("<<<<<<<\n", out);
119 fwrite(one.buf, one.len, 1, out);
120 fputs("=======\n", out);
121 fwrite(two.buf, two.len, 1, out);
122 fputs(">>>>>>>\n", out);
125 SHA1_Update(&ctx, one.buf ? one.buf : "",
127 SHA1_Update(&ctx, two.buf ? two.buf : "",
132 } else if (hunk == 1)
133 strbuf_addstr(&one, buf);
135 strbuf_addstr(&two, buf);
139 strbuf_release(&one);
140 strbuf_release(&two);
146 SHA1_Final(sha1, &ctx);
150 static int find_conflict(struct path_list *conflict)
153 if (read_cache() < 0)
154 return error("Could not read index");
155 for (i = 0; i+1 < active_nr; i++) {
156 struct cache_entry *e2 = active_cache[i];
157 struct cache_entry *e3 = active_cache[i+1];
158 if (ce_stage(e2) == 2 &&
160 ce_same_name(e2, e3) &&
161 S_ISREG(e2->ce_mode) &&
162 S_ISREG(e3->ce_mode)) {
163 path_list_insert((const char *)e2->name, conflict);
164 i++; /* skip over both #2 and #3 */
170 static int merge(const char *name, const char *path)
173 mmfile_t cur, base, other;
174 mmbuffer_t result = {NULL, 0};
175 xpparam_t xpp = {XDF_NEED_MINIMAL};
177 if (handle_file(path, NULL, rr_path(name, "thisimage")) < 0)
180 if (read_mmfile(&cur, rr_path(name, "thisimage")) ||
181 read_mmfile(&base, rr_path(name, "preimage")) ||
182 read_mmfile(&other, rr_path(name, "postimage")))
184 ret = xdl_merge(&base, &cur, "", &other, "",
185 &xpp, XDL_MERGE_ZEALOUS, &result);
187 FILE *f = fopen(path, "w");
189 return error("Could not write to %s", path);
190 fwrite(result.ptr, result.size, 1, f);
202 static void unlink_rr_item(const char *name)
204 unlink(rr_path(name, "thisimage"));
205 unlink(rr_path(name, "preimage"));
206 unlink(rr_path(name, "postimage"));
207 rmdir(git_path("rr-cache/%s", name));
210 static void garbage_collect(struct path_list *rr)
212 struct path_list to_remove = { NULL, 0, 0, 1 };
216 time_t now = time(NULL), then;
218 dir = opendir(git_path("rr-cache"));
219 while ((e = readdir(dir))) {
220 const char *name = e->d_name;
221 if (name[0] == '.' &&
222 (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')))
224 then = rerere_created_at(name);
227 cutoff = (has_resolution(name)
228 ? cutoff_resolve : cutoff_noresolve);
229 if (then < now - cutoff * 86400)
230 path_list_append(name, &to_remove);
232 for (i = 0; i < to_remove.nr; i++)
233 unlink_rr_item(to_remove.items[i].path);
234 path_list_clear(&to_remove, 0);
237 static int outf(void *dummy, mmbuffer_t *ptr, int nbuf)
240 for (i = 0; i < nbuf; i++)
241 if (write_in_full(1, ptr[i].ptr, ptr[i].size) != ptr[i].size)
246 static int diff_two(const char *file1, const char *label1,
247 const char *file2, const char *label2)
252 mmfile_t minus, plus;
254 if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2))
257 printf("--- a/%s\n+++ b/%s\n", label1, label2);
259 xpp.flags = XDF_NEED_MINIMAL;
260 memset(&xecfg, 0, sizeof(xecfg));
263 xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb);
270 static int do_plain_rerere(struct path_list *rr, int fd)
272 struct path_list conflict = { NULL, 0, 0, 1 };
275 find_conflict(&conflict);
278 * MERGE_RR records paths with conflicts immediately after merge
279 * failed. Some of the conflicted paths might have been hand resolved
280 * in the working tree since then, but the initial run would catch all
281 * and register their preimages.
284 for (i = 0; i < conflict.nr; i++) {
285 const char *path = conflict.items[i].path;
286 if (!path_list_has_path(rr, path)) {
287 unsigned char sha1[20];
290 ret = handle_file(path, sha1, NULL);
293 hex = xstrdup(sha1_to_hex(sha1));
294 path_list_insert(path, rr)->util = hex;
295 if (mkdir(git_path("rr-cache/%s", hex), 0755))
297 handle_file(path, NULL, rr_path(hex, "preimage"));
298 fprintf(stderr, "Recorded preimage for '%s'\n", path);
303 * Now some of the paths that had conflicts earlier might have been
304 * hand resolved. Others may be similar to a conflict already that
305 * was resolved before.
308 for (i = 0; i < rr->nr; i++) {
310 const char *path = rr->items[i].path;
311 const char *name = (const char *)rr->items[i].util;
313 if (has_resolution(name)) {
314 if (!merge(name, path)) {
315 fprintf(stderr, "Resolved '%s' using "
316 "previous resolution.\n", path);
317 goto tail_optimization;
321 /* Let's see if we have resolved it. */
322 ret = handle_file(path, NULL, NULL);
326 fprintf(stderr, "Recorded resolution for '%s'.\n", path);
327 copy_file(rr_path(name, "postimage"), path, 0666);
330 memmove(rr->items + i,
332 sizeof(rr->items[0]) * (rr->nr - i - 1));
337 return write_rr(rr, fd);
340 static int git_rerere_config(const char *var, const char *value, void *cb)
342 if (!strcmp(var, "gc.rerereresolved"))
343 cutoff_resolve = git_config_int(var, value);
344 else if (!strcmp(var, "gc.rerereunresolved"))
345 cutoff_noresolve = git_config_int(var, value);
346 else if (!strcmp(var, "rerere.enabled"))
347 rerere_enabled = git_config_bool(var, value);
349 return git_default_config(var, value, cb);
353 static int is_rerere_enabled(void)
356 const char *rr_cache;
362 rr_cache = git_path("rr-cache");
363 rr_cache_exists = !stat(rr_cache, &st) && S_ISDIR(st.st_mode);
364 if (rerere_enabled < 0)
365 return rr_cache_exists;
367 if (!rr_cache_exists &&
368 (mkdir(rr_cache, 0777) || adjust_shared_perm(rr_cache)))
369 die("Could not create directory %s", rr_cache);
373 static int setup_rerere(struct path_list *merge_rr)
377 git_config(git_rerere_config, NULL);
378 if (!is_rerere_enabled())
381 merge_rr_path = xstrdup(git_path("rr-cache/MERGE_RR"));
382 fd = hold_lock_file_for_update(&write_lock, merge_rr_path, 1);
389 struct path_list merge_rr = { NULL, 0, 0, 1 };
392 fd = setup_rerere(&merge_rr);
395 return do_plain_rerere(&merge_rr, fd);
398 int cmd_rerere(int argc, const char **argv, const char *prefix)
400 struct path_list merge_rr = { NULL, 0, 0, 1 };
403 fd = setup_rerere(&merge_rr);
408 return do_plain_rerere(&merge_rr, fd);
409 else if (!strcmp(argv[1], "clear")) {
410 for (i = 0; i < merge_rr.nr; i++) {
411 const char *name = (const char *)merge_rr.items[i].util;
412 if (!has_resolution(name))
413 unlink_rr_item(name);
415 unlink(merge_rr_path);
416 } else if (!strcmp(argv[1], "gc"))
417 garbage_collect(&merge_rr);
418 else if (!strcmp(argv[1], "status"))
419 for (i = 0; i < merge_rr.nr; i++)
420 printf("%s\n", merge_rr.items[i].path);
421 else if (!strcmp(argv[1], "diff"))
422 for (i = 0; i < merge_rr.nr; i++) {
423 const char *path = merge_rr.items[i].path;
424 const char *name = (const char *)merge_rr.items[i].util;
425 diff_two(rr_path(name, "preimage"), path, path, path);
428 usage(git_rerere_usage);
430 path_list_clear(&merge_rr, 1);