]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/rerere.c
strmap: enable allocations to come from a mem_pool
[thirdparty/git.git] / builtin / rerere.c
CommitLineData
baffc0e7 1#include "builtin.h"
658f3650 2#include "cache.h"
b2141fc1 3#include "config.h"
8ca12c0d 4#include "dir.h"
672d1b78 5#include "parse-options.h"
c455c87c 6#include "string-list.h"
5b2fd956 7#include "rerere.h"
658f3650
JS
8#include "xdiff/xdiff.h"
9#include "xdiff-interface.h"
01a10b0a 10#include "pathspec.h"
658f3650 11
672d1b78 12static const char * const rerere_usage[] = {
9c9b4f2f 13 N_("git rerere [clear | forget <path>... | status | remaining | diff | gc]"),
672d1b78
JN
14 NULL,
15};
658f3650 16
658f3650
JS
17static int outf(void *dummy, mmbuffer_t *ptr, int nbuf)
18{
19 int i;
20 for (i = 0; i < nbuf; i++)
06f46f23 21 if (write_in_full(1, ptr[i].ptr, ptr[i].size) < 0)
93822c22 22 return -1;
658f3650
JS
23 return 0;
24}
25
26static int diff_two(const char *file1, const char *label1,
27 const char *file2, const char *label2)
28{
29 xpparam_t xpp;
30 xdemitconf_t xecfg;
31 xdemitcb_t ecb;
32 mmfile_t minus, plus;
3efb9880 33 int ret;
658f3650
JS
34
35 if (read_mmfile(&minus, file1) || read_mmfile(&plus, file2))
3efb9880 36 return -1;
658f3650
JS
37
38 printf("--- a/%s\n+++ b/%s\n", label1, label2);
39 fflush(stdout);
9ccd0a88 40 memset(&xpp, 0, sizeof(xpp));
582aa00b 41 xpp.flags = 0;
30b25010 42 memset(&xecfg, 0, sizeof(xecfg));
658f3650 43 xecfg.ctxlen = 3;
611e42a5
JK
44 ecb.out_hunk = NULL;
45 ecb.out_line = outf;
3efb9880 46 ret = xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb);
658f3650
JS
47
48 free(minus.ptr);
49 free(plus.ptr);
3efb9880 50 return ret;
658f3650
JS
51}
52
d8b7db0a
KH
53int cmd_rerere(int argc, const char **argv, const char *prefix)
54{
183113a5 55 struct string_list merge_rr = STRING_LIST_INIT_DUP;
9dd330e6 56 int i, autoupdate = -1, flags = 0;
672d1b78
JN
57
58 struct option options[] = {
59 OPT_SET_INT(0, "rerere-autoupdate", &autoupdate,
0ff07f24 60 N_("register clean resolutions in index"), 1),
672d1b78
JN
61 OPT_END(),
62 };
63
64 argc = parse_options(argc, argv, prefix, options, rerere_usage, 0);
65
de3d8bb7
FC
66 git_config(git_xmerge_config, NULL);
67
672d1b78
JN
68 if (autoupdate == 1)
69 flags = RERERE_AUTOUPDATE;
70 if (autoupdate == 0)
71 flags = RERERE_NOAUTOUPDATE;
72
73 if (argc < 1)
35843b11 74 return repo_rerere(the_repository, flags);
99caeed0 75
672d1b78 76 if (!strcmp(argv[0], "forget")) {
01a10b0a 77 struct pathspec pathspec;
5d2c3b01 78 if (argc < 2)
2373b650 79 warning(_("'git rerere forget' without paths is deprecated"));
01a10b0a
NTND
80 parse_pathspec(&pathspec, 0, PATHSPEC_PREFER_CWD,
81 prefix, argv + 1);
35843b11 82 return rerere_forget(the_repository, &pathspec);
5a9f0395 83 }
99caeed0 84
672d1b78 85 if (!strcmp(argv[0], "clear")) {
55e6b354 86 rerere_clear(the_repository, &merge_rr);
672d1b78 87 } else if (!strcmp(argv[0], "gc"))
55e6b354 88 rerere_gc(the_repository, &merge_rr);
9dd330e6 89 else if (!strcmp(argv[0], "status")) {
55e6b354
NTND
90 if (setup_rerere(the_repository, &merge_rr,
91 flags | RERERE_READONLY) < 0)
9dd330e6 92 return 0;
658f3650 93 for (i = 0; i < merge_rr.nr; i++)
c455c87c 94 printf("%s\n", merge_rr.items[i].string);
9dd330e6 95 } else if (!strcmp(argv[0], "remaining")) {
35843b11 96 rerere_remaining(the_repository, &merge_rr);
ac49f5ca
MZ
97 for (i = 0; i < merge_rr.nr; i++) {
98 if (merge_rr.items[i].util != RERERE_RESOLVED)
99 printf("%s\n", merge_rr.items[i].string);
100 else
101 /* prepare for later call to
102 * string_list_clear() */
103 merge_rr.items[i].util = NULL;
104 }
9dd330e6 105 } else if (!strcmp(argv[0], "diff")) {
55e6b354
NTND
106 if (setup_rerere(the_repository, &merge_rr,
107 flags | RERERE_READONLY) < 0)
9dd330e6 108 return 0;
658f3650 109 for (i = 0; i < merge_rr.nr; i++) {
c455c87c 110 const char *path = merge_rr.items[i].string;
1d51eced 111 const struct rerere_id *id = merge_rr.items[i].util;
590f6e42 112 if (diff_two(rerere_path(id, "preimage"), path, path, path))
2373b650 113 die(_("unable to generate diff for '%s'"), rerere_path(id, NULL));
658f3650 114 }
9dd330e6 115 } else
672d1b78 116 usage_with_options(rerere_usage, options);
658f3650 117
c455c87c 118 string_list_clear(&merge_rr, 1);
658f3650
JS
119 return 0;
120}