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 /* 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)
32 return stat(rr_path(name, "preimage"), &st) ? (time_t) 0 : st.st_mtime;
35 static int has_resolution(const char *name)
38 return !stat(rr_path(name, "postimage"), &st);
41 static void read_rr(struct path_list *rr)
43 unsigned char sha1[20];
45 FILE *in = fopen(merge_rr_path, "r");
48 while (fread(buf, 40, 1, in) == 1) {
51 if (get_sha1_hex(buf, sha1))
52 die("corrupt MERGE_RR");
55 if (fgetc(in) != '\t')
56 die("corrupt MERGE_RR");
57 for (i = 0; i < sizeof(buf) && (buf[i] = fgetc(in)); i++)
60 die("filename too long");
61 path_list_insert(buf, rr)->util = name;
66 static struct lock_file write_lock;
68 static int write_rr(struct path_list *rr, int out_fd)
71 for (i = 0; i < rr->nr; i++) {
74 if (!rr->items[i].util)
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");
88 static int handle_file(const char *path,
89 unsigned char *sha1, const char *output)
93 int hunk = 0, hunk_no = 0;
94 struct strbuf one, two;
95 FILE *f = fopen(path, "r");
99 return error("Could not open %s", path);
102 out = fopen(output, "w");
105 return error("Could not write %s", output);
112 strbuf_init(&one, 0);
113 strbuf_init(&two, 0);
114 while (fgets(buf, sizeof(buf), f)) {
115 if (!prefixcmp(buf, "<<<<<<< "))
117 else if (!prefixcmp(buf, "======="))
119 else if (!prefixcmp(buf, ">>>>>>> ")) {
120 if (strbuf_cmp(&one, &two) > 0)
121 strbuf_swap(&one, &two);
125 fputs("<<<<<<<\n", out);
126 fwrite(one.buf, one.len, 1, out);
127 fputs("=======\n", out);
128 fwrite(two.buf, two.len, 1, out);
129 fputs(">>>>>>>\n", out);
132 SHA1_Update(&ctx, one.buf ? one.buf : "",
134 SHA1_Update(&ctx, two.buf ? two.buf : "",
139 } else if (hunk == 1)
140 strbuf_addstr(&one, buf);
142 strbuf_addstr(&two, buf);
146 strbuf_release(&one);
147 strbuf_release(&two);
153 SHA1_Final(sha1, &ctx);
157 return error("Could not parse conflict hunks in %s", path);
162 static int find_conflict(struct path_list *conflict)
165 if (read_cache() < 0)
166 return error("Could not read index");
167 for (i = 0; i+1 < active_nr; i++) {
168 struct cache_entry *e2 = active_cache[i];
169 struct cache_entry *e3 = active_cache[i+1];
170 if (ce_stage(e2) == 2 &&
172 ce_same_name(e2, e3) &&
173 S_ISREG(e2->ce_mode) &&
174 S_ISREG(e3->ce_mode)) {
175 path_list_insert((const char *)e2->name, conflict);
176 i++; /* skip over both #2 and #3 */
182 static int merge(const char *name, const char *path)
185 mmfile_t cur, base, other;
186 mmbuffer_t result = {NULL, 0};
187 xpparam_t xpp = {XDF_NEED_MINIMAL};
189 if (handle_file(path, NULL, rr_path(name, "thisimage")) < 0)
192 if (read_mmfile(&cur, rr_path(name, "thisimage")) ||
193 read_mmfile(&base, rr_path(name, "preimage")) ||
194 read_mmfile(&other, rr_path(name, "postimage")))
196 ret = xdl_merge(&base, &cur, "", &other, "",
197 &xpp, XDL_MERGE_ZEALOUS, &result);
199 FILE *f = fopen(path, "w");
201 return error("Could not write to %s", path);
202 fwrite(result.ptr, result.size, 1, f);
214 static void unlink_rr_item(const char *name)
216 unlink(rr_path(name, "thisimage"));
217 unlink(rr_path(name, "preimage"));
218 unlink(rr_path(name, "postimage"));
219 rmdir(git_path("rr-cache/%s", name));
222 static void garbage_collect(struct path_list *rr)
224 struct path_list to_remove = { NULL, 0, 0, 1 };
228 time_t now = time(NULL), then;
230 dir = opendir(git_path("rr-cache"));
231 while ((e = readdir(dir))) {
232 const char *name = e->d_name;
233 if (name[0] == '.' &&
234 (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')))
236 then = rerere_created_at(name);
239 cutoff = (has_resolution(name)
240 ? cutoff_resolve : cutoff_noresolve);
241 if (then < now - cutoff * 86400)
242 path_list_append(name, &to_remove);
244 for (i = 0; i < to_remove.nr; i++)
245 unlink_rr_item(to_remove.items[i].path);
246 path_list_clear(&to_remove, 0);
249 static int outf(void *dummy, mmbuffer_t *ptr, int nbuf)
252 for (i = 0; i < nbuf; i++)
253 if (write_in_full(1, ptr[i].ptr, ptr[i].size) != ptr[i].size)
258 static int diff_two(const char *file1, const char *label1,
259 const char *file2, const char *label2)
264 mmfile_t minus, plus;
266 if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2))
269 printf("--- a/%s\n+++ b/%s\n", label1, label2);
271 xpp.flags = XDF_NEED_MINIMAL;
272 memset(&xecfg, 0, sizeof(xecfg));
275 xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb);
282 static struct lock_file index_lock;
284 static int update_paths(struct path_list *update)
287 int fd = hold_locked_index(&index_lock, 0);
293 for (i = 0; i < update->nr; i++) {
294 struct path_list_item *item = &update->items[i];
295 if (add_file_to_cache(item->path, ADD_CACHE_IGNORE_ERRORS))
299 if (!status && active_cache_changed) {
300 if (write_cache(fd, active_cache, active_nr) ||
301 commit_locked_index(&index_lock))
302 die("Unable to write new index file");
304 rollback_lock_file(&index_lock);
308 static int do_plain_rerere(struct path_list *rr, int fd)
310 struct path_list conflict = { NULL, 0, 0, 1 };
311 struct path_list update = { NULL, 0, 0, 1 };
314 find_conflict(&conflict);
317 * MERGE_RR records paths with conflicts immediately after merge
318 * failed. Some of the conflicted paths might have been hand resolved
319 * in the working tree since then, but the initial run would catch all
320 * and register their preimages.
323 for (i = 0; i < conflict.nr; i++) {
324 const char *path = conflict.items[i].path;
325 if (!path_list_has_path(rr, path)) {
326 unsigned char sha1[20];
329 ret = handle_file(path, sha1, NULL);
332 hex = xstrdup(sha1_to_hex(sha1));
333 path_list_insert(path, rr)->util = hex;
334 if (mkdir(git_path("rr-cache/%s", hex), 0755))
336 handle_file(path, NULL, rr_path(hex, "preimage"));
337 fprintf(stderr, "Recorded preimage for '%s'\n", path);
342 * Now some of the paths that had conflicts earlier might have been
343 * hand resolved. Others may be similar to a conflict already that
344 * was resolved before.
347 for (i = 0; i < rr->nr; i++) {
349 const char *path = rr->items[i].path;
350 const char *name = (const char *)rr->items[i].util;
352 if (has_resolution(name)) {
353 if (!merge(name, path)) {
354 fprintf(stderr, "Resolved '%s' using "
355 "previous resolution.\n", path);
356 if (rerere_autoupdate)
357 path_list_insert(path, &update);
362 /* Let's see if we have resolved it. */
363 ret = handle_file(path, NULL, NULL);
367 fprintf(stderr, "Recorded resolution for '%s'.\n", path);
368 copy_file(rr_path(name, "postimage"), path, 0666);
370 rr->items[i].util = NULL;
374 update_paths(&update);
376 return write_rr(rr, fd);
379 static int git_rerere_config(const char *var, const char *value, void *cb)
381 if (!strcmp(var, "gc.rerereresolved"))
382 cutoff_resolve = git_config_int(var, value);
383 else if (!strcmp(var, "gc.rerereunresolved"))
384 cutoff_noresolve = git_config_int(var, value);
385 else if (!strcmp(var, "rerere.enabled"))
386 rerere_enabled = git_config_bool(var, value);
387 else if (!strcmp(var, "rerere.autoupdate"))
388 rerere_autoupdate = git_config_bool(var, value);
390 return git_default_config(var, value, cb);
394 static int is_rerere_enabled(void)
397 const char *rr_cache;
403 rr_cache = git_path("rr-cache");
404 rr_cache_exists = !stat(rr_cache, &st) && S_ISDIR(st.st_mode);
405 if (rerere_enabled < 0)
406 return rr_cache_exists;
408 if (!rr_cache_exists &&
409 (mkdir(rr_cache, 0777) || adjust_shared_perm(rr_cache)))
410 die("Could not create directory %s", rr_cache);
414 static int setup_rerere(struct path_list *merge_rr)
418 git_config(git_rerere_config, NULL);
419 if (!is_rerere_enabled())
422 merge_rr_path = xstrdup(git_path("rr-cache/MERGE_RR"));
423 fd = hold_lock_file_for_update(&write_lock, merge_rr_path, 1);
430 struct path_list merge_rr = { NULL, 0, 0, 1 };
433 fd = setup_rerere(&merge_rr);
436 return do_plain_rerere(&merge_rr, fd);
439 int cmd_rerere(int argc, const char **argv, const char *prefix)
441 struct path_list merge_rr = { NULL, 0, 0, 1 };
444 fd = setup_rerere(&merge_rr);
449 return do_plain_rerere(&merge_rr, fd);
450 else if (!strcmp(argv[1], "clear")) {
451 for (i = 0; i < merge_rr.nr; i++) {
452 const char *name = (const char *)merge_rr.items[i].util;
453 if (!has_resolution(name))
454 unlink_rr_item(name);
456 unlink(merge_rr_path);
457 } else if (!strcmp(argv[1], "gc"))
458 garbage_collect(&merge_rr);
459 else if (!strcmp(argv[1], "status"))
460 for (i = 0; i < merge_rr.nr; i++)
461 printf("%s\n", merge_rr.items[i].path);
462 else if (!strcmp(argv[1], "diff"))
463 for (i = 0; i < merge_rr.nr; i++) {
464 const char *path = merge_rr.items[i].path;
465 const char *name = (const char *)merge_rr.items[i].util;
466 diff_two(rr_path(name, "preimage"), path, path, path);
469 usage(git_rerere_usage);
471 path_list_clear(&merge_rr, 1);