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