]>
Commit | Line | Data |
---|---|---|
48de3158 | 1 | #include "cache.h" |
9e27beaa | 2 | #include "object-store.h" |
48de3158 CC |
3 | #include "promisor-remote.h" |
4 | #include "config.h" | |
db27dca5 | 5 | #include "transport.h" |
7ca3c0ac | 6 | #include "strvec.h" |
301f1e3a | 7 | #include "packfile.h" |
db27dca5 | 8 | |
ef7dc2e9 JT |
9 | struct promisor_remote_config { |
10 | struct promisor_remote *promisors; | |
11 | struct promisor_remote **promisors_tail; | |
12 | }; | |
60b7a92d | 13 | |
78cfdd0c EN |
14 | static int fetch_objects(struct repository *repo, |
15 | const char *remote_name, | |
db27dca5 CC |
16 | const struct object_id *oids, |
17 | int oid_nr) | |
18 | { | |
7ca3c0ac | 19 | struct child_process child = CHILD_PROCESS_INIT; |
db27dca5 | 20 | int i; |
7ca3c0ac JT |
21 | FILE *child_in; |
22 | ||
23 | child.git_cmd = 1; | |
24 | child.in = -1; | |
ef830cc4 | 25 | if (repo != the_repository) |
29fda24d | 26 | prepare_other_repo_env(&child.env, repo->gitdir); |
7ca3c0ac JT |
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"); | |
db27dca5 | 34 | |
78cfdd0c EN |
35 | trace2_data_intmax("promisor", repo, "fetch_count", oid_nr); |
36 | ||
db27dca5 | 37 | for (i = 0; i < oid_nr; i++) { |
7ca3c0ac JT |
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")); | |
db27dca5 | 42 | } |
7ca3c0ac JT |
43 | |
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; | |
db27dca5 | 47 | } |
48de3158 | 48 | |
ef7dc2e9 JT |
49 | static struct promisor_remote *promisor_remote_new(struct promisor_remote_config *config, |
50 | const char *remote_name) | |
48de3158 CC |
51 | { |
52 | struct promisor_remote *r; | |
53 | ||
54 | if (*remote_name == '/') { | |
55 | warning(_("promisor remote name cannot begin with '/': %s"), | |
56 | remote_name); | |
57 | return NULL; | |
58 | } | |
59 | ||
60 | FLEX_ALLOC_STR(r, name, remote_name); | |
61 | ||
ef7dc2e9 JT |
62 | *config->promisors_tail = r; |
63 | config->promisors_tail = &r->next; | |
48de3158 CC |
64 | |
65 | return r; | |
66 | } | |
67 | ||
ef7dc2e9 JT |
68 | static struct promisor_remote *promisor_remote_lookup(struct promisor_remote_config *config, |
69 | const char *remote_name, | |
48de3158 CC |
70 | struct promisor_remote **previous) |
71 | { | |
72 | struct promisor_remote *r, *p; | |
73 | ||
ef7dc2e9 | 74 | for (p = NULL, r = config->promisors; r; p = r, r = r->next) |
48de3158 CC |
75 | if (!strcmp(r->name, remote_name)) { |
76 | if (previous) | |
77 | *previous = p; | |
78 | return r; | |
79 | } | |
80 | ||
81 | return NULL; | |
82 | } | |
83 | ||
ef7dc2e9 JT |
84 | static void promisor_remote_move_to_tail(struct promisor_remote_config *config, |
85 | struct promisor_remote *r, | |
faf2abf4 CC |
86 | struct promisor_remote *previous) |
87 | { | |
afe8a907 | 88 | if (!r->next) |
65904b8b ES |
89 | return; |
90 | ||
faf2abf4 CC |
91 | if (previous) |
92 | previous->next = r->next; | |
93 | else | |
ef7dc2e9 | 94 | config->promisors = r->next ? r->next : r; |
faf2abf4 | 95 | r->next = NULL; |
ef7dc2e9 JT |
96 | *config->promisors_tail = r; |
97 | config->promisors_tail = &r->next; | |
faf2abf4 CC |
98 | } |
99 | ||
48de3158 CC |
100 | static int promisor_remote_config(const char *var, const char *value, void *data) |
101 | { | |
ef7dc2e9 | 102 | struct promisor_remote_config *config = data; |
48de3158 | 103 | const char *name; |
f5914f4b | 104 | size_t namelen; |
48de3158 CC |
105 | const char *subkey; |
106 | ||
107 | if (parse_config_key(var, "remote", &name, &namelen, &subkey) < 0) | |
108 | return 0; | |
109 | ||
110 | if (!strcmp(subkey, "promisor")) { | |
111 | char *remote_name; | |
112 | ||
113 | if (!git_config_bool(var, value)) | |
114 | return 0; | |
115 | ||
116 | remote_name = xmemdupz(name, namelen); | |
117 | ||
ef7dc2e9 JT |
118 | if (!promisor_remote_lookup(config, remote_name, NULL)) |
119 | promisor_remote_new(config, remote_name); | |
48de3158 CC |
120 | |
121 | free(remote_name); | |
122 | return 0; | |
123 | } | |
fa3d1b63 CC |
124 | if (!strcmp(subkey, "partialclonefilter")) { |
125 | struct promisor_remote *r; | |
126 | char *remote_name = xmemdupz(name, namelen); | |
127 | ||
ef7dc2e9 | 128 | r = promisor_remote_lookup(config, remote_name, NULL); |
fa3d1b63 | 129 | if (!r) |
ef7dc2e9 | 130 | r = promisor_remote_new(config, remote_name); |
fa3d1b63 CC |
131 | |
132 | free(remote_name); | |
133 | ||
134 | if (!r) | |
135 | return 0; | |
136 | ||
137 | return git_config_string(&r->partial_clone_filter, var, value); | |
138 | } | |
48de3158 CC |
139 | |
140 | return 0; | |
141 | } | |
142 | ||
ef7dc2e9 | 143 | static void promisor_remote_init(struct repository *r) |
48de3158 | 144 | { |
ef7dc2e9 JT |
145 | struct promisor_remote_config *config; |
146 | ||
147 | if (r->promisor_remote_config) | |
48de3158 | 148 | return; |
ef7dc2e9 | 149 | config = r->promisor_remote_config = |
c4bbd9bb | 150 | xcalloc(1, sizeof(*r->promisor_remote_config)); |
ef7dc2e9 | 151 | config->promisors_tail = &config->promisors; |
48de3158 | 152 | |
ef7dc2e9 | 153 | repo_config(r, promisor_remote_config, config); |
faf2abf4 | 154 | |
ef7dc2e9 | 155 | if (r->repository_format_partial_clone) { |
faf2abf4 CC |
156 | struct promisor_remote *o, *previous; |
157 | ||
ef7dc2e9 JT |
158 | o = promisor_remote_lookup(config, |
159 | r->repository_format_partial_clone, | |
faf2abf4 CC |
160 | &previous); |
161 | if (o) | |
ef7dc2e9 | 162 | promisor_remote_move_to_tail(config, o, previous); |
faf2abf4 | 163 | else |
ef7dc2e9 | 164 | promisor_remote_new(config, r->repository_format_partial_clone); |
faf2abf4 | 165 | } |
48de3158 CC |
166 | } |
167 | ||
ef7dc2e9 | 168 | void promisor_remote_clear(struct promisor_remote_config *config) |
9cfebc1f | 169 | { |
ef7dc2e9 JT |
170 | while (config->promisors) { |
171 | struct promisor_remote *r = config->promisors; | |
172 | config->promisors = config->promisors->next; | |
9cfebc1f CC |
173 | free(r); |
174 | } | |
175 | ||
ef7dc2e9 | 176 | config->promisors_tail = &config->promisors; |
9cfebc1f CC |
177 | } |
178 | ||
ef7dc2e9 | 179 | void repo_promisor_remote_reinit(struct repository *r) |
9cfebc1f | 180 | { |
ef7dc2e9 JT |
181 | promisor_remote_clear(r->promisor_remote_config); |
182 | FREE_AND_NULL(r->promisor_remote_config); | |
183 | promisor_remote_init(r); | |
9cfebc1f CC |
184 | } |
185 | ||
ef7dc2e9 JT |
186 | struct promisor_remote *repo_promisor_remote_find(struct repository *r, |
187 | const char *remote_name) | |
48de3158 | 188 | { |
ef7dc2e9 | 189 | promisor_remote_init(r); |
48de3158 CC |
190 | |
191 | if (!remote_name) | |
ef7dc2e9 | 192 | return r->promisor_remote_config->promisors; |
48de3158 | 193 | |
ef7dc2e9 | 194 | return promisor_remote_lookup(r->promisor_remote_config, remote_name, NULL); |
48de3158 CC |
195 | } |
196 | ||
ef7dc2e9 | 197 | int repo_has_promisor_remote(struct repository *r) |
48de3158 | 198 | { |
ef7dc2e9 | 199 | return !!repo_promisor_remote_find(r, NULL); |
48de3158 | 200 | } |
9e27beaa CC |
201 | |
202 | static int remove_fetched_oids(struct repository *repo, | |
203 | struct object_id **oids, | |
204 | int oid_nr, int to_free) | |
205 | { | |
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; | |
210 | ||
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++; | |
216 | } | |
217 | ||
218 | if (remaining_nr) { | |
219 | int j = 0; | |
ca56dadb | 220 | CALLOC_ARRAY(new_oids, remaining_nr); |
9e27beaa CC |
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); | |
227 | } | |
228 | ||
229 | free(remaining); | |
230 | ||
231 | return remaining_nr; | |
232 | } | |
233 | ||
00057bf1 JT |
234 | void promisor_remote_get_direct(struct repository *repo, |
235 | const struct object_id *oids, | |
236 | int oid_nr) | |
9e27beaa CC |
237 | { |
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; | |
301f1e3a | 242 | int i; |
9e27beaa | 243 | |
db7ed741 | 244 | if (oid_nr == 0) |
00057bf1 | 245 | return; |
db7ed741 | 246 | |
ef7dc2e9 | 247 | promisor_remote_init(repo); |
9e27beaa | 248 | |
ef7dc2e9 | 249 | for (r = repo->promisor_remote_config->promisors; r; r = r->next) { |
78cfdd0c | 250 | if (fetch_objects(repo, r->name, remaining_oids, remaining_nr) < 0) { |
9e27beaa CC |
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; | |
258 | } | |
259 | } | |
301f1e3a JT |
260 | goto all_fetched; |
261 | } | |
262 | ||
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])); | |
9e27beaa CC |
267 | } |
268 | ||
301f1e3a | 269 | all_fetched: |
9e27beaa CC |
270 | if (to_free) |
271 | free(remaining_oids); | |
9e27beaa | 272 | } |