blob: 8d6695681c1040aa2c1941228f325607c0c81e6b [file] [log] [blame]
Christian Couder48de3152019-06-25 15:40:27 +02001#include "cache.h"
Christian Couder9e27bea2019-06-25 15:40:28 +02002#include "object-store.h"
Christian Couder48de3152019-06-25 15:40:27 +02003#include "promisor-remote.h"
4#include "config.h"
Christian Couderdb27dca2019-06-25 15:40:37 +02005#include "transport.h"
Jonathan Tan7ca3c0a2020-08-17 21:01:36 -07006#include "strvec.h"
Christian Couderdb27dca2019-06-25 15:40:37 +02007
Jonathan Tanef7dc2e2021-06-17 10:13:23 -07008struct promisor_remote_config {
9 struct promisor_remote *promisors;
10 struct promisor_remote **promisors_tail;
11};
Christian Couder60b7a922019-06-25 15:40:38 +020012
Elijah Newren78cfdd02021-06-15 22:41:42 +000013static int fetch_objects(struct repository *repo,
14 const char *remote_name,
Christian Couderdb27dca2019-06-25 15:40:37 +020015 const struct object_id *oids,
16 int oid_nr)
17{
Jonathan Tan7ca3c0a2020-08-17 21:01:36 -070018 struct child_process child = CHILD_PROCESS_INIT;
Christian Couderdb27dca2019-06-25 15:40:37 +020019 int i;
Jonathan Tan7ca3c0a2020-08-17 21:01:36 -070020 FILE *child_in;
21
22 child.git_cmd = 1;
23 child.in = -1;
Jonathan Tanef830cc2021-06-17 10:13:26 -070024 if (repo != the_repository)
25 prepare_other_repo_env(&child.env_array, repo->gitdir);
Jonathan Tan7ca3c0a2020-08-17 21:01:36 -070026 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");
Christian Couderdb27dca2019-06-25 15:40:37 +020033
Elijah Newren78cfdd02021-06-15 22:41:42 +000034 trace2_data_intmax("promisor", repo, "fetch_count", oid_nr);
35
Christian Couderdb27dca2019-06-25 15:40:37 +020036 for (i = 0; i < oid_nr; i++) {
Jonathan Tan7ca3c0a2020-08-17 21:01:36 -070037 if (fputs(oid_to_hex(&oids[i]), child_in) < 0)
38 die_errno(_("promisor-remote: could not write to fetch subprocess"));
39 if (fputc('\n', child_in) < 0)
40 die_errno(_("promisor-remote: could not write to fetch subprocess"));
Christian Couderdb27dca2019-06-25 15:40:37 +020041 }
Jonathan Tan7ca3c0a2020-08-17 21:01:36 -070042
43 if (fclose(child_in) < 0)
44 die_errno(_("promisor-remote: could not close stdin to fetch subprocess"));
45 return finish_command(&child) ? -1 : 0;
Christian Couderdb27dca2019-06-25 15:40:37 +020046}
Christian Couder48de3152019-06-25 15:40:27 +020047
Jonathan Tanef7dc2e2021-06-17 10:13:23 -070048static struct promisor_remote *promisor_remote_new(struct promisor_remote_config *config,
49 const char *remote_name)
Christian Couder48de3152019-06-25 15:40:27 +020050{
51 struct promisor_remote *r;
52
53 if (*remote_name == '/') {
54 warning(_("promisor remote name cannot begin with '/': %s"),
55 remote_name);
56 return NULL;
57 }
58
59 FLEX_ALLOC_STR(r, name, remote_name);
60
Jonathan Tanef7dc2e2021-06-17 10:13:23 -070061 *config->promisors_tail = r;
62 config->promisors_tail = &r->next;
Christian Couder48de3152019-06-25 15:40:27 +020063
64 return r;
65}
66
Jonathan Tanef7dc2e2021-06-17 10:13:23 -070067static struct promisor_remote *promisor_remote_lookup(struct promisor_remote_config *config,
68 const char *remote_name,
Christian Couder48de3152019-06-25 15:40:27 +020069 struct promisor_remote **previous)
70{
71 struct promisor_remote *r, *p;
72
Jonathan Tanef7dc2e2021-06-17 10:13:23 -070073 for (p = NULL, r = config->promisors; r; p = r, r = r->next)
Christian Couder48de3152019-06-25 15:40:27 +020074 if (!strcmp(r->name, remote_name)) {
75 if (previous)
76 *previous = p;
77 return r;
78 }
79
80 return NULL;
81}
82
Jonathan Tanef7dc2e2021-06-17 10:13:23 -070083static void promisor_remote_move_to_tail(struct promisor_remote_config *config,
84 struct promisor_remote *r,
Christian Couderfaf2abf2019-06-25 15:40:30 +020085 struct promisor_remote *previous)
86{
Junio C Hamanoafe8a902022-05-02 09:50:37 -070087 if (!r->next)
Emily Shaffer65904b82019-09-30 15:03:55 -070088 return;
89
Christian Couderfaf2abf2019-06-25 15:40:30 +020090 if (previous)
91 previous->next = r->next;
92 else
Jonathan Tanef7dc2e2021-06-17 10:13:23 -070093 config->promisors = r->next ? r->next : r;
Christian Couderfaf2abf2019-06-25 15:40:30 +020094 r->next = NULL;
Jonathan Tanef7dc2e2021-06-17 10:13:23 -070095 *config->promisors_tail = r;
96 config->promisors_tail = &r->next;
Christian Couderfaf2abf2019-06-25 15:40:30 +020097}
98
Christian Couder48de3152019-06-25 15:40:27 +020099static int promisor_remote_config(const char *var, const char *value, void *data)
100{
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700101 struct promisor_remote_config *config = data;
Christian Couder48de3152019-06-25 15:40:27 +0200102 const char *name;
Jeff Kingf5914f42020-04-10 15:44:28 -0400103 size_t namelen;
Christian Couder48de3152019-06-25 15:40:27 +0200104 const char *subkey;
105
106 if (parse_config_key(var, "remote", &name, &namelen, &subkey) < 0)
107 return 0;
108
109 if (!strcmp(subkey, "promisor")) {
110 char *remote_name;
111
112 if (!git_config_bool(var, value))
113 return 0;
114
115 remote_name = xmemdupz(name, namelen);
116
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700117 if (!promisor_remote_lookup(config, remote_name, NULL))
118 promisor_remote_new(config, remote_name);
Christian Couder48de3152019-06-25 15:40:27 +0200119
120 free(remote_name);
121 return 0;
122 }
Christian Couderfa3d1b62019-06-25 15:40:32 +0200123 if (!strcmp(subkey, "partialclonefilter")) {
124 struct promisor_remote *r;
125 char *remote_name = xmemdupz(name, namelen);
126
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700127 r = promisor_remote_lookup(config, remote_name, NULL);
Christian Couderfa3d1b62019-06-25 15:40:32 +0200128 if (!r)
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700129 r = promisor_remote_new(config, remote_name);
Christian Couderfa3d1b62019-06-25 15:40:32 +0200130
131 free(remote_name);
132
133 if (!r)
134 return 0;
135
136 return git_config_string(&r->partial_clone_filter, var, value);
137 }
Christian Couder48de3152019-06-25 15:40:27 +0200138
139 return 0;
140}
141
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700142static void promisor_remote_init(struct repository *r)
Christian Couder48de3152019-06-25 15:40:27 +0200143{
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700144 struct promisor_remote_config *config;
145
146 if (r->promisor_remote_config)
Christian Couder48de3152019-06-25 15:40:27 +0200147 return;
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700148 config = r->promisor_remote_config =
149 xcalloc(sizeof(*r->promisor_remote_config), 1);
150 config->promisors_tail = &config->promisors;
Christian Couder48de3152019-06-25 15:40:27 +0200151
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700152 repo_config(r, promisor_remote_config, config);
Christian Couderfaf2abf2019-06-25 15:40:30 +0200153
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700154 if (r->repository_format_partial_clone) {
Christian Couderfaf2abf2019-06-25 15:40:30 +0200155 struct promisor_remote *o, *previous;
156
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700157 o = promisor_remote_lookup(config,
158 r->repository_format_partial_clone,
Christian Couderfaf2abf2019-06-25 15:40:30 +0200159 &previous);
160 if (o)
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700161 promisor_remote_move_to_tail(config, o, previous);
Christian Couderfaf2abf2019-06-25 15:40:30 +0200162 else
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700163 promisor_remote_new(config, r->repository_format_partial_clone);
Christian Couderfaf2abf2019-06-25 15:40:30 +0200164 }
Christian Couder48de3152019-06-25 15:40:27 +0200165}
166
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700167void promisor_remote_clear(struct promisor_remote_config *config)
Christian Couder9cfebc12019-06-25 15:40:29 +0200168{
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700169 while (config->promisors) {
170 struct promisor_remote *r = config->promisors;
171 config->promisors = config->promisors->next;
Christian Couder9cfebc12019-06-25 15:40:29 +0200172 free(r);
173 }
174
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700175 config->promisors_tail = &config->promisors;
Christian Couder9cfebc12019-06-25 15:40:29 +0200176}
177
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700178void repo_promisor_remote_reinit(struct repository *r)
Christian Couder9cfebc12019-06-25 15:40:29 +0200179{
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700180 promisor_remote_clear(r->promisor_remote_config);
181 FREE_AND_NULL(r->promisor_remote_config);
182 promisor_remote_init(r);
Christian Couder9cfebc12019-06-25 15:40:29 +0200183}
184
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700185struct promisor_remote *repo_promisor_remote_find(struct repository *r,
186 const char *remote_name)
Christian Couder48de3152019-06-25 15:40:27 +0200187{
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700188 promisor_remote_init(r);
Christian Couder48de3152019-06-25 15:40:27 +0200189
190 if (!remote_name)
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700191 return r->promisor_remote_config->promisors;
Christian Couder48de3152019-06-25 15:40:27 +0200192
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700193 return promisor_remote_lookup(r->promisor_remote_config, remote_name, NULL);
Christian Couder48de3152019-06-25 15:40:27 +0200194}
195
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700196int repo_has_promisor_remote(struct repository *r)
Christian Couder48de3152019-06-25 15:40:27 +0200197{
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700198 return !!repo_promisor_remote_find(r, NULL);
Christian Couder48de3152019-06-25 15:40:27 +0200199}
Christian Couder9e27bea2019-06-25 15:40:28 +0200200
201static int remove_fetched_oids(struct repository *repo,
202 struct object_id **oids,
203 int oid_nr, int to_free)
204{
205 int i, remaining_nr = 0;
206 int *remaining = xcalloc(oid_nr, sizeof(*remaining));
207 struct object_id *old_oids = *oids;
208 struct object_id *new_oids;
209
210 for (i = 0; i < oid_nr; i++)
211 if (oid_object_info_extended(repo, &old_oids[i], NULL,
212 OBJECT_INFO_SKIP_FETCH_OBJECT)) {
213 remaining[i] = 1;
214 remaining_nr++;
215 }
216
217 if (remaining_nr) {
218 int j = 0;
René Scharfeca56dad2021-03-13 17:17:22 +0100219 CALLOC_ARRAY(new_oids, remaining_nr);
Christian Couder9e27bea2019-06-25 15:40:28 +0200220 for (i = 0; i < oid_nr; i++)
221 if (remaining[i])
222 oidcpy(&new_oids[j++], &old_oids[i]);
223 *oids = new_oids;
224 if (to_free)
225 free(old_oids);
226 }
227
228 free(remaining);
229
230 return remaining_nr;
231}
232
233int promisor_remote_get_direct(struct repository *repo,
234 const struct object_id *oids,
235 int oid_nr)
236{
237 struct promisor_remote *r;
238 struct object_id *remaining_oids = (struct object_id *)oids;
239 int remaining_nr = oid_nr;
240 int to_free = 0;
241 int res = -1;
242
Jonathan Tandb7ed742020-04-02 12:19:16 -0700243 if (oid_nr == 0)
244 return 0;
245
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700246 promisor_remote_init(repo);
Christian Couder9e27bea2019-06-25 15:40:28 +0200247
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700248 for (r = repo->promisor_remote_config->promisors; r; r = r->next) {
Elijah Newren78cfdd02021-06-15 22:41:42 +0000249 if (fetch_objects(repo, r->name, remaining_oids, remaining_nr) < 0) {
Christian Couder9e27bea2019-06-25 15:40:28 +0200250 if (remaining_nr == 1)
251 continue;
252 remaining_nr = remove_fetched_oids(repo, &remaining_oids,
253 remaining_nr, to_free);
254 if (remaining_nr) {
255 to_free = 1;
256 continue;
257 }
258 }
259 res = 0;
260 break;
261 }
262
263 if (to_free)
264 free(remaining_oids);
265
266 return res;
267}