blob: ac3aa1e365760596990188548410a16900e005d7 [file] [log] [blame]
Elijah Newren5579f442023-04-11 00:41:48 -07001#include "git-compat-util.h"
Elijah Newrenf394e092023-03-21 06:25:54 +00002#include "gettext.h"
Elijah Newren41771fa2023-02-24 00:09:27 +00003#include "hex.h"
Elijah Newrena034e912023-05-16 06:34:06 +00004#include "object-store-ll.h"
Christian Couder48de3152019-06-25 15:40:27 +02005#include "promisor-remote.h"
6#include "config.h"
Elijah Newren74ea5c92023-04-11 03:00:38 +00007#include "trace2.h"
Christian Couderdb27dca2019-06-25 15:40:37 +02008#include "transport.h"
Jonathan Tan7ca3c0a2020-08-17 21:01:36 -07009#include "strvec.h"
Jonathan Tan301f1e32022-10-04 14:13:41 -070010#include "packfile.h"
Christian Couderdb27dca2019-06-25 15:40:37 +020011
Jonathan Tanef7dc2e2021-06-17 10:13:23 -070012struct promisor_remote_config {
13 struct promisor_remote *promisors;
14 struct promisor_remote **promisors_tail;
15};
Christian Couder60b7a922019-06-25 15:40:38 +020016
Elijah Newren78cfdd02021-06-15 22:41:42 +000017static int fetch_objects(struct repository *repo,
18 const char *remote_name,
Christian Couderdb27dca2019-06-25 15:40:37 +020019 const struct object_id *oids,
20 int oid_nr)
21{
Jonathan Tan7ca3c0a2020-08-17 21:01:36 -070022 struct child_process child = CHILD_PROCESS_INIT;
Christian Couderdb27dca2019-06-25 15:40:37 +020023 int i;
Jonathan Tan7ca3c0a2020-08-17 21:01:36 -070024 FILE *child_in;
25
26 child.git_cmd = 1;
27 child.in = -1;
Jonathan Tanef830cc2021-06-17 10:13:26 -070028 if (repo != the_repository)
Ævar Arnfjörð Bjarmason29fda242022-06-02 11:09:50 +020029 prepare_other_repo_env(&child.env, repo->gitdir);
Jonathan Tan7ca3c0a2020-08-17 21:01:36 -070030 strvec_pushl(&child.args, "-c", "fetch.negotiationAlgorithm=noop",
31 "fetch", remote_name, "--no-tags",
32 "--no-write-fetch-head", "--recurse-submodules=no",
33 "--filter=blob:none", "--stdin", NULL);
34 if (start_command(&child))
35 die(_("promisor-remote: unable to fork off fetch subprocess"));
36 child_in = xfdopen(child.in, "w");
Christian Couderdb27dca2019-06-25 15:40:37 +020037
Elijah Newren78cfdd02021-06-15 22:41:42 +000038 trace2_data_intmax("promisor", repo, "fetch_count", oid_nr);
39
Christian Couderdb27dca2019-06-25 15:40:37 +020040 for (i = 0; i < oid_nr; i++) {
Jonathan Tan7ca3c0a2020-08-17 21:01:36 -070041 if (fputs(oid_to_hex(&oids[i]), child_in) < 0)
42 die_errno(_("promisor-remote: could not write to fetch subprocess"));
43 if (fputc('\n', child_in) < 0)
44 die_errno(_("promisor-remote: could not write to fetch subprocess"));
Christian Couderdb27dca2019-06-25 15:40:37 +020045 }
Jonathan Tan7ca3c0a2020-08-17 21:01:36 -070046
47 if (fclose(child_in) < 0)
48 die_errno(_("promisor-remote: could not close stdin to fetch subprocess"));
49 return finish_command(&child) ? -1 : 0;
Christian Couderdb27dca2019-06-25 15:40:37 +020050}
Christian Couder48de3152019-06-25 15:40:27 +020051
Jonathan Tanef7dc2e2021-06-17 10:13:23 -070052static struct promisor_remote *promisor_remote_new(struct promisor_remote_config *config,
53 const char *remote_name)
Christian Couder48de3152019-06-25 15:40:27 +020054{
55 struct promisor_remote *r;
56
57 if (*remote_name == '/') {
58 warning(_("promisor remote name cannot begin with '/': %s"),
59 remote_name);
60 return NULL;
61 }
62
63 FLEX_ALLOC_STR(r, name, remote_name);
64
Jonathan Tanef7dc2e2021-06-17 10:13:23 -070065 *config->promisors_tail = r;
66 config->promisors_tail = &r->next;
Christian Couder48de3152019-06-25 15:40:27 +020067
68 return r;
69}
70
Jonathan Tanef7dc2e2021-06-17 10:13:23 -070071static struct promisor_remote *promisor_remote_lookup(struct promisor_remote_config *config,
72 const char *remote_name,
Christian Couder48de3152019-06-25 15:40:27 +020073 struct promisor_remote **previous)
74{
75 struct promisor_remote *r, *p;
76
Jonathan Tanef7dc2e2021-06-17 10:13:23 -070077 for (p = NULL, r = config->promisors; r; p = r, r = r->next)
Christian Couder48de3152019-06-25 15:40:27 +020078 if (!strcmp(r->name, remote_name)) {
79 if (previous)
80 *previous = p;
81 return r;
82 }
83
84 return NULL;
85}
86
Jonathan Tanef7dc2e2021-06-17 10:13:23 -070087static void promisor_remote_move_to_tail(struct promisor_remote_config *config,
88 struct promisor_remote *r,
Christian Couderfaf2abf2019-06-25 15:40:30 +020089 struct promisor_remote *previous)
90{
Junio C Hamanoafe8a902022-05-02 09:50:37 -070091 if (!r->next)
Emily Shaffer65904b82019-09-30 15:03:55 -070092 return;
93
Christian Couderfaf2abf2019-06-25 15:40:30 +020094 if (previous)
95 previous->next = r->next;
96 else
Jonathan Tanef7dc2e2021-06-17 10:13:23 -070097 config->promisors = r->next ? r->next : r;
Christian Couderfaf2abf2019-06-25 15:40:30 +020098 r->next = NULL;
Jonathan Tanef7dc2e2021-06-17 10:13:23 -070099 *config->promisors_tail = r;
100 config->promisors_tail = &r->next;
Christian Couderfaf2abf2019-06-25 15:40:30 +0200101}
102
Glen Chooa4e7e312023-06-28 19:26:22 +0000103static int promisor_remote_config(const char *var, const char *value,
104 const struct config_context *ctx UNUSED,
105 void *data)
Christian Couder48de3152019-06-25 15:40:27 +0200106{
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700107 struct promisor_remote_config *config = data;
Christian Couder48de3152019-06-25 15:40:27 +0200108 const char *name;
Jeff Kingf5914f42020-04-10 15:44:28 -0400109 size_t namelen;
Christian Couder48de3152019-06-25 15:40:27 +0200110 const char *subkey;
111
112 if (parse_config_key(var, "remote", &name, &namelen, &subkey) < 0)
113 return 0;
114
115 if (!strcmp(subkey, "promisor")) {
116 char *remote_name;
117
118 if (!git_config_bool(var, value))
119 return 0;
120
121 remote_name = xmemdupz(name, namelen);
122
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700123 if (!promisor_remote_lookup(config, remote_name, NULL))
124 promisor_remote_new(config, remote_name);
Christian Couder48de3152019-06-25 15:40:27 +0200125
126 free(remote_name);
127 return 0;
128 }
Christian Couderfa3d1b62019-06-25 15:40:32 +0200129 if (!strcmp(subkey, "partialclonefilter")) {
130 struct promisor_remote *r;
131 char *remote_name = xmemdupz(name, namelen);
132
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700133 r = promisor_remote_lookup(config, remote_name, NULL);
Christian Couderfa3d1b62019-06-25 15:40:32 +0200134 if (!r)
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700135 r = promisor_remote_new(config, remote_name);
Christian Couderfa3d1b62019-06-25 15:40:32 +0200136
137 free(remote_name);
138
139 if (!r)
140 return 0;
141
142 return git_config_string(&r->partial_clone_filter, var, value);
143 }
Christian Couder48de3152019-06-25 15:40:27 +0200144
145 return 0;
146}
147
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700148static void promisor_remote_init(struct repository *r)
Christian Couder48de3152019-06-25 15:40:27 +0200149{
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700150 struct promisor_remote_config *config;
151
152 if (r->promisor_remote_config)
Christian Couder48de3152019-06-25 15:40:27 +0200153 return;
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700154 config = r->promisor_remote_config =
SZEDER Gáborc4bbd9b2022-08-23 11:57:33 +0200155 xcalloc(1, sizeof(*r->promisor_remote_config));
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700156 config->promisors_tail = &config->promisors;
Christian Couder48de3152019-06-25 15:40:27 +0200157
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700158 repo_config(r, promisor_remote_config, config);
Christian Couderfaf2abf2019-06-25 15:40:30 +0200159
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700160 if (r->repository_format_partial_clone) {
Christian Couderfaf2abf2019-06-25 15:40:30 +0200161 struct promisor_remote *o, *previous;
162
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700163 o = promisor_remote_lookup(config,
164 r->repository_format_partial_clone,
Christian Couderfaf2abf2019-06-25 15:40:30 +0200165 &previous);
166 if (o)
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700167 promisor_remote_move_to_tail(config, o, previous);
Christian Couderfaf2abf2019-06-25 15:40:30 +0200168 else
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700169 promisor_remote_new(config, r->repository_format_partial_clone);
Christian Couderfaf2abf2019-06-25 15:40:30 +0200170 }
Christian Couder48de3152019-06-25 15:40:27 +0200171}
172
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700173void promisor_remote_clear(struct promisor_remote_config *config)
Christian Couder9cfebc12019-06-25 15:40:29 +0200174{
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700175 while (config->promisors) {
176 struct promisor_remote *r = config->promisors;
177 config->promisors = config->promisors->next;
Christian Couder9cfebc12019-06-25 15:40:29 +0200178 free(r);
179 }
180
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700181 config->promisors_tail = &config->promisors;
Christian Couder9cfebc12019-06-25 15:40:29 +0200182}
183
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700184void repo_promisor_remote_reinit(struct repository *r)
Christian Couder9cfebc12019-06-25 15:40:29 +0200185{
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700186 promisor_remote_clear(r->promisor_remote_config);
187 FREE_AND_NULL(r->promisor_remote_config);
188 promisor_remote_init(r);
Christian Couder9cfebc12019-06-25 15:40:29 +0200189}
190
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700191struct promisor_remote *repo_promisor_remote_find(struct repository *r,
192 const char *remote_name)
Christian Couder48de3152019-06-25 15:40:27 +0200193{
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700194 promisor_remote_init(r);
Christian Couder48de3152019-06-25 15:40:27 +0200195
196 if (!remote_name)
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700197 return r->promisor_remote_config->promisors;
Christian Couder48de3152019-06-25 15:40:27 +0200198
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700199 return promisor_remote_lookup(r->promisor_remote_config, remote_name, NULL);
Christian Couder48de3152019-06-25 15:40:27 +0200200}
201
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700202int repo_has_promisor_remote(struct repository *r)
Christian Couder48de3152019-06-25 15:40:27 +0200203{
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700204 return !!repo_promisor_remote_find(r, NULL);
Christian Couder48de3152019-06-25 15:40:27 +0200205}
Christian Couder9e27bea2019-06-25 15:40:28 +0200206
207static int remove_fetched_oids(struct repository *repo,
208 struct object_id **oids,
209 int oid_nr, int to_free)
210{
211 int i, remaining_nr = 0;
212 int *remaining = xcalloc(oid_nr, sizeof(*remaining));
213 struct object_id *old_oids = *oids;
214 struct object_id *new_oids;
215
216 for (i = 0; i < oid_nr; i++)
217 if (oid_object_info_extended(repo, &old_oids[i], NULL,
218 OBJECT_INFO_SKIP_FETCH_OBJECT)) {
219 remaining[i] = 1;
220 remaining_nr++;
221 }
222
223 if (remaining_nr) {
224 int j = 0;
René Scharfeca56dad2021-03-13 17:17:22 +0100225 CALLOC_ARRAY(new_oids, remaining_nr);
Christian Couder9e27bea2019-06-25 15:40:28 +0200226 for (i = 0; i < oid_nr; i++)
227 if (remaining[i])
228 oidcpy(&new_oids[j++], &old_oids[i]);
229 *oids = new_oids;
230 if (to_free)
231 free(old_oids);
232 }
233
234 free(remaining);
235
236 return remaining_nr;
237}
238
Jonathan Tan00057bf2022-10-04 14:13:40 -0700239void promisor_remote_get_direct(struct repository *repo,
240 const struct object_id *oids,
241 int oid_nr)
Christian Couder9e27bea2019-06-25 15:40:28 +0200242{
243 struct promisor_remote *r;
244 struct object_id *remaining_oids = (struct object_id *)oids;
245 int remaining_nr = oid_nr;
246 int to_free = 0;
Jonathan Tan301f1e32022-10-04 14:13:41 -0700247 int i;
Christian Couder9e27bea2019-06-25 15:40:28 +0200248
Jonathan Tandb7ed742020-04-02 12:19:16 -0700249 if (oid_nr == 0)
Jonathan Tan00057bf2022-10-04 14:13:40 -0700250 return;
Jonathan Tandb7ed742020-04-02 12:19:16 -0700251
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700252 promisor_remote_init(repo);
Christian Couder9e27bea2019-06-25 15:40:28 +0200253
Jonathan Tanef7dc2e2021-06-17 10:13:23 -0700254 for (r = repo->promisor_remote_config->promisors; r; r = r->next) {
Elijah Newren78cfdd02021-06-15 22:41:42 +0000255 if (fetch_objects(repo, r->name, remaining_oids, remaining_nr) < 0) {
Christian Couder9e27bea2019-06-25 15:40:28 +0200256 if (remaining_nr == 1)
257 continue;
258 remaining_nr = remove_fetched_oids(repo, &remaining_oids,
259 remaining_nr, to_free);
260 if (remaining_nr) {
261 to_free = 1;
262 continue;
263 }
264 }
Jonathan Tan301f1e32022-10-04 14:13:41 -0700265 goto all_fetched;
Christian Couder9e27bea2019-06-25 15:40:28 +0200266 }
267
Jonathan Tan301f1e32022-10-04 14:13:41 -0700268 for (i = 0; i < remaining_nr; i++) {
269 if (is_promisor_object(&remaining_oids[i]))
270 die(_("could not fetch %s from promisor remote"),
271 oid_to_hex(&remaining_oids[i]));
272 }
273
274all_fetched:
Christian Couder9e27bea2019-06-25 15:40:28 +0200275 if (to_free)
276 free(remaining_oids);
Christian Couder9e27bea2019-06-25 15:40:28 +0200277}