3 #include "xdiff/xdiff.h"
4 #include "xdiff-interface.h"
8 static const char git_rerere_usage[] =
9 "git-rerere [clear | status | diff | gc]";
11 /* these values are days */
12 static int cutoff_noresolve = 15;
13 static int cutoff_resolve = 60;
15 static char *merge_rr_path;
17 static const char *rr_path(const char *name, const char *file)
19 return git_path("rr-cache/%s/%s", name, file);
22 static void read_rr(struct path_list *rr)
24 unsigned char sha1[20];
26 FILE *in = fopen(merge_rr_path, "r");
29 while (fread(buf, 40, 1, in) == 1) {
32 if (get_sha1_hex(buf, sha1))
33 die("corrupt MERGE_RR");
36 if (fgetc(in) != '\t')
37 die("corrupt MERGE_RR");
38 for (i = 0; i < sizeof(buf) && (buf[i] = fgetc(in)); i++)
41 die("filename too long");
42 path_list_insert(buf, rr)->util = xstrdup(name);
47 static struct lock_file write_lock;
49 static int write_rr(struct path_list *rr, int out_fd)
52 for (i = 0; i < rr->nr; i++) {
53 const char *path = rr->items[i].path;
54 int length = strlen(path) + 1;
55 if (write_in_full(out_fd, rr->items[i].util, 40) != 40 ||
56 write_in_full(out_fd, "\t", 1) != 1 ||
57 write_in_full(out_fd, path, length) != length)
58 die("unable to write rerere record");
61 return commit_lock_file(&write_lock);
69 static void append_line(struct buffer *buffer, const char *line)
71 int len = strlen(line);
73 if (buffer->nr + len > buffer->alloc) {
74 buffer->alloc = alloc_nr(buffer->nr + len);
75 buffer->ptr = xrealloc(buffer->ptr, buffer->alloc);
77 memcpy(buffer->ptr + buffer->nr, line, len);
81 static int handle_file(const char *path,
82 unsigned char *sha1, const char *output)
86 int hunk = 0, hunk_no = 0;
87 struct buffer minus = { NULL, 0, 0 }, plus = { NULL, 0, 0 };
88 struct buffer *one = &minus, *two = +
89 FILE *f = fopen(path, "r");
93 return error("Could not open %s", path);
96 out = fopen(output, "w");
99 return error("Could not write %s", output);
107 while (fgets(buf, sizeof(buf), f)) {
108 if (!prefixcmp(buf, "<<<<<<< "))
110 else if (!prefixcmp(buf, "======="))
112 else if (!prefixcmp(buf, ">>>>>>> ")) {
115 if (memcmp(one->ptr, two->ptr, one->nr < two->nr ?
116 one->nr : two->nr) > 0) {
117 struct buffer *swap = one;
122 fputs("<<<<<<<\n", out);
123 fwrite(one->ptr, one->nr, 1, out);
124 fputs("=======\n", out);
125 fwrite(two->ptr, two->nr, 1, out);
126 fputs(">>>>>>>\n", out);
129 SHA1_Update(&ctx, one->ptr, one->nr);
130 SHA1_Update(&ctx, "\0", 1);
131 SHA1_Update(&ctx, two->ptr, two->nr);
132 SHA1_Update(&ctx, "\0", 1);
134 } else if (hunk == 1)
135 append_line(one, buf);
137 append_line(two, buf);
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 + 2 < active_nr; i++) {
156 struct cache_entry *e1 = active_cache[i];
157 struct cache_entry *e2 = active_cache[i+1];
158 struct cache_entry *e3 = active_cache[i+2];
159 if (ce_stage(e1) == 1 &&
162 ce_same_name(e1, e2) && ce_same_name(e1, e3) &&
163 S_ISREG(ntohl(e1->ce_mode)) &&
164 S_ISREG(ntohl(e2->ce_mode)) &&
165 S_ISREG(ntohl(e3->ce_mode))) {
166 path_list_insert((const char *)e1->name, conflict);
173 static int merge(const char *name, const char *path)
176 mmfile_t cur, base, other;
177 mmbuffer_t result = {NULL, 0};
178 xpparam_t xpp = {XDF_NEED_MINIMAL};
180 if (handle_file(path, NULL, rr_path(name, "thisimage")) < 0)
183 if (read_mmfile(&cur, rr_path(name, "thisimage")) ||
184 read_mmfile(&base, rr_path(name, "preimage")) ||
185 read_mmfile(&other, rr_path(name, "postimage")))
187 ret = xdl_merge(&base, &cur, "", &other, "",
188 &xpp, XDL_MERGE_ZEALOUS, &result);
190 FILE *f = fopen(path, "w");
192 return error("Could not write to %s", path);
193 fwrite(result.ptr, result.size, 1, f);
205 static void unlink_rr_item(const char *name)
207 unlink(rr_path(name, "thisimage"));
208 unlink(rr_path(name, "preimage"));
209 unlink(rr_path(name, "postimage"));
210 rmdir(git_path("rr-cache/%s", name));
213 static void garbage_collect(struct path_list *rr)
215 struct path_list to_remove = { NULL, 0, 0, 1 };
220 time_t now = time(NULL), then;
222 strlcpy(buf, git_path("rr-cache"), sizeof(buf));
225 strcpy(buf + len++, "/");
226 while ((e = readdir(dir))) {
227 const char *name = e->d_name;
229 if (name[0] == '.' && (name[1] == '\0' ||
230 (name[1] == '.' && name[2] == '\0')))
232 i = snprintf(buf + len, sizeof(buf) - len, "%s", name);
233 strlcpy(buf + len + i, "/preimage", sizeof(buf) - len - i);
237 strlcpy(buf + len + i, "/postimage", sizeof(buf) - len - i);
238 cutoff = stat(buf, &st) ? cutoff_noresolve : cutoff_resolve;
239 if (then < now - cutoff * 86400) {
241 path_list_insert(xstrdup(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;
275 xdl_diff(&minus, &plus, &xpp, &xecfg, &ecb);
282 static int copy_file(const char *src, const char *dest)
288 if (!(in = fopen(src, "r")))
289 return error("Could not open %s", src);
290 if (!(out = fopen(dest, "w")))
291 return error("Could not open %s", dest);
292 while ((count = fread(buffer, 1, sizeof(buffer), in)))
293 fwrite(buffer, 1, count, out);
299 static int do_plain_rerere(struct path_list *rr, int fd)
301 struct path_list conflict = { NULL, 0, 0, 1 };
304 find_conflict(&conflict);
307 * MERGE_RR records paths with conflicts immediately after merge
308 * failed. Some of the conflicted paths might have been hand resolved
309 * in the working tree since then, but the initial run would catch all
310 * and register their preimages.
313 for (i = 0; i < conflict.nr; i++) {
314 const char *path = conflict.items[i].path;
315 if (!path_list_has_path(rr, path)) {
316 unsigned char sha1[20];
319 ret = handle_file(path, sha1, NULL);
322 hex = xstrdup(sha1_to_hex(sha1));
323 path_list_insert(path, rr)->util = hex;
324 if (mkdir(git_path("rr-cache/%s", hex), 0755))
326 handle_file(path, NULL, rr_path(hex, "preimage"));
327 fprintf(stderr, "Recorded preimage for '%s'\n", path);
332 * Now some of the paths that had conflicts earlier might have been
333 * hand resolved. Others may be similar to a conflict already that
334 * was resolved before.
337 for (i = 0; i < rr->nr; i++) {
340 const char *path = rr->items[i].path;
341 const char *name = (const char *)rr->items[i].util;
343 if (!stat(rr_path(name, "preimage"), &st) &&
344 !stat(rr_path(name, "postimage"), &st)) {
345 if (!merge(name, path)) {
346 fprintf(stderr, "Resolved '%s' using "
347 "previous resolution.\n", path);
348 goto tail_optimization;
352 /* Let's see if we have resolved it. */
353 ret = handle_file(path, NULL, NULL);
357 fprintf(stderr, "Recorded resolution for '%s'.\n", path);
358 copy_file(path, rr_path(name, "postimage"));
361 memmove(rr->items + i,
363 sizeof(rr->items[0]) * (rr->nr - i - 1));
368 return write_rr(rr, fd);
371 static int git_rerere_config(const char *var, const char *value)
373 if (!strcmp(var, "gc.rerereresolved"))
374 cutoff_resolve = git_config_int(var, value);
375 else if (!strcmp(var, "gc.rerereunresolved"))
376 cutoff_noresolve = git_config_int(var, value);
378 return git_default_config(var, value);
382 int cmd_rerere(int argc, const char **argv, const char *prefix)
384 struct path_list merge_rr = { NULL, 0, 0, 1 };
388 if (stat(git_path("rr-cache"), &st) || !S_ISDIR(st.st_mode))
391 git_config(git_rerere_config);
393 merge_rr_path = xstrdup(git_path("rr-cache/MERGE_RR"));
394 fd = hold_lock_file_for_update(&write_lock, merge_rr_path, 1);
398 return do_plain_rerere(&merge_rr, fd);
399 else if (!strcmp(argv[1], "clear")) {
400 for (i = 0; i < merge_rr.nr; i++) {
401 const char *name = (const char *)merge_rr.items[i].util;
402 if (!stat(git_path("rr-cache/%s", name), &st) &&
403 S_ISDIR(st.st_mode) &&
404 stat(rr_path(name, "postimage"), &st))
405 unlink_rr_item(name);
407 unlink(merge_rr_path);
408 } else if (!strcmp(argv[1], "gc"))
409 garbage_collect(&merge_rr);
410 else if (!strcmp(argv[1], "status"))
411 for (i = 0; i < merge_rr.nr; i++)
412 printf("%s\n", merge_rr.items[i].path);
413 else if (!strcmp(argv[1], "diff"))
414 for (i = 0; i < merge_rr.nr; i++) {
415 const char *path = merge_rr.items[i].path;
416 const char *name = (const char *)merge_rr.items[i].util;
417 diff_two(rr_path(name, "preimage"), path, path, path);
420 usage(git_rerere_usage);
422 path_list_clear(&merge_rr, 1);