commit-graph: return generation from memory
[alt-git.git] / promisor-remote.c
blobfaa7612941cc2a70bf24f9dff2c02f2c5a2c0428
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 child.git_cmd = 1;
24 child.in = -1;
25 if (repo != the_repository)
26 prepare_other_repo_env(&child.env, repo->gitdir);
27 strvec_pushl(&child.args, "-c", "fetch.negotiationAlgorithm=noop",
28 "fetch", remote_name, "--no-tags",
29 "--no-write-fetch-head", "--recurse-submodules=no",
30 "--filter=blob:none", "--stdin", NULL);
31 if (start_command(&child))
32 die(_("promisor-remote: unable to fork off fetch subprocess"));
33 child_in = xfdopen(child.in, "w");
35 trace2_data_intmax("promisor", repo, "fetch_count", oid_nr);
37 for (i = 0; i < oid_nr; i++) {
38 if (fputs(oid_to_hex(&oids[i]), child_in) < 0)
39 die_errno(_("promisor-remote: could not write to fetch subprocess"));
40 if (fputc('\n', child_in) < 0)
41 die_errno(_("promisor-remote: could not write to fetch subprocess"));
44 if (fclose(child_in) < 0)
45 die_errno(_("promisor-remote: could not close stdin to fetch subprocess"));
46 return finish_command(&child) ? -1 : 0;
49 static struct promisor_remote *promisor_remote_new(struct promisor_remote_config *config,
50 const char *remote_name)
52 struct promisor_remote *r;
54 if (*remote_name == '/') {
55 warning(_("promisor remote name cannot begin with '/': %s"),
56 remote_name);
57 return NULL;
60 FLEX_ALLOC_STR(r, name, remote_name);
62 *config->promisors_tail = r;
63 config->promisors_tail = &r->next;
65 return r;
68 static struct promisor_remote *promisor_remote_lookup(struct promisor_remote_config *config,
69 const char *remote_name,
70 struct promisor_remote **previous)
72 struct promisor_remote *r, *p;
74 for (p = NULL, r = config->promisors; r; p = r, r = r->next)
75 if (!strcmp(r->name, remote_name)) {
76 if (previous)
77 *previous = p;
78 return r;
81 return NULL;
84 static void promisor_remote_move_to_tail(struct promisor_remote_config *config,
85 struct promisor_remote *r,
86 struct promisor_remote *previous)
88 if (!r->next)
89 return;
91 if (previous)
92 previous->next = r->next;
93 else
94 config->promisors = r->next ? r->next : r;
95 r->next = NULL;
96 *config->promisors_tail = r;
97 config->promisors_tail = &r->next;
100 static int promisor_remote_config(const char *var, const char *value, void *data)
102 struct promisor_remote_config *config = data;
103 const char *name;
104 size_t namelen;
105 const char *subkey;
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(config, remote_name, NULL))
119 promisor_remote_new(config, 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(config, remote_name, NULL);
129 if (!r)
130 r = promisor_remote_new(config, 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 void promisor_remote_init(struct repository *r)
145 struct promisor_remote_config *config;
147 if (r->promisor_remote_config)
148 return;
149 config = r->promisor_remote_config =
150 xcalloc(1, sizeof(*r->promisor_remote_config));
151 config->promisors_tail = &config->promisors;
153 repo_config(r, promisor_remote_config, config);
155 if (r->repository_format_partial_clone) {
156 struct promisor_remote *o, *previous;
158 o = promisor_remote_lookup(config,
159 r->repository_format_partial_clone,
160 &previous);
161 if (o)
162 promisor_remote_move_to_tail(config, o, previous);
163 else
164 promisor_remote_new(config, r->repository_format_partial_clone);
168 void promisor_remote_clear(struct promisor_remote_config *config)
170 while (config->promisors) {
171 struct promisor_remote *r = config->promisors;
172 config->promisors = config->promisors->next;
173 free(r);
176 config->promisors_tail = &config->promisors;
179 void repo_promisor_remote_reinit(struct repository *r)
181 promisor_remote_clear(r->promisor_remote_config);
182 FREE_AND_NULL(r->promisor_remote_config);
183 promisor_remote_init(r);
186 struct promisor_remote *repo_promisor_remote_find(struct repository *r,
187 const char *remote_name)
189 promisor_remote_init(r);
191 if (!remote_name)
192 return r->promisor_remote_config->promisors;
194 return promisor_remote_lookup(r->promisor_remote_config, remote_name, NULL);
197 int repo_has_promisor_remote(struct repository *r)
199 return !!repo_promisor_remote_find(r, NULL);
202 static int remove_fetched_oids(struct repository *repo,
203 struct object_id **oids,
204 int oid_nr, int to_free)
206 int i, remaining_nr = 0;
207 int *remaining = xcalloc(oid_nr, sizeof(*remaining));
208 struct object_id *old_oids = *oids;
209 struct object_id *new_oids;
211 for (i = 0; i < oid_nr; i++)
212 if (oid_object_info_extended(repo, &old_oids[i], NULL,
213 OBJECT_INFO_SKIP_FETCH_OBJECT)) {
214 remaining[i] = 1;
215 remaining_nr++;
218 if (remaining_nr) {
219 int j = 0;
220 CALLOC_ARRAY(new_oids, remaining_nr);
221 for (i = 0; i < oid_nr; i++)
222 if (remaining[i])
223 oidcpy(&new_oids[j++], &old_oids[i]);
224 *oids = new_oids;
225 if (to_free)
226 free(old_oids);
229 free(remaining);
231 return remaining_nr;
234 void promisor_remote_get_direct(struct repository *repo,
235 const struct object_id *oids,
236 int oid_nr)
238 struct promisor_remote *r;
239 struct object_id *remaining_oids = (struct object_id *)oids;
240 int remaining_nr = oid_nr;
241 int to_free = 0;
242 int i;
244 if (oid_nr == 0)
245 return;
247 promisor_remote_init(repo);
249 for (r = repo->promisor_remote_config->promisors; r; r = r->next) {
250 if (fetch_objects(repo, r->name, remaining_oids, remaining_nr) < 0) {
251 if (remaining_nr == 1)
252 continue;
253 remaining_nr = remove_fetched_oids(repo, &remaining_oids,
254 remaining_nr, to_free);
255 if (remaining_nr) {
256 to_free = 1;
257 continue;
260 goto all_fetched;
263 for (i = 0; i < remaining_nr; i++) {
264 if (is_promisor_object(&remaining_oids[i]))
265 die(_("could not fetch %s from promisor remote"),
266 oid_to_hex(&remaining_oids[i]));
269 all_fetched:
270 if (to_free)
271 free(remaining_oids);