Merge branch 'ap/maint-diff-rename-avoid-overlap' into maint
[git] / builtin / rerere.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "dir.h"
4 #include "parse-options.h"
5 #include "string-list.h"
6 #include "rerere.h"
7 #include "xdiff/xdiff.h"
8 #include "xdiff-interface.h"
9
10 static const char * const rerere_usage[] = {
11         N_("git rerere [clear | forget path... | status | remaining | diff | gc]"),
12         NULL,
13 };
14
15 static int outf(void *dummy, mmbuffer_t *ptr, int nbuf)
16 {
17         int i;
18         for (i = 0; i < nbuf; i++)
19                 if (write_in_full(1, ptr[i].ptr, ptr[i].size) != ptr[i].size)
20                         return -1;
21         return 0;
22 }
23
24 static int diff_two(const char *file1, const char *label1,
25                 const char *file2, const char *label2)
26 {
27         xpparam_t xpp;
28         xdemitconf_t xecfg;
29         xdemitcb_t ecb;
30         mmfile_t minus, plus;
31
32         if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2))
33                 return 1;
34
35         printf("--- a/%s\n+++ b/%s\n", label1, label2);
36         fflush(stdout);
37         memset(&xpp, 0, sizeof(xpp));
38         xpp.flags = 0;
39         memset(&xecfg, 0, sizeof(xecfg));
40         xecfg.ctxlen = 3;
41         ecb.outf = outf;
42         xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb);
43
44         free(minus.ptr);
45         free(plus.ptr);
46         return 0;
47 }
48
49 int cmd_rerere(int argc, const char **argv, const char *prefix)
50 {
51         struct string_list merge_rr = STRING_LIST_INIT_DUP;
52         int i, fd, autoupdate = -1, flags = 0;
53
54         struct option options[] = {
55                 OPT_SET_INT(0, "rerere-autoupdate", &autoupdate,
56                         N_("register clean resolutions in index"), 1),
57                 OPT_END(),
58         };
59
60         argc = parse_options(argc, argv, prefix, options, rerere_usage, 0);
61
62         if (autoupdate == 1)
63                 flags = RERERE_AUTOUPDATE;
64         if (autoupdate == 0)
65                 flags = RERERE_NOAUTOUPDATE;
66
67         if (argc < 1)
68                 return rerere(flags);
69
70         if (!strcmp(argv[0], "forget")) {
71                 const char **pathspec;
72                 if (argc < 2)
73                         warning("'git rerere forget' without paths is deprecated");
74                 pathspec = get_pathspec(prefix, argv + 1);
75                 return rerere_forget(pathspec);
76         }
77
78         fd = setup_rerere(&merge_rr, flags);
79         if (fd < 0)
80                 return 0;
81
82         if (!strcmp(argv[0], "clear")) {
83                 rerere_clear(&merge_rr);
84         } else if (!strcmp(argv[0], "gc"))
85                 rerere_gc(&merge_rr);
86         else if (!strcmp(argv[0], "status"))
87                 for (i = 0; i < merge_rr.nr; i++)
88                         printf("%s\n", merge_rr.items[i].string);
89         else if (!strcmp(argv[0], "remaining")) {
90                 rerere_remaining(&merge_rr);
91                 for (i = 0; i < merge_rr.nr; i++) {
92                         if (merge_rr.items[i].util != RERERE_RESOLVED)
93                                 printf("%s\n", merge_rr.items[i].string);
94                         else
95                                 /* prepare for later call to
96                                  * string_list_clear() */
97                                 merge_rr.items[i].util = NULL;
98                 }
99         } else if (!strcmp(argv[0], "diff"))
100                 for (i = 0; i < merge_rr.nr; i++) {
101                         const char *path = merge_rr.items[i].string;
102                         const char *name = (const char *)merge_rr.items[i].util;
103                         diff_two(rerere_path(name, "preimage"), path, path, path);
104                 }
105         else
106                 usage_with_options(rerere_usage, options);
107
108         string_list_clear(&merge_rr, 1);
109         return 0;
110 }