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