Merge branch 'mh/use-wincred-from-system'
[alt-git.git] / builtin / rerere.c
blobd4bd52797f4624d31e39a2ca263c21e3130afe8f
1 #include "builtin.h"
2 #include "cache.h"
3 #include "config.h"
4 #include "dir.h"
5 #include "gettext.h"
6 #include "parse-options.h"
7 #include "repository.h"
8 #include "string-list.h"
9 #include "rerere.h"
10 #include "wrapper.h"
11 #include "xdiff/xdiff.h"
12 #include "xdiff-interface.h"
13 #include "pathspec.h"
15 static const char * const rerere_usage[] = {
16 N_("git rerere [clear | forget <pathspec>... | diff | status | remaining | gc]"),
17 NULL,
20 static int outf(void *dummy UNUSED, mmbuffer_t *ptr, int nbuf)
22 int i;
23 for (i = 0; i < nbuf; i++)
24 if (write_in_full(1, ptr[i].ptr, ptr[i].size) < 0)
25 return -1;
26 return 0;
29 static int diff_two(const char *file1, const char *label1,
30 const char *file2, const char *label2)
32 xpparam_t xpp;
33 xdemitconf_t xecfg;
34 xdemitcb_t ecb = { .out_line = outf };
35 mmfile_t minus, plus;
36 int ret;
38 if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2))
39 return -1;
41 printf("--- a/%s\n+++ b/%s\n", label1, label2);
42 fflush(stdout);
43 memset(&xpp, 0, sizeof(xpp));
44 xpp.flags = 0;
45 memset(&xecfg, 0, sizeof(xecfg));
46 xecfg.ctxlen = 3;
47 ret = xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb);
49 free(minus.ptr);
50 free(plus.ptr);
51 return ret;
54 int cmd_rerere(int argc, const char **argv, const char *prefix)
56 struct string_list merge_rr = STRING_LIST_INIT_DUP;
57 int i, autoupdate = -1, flags = 0;
59 struct option options[] = {
60 OPT_SET_INT(0, "rerere-autoupdate", &autoupdate,
61 N_("register clean resolutions in index"), 1),
62 OPT_END(),
65 argc = parse_options(argc, argv, prefix, options, rerere_usage, 0);
67 git_config(git_xmerge_config, NULL);
69 if (autoupdate == 1)
70 flags = RERERE_AUTOUPDATE;
71 if (autoupdate == 0)
72 flags = RERERE_NOAUTOUPDATE;
74 if (argc < 1)
75 return repo_rerere(the_repository, flags);
77 if (!strcmp(argv[0], "forget")) {
78 struct pathspec pathspec;
79 if (argc < 2)
80 warning(_("'git rerere forget' without paths is deprecated"));
81 parse_pathspec(&pathspec, 0, PATHSPEC_PREFER_CWD,
82 prefix, argv + 1);
83 return rerere_forget(the_repository, &pathspec);
86 if (!strcmp(argv[0], "clear")) {
87 rerere_clear(the_repository, &merge_rr);
88 } else if (!strcmp(argv[0], "gc"))
89 rerere_gc(the_repository, &merge_rr);
90 else if (!strcmp(argv[0], "status")) {
91 if (setup_rerere(the_repository, &merge_rr,
92 flags | RERERE_READONLY) < 0)
93 return 0;
94 for (i = 0; i < merge_rr.nr; i++)
95 printf("%s\n", merge_rr.items[i].string);
96 } else if (!strcmp(argv[0], "remaining")) {
97 rerere_remaining(the_repository, &merge_rr);
98 for (i = 0; i < merge_rr.nr; i++) {
99 if (merge_rr.items[i].util != RERERE_RESOLVED)
100 printf("%s\n", merge_rr.items[i].string);
101 else
102 /* prepare for later call to
103 * string_list_clear() */
104 merge_rr.items[i].util = NULL;
106 } else if (!strcmp(argv[0], "diff")) {
107 if (setup_rerere(the_repository, &merge_rr,
108 flags | RERERE_READONLY) < 0)
109 return 0;
110 for (i = 0; i < merge_rr.nr; i++) {
111 const char *path = merge_rr.items[i].string;
112 const struct rerere_id *id = merge_rr.items[i].util;
113 if (diff_two(rerere_path(id, "preimage"), path, path, path))
114 die(_("unable to generate diff for '%s'"), rerere_path(id, NULL));
116 } else
117 usage_with_options(rerere_usage, options);
119 string_list_clear(&merge_rr, 1);
120 return 0;