]> git.ipfire.org Git - thirdparty/git.git/blob - promisor-remote.c
Git 2.45
[thirdparty/git.git] / promisor-remote.c
1 #include "git-compat-util.h"
2 #include "gettext.h"
3 #include "hex.h"
4 #include "object-store-ll.h"
5 #include "promisor-remote.h"
6 #include "config.h"
7 #include "trace2.h"
8 #include "transport.h"
9 #include "strvec.h"
10 #include "packfile.h"
11
12 struct promisor_remote_config {
13 struct promisor_remote *promisors;
14 struct promisor_remote **promisors_tail;
15 };
16
17 static int fetch_objects(struct repository *repo,
18 const char *remote_name,
19 const struct object_id *oids,
20 int oid_nr)
21 {
22 struct child_process child = CHILD_PROCESS_INIT;
23 int i;
24 FILE *child_in;
25
26 child.git_cmd = 1;
27 child.in = -1;
28 if (repo != the_repository)
29 prepare_other_repo_env(&child.env, repo->gitdir);
30 strvec_pushl(&child.args, "-c", "fetch.negotiationAlgorithm=noop",
31 "fetch", remote_name, "--no-tags",
32 "--no-write-fetch-head", "--recurse-submodules=no",
33 "--filter=blob:none", "--stdin", NULL);
34 if (start_command(&child))
35 die(_("promisor-remote: unable to fork off fetch subprocess"));
36 child_in = xfdopen(child.in, "w");
37
38 trace2_data_intmax("promisor", repo, "fetch_count", oid_nr);
39
40 for (i = 0; i < oid_nr; i++) {
41 if (fputs(oid_to_hex(&oids[i]), child_in) < 0)
42 die_errno(_("promisor-remote: could not write to fetch subprocess"));
43 if (fputc('\n', child_in) < 0)
44 die_errno(_("promisor-remote: could not write to fetch subprocess"));
45 }
46
47 if (fclose(child_in) < 0)
48 die_errno(_("promisor-remote: could not close stdin to fetch subprocess"));
49 return finish_command(&child) ? -1 : 0;
50 }
51
52 static struct promisor_remote *promisor_remote_new(struct promisor_remote_config *config,
53 const char *remote_name)
54 {
55 struct promisor_remote *r;
56
57 if (*remote_name == '/') {
58 warning(_("promisor remote name cannot begin with '/': %s"),
59 remote_name);
60 return NULL;
61 }
62
63 FLEX_ALLOC_STR(r, name, remote_name);
64
65 *config->promisors_tail = r;
66 config->promisors_tail = &r->next;
67
68 return r;
69 }
70
71 static struct promisor_remote *promisor_remote_lookup(struct promisor_remote_config *config,
72 const char *remote_name,
73 struct promisor_remote **previous)
74 {
75 struct promisor_remote *r, *p;
76
77 for (p = NULL, r = config->promisors; r; p = r, r = r->next)
78 if (!strcmp(r->name, remote_name)) {
79 if (previous)
80 *previous = p;
81 return r;
82 }
83
84 return NULL;
85 }
86
87 static void promisor_remote_move_to_tail(struct promisor_remote_config *config,
88 struct promisor_remote *r,
89 struct promisor_remote *previous)
90 {
91 if (!r->next)
92 return;
93
94 if (previous)
95 previous->next = r->next;
96 else
97 config->promisors = r->next ? r->next : r;
98 r->next = NULL;
99 *config->promisors_tail = r;
100 config->promisors_tail = &r->next;
101 }
102
103 static int promisor_remote_config(const char *var, const char *value,
104 const struct config_context *ctx UNUSED,
105 void *data)
106 {
107 struct promisor_remote_config *config = data;
108 const char *name;
109 size_t namelen;
110 const char *subkey;
111
112 if (parse_config_key(var, "remote", &name, &namelen, &subkey) < 0)
113 return 0;
114
115 if (!strcmp(subkey, "promisor")) {
116 char *remote_name;
117
118 if (!git_config_bool(var, value))
119 return 0;
120
121 remote_name = xmemdupz(name, namelen);
122
123 if (!promisor_remote_lookup(config, remote_name, NULL))
124 promisor_remote_new(config, remote_name);
125
126 free(remote_name);
127 return 0;
128 }
129 if (!strcmp(subkey, "partialclonefilter")) {
130 struct promisor_remote *r;
131 char *remote_name = xmemdupz(name, namelen);
132
133 r = promisor_remote_lookup(config, remote_name, NULL);
134 if (!r)
135 r = promisor_remote_new(config, remote_name);
136
137 free(remote_name);
138
139 if (!r)
140 return 0;
141
142 return git_config_string(&r->partial_clone_filter, var, value);
143 }
144
145 return 0;
146 }
147
148 static void promisor_remote_init(struct repository *r)
149 {
150 struct promisor_remote_config *config;
151
152 if (r->promisor_remote_config)
153 return;
154 config = r->promisor_remote_config =
155 xcalloc(1, sizeof(*r->promisor_remote_config));
156 config->promisors_tail = &config->promisors;
157
158 repo_config(r, promisor_remote_config, config);
159
160 if (r->repository_format_partial_clone) {
161 struct promisor_remote *o, *previous;
162
163 o = promisor_remote_lookup(config,
164 r->repository_format_partial_clone,
165 &previous);
166 if (o)
167 promisor_remote_move_to_tail(config, o, previous);
168 else
169 promisor_remote_new(config, r->repository_format_partial_clone);
170 }
171 }
172
173 void promisor_remote_clear(struct promisor_remote_config *config)
174 {
175 while (config->promisors) {
176 struct promisor_remote *r = config->promisors;
177 config->promisors = config->promisors->next;
178 free(r);
179 }
180
181 config->promisors_tail = &config->promisors;
182 }
183
184 void repo_promisor_remote_reinit(struct repository *r)
185 {
186 promisor_remote_clear(r->promisor_remote_config);
187 FREE_AND_NULL(r->promisor_remote_config);
188 promisor_remote_init(r);
189 }
190
191 struct promisor_remote *repo_promisor_remote_find(struct repository *r,
192 const char *remote_name)
193 {
194 promisor_remote_init(r);
195
196 if (!remote_name)
197 return r->promisor_remote_config->promisors;
198
199 return promisor_remote_lookup(r->promisor_remote_config, remote_name, NULL);
200 }
201
202 int repo_has_promisor_remote(struct repository *r)
203 {
204 return !!repo_promisor_remote_find(r, NULL);
205 }
206
207 static int remove_fetched_oids(struct repository *repo,
208 struct object_id **oids,
209 int oid_nr, int to_free)
210 {
211 int i, remaining_nr = 0;
212 int *remaining = xcalloc(oid_nr, sizeof(*remaining));
213 struct object_id *old_oids = *oids;
214 struct object_id *new_oids;
215
216 for (i = 0; i < oid_nr; i++)
217 if (oid_object_info_extended(repo, &old_oids[i], NULL,
218 OBJECT_INFO_SKIP_FETCH_OBJECT)) {
219 remaining[i] = 1;
220 remaining_nr++;
221 }
222
223 if (remaining_nr) {
224 int j = 0;
225 CALLOC_ARRAY(new_oids, remaining_nr);
226 for (i = 0; i < oid_nr; i++)
227 if (remaining[i])
228 oidcpy(&new_oids[j++], &old_oids[i]);
229 *oids = new_oids;
230 if (to_free)
231 free(old_oids);
232 }
233
234 free(remaining);
235
236 return remaining_nr;
237 }
238
239 void promisor_remote_get_direct(struct repository *repo,
240 const struct object_id *oids,
241 int oid_nr)
242 {
243 struct promisor_remote *r;
244 struct object_id *remaining_oids = (struct object_id *)oids;
245 int remaining_nr = oid_nr;
246 int to_free = 0;
247 int i;
248
249 if (oid_nr == 0)
250 return;
251
252 promisor_remote_init(repo);
253
254 for (r = repo->promisor_remote_config->promisors; r; r = r->next) {
255 if (fetch_objects(repo, r->name, remaining_oids, remaining_nr) < 0) {
256 if (remaining_nr == 1)
257 continue;
258 remaining_nr = remove_fetched_oids(repo, &remaining_oids,
259 remaining_nr, to_free);
260 if (remaining_nr) {
261 to_free = 1;
262 continue;
263 }
264 }
265 goto all_fetched;
266 }
267
268 for (i = 0; i < remaining_nr; i++) {
269 if (is_promisor_object(&remaining_oids[i]))
270 die(_("could not fetch %s from promisor remote"),
271 oid_to_hex(&remaining_oids[i]));
272 }
273
274 all_fetched:
275 if (to_free)
276 free(remaining_oids);
277 }