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