interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / builtin / rerere.c

151 lines · 3984 bytes Raw Blame History
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, mmfile_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))
38 return -1;
39 if (read_mmfile(&plus, file2)) {
40 free(minus.ptr);
41 return -1;
42 }
43
44 printf("--- a/%s\n+++ b/%s\n", label1, label2);
45 fflush(stdout);
46 memset(&xpp, 0, sizeof(xpp));
47 xpp.flags = 0;
48 memset(&xecfg, 0, sizeof(xecfg));
49 xecfg.ctxlen = 3;
50 ret = xdi_diff(&minus, &plus, &xpp, &xecfg, &ecb);
51
52 free(minus.ptr);
53 free(plus.ptr);
54 return ret;
55}
56
57int cmd_rerere(int argc,
58 const char **argv,
59 const char *prefix,
60 struct repository *repo UNUSED)
61{
62 struct string_list merge_rr = STRING_LIST_INIT_DUP;
63 int autoupdate = -1, skip_locked = 0, flags = 0;
64
65 struct option options[] = {
66 OPT_SET_INT(0, "rerere-autoupdate", &autoupdate,
67 N_("register clean resolutions in index"), 1),
68 OPT_HIDDEN_BOOL(0, "skip-locked", &skip_locked,
69 N_("skip gc while another process holds the lock")),
70 OPT_END(),
71 };
72
73 argc = parse_options(argc, argv, prefix, options, rerere_usage, 0);
74
75 if (skip_locked && (argc < 1 || strcmp(argv[0], "gc")))
76 die(_("the option '%s' requires '%s'"), "--skip-locked", "gc");
77
78 repo_config(the_repository, git_xmerge_config, NULL);
79
80 if (autoupdate == 1)
81 flags = RERERE_AUTOUPDATE;
82 if (autoupdate == 0)
83 flags = RERERE_NOAUTOUPDATE;
84
85 if (argc < 1)
86 return repo_rerere(the_repository, flags);
87
88 if (!strcmp(argv[0], "forget")) {
89 struct pathspec pathspec;
90 int ret;
91
92 if (argc < 2)
93 warning(_("'git rerere forget' without paths is deprecated"));
94 parse_pathspec(&pathspec, 0, PATHSPEC_PREFER_CWD,
95 prefix, argv + 1);
96
97 ret = rerere_forget(the_repository, &pathspec);
98
99 clear_pathspec(&pathspec);
100 return ret;
101 }
102
103 if (!strcmp(argv[0], "clear")) {
104 rerere_clear(the_repository, &merge_rr);
105 } else if (!strcmp(argv[0], "gc"))
106 rerere_gc(the_repository, &merge_rr,
107 skip_locked ? RERERE_NOWAIT : 0);
108 else if (!strcmp(argv[0], "status")) {
109 struct rerere_cache cache;
110
111 if (rerere_cache_setup(&cache, the_repository, &merge_rr,
112 flags | RERERE_READONLY) < 0)
113 return 0;
114 for (size_t i = 0; i < merge_rr.nr; i++)
115 printf("%s\n", merge_rr.items[i].string);
116
117 rerere_cache_release(&cache);
118 } else if (!strcmp(argv[0], "remaining")) {
119 rerere_remaining(the_repository, &merge_rr);
120 for (size_t i = 0; i < merge_rr.nr; i++) {
121 if (merge_rr.items[i].util != RERERE_RESOLVED)
122 printf("%s\n", merge_rr.items[i].string);
123 else
124 /* prepare for later call to
125 * string_list_clear() */
126 merge_rr.items[i].util = NULL;
127 }
128 } else if (!strcmp(argv[0], "diff")) {
129 struct strbuf buf = STRBUF_INIT;
130 struct rerere_cache cache;
131
132 if (rerere_cache_setup(&cache, the_repository, &merge_rr,
133 flags | RERERE_READONLY) < 0)
134 return 0;
135 for (size_t i = 0; i < merge_rr.nr; i++) {
136 const char *path = merge_rr.items[i].string;
137 const struct rerere_id *id = merge_rr.items[i].util;
138 if (diff_two(rerere_path(the_repository, &buf, id, "preimage"),
139 path, path, path))
140 die(_("unable to generate diff for '%s'"),
141 rerere_path(the_repository, &buf, id, NULL));
142 }
143
144 rerere_cache_release(&cache);
145 strbuf_release(&buf);
146 } else
147 usage_with_options(rerere_usage, options);
148
149 string_list_clear(&merge_rr, 1);
150 return 0;
151}