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
11static const char * const rerere_usage[] = {
12 N_("git rerere [clear | forget <path>... | status | remaining | diff | gc]"),
13 NULL,
14};
15
16static 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
25static 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 int ret;
33
34 if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2))
35 return -1;
36
37 printf("--- a/%s\n+++ b/%s\n", label1, label2);
38 fflush(stdout);
39 memset(&xpp, 0, sizeof(xpp));
40 xpp.flags = 0;
41 memset(&xecfg, 0, sizeof(xecfg));
42 xecfg.ctxlen = 3;
43 ecb.outf = outf;
44 ret = xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb);
45
46 free(minus.ptr);
47 free(plus.ptr);
48 return ret;
49}
50
51int cmd_rerere(int argc, const char **argv, const char *prefix)
52{
53 struct string_list merge_rr = STRING_LIST_INIT_DUP;
54 int i, fd, autoupdate = -1, flags = 0;
55
56 struct option options[] = {
57 OPT_SET_INT(0, "rerere-autoupdate", &autoupdate,
58 N_("register clean resolutions in index"), 1),
59 OPT_END(),
60 };
61
62 argc = parse_options(argc, argv, prefix, options, rerere_usage, 0);
63
64 git_config(git_xmerge_config, NULL);
65
66 if (autoupdate == 1)
67 flags = RERERE_AUTOUPDATE;
68 if (autoupdate == 0)
69 flags = RERERE_NOAUTOUPDATE;
70
71 if (argc < 1)
72 return rerere(flags);
73
74 if (!strcmp(argv[0], "forget")) {
75 struct pathspec pathspec;
76 if (argc < 2)
77 warning("'git rerere forget' without paths is deprecated");
78 parse_pathspec(&pathspec, 0, PATHSPEC_PREFER_CWD,
79 prefix, argv + 1);
80 return rerere_forget(&pathspec);
81 }
82
83 fd = setup_rerere(&merge_rr, flags);
84 if (fd < 0)
85 return 0;
86
87 if (!strcmp(argv[0], "clear")) {
88 rerere_clear(&merge_rr);
89 } else if (!strcmp(argv[0], "gc"))
90 rerere_gc(&merge_rr);
91 else if (!strcmp(argv[0], "status"))
92 for (i = 0; i < merge_rr.nr; i++)
93 printf("%s\n", merge_rr.items[i].string);
94 else if (!strcmp(argv[0], "remaining")) {
95 rerere_remaining(&merge_rr);
96 for (i = 0; i < merge_rr.nr; i++) {
97 if (merge_rr.items[i].util != RERERE_RESOLVED)
98 printf("%s\n", merge_rr.items[i].string);
99 else
100 /* prepare for later call to
101 * string_list_clear() */
102 merge_rr.items[i].util = NULL;
103 }
104 } else if (!strcmp(argv[0], "diff"))
105 for (i = 0; i < merge_rr.nr; i++) {
106 const char *path = merge_rr.items[i].string;
107 const char *name = (const char *)merge_rr.items[i].util;
108 if (diff_two(rerere_path(name, "preimage"), path, path, path))
109 die("unable to generate diff for %s", name);
110 }
111 else
112 usage_with_options(rerere_usage, options);
113
114 string_list_clear(&merge_rr, 1);
115 return 0;
116}