gitlab-ci: introduce stages and dependencies
[git/gitster.git] / builtin / rerere.c
blobf7143c3f5d5670c83ff9995e756cd12ea7626863
1 #define USE_THE_REPOSITORY_VARIABLE
2 #include "builtin.h"
3 #include "config.h"
4 #include "gettext.h"
5 #include "parse-options.h"
7 #include "string-list.h"
8 #include "rerere.h"
9 #include "xdiff/xdiff.h"
10 #include "xdiff-interface.h"
11 #include "pathspec.h"
13 static const char * const rerere_usage[] = {
14 N_("git rerere [clear | forget <pathspec>... | diff | status | remaining | gc]"),
15 NULL,
18 static int outf(void *dummy UNUSED, mmbuffer_t *ptr, int nbuf)
20 int i;
21 for (i = 0; i < nbuf; i++)
22 if (write_in_full(1, ptr[i].ptr, ptr[i].size) < 0)
23 return -1;
24 return 0;
27 static int diff_two(const char *file1, const char *label1,
28 const char *file2, const char *label2)
30 xpparam_t xpp;
31 xdemitconf_t xecfg;
32 xdemitcb_t ecb = { .out_line = outf };
33 mmfile_t minus, plus;
34 int ret;
36 if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2))
37 return -1;
39 printf("--- a/%s\n+++ b/%s\n", label1, label2);
40 fflush(stdout);
41 memset(&xpp, 0, sizeof(xpp));
42 xpp.flags = 0;
43 memset(&xecfg, 0, sizeof(xecfg));
44 xecfg.ctxlen = 3;
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,
53 const char **argv,
54 const char *prefix,
55 struct repository *repo UNUSED)
57 struct string_list merge_rr = STRING_LIST_INIT_DUP;
58 int i, autoupdate = -1, flags = 0;
60 struct option options[] = {
61 OPT_SET_INT(0, "rerere-autoupdate", &autoupdate,
62 N_("register clean resolutions in index"), 1),
63 OPT_END(),
66 argc = parse_options(argc, argv, prefix, options, rerere_usage, 0);
68 git_config(git_xmerge_config, NULL);
70 if (autoupdate == 1)
71 flags = RERERE_AUTOUPDATE;
72 if (autoupdate == 0)
73 flags = RERERE_NOAUTOUPDATE;
75 if (argc < 1)
76 return repo_rerere(the_repository, flags);
78 if (!strcmp(argv[0], "forget")) {
79 struct pathspec pathspec;
80 int ret;
82 if (argc < 2)
83 warning(_("'git rerere forget' without paths is deprecated"));
84 parse_pathspec(&pathspec, 0, PATHSPEC_PREFER_CWD,
85 prefix, argv + 1);
87 ret = rerere_forget(the_repository, &pathspec);
89 clear_pathspec(&pathspec);
90 return ret;
93 if (!strcmp(argv[0], "clear")) {
94 rerere_clear(the_repository, &merge_rr);
95 } else if (!strcmp(argv[0], "gc"))
96 rerere_gc(the_repository, &merge_rr);
97 else if (!strcmp(argv[0], "status")) {
98 if (setup_rerere(the_repository, &merge_rr,
99 flags | RERERE_READONLY) < 0)
100 return 0;
101 for (i = 0; i < merge_rr.nr; i++)
102 printf("%s\n", merge_rr.items[i].string);
103 } else if (!strcmp(argv[0], "remaining")) {
104 rerere_remaining(the_repository, &merge_rr);
105 for (i = 0; i < merge_rr.nr; i++) {
106 if (merge_rr.items[i].util != RERERE_RESOLVED)
107 printf("%s\n", merge_rr.items[i].string);
108 else
109 /* prepare for later call to
110 * string_list_clear() */
111 merge_rr.items[i].util = NULL;
113 } else if (!strcmp(argv[0], "diff")) {
114 if (setup_rerere(the_repository, &merge_rr,
115 flags | RERERE_READONLY) < 0)
116 return 0;
117 for (i = 0; i < merge_rr.nr; i++) {
118 const char *path = merge_rr.items[i].string;
119 const struct rerere_id *id = merge_rr.items[i].util;
120 if (diff_two(rerere_path(id, "preimage"), path, path, path))
121 die(_("unable to generate diff for '%s'"), rerere_path(id, NULL));
123 } else
124 usage_with_options(rerere_usage, options);
126 string_list_clear(&merge_rr, 1);
127 return 0;