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