promisor-remote: lazy-fetch objects in subprocess
[git/debian.git] / promisor-remote.c
blob6530e26f98cc4df511527a10fda2d605ac5b1e20
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"
8 static char *repository_format_partial_clone;
9 static const char *core_partial_clone_filter_default;
11 void set_repository_format_partial_clone(char *partial_clone)
13 repository_format_partial_clone = xstrdup_or_null(partial_clone);
16 static int fetch_objects(const char *remote_name,
17 const struct object_id *oids,
18 int oid_nr)
20 struct child_process child = CHILD_PROCESS_INIT;
21 int i;
22 FILE *child_in;
24 child.git_cmd = 1;
25 child.in = -1;
26 strvec_pushl(&child.args, "-c", "fetch.negotiationAlgorithm=noop",
27 "fetch", remote_name, "--no-tags",
28 "--no-write-fetch-head", "--recurse-submodules=no",
29 "--filter=blob:none", "--stdin", NULL);
30 if (start_command(&child))
31 die(_("promisor-remote: unable to fork off fetch subprocess"));
32 child_in = xfdopen(child.in, "w");
34 for (i = 0; i < oid_nr; i++) {
35 if (fputs(oid_to_hex(&oids[i]), child_in) < 0)
36 die_errno(_("promisor-remote: could not write to fetch subprocess"));
37 if (fputc('\n', child_in) < 0)
38 die_errno(_("promisor-remote: could not write to fetch subprocess"));
41 if (fclose(child_in) < 0)
42 die_errno(_("promisor-remote: could not close stdin to fetch subprocess"));
43 return finish_command(&child) ? -1 : 0;
46 static struct promisor_remote *promisors;
47 static struct promisor_remote **promisors_tail = &promisors;
49 static struct promisor_remote *promisor_remote_new(const char *remote_name)
51 struct promisor_remote *r;
53 if (*remote_name == '/') {
54 warning(_("promisor remote name cannot begin with '/': %s"),
55 remote_name);
56 return NULL;
59 FLEX_ALLOC_STR(r, name, remote_name);
61 *promisors_tail = r;
62 promisors_tail = &r->next;
64 return r;
67 static struct promisor_remote *promisor_remote_lookup(const char *remote_name,
68 struct promisor_remote **previous)
70 struct promisor_remote *r, *p;
72 for (p = NULL, r = promisors; r; p = r, r = r->next)
73 if (!strcmp(r->name, remote_name)) {
74 if (previous)
75 *previous = p;
76 return r;
79 return NULL;
82 static void promisor_remote_move_to_tail(struct promisor_remote *r,
83 struct promisor_remote *previous)
85 if (r->next == NULL)
86 return;
88 if (previous)
89 previous->next = r->next;
90 else
91 promisors = r->next ? r->next : r;
92 r->next = NULL;
93 *promisors_tail = r;
94 promisors_tail = &r->next;
97 static int promisor_remote_config(const char *var, const char *value, void *data)
99 const char *name;
100 size_t namelen;
101 const char *subkey;
103 if (!strcmp(var, "core.partialclonefilter"))
104 return git_config_string(&core_partial_clone_filter_default,
105 var, value);
107 if (parse_config_key(var, "remote", &name, &namelen, &subkey) < 0)
108 return 0;
110 if (!strcmp(subkey, "promisor")) {
111 char *remote_name;
113 if (!git_config_bool(var, value))
114 return 0;
116 remote_name = xmemdupz(name, namelen);
118 if (!promisor_remote_lookup(remote_name, NULL))
119 promisor_remote_new(remote_name);
121 free(remote_name);
122 return 0;
124 if (!strcmp(subkey, "partialclonefilter")) {
125 struct promisor_remote *r;
126 char *remote_name = xmemdupz(name, namelen);
128 r = promisor_remote_lookup(remote_name, NULL);
129 if (!r)
130 r = promisor_remote_new(remote_name);
132 free(remote_name);
134 if (!r)
135 return 0;
137 return git_config_string(&r->partial_clone_filter, var, value);
140 return 0;
143 static int initialized;
145 static void promisor_remote_init(void)
147 if (initialized)
148 return;
149 initialized = 1;
151 git_config(promisor_remote_config, NULL);
153 if (repository_format_partial_clone) {
154 struct promisor_remote *o, *previous;
156 o = promisor_remote_lookup(repository_format_partial_clone,
157 &previous);
158 if (o)
159 promisor_remote_move_to_tail(o, previous);
160 else
161 promisor_remote_new(repository_format_partial_clone);
165 static void promisor_remote_clear(void)
167 while (promisors) {
168 struct promisor_remote *r = promisors;
169 promisors = promisors->next;
170 free(r);
173 promisors_tail = &promisors;
176 void promisor_remote_reinit(void)
178 initialized = 0;
179 promisor_remote_clear();
180 promisor_remote_init();
183 struct promisor_remote *promisor_remote_find(const char *remote_name)
185 promisor_remote_init();
187 if (!remote_name)
188 return promisors;
190 return promisor_remote_lookup(remote_name, NULL);
193 int has_promisor_remote(void)
195 return !!promisor_remote_find(NULL);
198 static int remove_fetched_oids(struct repository *repo,
199 struct object_id **oids,
200 int oid_nr, int to_free)
202 int i, remaining_nr = 0;
203 int *remaining = xcalloc(oid_nr, sizeof(*remaining));
204 struct object_id *old_oids = *oids;
205 struct object_id *new_oids;
207 for (i = 0; i < oid_nr; i++)
208 if (oid_object_info_extended(repo, &old_oids[i], NULL,
209 OBJECT_INFO_SKIP_FETCH_OBJECT)) {
210 remaining[i] = 1;
211 remaining_nr++;
214 if (remaining_nr) {
215 int j = 0;
216 new_oids = xcalloc(remaining_nr, sizeof(*new_oids));
217 for (i = 0; i < oid_nr; i++)
218 if (remaining[i])
219 oidcpy(&new_oids[j++], &old_oids[i]);
220 *oids = new_oids;
221 if (to_free)
222 free(old_oids);
225 free(remaining);
227 return remaining_nr;
230 int promisor_remote_get_direct(struct repository *repo,
231 const struct object_id *oids,
232 int oid_nr)
234 struct promisor_remote *r;
235 struct object_id *remaining_oids = (struct object_id *)oids;
236 int remaining_nr = oid_nr;
237 int to_free = 0;
238 int res = -1;
240 if (oid_nr == 0)
241 return 0;
243 promisor_remote_init();
245 for (r = promisors; r; r = r->next) {
246 if (fetch_objects(r->name, remaining_oids, remaining_nr) < 0) {
247 if (remaining_nr == 1)
248 continue;
249 remaining_nr = remove_fetched_oids(repo, &remaining_oids,
250 remaining_nr, to_free);
251 if (remaining_nr) {
252 to_free = 1;
253 continue;
256 res = 0;
257 break;
260 if (to_free)
261 free(remaining_oids);
263 return res;