Merge branch 'maint'
[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                         int one_is_longer = (one->nr > two->nr);
121                         int common_len = one_is_longer ? two->nr : one->nr;
122                         int cmp = memcmp(one->ptr, two->ptr, common_len);
123
124                         hunk_no++;
125                         hunk = 0;
126                         if ((cmp > 0) || ((cmp == 0) && one_is_longer)) {
127                                 struct buffer *swap = one;
128                                 one = two;
129                                 two = swap;
130                         }
131                         if (out) {
132                                 fputs("<<<<<<<\n", out);
133                                 fwrite(one->ptr, one->nr, 1, out);
134                                 fputs("=======\n", out);
135                                 fwrite(two->ptr, two->nr, 1, out);
136                                 fputs(">>>>>>>\n", out);
137                         }
138                         if (sha1) {
139                                 SHA1_Update(&ctx, one->ptr, one->nr);
140                                 SHA1_Update(&ctx, "\0", 1);
141                                 SHA1_Update(&ctx, two->ptr, two->nr);
142                                 SHA1_Update(&ctx, "\0", 1);
143                         }
144                         clear_buffer(one);
145                         clear_buffer(two);
146                 } else if (hunk == 1)
147                         append_line(one, buf);
148                 else if (hunk == 2)
149                         append_line(two, buf);
150                 else if (out)
151                         fputs(buf, out);
152         }
153
154         fclose(f);
155         if (out)
156                 fclose(out);
157         if (sha1)
158                 SHA1_Final(sha1, &ctx);
159         return hunk_no;
160 }
161
162 static int find_conflict(struct path_list *conflict)
163 {
164         int i;
165         if (read_cache() < 0)
166                 return error("Could not read index");
167         for (i = 0; i + 2 < active_nr; i++) {
168                 struct cache_entry *e1 = active_cache[i];
169                 struct cache_entry *e2 = active_cache[i+1];
170                 struct cache_entry *e3 = active_cache[i+2];
171                 if (ce_stage(e1) == 1 &&
172                     ce_stage(e2) == 2 &&
173                     ce_stage(e3) == 3 &&
174                     ce_same_name(e1, e2) && ce_same_name(e1, e3) &&
175                     S_ISREG(ntohl(e1->ce_mode)) &&
176                     S_ISREG(ntohl(e2->ce_mode)) &&
177                     S_ISREG(ntohl(e3->ce_mode))) {
178                         path_list_insert((const char *)e1->name, conflict);
179                         i += 2;
180                 }
181         }
182         return 0;
183 }
184
185 static int merge(const char *name, const char *path)
186 {
187         int ret;
188         mmfile_t cur, base, other;
189         mmbuffer_t result = {NULL, 0};
190         xpparam_t xpp = {XDF_NEED_MINIMAL};
191
192         if (handle_file(path, NULL, rr_path(name, "thisimage")) < 0)
193                 return 1;
194
195         if (read_mmfile(&cur, rr_path(name, "thisimage")) ||
196                         read_mmfile(&base, rr_path(name, "preimage")) ||
197                         read_mmfile(&other, rr_path(name, "postimage")))
198                 return 1;
199         ret = xdl_merge(&base, &cur, "", &other, "",
200                         &xpp, XDL_MERGE_ZEALOUS, &result);
201         if (!ret) {
202                 FILE *f = fopen(path, "w");
203                 if (!f)
204                         return error("Could not write to %s", path);
205                 fwrite(result.ptr, result.size, 1, f);
206                 fclose(f);
207         }
208
209         free(cur.ptr);
210         free(base.ptr);
211         free(other.ptr);
212         free(result.ptr);
213
214         return ret;
215 }
216
217 static void unlink_rr_item(const char *name)
218 {
219         unlink(rr_path(name, "thisimage"));
220         unlink(rr_path(name, "preimage"));
221         unlink(rr_path(name, "postimage"));
222         rmdir(git_path("rr-cache/%s", name));
223 }
224
225 static void garbage_collect(struct path_list *rr)
226 {
227         struct path_list to_remove = { NULL, 0, 0, 1 };
228         char buf[1024];
229         DIR *dir;
230         struct dirent *e;
231         int len, i, cutoff;
232         time_t now = time(NULL), then;
233
234         strlcpy(buf, git_path("rr-cache"), sizeof(buf));
235         len = strlen(buf);
236         dir = opendir(buf);
237         strcpy(buf + len++, "/");
238         while ((e = readdir(dir))) {
239                 const char *name = e->d_name;
240                 struct stat st;
241                 if (name[0] == '.' && (name[1] == '\0' ||
242                                         (name[1] == '.' && name[2] == '\0')))
243                         continue;
244                 i = snprintf(buf + len, sizeof(buf) - len, "%s", name);
245                 strlcpy(buf + len + i, "/preimage", sizeof(buf) - len - i);
246                 if (stat(buf, &st))
247                         continue;
248                 then = st.st_mtime;
249                 strlcpy(buf + len + i, "/postimage", sizeof(buf) - len - i);
250                 cutoff = stat(buf, &st) ? cutoff_noresolve : cutoff_resolve;
251                 if (then < now - cutoff * 86400) {
252                         buf[len + i] = '\0';
253                         path_list_insert(xstrdup(name), &to_remove);
254                 }
255         }
256         for (i = 0; i < to_remove.nr; i++)
257                 unlink_rr_item(to_remove.items[i].path);
258         path_list_clear(&to_remove, 0);
259 }
260
261 static int outf(void *dummy, mmbuffer_t *ptr, int nbuf)
262 {
263         int i;
264         for (i = 0; i < nbuf; i++)
265                 if (write_in_full(1, ptr[i].ptr, ptr[i].size) != ptr[i].size)
266                         return -1;
267         return 0;
268 }
269
270 static int diff_two(const char *file1, const char *label1,
271                 const char *file2, const char *label2)
272 {
273         xpparam_t xpp;
274         xdemitconf_t xecfg;
275         xdemitcb_t ecb;
276         mmfile_t minus, plus;
277
278         if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2))
279                 return 1;
280
281         printf("--- a/%s\n+++ b/%s\n", label1, label2);
282         fflush(stdout);
283         xpp.flags = XDF_NEED_MINIMAL;
284         xecfg.ctxlen = 3;
285         xecfg.flags = 0;
286         ecb.outf = outf;
287         xdl_diff(&minus, &plus, &xpp, &xecfg, &ecb);
288
289         free(minus.ptr);
290         free(plus.ptr);
291         return 0;
292 }
293
294 static int copy_file(const char *src, const char *dest)
295 {
296         FILE *in, *out;
297         char buffer[32768];
298         int count;
299
300         if (!(in = fopen(src, "r")))
301                 return error("Could not open %s", src);
302         if (!(out = fopen(dest, "w")))
303                 return error("Could not open %s", dest);
304         while ((count = fread(buffer, 1, sizeof(buffer), in)))
305                 fwrite(buffer, 1, count, out);
306         fclose(in);
307         fclose(out);
308         return 0;
309 }
310
311 static int do_plain_rerere(struct path_list *rr, int fd)
312 {
313         struct path_list conflict = { NULL, 0, 0, 1 };
314         int i;
315
316         find_conflict(&conflict);
317
318         /*
319          * MERGE_RR records paths with conflicts immediately after merge
320          * failed.  Some of the conflicted paths might have been hand resolved
321          * in the working tree since then, but the initial run would catch all
322          * and register their preimages.
323          */
324
325         for (i = 0; i < conflict.nr; i++) {
326                 const char *path = conflict.items[i].path;
327                 if (!path_list_has_path(rr, path)) {
328                         unsigned char sha1[20];
329                         char *hex;
330                         int ret;
331                         ret = handle_file(path, sha1, NULL);
332                         if (ret < 1)
333                                 continue;
334                         hex = xstrdup(sha1_to_hex(sha1));
335                         path_list_insert(path, rr)->util = hex;
336                         if (mkdir(git_path("rr-cache/%s", hex), 0755))
337                                 continue;;
338                         handle_file(path, NULL, rr_path(hex, "preimage"));
339                         fprintf(stderr, "Recorded preimage for '%s'\n", path);
340                 }
341         }
342
343         /*
344          * Now some of the paths that had conflicts earlier might have been
345          * hand resolved.  Others may be similar to a conflict already that
346          * was resolved before.
347          */
348
349         for (i = 0; i < rr->nr; i++) {
350                 struct stat st;
351                 int ret;
352                 const char *path = rr->items[i].path;
353                 const char *name = (const char *)rr->items[i].util;
354
355                 if (!stat(rr_path(name, "preimage"), &st) &&
356                                 !stat(rr_path(name, "postimage"), &st)) {
357                         if (!merge(name, path)) {
358                                 fprintf(stderr, "Resolved '%s' using "
359                                                 "previous resolution.\n", path);
360                                 goto tail_optimization;
361                         }
362                 }
363
364                 /* Let's see if we have resolved it. */
365                 ret = handle_file(path, NULL, NULL);
366                 if (ret)
367                         continue;
368
369                 fprintf(stderr, "Recorded resolution for '%s'.\n", path);
370                 copy_file(path, rr_path(name, "postimage"));
371 tail_optimization:
372                 if (i < rr->nr - 1)
373                         memmove(rr->items + i,
374                                 rr->items + i + 1,
375                                 sizeof(rr->items[0]) * (rr->nr - i - 1));
376                 rr->nr--;
377                 i--;
378         }
379
380         return write_rr(rr, fd);
381 }
382
383 static int git_rerere_config(const char *var, const char *value)
384 {
385         if (!strcmp(var, "gc.rerereresolved"))
386                 cutoff_resolve = git_config_int(var, value);
387         else if (!strcmp(var, "gc.rerereunresolved"))
388                 cutoff_noresolve = git_config_int(var, value);
389         else
390                 return git_default_config(var, value);
391         return 0;
392 }
393
394 int cmd_rerere(int argc, const char **argv, const char *prefix)
395 {
396         struct path_list merge_rr = { NULL, 0, 0, 1 };
397         int i, fd = -1;
398         struct stat st;
399
400         if (stat(git_path("rr-cache"), &st) || !S_ISDIR(st.st_mode))
401                 return 0;
402
403         git_config(git_rerere_config);
404
405         merge_rr_path = xstrdup(git_path("rr-cache/MERGE_RR"));
406         fd = hold_lock_file_for_update(&write_lock, merge_rr_path, 1);
407         read_rr(&merge_rr);
408
409         if (argc < 2)
410                 return do_plain_rerere(&merge_rr, fd);
411         else if (!strcmp(argv[1], "clear")) {
412                 for (i = 0; i < merge_rr.nr; i++) {
413                         const char *name = (const char *)merge_rr.items[i].util;
414                         if (!stat(git_path("rr-cache/%s", name), &st) &&
415                                         S_ISDIR(st.st_mode) &&
416                                         stat(rr_path(name, "postimage"), &st))
417                                 unlink_rr_item(name);
418                 }
419                 unlink(merge_rr_path);
420         } else if (!strcmp(argv[1], "gc"))
421                 garbage_collect(&merge_rr);
422         else if (!strcmp(argv[1], "status"))
423                 for (i = 0; i < merge_rr.nr; i++)
424                         printf("%s\n", merge_rr.items[i].path);
425         else if (!strcmp(argv[1], "diff"))
426                 for (i = 0; i < merge_rr.nr; i++) {
427                         const char *path = merge_rr.items[i].path;
428                         const char *name = (const char *)merge_rr.items[i].util;
429                         diff_two(rr_path(name, "preimage"), path, path, path);
430                 }
431         else
432                 usage(git_rerere_usage);
433
434         path_list_clear(&merge_rr, 1);
435         return 0;
436 }
437