]>
Commit | Line | Data |
---|---|---|
5579f44d | 1 | #include "git-compat-util.h" |
f394e093 | 2 | #include "gettext.h" |
41771fa4 | 3 | #include "hex.h" |
9e27beaa | 4 | #include "object-store.h" |
48de3158 CC |
5 | #include "promisor-remote.h" |
6 | #include "config.h" | |
74ea5c95 | 7 | #include "trace2.h" |
db27dca5 | 8 | #include "transport.h" |
7ca3c0ac | 9 | #include "strvec.h" |
301f1e3a | 10 | #include "packfile.h" |
db27dca5 | 11 | |
ef7dc2e9 JT |
12 | struct promisor_remote_config { |
13 | struct promisor_remote *promisors; | |
14 | struct promisor_remote **promisors_tail; | |
15 | }; | |
60b7a92d | 16 | |
78cfdd0c EN |
17 | static int fetch_objects(struct repository *repo, |
18 | const char *remote_name, | |
db27dca5 CC |
19 | const struct object_id *oids, |
20 | int oid_nr) | |
21 | { | |
7ca3c0ac | 22 | struct child_process child = CHILD_PROCESS_INIT; |
db27dca5 | 23 | int i; |
7ca3c0ac JT |
24 | FILE *child_in; |
25 | ||
26 | child.git_cmd = 1; | |
27 | child.in = -1; | |
ef830cc4 | 28 | if (repo != the_repository) |
29fda24d | 29 | prepare_other_repo_env(&child.env, repo->gitdir); |
7ca3c0ac JT |
30 | 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"); | |
db27dca5 | 37 | |
78cfdd0c EN |
38 | trace2_data_intmax("promisor", repo, "fetch_count", oid_nr); |
39 | ||
db27dca5 | 40 | for (i = 0; i < oid_nr; i++) { |
7ca3c0ac JT |
41 | 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")); | |
db27dca5 | 45 | } |
7ca3c0ac JT |
46 | |
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; | |
db27dca5 | 50 | } |
48de3158 | 51 | |
ef7dc2e9 JT |
52 | static struct promisor_remote *promisor_remote_new(struct promisor_remote_config *config, |
53 | const char *remote_name) | |
48de3158 CC |
54 | { |
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 | ||
ef7dc2e9 JT |
65 | *config->promisors_tail = r; |
66 | config->promisors_tail = &r->next; | |
48de3158 CC |
67 | |
68 | return r; | |
69 | } | |
70 | ||
ef7dc2e9 JT |
71 | static struct promisor_remote *promisor_remote_lookup(struct promisor_remote_config *config, |
72 | const char *remote_name, | |
48de3158 CC |
73 | struct promisor_remote **previous) |
74 | { | |
75 | struct promisor_remote *r, *p; | |
76 | ||
ef7dc2e9 | 77 | for (p = NULL, r = config->promisors; r; p = r, r = r->next) |
48de3158 CC |
78 | if (!strcmp(r->name, remote_name)) { |
79 | if (previous) | |
80 | *previous = p; | |
81 | return r; | |
82 | } | |
83 | ||
84 | return NULL; | |
85 | } | |
86 | ||
ef7dc2e9 JT |
87 | static void promisor_remote_move_to_tail(struct promisor_remote_config *config, |
88 | struct promisor_remote *r, | |
faf2abf4 CC |
89 | struct promisor_remote *previous) |
90 | { | |
afe8a907 | 91 | if (!r->next) |
65904b8b ES |
92 | return; |
93 | ||
faf2abf4 CC |
94 | if (previous) |
95 | previous->next = r->next; | |
96 | else | |
ef7dc2e9 | 97 | config->promisors = r->next ? r->next : r; |
faf2abf4 | 98 | r->next = NULL; |
ef7dc2e9 JT |
99 | *config->promisors_tail = r; |
100 | config->promisors_tail = &r->next; | |
faf2abf4 CC |
101 | } |
102 | ||
48de3158 CC |
103 | static int promisor_remote_config(const char *var, const char *value, void *data) |
104 | { | |
ef7dc2e9 | 105 | struct promisor_remote_config *config = data; |
48de3158 | 106 | const char *name; |
f5914f4b | 107 | size_t namelen; |
48de3158 CC |
108 | const char *subkey; |
109 | ||
110 | if (parse_config_key(var, "remote", &name, &namelen, &subkey) < 0) | |
111 | return 0; | |
112 | ||
113 | if (!strcmp(subkey, "promisor")) { | |
114 | char *remote_name; | |
115 | ||
116 | if (!git_config_bool(var, value)) | |
117 | return 0; | |
118 | ||
119 | remote_name = xmemdupz(name, namelen); | |
120 | ||
ef7dc2e9 JT |
121 | if (!promisor_remote_lookup(config, remote_name, NULL)) |
122 | promisor_remote_new(config, remote_name); | |
48de3158 CC |
123 | |
124 | free(remote_name); | |
125 | return 0; | |
126 | } | |
fa3d1b63 CC |
127 | if (!strcmp(subkey, "partialclonefilter")) { |
128 | struct promisor_remote *r; | |
129 | char *remote_name = xmemdupz(name, namelen); | |
130 | ||
ef7dc2e9 | 131 | r = promisor_remote_lookup(config, remote_name, NULL); |
fa3d1b63 | 132 | if (!r) |
ef7dc2e9 | 133 | r = promisor_remote_new(config, remote_name); |
fa3d1b63 CC |
134 | |
135 | free(remote_name); | |
136 | ||
137 | if (!r) | |
138 | return 0; | |
139 | ||
140 | return git_config_string(&r->partial_clone_filter, var, value); | |
141 | } | |
48de3158 CC |
142 | |
143 | return 0; | |
144 | } | |
145 | ||
ef7dc2e9 | 146 | static void promisor_remote_init(struct repository *r) |
48de3158 | 147 | { |
ef7dc2e9 JT |
148 | struct promisor_remote_config *config; |
149 | ||
150 | if (r->promisor_remote_config) | |
48de3158 | 151 | return; |
ef7dc2e9 | 152 | config = r->promisor_remote_config = |
c4bbd9bb | 153 | xcalloc(1, sizeof(*r->promisor_remote_config)); |
ef7dc2e9 | 154 | config->promisors_tail = &config->promisors; |
48de3158 | 155 | |
ef7dc2e9 | 156 | repo_config(r, promisor_remote_config, config); |
faf2abf4 | 157 | |
ef7dc2e9 | 158 | if (r->repository_format_partial_clone) { |
faf2abf4 CC |
159 | struct promisor_remote *o, *previous; |
160 | ||
ef7dc2e9 JT |
161 | o = promisor_remote_lookup(config, |
162 | r->repository_format_partial_clone, | |
faf2abf4 CC |
163 | &previous); |
164 | if (o) | |
ef7dc2e9 | 165 | promisor_remote_move_to_tail(config, o, previous); |
faf2abf4 | 166 | else |
ef7dc2e9 | 167 | promisor_remote_new(config, r->repository_format_partial_clone); |
faf2abf4 | 168 | } |
48de3158 CC |
169 | } |
170 | ||
ef7dc2e9 | 171 | void promisor_remote_clear(struct promisor_remote_config *config) |
9cfebc1f | 172 | { |
ef7dc2e9 JT |
173 | while (config->promisors) { |
174 | struct promisor_remote *r = config->promisors; | |
175 | config->promisors = config->promisors->next; | |
9cfebc1f CC |
176 | free(r); |
177 | } | |
178 | ||
ef7dc2e9 | 179 | config->promisors_tail = &config->promisors; |
9cfebc1f CC |
180 | } |
181 | ||
ef7dc2e9 | 182 | void repo_promisor_remote_reinit(struct repository *r) |
9cfebc1f | 183 | { |
ef7dc2e9 JT |
184 | promisor_remote_clear(r->promisor_remote_config); |
185 | FREE_AND_NULL(r->promisor_remote_config); | |
186 | promisor_remote_init(r); | |
9cfebc1f CC |
187 | } |
188 | ||
ef7dc2e9 JT |
189 | struct promisor_remote *repo_promisor_remote_find(struct repository *r, |
190 | const char *remote_name) | |
48de3158 | 191 | { |
ef7dc2e9 | 192 | promisor_remote_init(r); |
48de3158 CC |
193 | |
194 | if (!remote_name) | |
ef7dc2e9 | 195 | return r->promisor_remote_config->promisors; |
48de3158 | 196 | |
ef7dc2e9 | 197 | return promisor_remote_lookup(r->promisor_remote_config, remote_name, NULL); |
48de3158 CC |
198 | } |
199 | ||
ef7dc2e9 | 200 | int repo_has_promisor_remote(struct repository *r) |
48de3158 | 201 | { |
ef7dc2e9 | 202 | return !!repo_promisor_remote_find(r, NULL); |
48de3158 | 203 | } |
9e27beaa CC |
204 | |
205 | static int remove_fetched_oids(struct repository *repo, | |
206 | struct object_id **oids, | |
207 | int oid_nr, int to_free) | |
208 | { | |
209 | int i, remaining_nr = 0; | |
210 | int *remaining = xcalloc(oid_nr, sizeof(*remaining)); | |
211 | struct object_id *old_oids = *oids; | |
212 | struct object_id *new_oids; | |
213 | ||
214 | for (i = 0; i < oid_nr; i++) | |
215 | if (oid_object_info_extended(repo, &old_oids[i], NULL, | |
216 | OBJECT_INFO_SKIP_FETCH_OBJECT)) { | |
217 | remaining[i] = 1; | |
218 | remaining_nr++; | |
219 | } | |
220 | ||
221 | if (remaining_nr) { | |
222 | int j = 0; | |
ca56dadb | 223 | CALLOC_ARRAY(new_oids, remaining_nr); |
9e27beaa CC |
224 | for (i = 0; i < oid_nr; i++) |
225 | if (remaining[i]) | |
226 | oidcpy(&new_oids[j++], &old_oids[i]); | |
227 | *oids = new_oids; | |
228 | if (to_free) | |
229 | free(old_oids); | |
230 | } | |
231 | ||
232 | free(remaining); | |
233 | ||
234 | return remaining_nr; | |
235 | } | |
236 | ||
00057bf1 JT |
237 | void promisor_remote_get_direct(struct repository *repo, |
238 | const struct object_id *oids, | |
239 | int oid_nr) | |
9e27beaa CC |
240 | { |
241 | struct promisor_remote *r; | |
242 | struct object_id *remaining_oids = (struct object_id *)oids; | |
243 | int remaining_nr = oid_nr; | |
244 | int to_free = 0; | |
301f1e3a | 245 | int i; |
9e27beaa | 246 | |
db7ed741 | 247 | if (oid_nr == 0) |
00057bf1 | 248 | return; |
db7ed741 | 249 | |
ef7dc2e9 | 250 | promisor_remote_init(repo); |
9e27beaa | 251 | |
ef7dc2e9 | 252 | for (r = repo->promisor_remote_config->promisors; r; r = r->next) { |
78cfdd0c | 253 | if (fetch_objects(repo, r->name, remaining_oids, remaining_nr) < 0) { |
9e27beaa CC |
254 | if (remaining_nr == 1) |
255 | continue; | |
256 | remaining_nr = remove_fetched_oids(repo, &remaining_oids, | |
257 | remaining_nr, to_free); | |
258 | if (remaining_nr) { | |
259 | to_free = 1; | |
260 | continue; | |
261 | } | |
262 | } | |
301f1e3a JT |
263 | goto all_fetched; |
264 | } | |
265 | ||
266 | for (i = 0; i < remaining_nr; i++) { | |
267 | if (is_promisor_object(&remaining_oids[i])) | |
268 | die(_("could not fetch %s from promisor remote"), | |
269 | oid_to_hex(&remaining_oids[i])); | |
9e27beaa CC |
270 | } |
271 | ||
301f1e3a | 272 | all_fetched: |
9e27beaa CC |
273 | if (to_free) |
274 | free(remaining_oids); | |
9e27beaa | 275 | } |