t4301: verify that merge-tree fails on missing blob objects
[git/gitster.git] / promisor-remote.c
blobac3aa1e365760596990188548410a16900e005d7
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"
12 struct promisor_remote_config {
13 struct promisor_remote *promisors;
14 struct promisor_remote **promisors_tail;
17 static int fetch_objects(struct repository *repo,
18 const char *remote_name,
19 const struct object_id *oids,
20 int oid_nr)
22 struct child_process child = CHILD_PROCESS_INIT;
23 int i;
24 FILE *child_in;
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");
38 trace2_data_intmax("promisor", repo, "fetch_count", oid_nr);
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"));
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;
52 static struct promisor_remote *promisor_remote_new(struct promisor_remote_config *config,
53 const char *remote_name)
55 struct promisor_remote *r;
57 if (*remote_name == '/') {
58 warning(_("promisor remote name cannot begin with '/': %s"),
59 remote_name);
60 return NULL;
63 FLEX_ALLOC_STR(r, name, remote_name);
65 *config->promisors_tail = r;
66 config->promisors_tail = &r->next;
68 return r;
71 static struct promisor_remote *promisor_remote_lookup(struct promisor_remote_config *config,
72 const char *remote_name,
73 struct promisor_remote **previous)
75 struct promisor_remote *r, *p;
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;
84 return NULL;
87 static void promisor_remote_move_to_tail(struct promisor_remote_config *config,
88 struct promisor_remote *r,
89 struct promisor_remote *previous)
91 if (!r->next)
92 return;
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;
103 static int promisor_remote_config(const char *var, const char *value,
104 const struct config_context *ctx UNUSED,
105 void *data)
107 struct promisor_remote_config *config = data;
108 const char *name;
109 size_t namelen;
110 const char *subkey;
112 if (parse_config_key(var, "remote", &name, &namelen, &subkey) < 0)
113 return 0;
115 if (!strcmp(subkey, "promisor")) {
116 char *remote_name;
118 if (!git_config_bool(var, value))
119 return 0;
121 remote_name = xmemdupz(name, namelen);
123 if (!promisor_remote_lookup(config, remote_name, NULL))
124 promisor_remote_new(config, remote_name);
126 free(remote_name);
127 return 0;
129 if (!strcmp(subkey, "partialclonefilter")) {
130 struct promisor_remote *r;
131 char *remote_name = xmemdupz(name, namelen);
133 r = promisor_remote_lookup(config, remote_name, NULL);
134 if (!r)
135 r = promisor_remote_new(config, remote_name);
137 free(remote_name);
139 if (!r)
140 return 0;
142 return git_config_string(&r->partial_clone_filter, var, value);
145 return 0;
148 static void promisor_remote_init(struct repository *r)
150 struct promisor_remote_config *config;
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;
158 repo_config(r, promisor_remote_config, config);
160 if (r->repository_format_partial_clone) {
161 struct promisor_remote *o, *previous;
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);
173 void promisor_remote_clear(struct promisor_remote_config *config)
175 while (config->promisors) {
176 struct promisor_remote *r = config->promisors;
177 config->promisors = config->promisors->next;
178 free(r);
181 config->promisors_tail = &config->promisors;
184 void repo_promisor_remote_reinit(struct repository *r)
186 promisor_remote_clear(r->promisor_remote_config);
187 FREE_AND_NULL(r->promisor_remote_config);
188 promisor_remote_init(r);
191 struct promisor_remote *repo_promisor_remote_find(struct repository *r,
192 const char *remote_name)
194 promisor_remote_init(r);
196 if (!remote_name)
197 return r->promisor_remote_config->promisors;
199 return promisor_remote_lookup(r->promisor_remote_config, remote_name, NULL);
202 int repo_has_promisor_remote(struct repository *r)
204 return !!repo_promisor_remote_find(r, NULL);
207 static int remove_fetched_oids(struct repository *repo,
208 struct object_id **oids,
209 int oid_nr, int to_free)
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;
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++;
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);
234 free(remaining);
236 return remaining_nr;
239 void promisor_remote_get_direct(struct repository *repo,
240 const struct object_id *oids,
241 int oid_nr)
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;
249 if (oid_nr == 0)
250 return;
252 promisor_remote_init(repo);
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;
265 goto all_fetched;
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]));
274 all_fetched:
275 if (to_free)
276 free(remaining_oids);