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