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