Merge branch 'jc/bisect'
[git] / builtin-rerere.c
1 #include "cache.h"
2 #include "path-list.h"
3 #include "xdiff/xdiff.h"
4 #include "xdiff-interface.h"
5
6 #include <time.h>
7
8 static const char git_rerere_usage[] =
9 "git-rerere [clear | status | diff | gc]";
10
11 /* these values are days */
12 static int cutoff_noresolve = 15;
13 static int cutoff_resolve = 60;
14
15 static char *merge_rr_path;
16
17 static const char *rr_path(const char *name, const char *file)
18 {
19         return git_path("rr-cache/%s/%s", name, file);
20 }
21
22 static void read_rr(struct path_list *rr)
23 {
24         unsigned char sha1[20];
25         char buf[PATH_MAX];
26         FILE *in = fopen(merge_rr_path, "r");
27         if (!in)
28                 return;
29         while (fread(buf, 40, 1, in) == 1) {
30                 int i;
31                 char *name;
32                 if (get_sha1_hex(buf, sha1))
33                         die("corrupt MERGE_RR");
34                 buf[40] = '\0';
35                 name = xstrdup(buf);
36                 if (fgetc(in) != '\t')
37                         die("corrupt MERGE_RR");
38                 for (i = 0; i < sizeof(buf) && (buf[i] = fgetc(in)); i++)
39                         ; /* do nothing */
40                 if (i == sizeof(buf))
41                         die("filename too long");
42                 path_list_insert(buf, rr)->util = xstrdup(name);
43         }
44         fclose(in);
45 }
46
47 static struct lock_file write_lock;
48
49 static int write_rr(struct path_list *rr, int out_fd)
50 {
51         int i;
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");
59         }
60         close(out_fd);
61         return commit_lock_file(&write_lock);
62 }
63
64 struct buffer {
65         char *ptr;
66         int nr, alloc;
67 };
68
69 static void append_line(struct buffer *buffer, const char *line)
70 {
71         int len = strlen(line);
72
73         if (buffer->nr + len > buffer->alloc) {
74                 buffer->alloc = alloc_nr(buffer->nr + len);
75                 buffer->ptr = xrealloc(buffer->ptr, buffer->alloc);
76         }
77         memcpy(buffer->ptr + buffer->nr, line, len);
78         buffer->nr += len;
79 }
80
81 static void clear_buffer(struct buffer *buffer)
82 {
83         free(buffer->ptr);
84         buffer->ptr = NULL;
85         buffer->nr = buffer->alloc = 0;
86 }
87
88 static int handle_file(const char *path,
89          unsigned char *sha1, const char *output)
90 {
91         SHA_CTX ctx;
92         char buf[1024];
93         int hunk = 0, hunk_no = 0;
94         struct buffer minus = { NULL, 0, 0 }, plus = { NULL, 0, 0 };
95         struct buffer *one = &minus, *two = &plus;
96         FILE *f = fopen(path, "r");
97         FILE *out;
98
99         if (!f)
100                 return error("Could not open %s", path);
101
102         if (output) {
103                 out = fopen(output, "w");
104                 if (!out) {
105                         fclose(f);
106                         return error("Could not write %s", output);
107                 }
108         } else
109                 out = NULL;
110
111         if (sha1)
112                 SHA1_Init(&ctx);
113
114         while (fgets(buf, sizeof(buf), f)) {
115                 if (!prefixcmp(buf, "<<<<<<< "))
116                         hunk = 1;
117                 else if (!prefixcmp(buf, "======="))
118                         hunk = 2;
119                 else if (!prefixcmp(buf, ">>>>>>> ")) {
120                         hunk_no++;
121                         hunk = 0;
122                         if (memcmp(one->ptr, two->ptr, one->nr < two->nr ?
123                                                 one->nr : two->nr) > 0) {
124                                 struct buffer *swap = one;
125                                 one = two;
126                                 two = swap;
127                         }
128                         if (out) {
129                                 fputs("<<<<<<<\n", out);
130                                 fwrite(one->ptr, one->nr, 1, out);
131                                 fputs("=======\n", out);
132                                 fwrite(two->ptr, two->nr, 1, out);
133                                 fputs(">>>>>>>\n", out);
134                         }
135                         if (sha1) {
136                                 SHA1_Update(&ctx, one->ptr, one->nr);
137                                 SHA1_Update(&ctx, "\0", 1);
138                                 SHA1_Update(&ctx, two->ptr, two->nr);
139                                 SHA1_Update(&ctx, "\0", 1);
140                         }
141                         clear_buffer(one);
142                         clear_buffer(two);
143                 } else if (hunk == 1)
144                         append_line(one, buf);
145                 else if (hunk == 2)
146                         append_line(two, buf);
147                 else if (out)
148                         fputs(buf, out);
149         }
150
151         fclose(f);
152         if (out)
153                 fclose(out);
154         if (sha1)
155                 SHA1_Final(sha1, &ctx);
156         return hunk_no;
157 }
158
159 static int find_conflict(struct path_list *conflict)
160 {
161         int i;
162         if (read_cache() < 0)
163                 return error("Could not read index");
164         for (i = 0; i + 2 < active_nr; i++) {
165                 struct cache_entry *e1 = active_cache[i];
166                 struct cache_entry *e2 = active_cache[i+1];
167                 struct cache_entry *e3 = active_cache[i+2];
168                 if (ce_stage(e1) == 1 &&
169                     ce_stage(e2) == 2 &&
170                     ce_stage(e3) == 3 &&
171                     ce_same_name(e1, e2) && ce_same_name(e1, e3) &&
172                     S_ISREG(ntohl(e1->ce_mode)) &&
173                     S_ISREG(ntohl(e2->ce_mode)) &&
174                     S_ISREG(ntohl(e3->ce_mode))) {
175                         path_list_insert((const char *)e1->name, conflict);
176                         i += 2;
177                 }
178         }
179         return 0;
180 }
181
182 static int merge(const char *name, const char *path)
183 {
184         int ret;
185         mmfile_t cur, base, other;
186         mmbuffer_t result = {NULL, 0};
187         xpparam_t xpp = {XDF_NEED_MINIMAL};
188
189         if (handle_file(path, NULL, rr_path(name, "thisimage")) < 0)
190                 return 1;
191
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")))
195                 return 1;
196         ret = xdl_merge(&base, &cur, "", &other, "",
197                         &xpp, XDL_MERGE_ZEALOUS, &result);
198         if (!ret) {
199                 FILE *f = fopen(path, "w");
200                 if (!f)
201                         return error("Could not write to %s", path);
202                 fwrite(result.ptr, result.size, 1, f);
203                 fclose(f);
204         }
205
206         free(cur.ptr);
207         free(base.ptr);
208         free(other.ptr);
209         free(result.ptr);
210
211         return ret;
212 }
213
214 static void unlink_rr_item(const char *name)
215 {
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));
220 }
221
222 static void garbage_collect(struct path_list *rr)
223 {
224         struct path_list to_remove = { NULL, 0, 0, 1 };
225         char buf[1024];
226         DIR *dir;
227         struct dirent *e;
228         int len, i, cutoff;
229         time_t now = time(NULL), then;
230
231         strlcpy(buf, git_path("rr-cache"), sizeof(buf));
232         len = strlen(buf);
233         dir = opendir(buf);
234         strcpy(buf + len++, "/");
235         while ((e = readdir(dir))) {
236                 const char *name = e->d_name;
237                 struct stat st;
238                 if (name[0] == '.' && (name[1] == '\0' ||
239                                         (name[1] == '.' && name[2] == '\0')))
240                         continue;
241                 i = snprintf(buf + len, sizeof(buf) - len, "%s", name);
242                 strlcpy(buf + len + i, "/preimage", sizeof(buf) - len - i);
243                 if (stat(buf, &st))
244                         continue;
245                 then = st.st_mtime;
246                 strlcpy(buf + len + i, "/postimage", sizeof(buf) - len - i);
247                 cutoff = stat(buf, &st) ? cutoff_noresolve : cutoff_resolve;
248                 if (then < now - cutoff * 86400) {
249                         buf[len + i] = '\0';
250                         path_list_insert(xstrdup(name), &to_remove);
251                 }
252         }
253         for (i = 0; i < to_remove.nr; i++)
254                 unlink_rr_item(to_remove.items[i].path);
255         path_list_clear(&to_remove, 0);
256 }
257
258 static int outf(void *dummy, mmbuffer_t *ptr, int nbuf)
259 {
260         int i;
261         for (i = 0; i < nbuf; i++)
262                 if (write_in_full(1, ptr[i].ptr, ptr[i].size) != ptr[i].size)
263                         return -1;
264         return 0;
265 }
266
267 static int diff_two(const char *file1, const char *label1,
268                 const char *file2, const char *label2)
269 {
270         xpparam_t xpp;
271         xdemitconf_t xecfg;
272         xdemitcb_t ecb;
273         mmfile_t minus, plus;
274
275         if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2))
276                 return 1;
277
278         printf("--- a/%s\n+++ b/%s\n", label1, label2);
279         fflush(stdout);
280         xpp.flags = XDF_NEED_MINIMAL;
281         xecfg.ctxlen = 3;
282         xecfg.flags = 0;
283         ecb.outf = outf;
284         xdl_diff(&minus, &plus, &xpp, &xecfg, &ecb);
285
286         free(minus.ptr);
287         free(plus.ptr);
288         return 0;
289 }
290
291 static int copy_file(const char *src, const char *dest)
292 {
293         FILE *in, *out;
294         char buffer[32768];
295         int count;
296
297         if (!(in = fopen(src, "r")))
298                 return error("Could not open %s", src);
299         if (!(out = fopen(dest, "w")))
300                 return error("Could not open %s", dest);
301         while ((count = fread(buffer, 1, sizeof(buffer), in)))
302                 fwrite(buffer, 1, count, out);
303         fclose(in);
304         fclose(out);
305         return 0;
306 }
307
308 static int do_plain_rerere(struct path_list *rr, int fd)
309 {
310         struct path_list conflict = { NULL, 0, 0, 1 };
311         int i;
312
313         find_conflict(&conflict);
314
315         /*
316          * MERGE_RR records paths with conflicts immediately after merge
317          * failed.  Some of the conflicted paths might have been hand resolved
318          * in the working tree since then, but the initial run would catch all
319          * and register their preimages.
320          */
321
322         for (i = 0; i < conflict.nr; i++) {
323                 const char *path = conflict.items[i].path;
324                 if (!path_list_has_path(rr, path)) {
325                         unsigned char sha1[20];
326                         char *hex;
327                         int ret;
328                         ret = handle_file(path, sha1, NULL);
329                         if (ret < 1)
330                                 continue;
331                         hex = xstrdup(sha1_to_hex(sha1));
332                         path_list_insert(path, rr)->util = hex;
333                         if (mkdir(git_path("rr-cache/%s", hex), 0755))
334                                 continue;;
335                         handle_file(path, NULL, rr_path(hex, "preimage"));
336                         fprintf(stderr, "Recorded preimage for '%s'\n", path);
337                 }
338         }
339
340         /*
341          * Now some of the paths that had conflicts earlier might have been
342          * hand resolved.  Others may be similar to a conflict already that
343          * was resolved before.
344          */
345
346         for (i = 0; i < rr->nr; i++) {
347                 struct stat st;
348                 int ret;
349                 const char *path = rr->items[i].path;
350                 const char *name = (const char *)rr->items[i].util;
351
352                 if (!stat(rr_path(name, "preimage"), &st) &&
353                                 !stat(rr_path(name, "postimage"), &st)) {
354                         if (!merge(name, path)) {
355                                 fprintf(stderr, "Resolved '%s' using "
356                                                 "previous resolution.\n", path);
357                                 goto tail_optimization;
358                         }
359                 }
360
361                 /* Let's see if we have resolved it. */
362                 ret = handle_file(path, NULL, NULL);
363                 if (ret)
364                         continue;
365
366                 fprintf(stderr, "Recorded resolution for '%s'.\n", path);
367                 copy_file(path, rr_path(name, "postimage"));
368 tail_optimization:
369                 if (i < rr->nr - 1)
370                         memmove(rr->items + i,
371                                 rr->items + i + 1,
372                                 sizeof(rr->items[0]) * (rr->nr - i - 1));
373                 rr->nr--;
374                 i--;
375         }
376
377         return write_rr(rr, fd);
378 }
379
380 static int git_rerere_config(const char *var, const char *value)
381 {
382         if (!strcmp(var, "gc.rerereresolved"))
383                 cutoff_resolve = git_config_int(var, value);
384         else if (!strcmp(var, "gc.rerereunresolved"))
385                 cutoff_noresolve = git_config_int(var, value);
386         else
387                 return git_default_config(var, value);
388         return 0;
389 }
390
391 int cmd_rerere(int argc, const char **argv, const char *prefix)
392 {
393         struct path_list merge_rr = { NULL, 0, 0, 1 };
394         int i, fd = -1;
395         struct stat st;
396
397         if (stat(git_path("rr-cache"), &st) || !S_ISDIR(st.st_mode))
398                 return 0;
399
400         git_config(git_rerere_config);
401
402         merge_rr_path = xstrdup(git_path("rr-cache/MERGE_RR"));
403         fd = hold_lock_file_for_update(&write_lock, merge_rr_path, 1);
404         read_rr(&merge_rr);
405
406         if (argc < 2)
407                 return do_plain_rerere(&merge_rr, fd);
408         else if (!strcmp(argv[1], "clear")) {
409                 for (i = 0; i < merge_rr.nr; i++) {
410                         const char *name = (const char *)merge_rr.items[i].util;
411                         if (!stat(git_path("rr-cache/%s", name), &st) &&
412                                         S_ISDIR(st.st_mode) &&
413                                         stat(rr_path(name, "postimage"), &st))
414                                 unlink_rr_item(name);
415                 }
416                 unlink(merge_rr_path);
417         } else if (!strcmp(argv[1], "gc"))
418                 garbage_collect(&merge_rr);
419         else if (!strcmp(argv[1], "status"))
420                 for (i = 0; i < merge_rr.nr; i++)
421                         printf("%s\n", merge_rr.items[i].path);
422         else if (!strcmp(argv[1], "diff"))
423                 for (i = 0; i < merge_rr.nr; i++) {
424                         const char *path = merge_rr.items[i].path;
425                         const char *name = (const char *)merge_rr.items[i].util;
426                         diff_two(rr_path(name, "preimage"), path, path, path);
427                 }
428         else
429                 usage(git_rerere_usage);
430
431         path_list_clear(&merge_rr, 1);
432         return 0;
433 }
434