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