Merge branch 'fixes/2.45.1/2.42' into maint-2.42
[alt-git.git] / promisor-remote.c
blob7faf33b4d7cd27f62c2b115955395876e628e3a3
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 /* TODO: This should use NO_LAZY_FETCH_ENVIRONMENT */
27 if (git_env_bool("GIT_NO_LAZY_FETCH", 0)) {
28 static int warning_shown;
29 if (!warning_shown) {
30 warning_shown = 1;
31 warning(_("lazy fetching disabled; some objects may not be available"));
33 return -1;
36 child.git_cmd = 1;
37 child.in = -1;
38 if (repo != the_repository)
39 prepare_other_repo_env(&child.env, repo->gitdir);
40 strvec_pushl(&child.args, "-c", "fetch.negotiationAlgorithm=noop",
41 "fetch", remote_name, "--no-tags",
42 "--no-write-fetch-head", "--recurse-submodules=no",
43 "--filter=blob:none", "--stdin", NULL);
44 if (start_command(&child))
45 die(_("promisor-remote: unable to fork off fetch subprocess"));
46 child_in = xfdopen(child.in, "w");
48 trace2_data_intmax("promisor", repo, "fetch_count", oid_nr);
50 for (i = 0; i < oid_nr; i++) {
51 if (fputs(oid_to_hex(&oids[i]), child_in) < 0)
52 die_errno(_("promisor-remote: could not write to fetch subprocess"));
53 if (fputc('\n', child_in) < 0)
54 die_errno(_("promisor-remote: could not write to fetch subprocess"));
57 if (fclose(child_in) < 0)
58 die_errno(_("promisor-remote: could not close stdin to fetch subprocess"));
59 return finish_command(&child) ? -1 : 0;
62 static struct promisor_remote *promisor_remote_new(struct promisor_remote_config *config,
63 const char *remote_name)
65 struct promisor_remote *r;
67 if (*remote_name == '/') {
68 warning(_("promisor remote name cannot begin with '/': %s"),
69 remote_name);
70 return NULL;
73 FLEX_ALLOC_STR(r, name, remote_name);
75 *config->promisors_tail = r;
76 config->promisors_tail = &r->next;
78 return r;
81 static struct promisor_remote *promisor_remote_lookup(struct promisor_remote_config *config,
82 const char *remote_name,
83 struct promisor_remote **previous)
85 struct promisor_remote *r, *p;
87 for (p = NULL, r = config->promisors; r; p = r, r = r->next)
88 if (!strcmp(r->name, remote_name)) {
89 if (previous)
90 *previous = p;
91 return r;
94 return NULL;
97 static void promisor_remote_move_to_tail(struct promisor_remote_config *config,
98 struct promisor_remote *r,
99 struct promisor_remote *previous)
101 if (!r->next)
102 return;
104 if (previous)
105 previous->next = r->next;
106 else
107 config->promisors = r->next ? r->next : r;
108 r->next = NULL;
109 *config->promisors_tail = r;
110 config->promisors_tail = &r->next;
113 static int promisor_remote_config(const char *var, const char *value,
114 const struct config_context *ctx UNUSED,
115 void *data)
117 struct promisor_remote_config *config = data;
118 const char *name;
119 size_t namelen;
120 const char *subkey;
122 if (parse_config_key(var, "remote", &name, &namelen, &subkey) < 0)
123 return 0;
125 if (!strcmp(subkey, "promisor")) {
126 char *remote_name;
128 if (!git_config_bool(var, value))
129 return 0;
131 remote_name = xmemdupz(name, namelen);
133 if (!promisor_remote_lookup(config, remote_name, NULL))
134 promisor_remote_new(config, remote_name);
136 free(remote_name);
137 return 0;
139 if (!strcmp(subkey, "partialclonefilter")) {
140 struct promisor_remote *r;
141 char *remote_name = xmemdupz(name, namelen);
143 r = promisor_remote_lookup(config, remote_name, NULL);
144 if (!r)
145 r = promisor_remote_new(config, remote_name);
147 free(remote_name);
149 if (!r)
150 return 0;
152 return git_config_string(&r->partial_clone_filter, var, value);
155 return 0;
158 static void promisor_remote_init(struct repository *r)
160 struct promisor_remote_config *config;
162 if (r->promisor_remote_config)
163 return;
164 config = r->promisor_remote_config =
165 xcalloc(1, sizeof(*r->promisor_remote_config));
166 config->promisors_tail = &config->promisors;
168 repo_config(r, promisor_remote_config, config);
170 if (r->repository_format_partial_clone) {
171 struct promisor_remote *o, *previous;
173 o = promisor_remote_lookup(config,
174 r->repository_format_partial_clone,
175 &previous);
176 if (o)
177 promisor_remote_move_to_tail(config, o, previous);
178 else
179 promisor_remote_new(config, r->repository_format_partial_clone);
183 void promisor_remote_clear(struct promisor_remote_config *config)
185 while (config->promisors) {
186 struct promisor_remote *r = config->promisors;
187 config->promisors = config->promisors->next;
188 free(r);
191 config->promisors_tail = &config->promisors;
194 void repo_promisor_remote_reinit(struct repository *r)
196 promisor_remote_clear(r->promisor_remote_config);
197 FREE_AND_NULL(r->promisor_remote_config);
198 promisor_remote_init(r);
201 struct promisor_remote *repo_promisor_remote_find(struct repository *r,
202 const char *remote_name)
204 promisor_remote_init(r);
206 if (!remote_name)
207 return r->promisor_remote_config->promisors;
209 return promisor_remote_lookup(r->promisor_remote_config, remote_name, NULL);
212 int repo_has_promisor_remote(struct repository *r)
214 return !!repo_promisor_remote_find(r, NULL);
217 static int remove_fetched_oids(struct repository *repo,
218 struct object_id **oids,
219 int oid_nr, int to_free)
221 int i, remaining_nr = 0;
222 int *remaining = xcalloc(oid_nr, sizeof(*remaining));
223 struct object_id *old_oids = *oids;
224 struct object_id *new_oids;
226 for (i = 0; i < oid_nr; i++)
227 if (oid_object_info_extended(repo, &old_oids[i], NULL,
228 OBJECT_INFO_SKIP_FETCH_OBJECT)) {
229 remaining[i] = 1;
230 remaining_nr++;
233 if (remaining_nr) {
234 int j = 0;
235 CALLOC_ARRAY(new_oids, remaining_nr);
236 for (i = 0; i < oid_nr; i++)
237 if (remaining[i])
238 oidcpy(&new_oids[j++], &old_oids[i]);
239 *oids = new_oids;
240 if (to_free)
241 free(old_oids);
244 free(remaining);
246 return remaining_nr;
249 void promisor_remote_get_direct(struct repository *repo,
250 const struct object_id *oids,
251 int oid_nr)
253 struct promisor_remote *r;
254 struct object_id *remaining_oids = (struct object_id *)oids;
255 int remaining_nr = oid_nr;
256 int to_free = 0;
257 int i;
259 if (oid_nr == 0)
260 return;
262 promisor_remote_init(repo);
264 for (r = repo->promisor_remote_config->promisors; r; r = r->next) {
265 if (fetch_objects(repo, r->name, remaining_oids, remaining_nr) < 0) {
266 if (remaining_nr == 1)
267 continue;
268 remaining_nr = remove_fetched_oids(repo, &remaining_oids,
269 remaining_nr, to_free);
270 if (remaining_nr) {
271 to_free = 1;
272 continue;
275 goto all_fetched;
278 for (i = 0; i < remaining_nr; i++) {
279 if (is_promisor_object(&remaining_oids[i]))
280 die(_("could not fetch %s from promisor remote"),
281 oid_to_hex(&remaining_oids[i]));
284 all_fetched:
285 if (to_free)
286 free(remaining_oids);