clone_submodule: avoid using `access()` on directories
[git.git] / promisor-remote.c
blob550a38f75261a0de97ecb4b7cf4452b0600d34e6
1 #include "cache.h"
2 #include "object-store.h"
3 #include "promisor-remote.h"
4 #include "config.h"
5 #include "transport.h"
6 #include "strvec.h"
7 #include "packfile.h"
9 struct promisor_remote_config {
10 struct promisor_remote *promisors;
11 struct promisor_remote **promisors_tail;
14 static int fetch_objects(struct repository *repo,
15 const char *remote_name,
16 const struct object_id *oids,
17 int oid_nr)
19 struct child_process child = CHILD_PROCESS_INIT;
20 int i;
21 FILE *child_in;
23 /* TODO: This should use NO_LAZY_FETCH_ENVIRONMENT */
24 if (git_env_bool("GIT_NO_LAZY_FETCH", 0)) {
25 static int warning_shown;
26 if (!warning_shown) {
27 warning_shown = 1;
28 warning(_("lazy fetching disabled; some objects may not be available"));
30 return -1;
33 child.git_cmd = 1;
34 child.in = -1;
35 if (repo != the_repository)
36 prepare_other_repo_env(&child.env, repo->gitdir);
37 strvec_pushl(&child.args, "-c", "fetch.negotiationAlgorithm=noop",
38 "fetch", remote_name, "--no-tags",
39 "--no-write-fetch-head", "--recurse-submodules=no",
40 "--filter=blob:none", "--stdin", NULL);
41 if (start_command(&child))
42 die(_("promisor-remote: unable to fork off fetch subprocess"));
43 child_in = xfdopen(child.in, "w");
45 trace2_data_intmax("promisor", repo, "fetch_count", oid_nr);
47 for (i = 0; i < oid_nr; i++) {
48 if (fputs(oid_to_hex(&oids[i]), child_in) < 0)
49 die_errno(_("promisor-remote: could not write to fetch subprocess"));
50 if (fputc('\n', child_in) < 0)
51 die_errno(_("promisor-remote: could not write to fetch subprocess"));
54 if (fclose(child_in) < 0)
55 die_errno(_("promisor-remote: could not close stdin to fetch subprocess"));
56 return finish_command(&child) ? -1 : 0;
59 static struct promisor_remote *promisor_remote_new(struct promisor_remote_config *config,
60 const char *remote_name)
62 struct promisor_remote *r;
64 if (*remote_name == '/') {
65 warning(_("promisor remote name cannot begin with '/': %s"),
66 remote_name);
67 return NULL;
70 FLEX_ALLOC_STR(r, name, remote_name);
72 *config->promisors_tail = r;
73 config->promisors_tail = &r->next;
75 return r;
78 static struct promisor_remote *promisor_remote_lookup(struct promisor_remote_config *config,
79 const char *remote_name,
80 struct promisor_remote **previous)
82 struct promisor_remote *r, *p;
84 for (p = NULL, r = config->promisors; r; p = r, r = r->next)
85 if (!strcmp(r->name, remote_name)) {
86 if (previous)
87 *previous = p;
88 return r;
91 return NULL;
94 static void promisor_remote_move_to_tail(struct promisor_remote_config *config,
95 struct promisor_remote *r,
96 struct promisor_remote *previous)
98 if (!r->next)
99 return;
101 if (previous)
102 previous->next = r->next;
103 else
104 config->promisors = r->next ? r->next : r;
105 r->next = NULL;
106 *config->promisors_tail = r;
107 config->promisors_tail = &r->next;
110 static int promisor_remote_config(const char *var, const char *value, void *data)
112 struct promisor_remote_config *config = data;
113 const char *name;
114 size_t namelen;
115 const char *subkey;
117 if (parse_config_key(var, "remote", &name, &namelen, &subkey) < 0)
118 return 0;
120 if (!strcmp(subkey, "promisor")) {
121 char *remote_name;
123 if (!git_config_bool(var, value))
124 return 0;
126 remote_name = xmemdupz(name, namelen);
128 if (!promisor_remote_lookup(config, remote_name, NULL))
129 promisor_remote_new(config, remote_name);
131 free(remote_name);
132 return 0;
134 if (!strcmp(subkey, "partialclonefilter")) {
135 struct promisor_remote *r;
136 char *remote_name = xmemdupz(name, namelen);
138 r = promisor_remote_lookup(config, remote_name, NULL);
139 if (!r)
140 r = promisor_remote_new(config, remote_name);
142 free(remote_name);
144 if (!r)
145 return 0;
147 return git_config_string(&r->partial_clone_filter, var, value);
150 return 0;
153 static void promisor_remote_init(struct repository *r)
155 struct promisor_remote_config *config;
157 if (r->promisor_remote_config)
158 return;
159 config = r->promisor_remote_config =
160 xcalloc(1, sizeof(*r->promisor_remote_config));
161 config->promisors_tail = &config->promisors;
163 repo_config(r, promisor_remote_config, config);
165 if (r->repository_format_partial_clone) {
166 struct promisor_remote *o, *previous;
168 o = promisor_remote_lookup(config,
169 r->repository_format_partial_clone,
170 &previous);
171 if (o)
172 promisor_remote_move_to_tail(config, o, previous);
173 else
174 promisor_remote_new(config, r->repository_format_partial_clone);
178 void promisor_remote_clear(struct promisor_remote_config *config)
180 while (config->promisors) {
181 struct promisor_remote *r = config->promisors;
182 config->promisors = config->promisors->next;
183 free(r);
186 config->promisors_tail = &config->promisors;
189 void repo_promisor_remote_reinit(struct repository *r)
191 promisor_remote_clear(r->promisor_remote_config);
192 FREE_AND_NULL(r->promisor_remote_config);
193 promisor_remote_init(r);
196 struct promisor_remote *repo_promisor_remote_find(struct repository *r,
197 const char *remote_name)
199 promisor_remote_init(r);
201 if (!remote_name)
202 return r->promisor_remote_config->promisors;
204 return promisor_remote_lookup(r->promisor_remote_config, remote_name, NULL);
207 int repo_has_promisor_remote(struct repository *r)
209 return !!repo_promisor_remote_find(r, NULL);
212 static int remove_fetched_oids(struct repository *repo,
213 struct object_id **oids,
214 int oid_nr, int to_free)
216 int i, remaining_nr = 0;
217 int *remaining = xcalloc(oid_nr, sizeof(*remaining));
218 struct object_id *old_oids = *oids;
219 struct object_id *new_oids;
221 for (i = 0; i < oid_nr; i++)
222 if (oid_object_info_extended(repo, &old_oids[i], NULL,
223 OBJECT_INFO_SKIP_FETCH_OBJECT)) {
224 remaining[i] = 1;
225 remaining_nr++;
228 if (remaining_nr) {
229 int j = 0;
230 CALLOC_ARRAY(new_oids, remaining_nr);
231 for (i = 0; i < oid_nr; i++)
232 if (remaining[i])
233 oidcpy(&new_oids[j++], &old_oids[i]);
234 *oids = new_oids;
235 if (to_free)
236 free(old_oids);
239 free(remaining);
241 return remaining_nr;
244 void promisor_remote_get_direct(struct repository *repo,
245 const struct object_id *oids,
246 int oid_nr)
248 struct promisor_remote *r;
249 struct object_id *remaining_oids = (struct object_id *)oids;
250 int remaining_nr = oid_nr;
251 int to_free = 0;
252 int i;
254 if (oid_nr == 0)
255 return;
257 promisor_remote_init(repo);
259 for (r = repo->promisor_remote_config->promisors; r; r = r->next) {
260 if (fetch_objects(repo, r->name, remaining_oids, remaining_nr) < 0) {
261 if (remaining_nr == 1)
262 continue;
263 remaining_nr = remove_fetched_oids(repo, &remaining_oids,
264 remaining_nr, to_free);
265 if (remaining_nr) {
266 to_free = 1;
267 continue;
270 goto all_fetched;
273 for (i = 0; i < remaining_nr; i++) {
274 if (is_promisor_object(&remaining_oids[i]))
275 die(_("could not fetch %s from promisor remote"),
276 oid_to_hex(&remaining_oids[i]));
279 all_fetched:
280 if (to_free)
281 free(remaining_oids);