Git fork
1#define USE_THE_REPOSITORY_VARIABLE
2
3#include "builtin.h"
4#include "config.h"
5#include "gettext.h"
6#include "parse-options.h"
7#include "rerere.h"
8#include "strbuf.h"
9#include "string-list.h"
10#include "xdiff/xdiff.h"
11#include "xdiff-interface.h"
12#include "pathspec.h"
13
14static const char * const rerere_usage[] = {
15 N_("git rerere [clear | forget <pathspec>... | diff | status | remaining | gc]"),
16 NULL,
17};
18
19static int outf(void *dummy UNUSED, mmbuffer_t *ptr, int nbuf)
20{
21 int i;
22 for (i = 0; i < nbuf; i++)
23 if (write_in_full(1, ptr[i].ptr, ptr[i].size) < 0)
24 return -1;
25 return 0;
26}
27
28static int diff_two(const char *file1, const char *label1,
29 const char *file2, const char *label2)
30{
31 xpparam_t xpp;
32 xdemitconf_t xecfg;
33 xdemitcb_t ecb = { .out_line = outf };
34 mmfile_t minus, plus;
35 int ret;
36
37 if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2))
38 return -1;
39
40 printf("--- a/%s\n+++ b/%s\n", label1, label2);
41 fflush(stdout);
42 memset(&xpp, 0, sizeof(xpp));
43 xpp.flags = 0;
44 memset(&xecfg, 0, sizeof(xecfg));
45 xecfg.ctxlen = 3;
46 ret = xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb);
47
48 free(minus.ptr);
49 free(plus.ptr);
50 return ret;
51}
52
53int cmd_rerere(int argc,
54 const char **argv,
55 const char *prefix,
56 struct repository *repo UNUSED)
57{
58 struct string_list merge_rr = STRING_LIST_INIT_DUP;
59 int autoupdate = -1, flags = 0;
60
61 struct option options[] = {
62 OPT_SET_INT(0, "rerere-autoupdate", &autoupdate,
63 N_("register clean resolutions in index"), 1),
64 OPT_END(),
65 };
66
67 argc = parse_options(argc, argv, prefix, options, rerere_usage, 0);
68
69 repo_config(the_repository, git_xmerge_config, NULL);
70
71 if (autoupdate == 1)
72 flags = RERERE_AUTOUPDATE;
73 if (autoupdate == 0)
74 flags = RERERE_NOAUTOUPDATE;
75
76 if (argc < 1)
77 return repo_rerere(the_repository, flags);
78
79 if (!strcmp(argv[0], "forget")) {
80 struct pathspec pathspec;
81 int ret;
82
83 if (argc < 2)
84 warning(_("'git rerere forget' without paths is deprecated"));
85 parse_pathspec(&pathspec, 0, PATHSPEC_PREFER_CWD,
86 prefix, argv + 1);
87
88 ret = rerere_forget(the_repository, &pathspec);
89
90 clear_pathspec(&pathspec);
91 return ret;
92 }
93
94 if (!strcmp(argv[0], "clear")) {
95 rerere_clear(the_repository, &merge_rr);
96 } else if (!strcmp(argv[0], "gc"))
97 rerere_gc(the_repository, &merge_rr);
98 else if (!strcmp(argv[0], "status")) {
99 if (setup_rerere(the_repository, &merge_rr,
100 flags | RERERE_READONLY) < 0)
101 return 0;
102 for (size_t i = 0; i < merge_rr.nr; i++)
103 printf("%s\n", merge_rr.items[i].string);
104 } else if (!strcmp(argv[0], "remaining")) {
105 rerere_remaining(the_repository, &merge_rr);
106 for (size_t i = 0; i < merge_rr.nr; i++) {
107 if (merge_rr.items[i].util != RERERE_RESOLVED)
108 printf("%s\n", merge_rr.items[i].string);
109 else
110 /* prepare for later call to
111 * string_list_clear() */
112 merge_rr.items[i].util = NULL;
113 }
114 } else if (!strcmp(argv[0], "diff")) {
115 struct strbuf buf = STRBUF_INIT;
116 if (setup_rerere(the_repository, &merge_rr,
117 flags | RERERE_READONLY) < 0)
118 return 0;
119 for (size_t i = 0; i < merge_rr.nr; i++) {
120 const char *path = merge_rr.items[i].string;
121 const struct rerere_id *id = merge_rr.items[i].util;
122 if (diff_two(rerere_path(&buf, id, "preimage"), path, path, path))
123 die(_("unable to generate diff for '%s'"), rerere_path(&buf, id, NULL));
124 }
125
126 strbuf_release(&buf);
127 } else
128 usage_with_options(rerere_usage, options);
129
130 string_list_clear(&merge_rr, 1);
131 return 0;
132}